2

I have: PHP 5.3.8, PHPUnit 3.5.15, Netbeans 7.0.1

While using the standard example of Netbeans for PHPUnit testing, it runs perfectly. By adding just the "namespace test;" I get the error that Calculator.php is not a file nor a directory. How to solve this problem? (I would like to use the namespace declarative in my project)

THE CLASS TO TEST:

namespace test;

class Calculator {

/**
 * @assert (0, 0) == 0
 * @assert (0, 1) == 1
 * @assert (1, 0) == 1
 * @assert (1, 1) == 2
 * @assert (1, 2) == 4
 */
public function add($a, $b) {
    return $a + $b;
}

}

?>

THE UNIT TEST:

require_once dirname(__FILE__) . '/../Calculator.php';

/**
 * Test class for Calculator.
 * Generated by PHPUnit on 2011-09-11 at 00:52:24.
 */
class CalculatorTest extends PHPUnit_Framework_TestCase {

/**
 * @var Calculator
 */
protected $object;

/**
 * Sets up the fixture, for example, opens a network connection.
 * This method is called before a test is executed.
 */
protected function setUp() {
    $this->object = new Calculator;
}

/**
 * Tears down the fixture, for example, closes a network connection.
 * This method is called after a test is executed.
 */
protected function tearDown() {

}

/**
 * Generated from @assert (0, 0) == 0.
 */
public function testAdd() {
    $this->assertEquals(
            0, $this->object->add(0, 0)
    );
}

/**
 * Generated from @assert (0, 1) == 1.
 */
public function testAdd2() {
    $this->assertEquals(
            1, $this->object->add(0, 1)
    );
}

/**
 * Generated from @assert (1, 0) == 1.
 */
public function testAdd3() {
    $this->assertEquals(
            1, $this->object->add(1, 0)
    );
}

/**
 * Generated from @assert (1, 1) == 2.
 */
public function testAdd4() {
    $this->assertEquals(
            2, $this->object->add(1, 1)
    );
}

/**
 * Generated from @assert (1, 2) == 4.
 */
public function testAdd5() {
    $this->assertEquals(
            4, $this->object->add(1, 2)
    );
}

}

?>

2 Answers 2

4

@Tomasz answer is, of course, correct. As a little addon:

From what i understand it has become common practice to put your tests in the same namespace as your production class.

namespace test;
require_once dirname(__FILE__) . '/../Calculator.php';

class CalculatorTest extends \PHPUnit_Framework_TestCase {

then you can continue using

$this->object = new Calculator:
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Ahh, this was my problem: the backslash before the superclass. \PHPUnit_FrameworkTestCase solved it. Makes sense too.
3

Learn about using namespaced classes in your code. You need to create Calculator class instance not with:

$this->object = new Calculator;

but:

$this->object = new \test\Calculator;

I assume that class is loaded. If not see your autoloader or correct file path.

1 Comment

Worked like a charm, thank you Tomasz (I also had to remove the namespace from the Unit Test)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.