Hugo Mota
Hugo Mota

Reputation: 11567

How to bundle PHPUnit with my code?

I would like to:

How can I do this? I tried downloading the code here and including Autoload.php but it still have some dependencies. Maybe there's some better approach for this than trying to bundle it with my code...?

Upvotes: 5

Views: 189

Answers (3)

Alister Bulman
Alister Bulman

Reputation: 35169

There is a consistent package management solution for PHP - http://getComposer.org. It also now has a means to install PHPunit in the usual composer style, http://packagist.org/packages/phpunit/phpunit

With the software installed, it will put the phpunit command line script into the local 'bin/' directory, so you can call it, though you will likely want to have a shell script that also sets the configuration file it will use.

The usual setup is a tests/ subdirectory with the hierarchy of PHPunit-extending classes that run the actual tests.

Upvotes: 1

edorian
edorian

Reputation: 38981

To include PHPUnit in your projects source files I'd suggest following the guide:

Using PHPUnit From a Git Checkout from the PHPUnit Contributung section.

It tells you about all the packages you need to install and shows you show to build a runner/wrapper script for the phpunit executable.

#!/bin/bash
php -d include_path='.:../phpunit/:../dbunit/:../php-code-coverage/:../php-file-iterator/:../php-invoker/:../php-text-template/:../php-timer:../php-token-stream:../phpunit-mock-objects/:../phpunit-selenium/:../phpunit-story/:/usr/local/lib/php' ../phpunit/phpunit.php $*

You can adapt the path to your need or if you want to wrap it in another script you can also use phpunit somewhat programmatically by

require '/path/to/phpunit/PHPUnit/Autoload.php';
PHPUnit_TextUI_Command::main();

This assumes that you ether have a phpunit.xml.dist file or that you use the proper cli parameters when calling your wrapper script.


You can also use the pear packages and unpack all the stable versions instead of working from the git checkout to save some disk and repo space. The wrapper script and all the include path work is the same :)


Related SO questions:

PHP - Is there a portable version of PHPUnit?

PHPUNIT without installation

Upvotes: 2

Matt Whipple
Matt Whipple

Reputation: 7134

The dependencies will be dependent on what add-ons you're using, PHPUnit by itself should be self contained. Since there's no particularly consistent package management solution for PHP (and you've eliminated the most viable options aside from wheel reinvention), your best bet would be to include the files in the source tree separate from the application code. Creating a sibling directory from whatever your APPLICATION_ROOT or similar would be that is named "test" and that has a "lib" or similar directory full of PHPUnit and any dependencies you need for it would likely be a good plan. There should be no overlapping and a one way dependency from the test dir to the main application source.

I'm assuming you're looking for a healthcheck automated test page, so you could create the single page that includes what is needed from that test directory. Ideally if you have the web directory which exposes your static resources you could have the PHP file that is in charge of loading the Front Controller for your application by including the application folder from outside of the document root, and then a second file that loads the test suite. That would allow your application directory to remain focused on the application code itself, the test directory to house your testing code, and then the 2 small include files which are in charge of loading the codebases (with any kind of shared constant definitions, etc. also extracted and kept DRY).

Upvotes: 1

Related Questions