Home » Php » php – How to include file in phpunit test?

php – How to include file in phpunit test?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m having some trouble including a file in a phpunit test. For example: when I execute the following code in PhpStorm I get the expected output.

Code:

class NifvalidationTest extends PHPUnit_Framework_TestCase
{
    public function test_apiRequest()
    {
        $result = 1+1;
        $this->assertEquals(2, $result);
    }
}

Output:

Testing started at 16:58 ...
PHPUnit 5.2.12 by Sebastian Bergmann and contributors.



Time: 120 ms, Memory: 11.50Mb

OK (1 test, 1 assertion)

Process finished with exit code 0

But when I need to access a method from another class using the include, I dont get the expected output. Just as an example, when I execute the following code:

class NifvalidationTest extends PHPUnit_Framework_TestCase
{
    public function test_apiRequest()
    {
        include('/../nifvalidation.php');
        $result = 1+1;
        $this->assertEquals(2, $result);
    }
}

I get this instead of the expected output:

Testing started at 17:05 ...
PHPUnit 5.2.12 by Sebastian Bergmann and contributors.


Process finished with exit code 0

Any ideas on why the include is breaking the test?

Note 1: In the above example I dont need to include the file, but I need in another tests.

Note 2: The path to the file ‘nifvalidation.php’ is correct.

How to&Answers:

I think your include path is wrong.
Your structure may be somewhat like this
ParentDir
  -> nifvalidation.php
  -> testsFolder
     -> NifvalidationTest.php

instead of

include('/../nifvalidation.php')

use

include(dirname(__FILE__)."/../nifvalidation.php");

Answer:

You can use the bootstrap flag when invoking the tests from the command line to include any files, define constants, and load all your variables, classes, etc.

--bootstrap <file>        A "bootstrap" PHP file that is run before the tests.

For example, create an autoload.php which defines the constants and includes your files, then you can invoke it from the command line as such:

phpunit --bootstrap autoload.php testsFolder/NifvalidationTest

For a more automated approach, you can also create a phpunit.xml file which contains the bootstrap information:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="autoload.php">
    <testsuites>
        <testsuite name="Nifvalidation Test Suite">
            <directory>./testsFolder</directory>
        </testsuite>
    </testsuites>
</phpunit>

In this specific case, based on your comments about the contents of nifvalidation.php the script exits because PS_VERSION is not defined. If you’re doing isolated unit tests that simply require one dummy constant defined, then you can define that to be present in your test environment. Then you can simply bootstrap your nifvalidation.php file.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../nifvalidation.php">
    <testsuites>
        <testsuite name="Nifvalidation Test Suite">
            <directory>./testsFolder</directory>
        </testsuite>
    </testsuites>
    <php>
        <const name="PS_VERSION" value="whatever you need here"/>
    </php>
</phpunit>

Answer:

Use require() or require_once() instead of using include()