2

I have a MacBook Pro, installed PEAR, installed PHPUnit, so at the command line I can type phpunit and I get the usage help.

Now I want to get a test going so that I can build from there.

I have a file named index.php with this content:

<?php

require_once '?????';


class Product {

    protected $id;

    public function __construct($id) 
    {
        $this->id = $id;
    }

    public function get_id()
    {
        return $this->id;
    }

}

class ProductTest extends PHPUnit_Framework_TestCase
{
    function testBasis()
    {
        $instance = new Product(1);

        $this->assertInstanceOf('Product',$instance);
        $this->assert($instance->get_id(), 1);
    }
}

At the command line, I want to go to the directory in which the file is located and type something like:

phpunit ?????

What are the next steps so that I am able to test the above class with PHPUnit from the command line?

3
  • phpunit.de/manual/current/en/writing-tests-for-phpunit.html Commented Jan 4, 2011 at 15:06
  • yes, that is the documentation I am reading (chapter 4) where chapter 3 shows how to install pear/phpunit which I did, and chapter 4 assumes that the rest in installed, but when I type "phpunit productTest" it can't find "./ProductTest.php" and when I type "phpunit index.php" it says, "callt o undefined method ProductTest::assert()", I need to get a basic example working so I can use that documentation. Commented Jan 4, 2011 at 15:13
  • 1
    Becouse assert is not defined , have a look at chapter 21 to see all available functions ( eg. looks like you're trying to use assert when you realy need is assertEquals() , allso assertInstanceOf() needs to be replaced with isInstanceOf() ) Commented Jan 4, 2011 at 15:20

2 Answers 2

7
  1. If you installed phpunit proprely you don't need the include line .
  2. class ProductTest extends PHPUnit_Framework_TestCase
  3. Save the file ProductTest.php
  4. At the command line browse using "cd" to the directory where you saved ProductTest.php
  5. If you installed phpunit proprely you should be able to enter phpunit --verbose ProductTest.php

Your ProductTest.php file will have to look like this :

<?php

class Product {

    protected $id;

    public function __construct($id) 
    {
        $this->id = $id;
    }

    public function get_id()
    {
        return $this->id;
    }

}

class ProductTest extends PHPUnit_Framework_TestCase
{
    function testBasis()
    {
        $instance = new Product(1);

        $this->isInstanceOf('Product',$instance);
        $this->assertEquals($instance->get_id(), 1);
    }
}

?>

At the command line running phpunit --verbose ProductTest , will output :

PHPUnit 3.4.13 by Sebastian Bergmann.

ProductTest
.

Time: 0 seconds, Memory: 6.50Mb

OK (1 test, 1 assertion)
dorin@ubuntu:/var/www$ phpunit --verbose ProductTest
PHPUnit 3.4.13 by Sebastian Bergmann.

ProductTest
.

Time: 0 seconds, Memory: 6.50Mb

OK (1 test, 1 assertion)
Sign up to request clarification or add additional context in comments.

3 Comments

ok, I did that and it tells me PHP Fatal error: Call to undefined method ProductTest::assert() which is telling me that it is not finding PHPUnit_Framework_TestCase or what could be the reason?
copy my exact code , save it in a file called ProductTest.php and you'll see all tests will pass .
yes, it works now, assert and the file name were the problems, thanks
0

I upgraded the phpunit version from 3.4 to 3.6 using these instructions and it solved my assertInstanceOf missing function problem. if someone came on this thread for looking same problem, should think to upgrade to latest version of phpunint test

Comments

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.