PHPUnit whitelist code coverage - php

The whitelist isn't working how I would expect when get code coverage reports. I am using Zend Framework and my Zend files are also in the library directory just like the project quickstart.
I'm running PHPUnit 3.6 from Netbeans 7.0.1
I only really want to see the coverage for the classes for MyLib.
<whitelist>
<directory suffix=".php">../../library/MyLib</directory>
</whitelist>
But in the report I seem to get a percentage value for several Zend files eg. Zend_Controller_Front, Zend_Loader_Autoloader etc.
Obviously I haven't written any tests for these classes and it is no surprise that I have 0% coverage.
What have I missed?

Please use absolute paths in your XML file and/or ensure those are correctly resolved by your phpunit test-runner call. Also ensure that your XML file is loaded.

Related

PHPUnit command-line. Configuring --bootstrap

Sorry for the basic nature of this question but I've tried figuring this out through phpunit docs and online searching but can't piece it together.
I have a number of php classes that I would like to test against via a phpunit cli interface script (windows box) but I'm stuck on a very basic thing.
my test subjects are, e.g., c:\src\classes and test files are here, c:\src\tests
I can run individually by doing the following:
$> phpunit --bootstrap c:\src\classes\<name of class subject> c:\src\tests\<name of test file>
what I want to be able to is:
$> phpunit --bootstrap <something that registers multiple class subjects> c:\src\tests
so it would run against all tests in the c:\src\tests directory.
I've found references to the phpunit.xml that would be read prior to each phunit execution which I assume is where I could provide information about where the classes are for the tests, but all references that I've found to this has been unhelpful and have sent me down a couple of rat holes. Could someone point me to documentation that shows this configuration in plain black and white?
thanks!
What you should pass to --bootstrap is the path of a PHP script that registers an autoloader that is responsible for loading (only) your classes.
Such an autoloader can be created using a tool like phpab. Composer also generates an autoloader for you when you use it to manage the dependencies of your project.

PHPUnit --no-coverage option to overwrite config file

I use PHPUnit to test my projects, using a phpunit.xml.dist config file.
The default configuration for all my projects is set to generate html code coverage reports.
Is there anyway I can run the phpunit command at a given time without generating the code coverage reports without having to change the config file?
This would be something like a --no-coverage option.
As noted by Gonçalo Queirós in this answer, the --no-coverage option is supported since version 4.8.
I couldn't find anything from the documentation to reference any sort of functionality that would provide the option to be a toggle.
You could point PHPunit to a second phpunit.xml that doesn't have the code coverage
You can see this very question being asked prior.
Is there a way to disable code coverage in PHPUnit for a single test?

How to consolidate ZF2 unit/application module tests into a single call?

I'm following the ZF2 convention of storing tests in the modules and everything is working fine when tests are run from within each module. What I would like to do is have a root level phpunit.xml that calls the individual module tests and consolidates them to produce code coverage data and other metrics.
The problem is that each individual test suite is bootstrapped within the modular phpunit.xml files. The only way I'm aware of doing things is to configure the bootstrap in each phpunit.xml file which obviously doesn't get picked up when running tests from root as individual xml files are ignored.
So my question is: is there a way for a root-level phpunit.xml file to read individual phpunit.xml and bootstrap files from modules, a kind of phpunit config inheritance if you will? I could go down the route of writing this in Phing or a CI script but I'd like it done quick and dirty while in development and this solution still wouldn't produce a consolidated code report.
Basically, I want something like this, but rather than run the tests, I want it to run the individual phpunit.xml files within each module. Is this possible?
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<testsuites>
<testsuite name="Site Tests">
<directory>./module/Application/test/ApplicationTest</directory>
<directory>./module/User/test/UserTest</directory>
</testsuite>
</testsuites>
</phpunit>
Update
The aim is to have code metrics generated by PHPUnit that give a project overview, not a modular specific overview. I appreciate the scripts in the answers will run the unit tests on a per-module basis but this is not what I'm looking for. I understand that this may be a limitation as far as PHPUnit is concerned. I will look into this over the next few days and try and find a solution as it seems like something that would be useful in a lot of projects that deal with custom modules.
Update 2
Robert Basic came up with a script that creates a directory structure with the module reports inside and it works great but would be nice to have it running within PHPUnit with the proper metrics reporting.
https://gist.github.com/robertbasic/5329789
If you are using Linux you could create a simple script. Not exactly the solution you wanted, but could help none the less:
#!/bin/sh
modDir=$(pwd)
for i in *; do
if [[ -d $i/test ]]; then
cd $i/test
phpunit
cd $modDir
fi
done
You could just drop that in to a runtests.sh file in the module directory.
Just a thought :)
Having previously discussed this on irc with various people I'm pretty sure you can't, you need to write a script to loop through the modules and run them on a module by module basis.

How to bundle PHPUnit with my code?

I would like to:
Run tests with PHPUnit regardless of my environment (and if PHPUnit or PEAR is installed or not)
Show test results on screen if possible
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...?
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
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).
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.

Don't test groups( PHPUnit support #group annotation) functions of class extends from Yii in Netbeans

Current I've a problem with Yii, when I'd using "#group annotation" ( support by PHPUnit ) to test a groups function in my class extends from Yii.
I'd configured as guidance in forum of Yii (NetBeans IDE and Yii projects).
I'd tested all functions in my class, when I press Ctrl + F6. It runs very good.
But when I'd used "#group annotation" (support by PHPUnit). It doesn't run, I don't see the dialog box.
When I check "Ask for Test Groups Before Running Tests" in dialog configure PHPUnit. When I press Ctrl + F6, I don't see the dialog box.
I thing this's problem here are two file "bootstrap.php" and "phpunit.php", but I don't how alter it?
I had question as it for forum Yii, and demo code.
How to alter bootstrap.php or phpunit.xml?
Just open in your favourite text editor, Netbeans will suffice. As to the meaning of these files, Referring to phpunit documentation available in english, french and japanese it would seem.
bootstrap - A "bootstrap" PHP file that is run before the tests.
configuration - If phpunit.xml or phpunit.xml.dist (in that order) exist in the current working directory and --configuration is not used, the configuration will be automatically read from that file.
I would not worry about bootstrap.php if you haven't needed it you probably won't and suffice to say nothing you can add there will improve the behaviour in Netbeans.
With regards to configuring groups the phpunit documentation is quite elaborate:
The <groups> element and its <include>, <exclude>, and <group> children can be used to select groups of tests from a suite of tests that should (not) be run.
<groups>
<include>
<group>name</group>
</include>
<exclude>
<group>name</group>
</exclude>
</groups>
The XML configuration above corresponds to invoking the TextUI test runner with the following switches:
--group name
--exclude-group name
Again nothing which would improve netbeans with regard to the Ctrl+F6 usage you are after, instead I would also suggest you refer to Devin M's explanation, as #hakre already suggested, combine that with the Netbeans - run test groups documentation and there is nothing more I could add still as it is complete, I am afraid.
Good luck and please let us know how things have turned out for you.

Categories