Search code examples
symfonycygwinphpunitcomposer-php

Using PHPUnit as Symfony 2 dependency


I'd like to use PHPUnit to test my Symfony 2 applications. I added PHPUnit to my Symfony 2 project composer.json file as described in the documentation:

"require": {
        ...
        "phpunit/phpunit": "3.7.*"
    },

How can I call PHPUnit now to run tests? I tried to run it via

$ php /path/to/symfony2app/vendor/phpunit/phpunit.php

but I get the following error message:

Warning: require_once(File/Iterator/Autoload.php): failed to open stream: No such file or directory in C:\...\vendor\phpunit\phpunit\PHPUnit\Autoload.php on line 45

Fatal error: require_once(): Failed opening required 'File/Iterator/Autoload.php' (include_path='.;\...\xampp\php\PEAR') in C:\...\vendor\phpunit\phpunit\PHPUnit\Autoload.php on line 45

I guess it is a problem with PEAR missing, but shouldn't PEAR be a dependency in PHPUnit and be installed via composer update?


Solution

  • Composer creates references to the binaries (executable scripts) of the packages it installs inside the so-called "bin-dir". By default, it is located in vendor/bin/, so you would run it as such: php vendor/bin/phpunit. You can usually just call vendor/bin/phpunit too, it should know to execute it with php.

    Under Cygwin it might be necessary to call the .bat file directly, as vendor/bin/phpunit.bat.

    If you would rather have this more accessible, you can add this to your composer.json:

        "config": { 
            "bin-dir": "bin"
        }
    

    Then the file would become accessible as just bin/phpunit within your project, a bit shorter and less hidden.

    Note that none of this is really cygwin specific, it should also work fine under the regular windows cmd and on native unix environments.