News Archive
PhpRiot Newsletter
Your Email Address:

More information

How To Run Your Component's Unit Tests

Note: This article was originally published at Planet PHP on 26 March 2011.
Planet PHP

In my Beyond Frameworks talk, I explained how a component-based architecture can help answer some of the important (i.e. expensive!) questions you might face when creating long-lived apps that rely on a PHP framework. In this series of blog posts, I'm going to look at how to go about creating and working with components.

Earlier today, I created some very basic unit tests for my repustateApi component. Unit tests are an essential part of developing your component, but how do you run them?

Running Your Component's Code From The Command-Line

Many old-skool developers choose to work largely from the command-line. You can happily run PHPUnit by hand from the command line yourself each time, but if you're taking advantage of the extra data that PHPUnit can report back on, that soon gets to be a lot of typing! This is where the build.xml file in our skeleton comes in handy a

To run your unit tests from the command line, all you need to do is run the command phing test:

stuart:~/Devel/sental/repustateApi$ phing test Buildfile: /home/stuart/Devel/sental/repustateApi/build.xml [property] Loading /home/stuart/Devel/sental/repustateApi/build.propertieslint: [phplint] /home/stuart/Devel/sental/repustateApi/src/php/Gradwell/Repustate Api/Client.php: No syntax errors detected run-unittests: [delete] Deleting directory /home/stuart/Devel/sental/repustateApi/review/ code-coverage [mkdir] Created dir: /home/stuart/Devel/sental/repustateApi/review/code-c overage [exec] Executing command: phpunit --bootstrap=/home/stuart/Devel/sental/ repustateApi/src/tests/unit-tests/bootstrap.php --coverage-html /home/stuart/ Devel/sental/repustateApi/review/code-coverage --coverage-clover /home/stuart /Devel/sental/repustateApi/review/logs/phpunit.xml /home/stuart/Devel/sental/ repustateApi/src/tests/unit-tests 2&1 [exec] PHPUnit 3.5.3 by Sebastian Bergmann. [exec] [exec] .EE [exec] [exec] Time: 0 seconds, Memory: 4.00Mb [exec] [exec] There were 2 errors: [exec] [exec] 1) Gradwell\RepustateApi\ClientTest::testCanRetrieveSentimentScor eForTextByJson [exec] Exception: oh dear [exec] [exec] /home/stuart/Devel/sental/repustateApi/src/php/Gradwell/Repustate Api/Client.php:209 [exec] /home/stuart/Devel/sental/repustateApi/src/php/Gradwell/Repustate Api/Client.php:151 [exec] /home/stuart/Devel/sental/repustateApi/src/php/Gradwell/Repustate Api/Client.php:90 [exec] /home/stuart/Devel/sental/repustateApi/src/tests/unit-tests/php/G radwell/RepustateApi/ClientTest.php:70 [exec] [exec] 2) Gradwell\RepustateApi\ClientTest::testCanRetrieveSentimentScor eForUrlByJson [exec] Exception: oh dear [exec] [exec] /home/stuart/Devel/sental/repustateApi/src/php/Gradwell/Repustate Api/Client.php:209 [exec] /home/stuart/Devel/sental/repustateApi/src/php/Gradwell/Repustate Api/Client.php:151 [exec] /home/stuart/Devel/sental/repustateApi/src/php/Gradwell/Repustate Api/Client.php:116 [exec] /home/stuart/Devel/sental/repustateApi/src/tests/unit-tests/php/G radwell/RepustateApi/ClientTest.php:83 [exec] [exec] FAILURES! [exec] Tests: 3, Assertions: 1, Errors: 2. [exec] [exec] Writing code coverage data to XML file, this may take a moment. [exec] [exec] Generating code coverage report, this may take a moment. Execution of target "run-unittests" failed for the following reason: /home/st uart/Devel/sental/repustateApi/build.xml:108:40: Task exited with code 2 BUILD FAILED /home/stuart/Devel/sental/repustateApi/build.xml:108:40: Task exited with cod e 2 Total time: 0.5951 seconds

That went bang in a spectacular way! At this stage, where we have tests but don't yet have a working Repustate API client, that's exactly what should happen. As I work through the code, and satisfy the tests, we should quickly get to the point where we have more tests, and that they all pass.

Here's an example of another component - our CommandLineLib - which has many tests that all succeed, just so that you can see what a successful test run should look like:

Buildfile: /home/stuart/Devel/GWC/CommandLineLib/bu

Truncated by Planet PHP, read more at the original (another 7227 bytes)