Search code examples
phpatoum

Concerning test cases with Atoum for PHP code , how can I unit test a class which uses require_once?


I can use Atoum to unit test a class as following:

<?php

namespace vendor2\project2;

class helloWorld2
{
    public function say()
    {
        return 'Hello World!';
    }

     public function add($a,$b)
    {
        return ($a+$b);
        //return 'Hello World!';
    }
}
?>  

Everything is fine and I accomplish several test cases
But when I add require_once to the class which I want to test it then Atoum can't test that class:

<?php

namespace vendor2\project2;

$ServerRoot = realpath(__DIR__. '/..');
require_once $ServerRoot.'/db/dbTables/M4_Warrior.php';

class helloWorld2
{
    public function say()
    {
        return 'Hello World!';
    }

     public function add($a,$b)
    {
        return ($a+$b);
        //return 'Hello World!';
    }
}
?>

When I comment the line with require_once everything is fine
Why?

The test php file is as following too:

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace vendor2\project2\tests\units;

$ServerRoot = realpath(__DIR__. '/../..');
require_once $ServerRoot.'/HandleCommands/helloWorld2.php';

use atoum;

/**
 * Test class for helloWorld.
 *
 * @author Vh80705
 */
class helloWorld2 extends atoum {

    // put your code here

    public function test1() {
        $true  = true;
        $false = false;

        $this
            ->boolean($true)
                ->isFalse();     // fails

    }    

    public function test2() {
        $true  = true;
        $false = false;

        $this
            ->boolean($false)
                ->isFalse();     // succeed        
    }    

    public function test3 ()
    {
        $this
            // creation of a new instance of the tested class
            ->given($this->newTestedInstance)

            ->then

                    // we test that the getHiAtoum method returns
                    // a string...
                    ->string($this->testedInstance->say())
                        // ... and that this string is the one we want,
                        // namely 'Hi atoum !'
                        ->isEqualTo('Hi atoum !')
        ;
    }    

    public function test4 ()
    {
        $this
            // creation of a new instance of the tested class
            ->given($this->newTestedInstance)

            ->then

                    // we test that the getHiAtoum method returns
                    // a string...
                    ->string($this->testedInstance->say())
                        // ... and that this string is the one we want,
                        // namely 'Hi atoum !'
                        ->isEqualTo('Hello World!')
        ;
    }    

    public function test5 ()
    {
        $this
            // creation of a new instance of the tested class
            ->given($this->newTestedInstance)

            ->then

                    // we test that the getHiAtoum method returns
                    // a string...
                    ->integer($this->testedInstance->add(1,2))
                        // ... and that this string is the one we want,
                        // namely 'Hi atoum !'
                        ->isEqualTo(3)
        ;
    }        

    public function test6 ()
    {
        $this
            // creation of a new instance of the tested class
            ->given($this->newTestedInstance)

            ->then

                    // we test that the getHiAtoum method returns
                    // a string...
                    ->integer($this->testedInstance->add(1,2))
                        // ... and that this string is the one we want,
                        // namely 'Hi atoum !'
                        ->isEqualTo(4)
        ;
    }        

    public function testSkipped() {
        $this->skip('This test was skipped');
    }
}

Solution

  • Strange but it's really true!

    Atoum cares about ?> at the end of PHP files
    There should be nothing after ?>
    Even new line

    I replaced all ?> in all PHP files with space and the problem is solved