59

I want to start unit testing my symfony 2 application with phpunit. I installed phpunit using composer (per-project dependancy). http://www.phpunit.de/manual/current/en/installation.html

How do I now run the phpunit command on Zend Server? I don't have pear installed.

Tjorriemorrie
  • 16,818
  • 20
  • 89
  • 131

6 Answers6

79

If you followed the documentation, you have set the phpunit/phpunit dependency as a 'dev-dependency'.

If you don't have composer, you need to install it first. This is explained in the documentation: Installation *nix or Installation Windows. If you already installed composer, it is a good practise to update composer to the latest version by running the self-update command:

$ php composer.phar self-update

After your have done that, you need to install all dependencies, including the dev dependencies. This is done by running the update command with the --dev switch:

$ php composer.phar update --dev

All the dependencies are installed in the vendor directory. PHPunit runs from the console. Composer automatic put the console files inside the vendor/bin directory. You need to execute the phpunit file in there:

$ vendor/bin/phpunit -c app/

The -c switch tells PHPUnit to look for the configuration file in the app directory, Symfony2 already set up the correct configuration to run all tests that are in the <bundle>/Tests directory.

UPDATE (05-04-2013)

Composer has changed their update/install commands. update will install dev dependencies by default and if you want to install dev dependencies, you need to use the --dev option.

UPDATE (11-06-2013)

Composer has changed their commands again, the install command will also install dev dependencies.

Wouter J
  • 41,455
  • 15
  • 107
  • 112
  • Aha! missed the --dev :p Anyway, I get the following error now: `Warning: include(C:\Program Files (x86)\Zend\Apache2\Program Files (x86)\Zend\Ap ache2\htdocs\project1\vendor\phpunit\phpunit\PHPUnit\TextUI\Command.php): failed to open stream: No such file or directory in C:\Program Files (x86)\Zend\Apache 2\htdocs\project1\vendor\composer\ClassLoader.php on line 150.` The file does exist though. – Tjorriemorrie Dec 07 '12 at 14:36
  • I continued my error here: http://stackoverflow.com/questions/13765651/phpunits-textui-command-php-not-found – Tjorriemorrie Dec 07 '12 at 15:04
  • @WouterJ Did you mean "update will _not_ install dev dependencies by dafault"? – Danack Jul 10 '13 at 18:22
  • @Danack no, the update command will install dev dependencies by default – Wouter J Jul 10 '13 at 19:12
  • @WouterJ Er, looks like I need to run composer self-update again. – Danack Jul 11 '13 at 09:40
  • I ended up installing all these dependencies for PHPUnit to stop complaining: php-invoker, dbunit, phpunit-selenium and phpunit-story. Don't know why it needs all that. – Parziphal Jul 22 '14 at 20:23
  • @WouterJ, Regarding your updates, Why does composer keep changing their commands in non backwards-compatible ways? – Pacerier Jul 30 '15 at 10:02
  • @Pacerier the commands were changed once. And it was back in 2013, where Composer was still in early development. – Wouter J Jul 30 '15 at 10:11
24

What about more composer way?

composer exec phpunit

It can be used for every binary file in vendor/bin directory.

StalkAlex
  • 743
  • 7
  • 16
17

UPDATE (12-02-2014)

Composer and PHPUnit have changed their commands again. The install command will also install dev dependencies:

Composer.json:

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

Run it:

$ composer.phar update --prefer-dist --dev

Now you can run your tests by:

$ bin/phpunit -c /app

Cheers,

medina
  • 8,051
  • 4
  • 25
  • 24
13

I like to define a script within composer.json, so that I can just run:

$ composer test
# ... runs phpunit

To do so, I need to modify composer.json to contain an entry like this:

"scripts": {
    "test": [
        "phpunit tests/*.php"
    ]
}
Flimm
  • 136,138
  • 45
  • 251
  • 267
9

For Symfony 3 add "phpunit/phpunit": "5.4.*" to the "require-dev" section in your composer.json and run tests from the applications root directory with:

./vendor/bin/phpunit tests
André
  • 2,042
  • 1
  • 23
  • 26
5

Add it as dev dependency, in your project directory:

composer require --dev "phpunit/phpunit=4.8.*"

The installed phpunit can now be executed with:

./vendor/bin/phpunit
Alex
  • 3,264
  • 1
  • 25
  • 40