10

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.

Rui Moreira
  • 167
  • 2
  • 4
  • 11
  • http://stackoverflow.com/questions/15193816/phpunit-doesnt-allow-me-to-include-files – Felippe Duarte May 24 '16 at 16:14
  • Thank you for the answer @FelippeDuarte, but Im having some trouble understanding the best answer in that question. – Rui Moreira May 24 '16 at 18:47
  • Your problem seems to be something with your nifvalidation.php file. Could you show the code for us? – Felippe Duarte May 24 '16 at 19:30
  • @FelippeDuarte you're right. The problem is with my nifvalidation.php file, since I tested with a different file and there is no problem. My nifvalidation.php has a class that extends a prestashop class: – Rui Moreira May 24 '16 at 21:35

3 Answers3

8

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>
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
7

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");
Akash Panda
  • 330
  • 3
  • 13
  • Yes, the structure is like that. I tried using the include as you suggested but unfortunately doesn't work either. – Rui Moreira May 24 '16 at 18:41
0

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

Fredrik Schöld
  • 1,588
  • 13
  • 20