CakePHP v1.3: Full Code Coverage Report - php

In CakePHP v2.x, you can generate a unit testing code coverage report from the command line using a test suite that fires all of your app's tests and the --coverage-html option. (Example: cake test app All --coverage-html=tmp/coverage/)
I've been searching for an equivalent for Cake v1.3, which uses SimpleTest. When testing via the browser, it's possible to view code coverage for each individual test case, but not for the whole App category.
Similarly, running tests from the command line via cake testsuite app all does not seem capable of taking any command-line options for generating coverage output. The v1.3 Book doesn't dive into any more detail on the subject.
So my question is: Is there a way to generate a "full" code coverage report for a Cake v1.3 application from the command line?
Additional Information for future searchers:
Per #mark-story's suggestion, I tried running cake testsuite app all cov, but that produces the following fatal error for me:
PHP Fatal error: Class 'CodeCoverageManager' not found in
cake/tests/lib/reporter/cake_base_reporter.php on line 131
This is with SimpleTest v1.0.1 in my app/vendors/simpletest/ folder. I tried upgrading to SimpleTest v1.1.0, but that resulted in a different fatal error:
PHP Fatal error: Call to undefined method CakeCliReporter::SimpleReporter() in
cake/tests/lib/reporter/cake_base_reporter.php on line 85

You might want to try addin 'cov' to the end of the cli runner arguments. The help() command indicates this will work. It has been a whilre, but I think this will output the % of code covered. There is no way to generate a detailed set of coverage reports like you can with phpunit.

Related

Laravel 4 testing "Command"s?

I've tried testing my commands in Laravel 4, as they are significant part of my system, but it seems like the documentation coverage is so poor, that they only explain basic testing of controllers and some models.
In Commands, you can pass arguments via command line to the class and it's received via $this->input property, something I don't know how to emulate.
Whenever I try to run the test for my command, when it expects an argument in "fire" method, I get this error:
Fatal error: Call to a member function getArgument() on a non-object in /var/www/html/project/vendor/laravel/framework/src/Illuminate/Console/Command.php on line 153
Which is logical, there's no argument passed. Is there a way to test this functionality?...
Thanks
Most of it can be done using Symfony Command Tester (since Command is based on Symfony Console), example: http://alexandre-salome.fr/blog/Test-your-commands-in-Symfony2. However this would start to fail if you have to call another artisan command such as $this->call('db:seed'); or etc because this is actually Illuminate\Console\Application specific syntax.
I'm all open if there anyone that have a solution for above scenario.
I have recently made a post about Testing Laravel commands.
If you have a specific part of code that you want to use in multiple commands then you have to move that part of the code somewhere where both commands can use it (Event handler, trait, another class ...) and then in your command reference that code instead of using $this->call('db:seed');

"No tests executed" I cannot run any PHP test in NetBeans at all

I am very new to NetBeans and I just wanted to run a Code Coverage teston my code developed earlier. I do not know what I am doing wrong.
I am using NetBeans 7.3.1 with Wamp Server 2.4, installed PHPUnit and Skeleton Generator through PEAR, and set those files in NetBeans settings.
I made the project using an existing sources. Running in my browser seems OK. I can even debug with XDebug.
But when I right click on the project name and "Test", it says "No tests executed.(0.0 s)" in the Test Result window, and this in the Output window:
PHPUnit 3.7.23 by Sebastian Bergmann.
Configuration read from C:\wamp\www\test\configuration.xml
Time: 141 ms, Memory: 2.00Mb
No tests executed!
Generating code coverage report in Clover XML format ... done
I tried running test on a PHP file, it returns an information dialog box "Test file for the selected source file was not found."
I right clicked on the PHP file, and selected "Tools->Create PHPUnit tests", it returns a warning dialog box "Tests were not generated for the following files: (file name) Review the log in Output Window." but nothing was changed in the Output window.
I generated PHPUnit Bootstrap and XML Configuration, but they did not help.
I wished I can provide screenshots, but I cannot. I am trying to be specific as possible.
I appreciate any help.
I got this error too.. I also got this in the Output tab
Fatal error: Class 'package\path\Tests\TestCase' not found in project\path\package\path\Tests\ClientTest.php on line 25
Here, TestCase is the subclass of PHPUnit_Framework_TestCase and that, along with the current test file is in a different directory structure than the source files. So, in my case, it was a path issue.
Something I noted is I need to add a bootstrap file to include files in the test path and the source path to the runtime.
This bootstrap could be generated by NetBeans. See instructions here: https://netbeans.org/kb/docs/php/phpunit.html#project-specific-configurations
I of course, had another bootstrap.php (added by another developer). I only had to link the bootstrap like follows,
Good luck!
PHPUnits message:
No tests executed!
just means that no tests were found. This can have multiple reasons:
First of all, if there are no tests, no tests are executed.
But it is also possible that there are tests, but the configuration says to skip those (for example some tests are grouped into a group named slow and the configuration XML says to exclude slow from being run).
For your case this most certainly is a configuration issue. I'm not fluent with Netbeans but as you can successfully execute PHPUnit already, that part of your setup looks working.
Next step is to find out when you invoke the test-runner where it looks for tests. It might just be that the tests directory is missing or not specified. E.g. check which configuration is in configuration.xml for example and compare that with the documentation to double-check all settings.
This is not a very concrete answer and somebody who is more fluent with Netbeans might help more, however as you'er not showing further screenshots, this is my honest best bet how to go on with trouble-shooting and why.
Seems I am getting closer to the solution. I found out the problem is not in the configurations, but in the code itself.
I do not know if this is a silly problem, but seems the test only accepts PHP codes written in OOP. I tried making another file with a class and I can make test file out of it with no problem. At least I can see the test result changes from "No test executed" to "No test passed, 1 test skipped"
My codes are written for months without OOP and I know nothing about it at all. Does this mean I have to rewrite all of them again?
It will happen when you put an assertion in a constructor of your test class and this assertion fails.
Then you will see only the warning 'No tests executed!' (not very helpful) but i suppose one should not put assertions in the constructors (this is in phpunit 4.1~).
for it was because my test names were not starting with 'test'

sfYaml echoln() error in Zend Framework project PHPUnit tests

I'm starting a Zend Framework project and I decided to use Doctrine 1.2.3 instead of the Zend_Db classes. I configured my project and doctrine command line script works just fine, my index controller can query my database without problems, but Unit Tests with PHPUnit throws an error:
Fatal error: Cannot redeclare echoln() (previously declared in D:\Zend\Apache2\vhost\myproject\library\Doctrine\Parser\sfYaml\sfYaml.php:132) in D:\Zend\ZendServer\bin\PEAR\SymfonyComponents\YAML\sfYaml.php on line 135
I'm using Zend Server with PHP 5.3. PHPUnit requires the PEAR SymfonyComponents' YAML package. This happens everytime my unit test bootstrap tries to re-create and load the whole testing database, specially when loading the fixtures files. This is the full command and output:
D:\Zend\Apache2\vhost\myproject\tests>phpunit
build-all-reload - Successfully dropped database for connection named 'doctrine'
build-all-reload - Successfully created database for connection named 'doctrine'
build-all-reload - Created tables successfully
build-all-reload - Data was successfully loaded
PHPUnit 3.5.10 by Sebastian Bergmann.
Fatal error: Cannot redeclare echoln() (previously declared in D:\Zend\Apache2\vhost\myproject\library\Doctrine\Parser\sfYaml\sfYaml.php:132) in D:\Zend\ZendServer\bin\PEAR\SymfonyComponents\YAML\sfYaml.php on line 135
D:\Zend\Apache2\vhost\myproject\tests>
As result, my database is completely created and data fixtures are loaded successfully. But this error stops the Unit Testing process. I only have one test class for Doctrine models but this error happens even if I comment its testing code.
It is strange, because if I execute the doctrine script by hand, It runs without problems, and no error happens when the system runs via web. This just happens for Unit Testing.
How can I avoid this libraries collition, if both, Doctrine and PHPUnit include the same sfYaml class from PEAR and from inside Doctrine, without breaking the working system?
You can see in the error message that sfYaml.php is being loaded from two different locations. You must clean up your include path so it has only one copy of each library you need. Use var_dump(get_include_path()) in your bootstrap.php to diagnose the issue.

Why is PHPUnit in Hudson CI using my localhost path AND my actual filepath?

EDIT 3: Solved. See below.
EDIT 2: I think Chadwick's on the right track with his comment. Hudson/PHPUnit is taking the localhost (the Hudson workspace) AND my local file structure and using both to run the unit tests. So it's redeclaring everything that was already declared. Why is this happening and how can I change it?
I've since reported this issue on Hudson's JIRA Server. If I get a resolution there, I'll post here. Otherwise, any help would be appreciated since my builds are going nowhere now.
My build keeps failing and I can't for the life of me understand why. Here's what I get back.
phpunit:
[exec] PHP Fatal error: Cannot redeclare generate_options() (previously declared in
<http://localhost:8080/job/Goals/ws/Goals/includes/functions/registration_fns.php>:5) in /Users
/joshsmith/Sites/Goals/Goals/includes/functions/registration_fns.php on line 32
But this particular function starts at line 5 and ends at line 32! So what in the world is going on here?
And just so you know PHPUnit works on its own outside of Hudson. Here's my terminal output from a successful test run:
Macintosh:goals joshsmith$ phpunit alltests.class
PHPUnit 3.4.14 by Sebastian Bergmann.
.............................
Time: 14 seconds, Memory: 9.75Mb
OK (29 tests, 67 assertions)
Can anyone help me figure this craziness out?
EDIT: Under Chadwick's suggestion, I tried renaming the function in case it were trying to redeclare a function internal to Hudson. This didn't work, and is clearly some other obscure issue.
This has been solved. It was an issue where I had hardcoded the paths in a single initializer file. As a result, they were being redeclared by PHPUnit.
It sounds like generate_options() is declared both in your registration_fns.php file and in some file by Hudson. PHP function names are global, so this is an inherent risk of combining code from a third party.
One way around it is to rename one of the functions (and of course all calls to it) - you can modify the Hudson code where it is declared, or modify your own, but personally I recommend changing your own. Should you upgrade Hudson in the future, you'd likely have to modify it again.
Since PHP 5.3, you also have the option of using namespaces.

PHPUnit code coverage report generation fails

I'm trying some supposedly nice features of PHPUnit, but I cannot generate a freakin' code coverage report. What I get is:
rolf#dev ~/projects/current/tests $ scripts/phpunit --configuration $PROJECTS/current/tests/conf/inc/tests.xml
[...]
Generating code coverage report, this may take a moment.
[...]
Fatal error: require_once(): Failed opening required 'lib/DataSource.php' (include_path=':::') in path/to/lib/WS/DataParser.php on line 10
However, in this very class, a specific include path is defined, and the require_once works like a charm when the application is launched.
Could it be that PHPUnit cannot solve include paths ?
Thanks in advance and long live stackoverflöw!
Rolf
If you're using the latest PHPUNIT (3.5+) it might be because Sebastion has started using an autoloader himself within the program.
You've got to add the line
spl_autoload_register('__autoload');
after your __autoload() function gets created / included / required
so for example, the file that has my autoloader is called functions. in my bootstrap I do the following:
require_once(ROOT_PATH.'/lib/utils/functions.php');
spl_autoload_register('__autoload');
Are you changing your include_path in any of your tests/code? Because it looks really odd (:::)

Categories