5

I'm developing a project in symfony2 and I'm new with unit testing.

I have installed PHPUnit 3.6.10 via PEAR and it works from the terminal when I digit the phpunit command.

I wrote my first test class following the SensioLab suggestions (http://symfony.com/doc/current/book/testing.html) but when I use the command

php -c app src/My/CalendarBundle/Tests/Calendar/CalendarTest.php 

I got

Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /Library/WebServer/Documents/calendar/src/My/CalendarBundle/Tests/Calendar/CalendarTest.php on line 7

Here you are my test class:

<?php
namespace My\CalendarBundle\Tests\Calendar;

use My\CalendarBundle\Calendar\Calendar;

class CalendarTest extends \PHPUnit_Framework_TestCase
{
    public function testGetNextMonth()
    {
        $calendar = new Calendar('09', '2012', null);        
        $result = $calendar->getNextMonth();

        $this->assertEquals(10, $result);
    }
}

I read this discussion Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...? but the symfony documentation doesn't say to include PHPUnit...

What I'm doing wrong? Thank you

Community
  • 1
  • 1
Gianluca78
  • 794
  • 1
  • 9
  • 24

2 Answers2

11

I just had a similar issue (with DoctrineFixturesBundle), and solved it by adding PHPUnit to Symfony (as opposed to installing PHPUnit via PEAR).

What worked for me was:

1) add "phpunit/phpunit": "4.0.*" to the require section of composer.json:

{
    "require": {
        ...
        "phpunit/phpunit": "4.0.*"
    }
}

2) running from the commandline:

php composer.phar update phpunit/phpunit
Paolo Stefan
  • 10,112
  • 5
  • 45
  • 64
  • 9
    Chances are you don't need phpunit in your production environment. That said I would add it to **require-dev** not **require**. – Marcel Burkhard Nov 14 '14 at 07:03
  • 2
    Or you can install it from command line: composer require --dev phpunit/phpunit (--dev parameter for placing the dependency to the require-dev section of composer.json ) – Vasily Aug 18 '15 at 09:54
0

In case someone runs into a similar issue.

1- Install PHPUnit following this procedure:

$ pear config-set auto_discover 1
$ pear install pear.phpunit.de/PHPUnit

2- Run your tests as described here:

$ phpunit -c app/ src/My/CalendarBundle/Tests/Calendar/CalendarTest.php 

The -c app/ option will be looking for a configuration file in the app/ directory. This configuration file is app/phpunit.xml.dist.

Mick
  • 30,759
  • 16
  • 111
  • 130