1

I have a PHP project, with the following project structure.

php_test_app
    src
        Vegetable.php
    tests
        StackTest.php
        VegetableTest.php

The code of these files is shown below. I use PDT and PTI in Eclipse. PHPUnit in Eclipse recognizes that VegetableTest.php belongs to Vegetable.php, because you can toggle between them using the toggle button.

I first try to run the test code by selecting the tests directory in the PHP Explorer and click Run Selected PHPUnit Test. It runs both tests, both the VegetableTest fails with the following trace: Fatal error: Class 'Vegetable' not found in /Users/erwin/Documents/workspace/php_test_app/tests/VegetableTest.php on line 8. A similar issue was posted here: phpunit cannot find Class, PHP Fatal error.

Indeed, I haven't included my source code yet, so now I uncomment the include in VegetableTest.php, shown below. If I now try to run the tests in the same way, PHPUnit does not recognize any test code! Even the StackTest, which is unaltered, is not recognized.

  1. How should I make the include such that the unit tests are recognized?
  2. Do I need to specify the full path, or just the name of the file in which the class is defined?

Changing the include statement also doesn't work; I have tried the following.

include 'Vegetable.php';
include 'src/Vegetable.php';
include '../src/Vegetable.php';

Vegetable.php

<?php

// base class with member properties and methods
class Vegetable {

    var $edible;
    var $color;

    function Vegetable($edible, $color="green")
    {
        $this->edible = $edible;
        $this->color = $color;
    }

    function is_edible()
    {
        return $this->edible;
    }

    function what_color()
    {
        return $this->color;
    }

} // end of class Vegetable

// extends the base class
class Spinach extends Vegetable {

    var $cooked = false;

    function Spinach()
    {
        $this->Vegetable(true, "green");
    }

    function cook_it()
    {
        $this->cooked = true;
    }

    function is_cooked()
    {
        return $this->cooked;
    }

} // end of class Spinach

StackTest.php

<?php
class StackTest extends PHPUnit_Framework_TestCase
{
    public function testPushAndPop()
    {
        $stack = array();
        $this->assertEquals(0, count($stack));

        array_push($stack, 'foo');
        $this->assertEquals('foo', $stack[count($stack)-1]);
        $this->assertEquals(1, count($stack));

        $this->assertEquals('foo', array_pop($stack));
        $this->assertEquals(0, count($stack));
    }
}
?>

VegetableTest.php

<?php
// require_once ('../src/Vegetable.php');

class VegetableTest extends PHPUnit_Framework_TestCase
{
    public function test_constructor_two_arguments()
    {
        $tomato = new Vegetable($edible=True, $color="red");

        $r = $tomato.is_edible();
        $this->assertTrue($r);

        $r = $tomato.what_color();
        $e = "red";
        $this->assertEqual($r, $e);
    }
}

class SpinachTest extends PHPUnit_Framework_TestCase
{
    public function test_constructor_two_arguments()
    {
        $spinach = new Spinach($edible=True);

        $r = $spinach.is_edible();
        $this->assertTrue($r);

        $r = $spinach.what_color();
        $e = "green";
        $this->assertEqual($r, $e);
    }
}
?>
Community
  • 1
  • 1
physicalattraction
  • 6,485
  • 10
  • 63
  • 122

1 Answers1

1

phpunit --bootstrap src/Vegetable.php tests instructs PHPUnit to load src/Vegetable.php before running the tests found in tests.

Note that --bootstrap should be used with an autoloader script, for instance one generated by Composer or PHPAB.

Also have a look at the Getting Started section on PHPUnit's website.

Sebastian Bergmann
  • 7,837
  • 1
  • 27
  • 35
  • Thanks. I now included the bootstrap file, and it seems to recognize at least the existence of the class. However, now it does not recognize the first function in the class: `Fatal error: Call to undefined function is_edible() in /Users/erwin/Documents/workspace/php_test_app/tests/VegetableTest.php on line 9` – physicalattraction Mar 26 '15 at 07:53
  • It also still leaves open the question why no tests are recognized if you manually include the files in the tests. – physicalattraction Mar 26 '15 at 07:56
  • I think I found an answer to both my questions. The first one was a simple PHP error (I used `$obj.method()` instead of `$obj->method()`). The scond one is that it does not run if there is a parse error, which I found in the log. – physicalattraction Mar 26 '15 at 12:14