I am using Laravel to build my own website, and I want to know the coverage for my source code during a user interaction event instead of writing testing cases using PHPUnit.
From my perspective, PHPUnit only generate reports that the unit tests touch, but if I want to get the code coverage during the execution, meaning generate the code coverage report after php artisan serve
What should I do to achieve this feature? For instance:
$filter = new Filter;
$filter->includeDirectory('/path/to/directory');
$coverage = new CodeCoverage(
(new Selector)->forLineCoverage($filter),
$filter
);
$coverage->start();
// Some User Interactions to the Web Service
$coverage->stop();
// Generating reports during the interactions
(new HtmlReport)->process($coverage, '/tmp/code-coverage-report');
All comments, answers, and bits of advice are welcome.
Thank you.
Related
To contextualise, in similar tools, PHPMD and PHPCS, one can specify a custom formatter for result output, eg:
PHPMD:
vendor/bin/phpmd test \\my\\namespace\\renderers\\phpmd\\AdamFormat phpmd.xml
PHPCS:
vendor/bin/phpcs --standard=phpcs.xml --report=./src/renderers/phpcs/AdamFormat.php
I'm looking to do the same thing for PHPUnit, but have thusfar drawn a blank (investigation, googling, searching here). Looking at the code of PHPUnit, it all seems a bit hard-codey to me:
Code coverage handler:
if (isset($arguments['coverageClover'])) {
$this->printer->write(
"\nGenerating code coverage report in Clover XML format ..."
);
try {
$writer = new CloverReport;
Logging:
if (isset($arguments['testdoxHTMLFile'])) {
$result->addListener(
new HtmlResultPrinter(
I have not spotted anywhere in the docs that suggest otherwise. Seems like an odd shortfall to me.
So two questions:
Am I reading things right? PHPUnit doesn't support this?
Presuming that's "yes: not supported", has anyone had any success with a tactic to circumventing this in a non Heath Robinson manner?
I realise that one can use the --coverage-php option to output the results as PHP variables for another process to then utilise to do [whatever], but that seems like an inside out approach to me, and falls into the Heath Robinson category.
We want to use the most current PHP_CodeCoverage API (https://github.com/sebastianbergmann/php-code-coverage) in our project.
To be sure it's really the case we just made an addtional script in bin/behat-coverage
#!/usr/bin/env php
<?php
require_once (\dirname(__FILE__).'/../vendor/autoload.php');
// Making coverage according to https://github.com/sebastianbergmann/php-code-coverage
$filter = new \SebastianBergmann\CodeCoverage\Filter();
$filter->addDirectoryToWhitelist(\dirname(__FILE__).'/../src');
$filter->removeDirectoryFromWhitelist(\dirname(__FILE__).'/../vendor');
$coverage = new \SebastianBergmann\CodeCoverage\CodeCoverage(new \SebastianBergmann\CodeCoverage\Driver\Xdebug(), $filter);
/**
* Creates the code coverage.
* NOTE! Behat calls the function exit (indirectly). So we have to make the coverage report in a function which
* is fired on exit.
*/
function ____on_behat_shutdown____() {
global $coverage;
$coverage->stop();
$writer = new \SebastianBergmann\CodeCoverage\Report\Html\Facade;
$writer->process($coverage, \dirname(__FILE__).'/../var/code-coverage-report');
}
register_shutdown_function('____on_behat_shutdown____');
$coverage->start('myproj');
require_once(\dirname(__FILE__).'/../vendor/behat/behat/bin/behat');
This seems to work fine.
But:
Is there a way to make execution speed the same as issuing vendor/bin/behat directly?
Is this the best way to use the most current PHP_CodeCoverage API?
Is there a way to make the output colorful again in Git Bash/Power Shell?
Is there a way to custom adjust colors in the shells mentioned above?
I would like to generate code coverage from functional testing in Silex App via PHPUnit. I created sandbox where you could reproduce.
The question is: Why Controller::indexAction() method is marked as Not Executed code in code coverage report?
Thank you!
No time for testing.
What i have seen:
You are setting your test array for the first test in app.php
return new \Symfony\Component\HttpFoundation\JsonResponse(['foo' => 'bar']);
Why? And did the test fail if you remove that? Maybe here the Controller isn't tested.
Then you are testing the 2 methods not in the same way.
Maybe that leads to the solution of the problem.
I want to use a local installation of PHPUnit (via composer) to run my tests and display it on screen (acessing /admin/tests for instance). But the only way to run tests I found in the documentation was the command line tool.
Bellow is an hypothetical example of what I'm looking for:
$session = new PHPUnit_TestSession('path/to/folder');
$results = $session->runAll();
echo $results->failuresCount();
// other hipotetical $result->methods...
// maybe $results->dump()
This may be an overkill but you are in for a treat: https://github.com/NSinopoli/VisualPHPUnit :)
EDIT Here is a rudimentary use of PHPUnit using the TextUI_TestRunner
// make sure you have PHPUnit on your path
require_once "PHPUnit/Framework/TestSuite.php";
require_once "PHPUnit/TextUI/TestRunner.php";
$suite = new PHPUnit_Framework_TestSuite();
$suite->addTestSuite('YourTestCase');
// run the test suite with TextUI_TestRunner
PHPUnit_TextUI_TestRunner::run($suite);
The YourTestCase class is a subclass of PHPUnit_Framework_TestCase, which you can read more on how to write at the official website: http://www.phpunit.de/manual/3.2/en/writing-tests-for-phpunit.html
However, I'd also recommend getting a copy of this book: http://www.amazon.com/Advanced-PHP-Programming-George-Schlossnagle/dp/0672325616 The author teaches you quite a few cool tricks, including autoloading tests, etc.
Does anyone know how to code restler to work with php and mysql to produce something like the following:
I want to create a XML API Web Service and not sure where to start.
I want people to be able to query the database for information such as the following using a http request.
Example of Data
BrandName
Price
ShortDescription
SKU
Example Query
http://website.com/productxml?dep=1&Count=3&BrandName=Y&Price=Y
How would I go about writing such a script as I have searched the internet and cant find any examples and was wondering if you can help.
Thanks in advance
Roy
You could use Restler (http://luracast.com/products/restler/) and build a method
class YourClass {
public function productxml($dep, $Count, $BrandName, $Price) {
// your MySQL stuff
}
}
which handles your request.
See the examples (http://help.luracast.com/restler/examples/) how this can be done.
Hope this helps.
Greets.
You could use Restler #Restler Luracast.
The development has increased alot and its stable.
The fun part about this framework is that it supports multiple formats. All these formats can be added by just inserting a single line of code:
require_once '../../../vendor/restler.php';
use Luracast\Restler\Restler;
$r = new Restler();
$r->setSupportedFormats('JsonFormat', 'XmlFormat'); <---- Add format here
$r->addAPIClass('BMI');
$r->handle();
Also I would like to refer to my Luracast Restler template on bitbucket its public and its there for everybody to see.
I combined Restler with Doctrine so catching data from databases has never been easier. Its a raw version for now but I'll update it soon.
My version uses vagrant. Its a extension to virtualisation technology that makes development setup easy and fast. Once your application is ready you can deploy it to your server.
Link:Restler+Doctrine
1) Install virtualbox + vagrant
2) Clone my repository
3) Move to the cloned directory.
4) vagrant up
5) Enjoy and start programming your REST API in less than 10 minutes.