The way we've been developing, many of our PHP files are included only with the expectation that they will output content; no methods are called, etc. My question is, what's the "correct" way to test this with PHPUnit? Should I have the require placed in the setUp so that it waits till then to run, or is that considered bad mojo?
That's about the only way you can test it. I assume you're planning to use output buffering to capture the content produced so that you can check it against expectations.
The best way is probably to refactor your code so as much work as possible is being done in self-contained classes / functions that return results to the caller, suitable for being tested from PHPUnit in a more conventional way.
If your application is a web application (i.e.: outputs content that requires a browser to be readable), then PHPUnit is integrated with Selenium, which is a Web Application Testing System. It's indeed very useful, especially since it has a Firefox plugin.
You can read more on PHPUnit and Selenium in the PHPUnit Manual.
If your templates (i.e.: include files) are used in command line scripts, then use output_buffering and regular expression assertions or regular assertions (i.e.: assertRegExp, assertEquals).
Related
My question is quite simple. I'm coming from Python world, where it's very simple to execute Selenium testing code within a program, just writing something like:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.python.org")
driver.close()
When using PHP, things are getting harder: I wrote something like that
require 'vendor/autoload.php';
class MyTest extends PHPUnit_Extensions_Selenium2TestCase {
public function setUp() {
$this->setBrowser('Firefox');
$this->setBrowserUrl('http://www.python.org');
}
public function testToto() {
$this->url('/');
}
}
...which kinda works when I execute phpunit MyTest.php.
But what I would like to do is to instanciate my test class in PHP code, and execute my Selenium commands "programmatically", like:
$myTest = new MyTest();
$myTest->testToto();
And here it sucks :(
PHP Fatal error: Uncaught exception 'PHPUnit_Extensions_Selenium2TestCase_Exception' with message 'There is currently no active session to execute the 'url' command.
So is there a way to execute Selenium code directly from PHP script without executing command line things with phpunit?
Edit: What am I trying to achieve? My project is to build a testing application which must be able to launch tests within a UI built by a end user thanks to a user friendly drag and drop builder (the user chooses which test he wants to execute first, then another, and so on). So I would like to avid ececuting phpunit commands with a ugly PHP exec: to me, the best option is to launch test case methods programmatically!
I think the pain comes from trying to use the PHPUnit Webdriver integration, without really using PHPUnit.
You can write code like your Python example by using a standalone Webdriver implementation (that does not need PHPUnit). I recommend the one written by Facebook:
https://github.com/facebook/php-webdriver
but there are some more:
http://docs.seleniumhq.org/docs/03_webdriver.jsp#php
You can also use these implementations inside PHPUnit tests. I do that as I don't like the PHPUnit Webdriver implementation.
With these it's trivial to write your example in PHP.
Well, a very nice question first of all. The short answer is yes you can, but it's too much pain. PHPUnit is just a modestly complicated, huge, scary and amazing library with a gadzillion of extensions. In the nutshell it reads the configuration, finds the tests, and runs them.
You can put a break point inside your test and trace to the top what it does, what parameters it accepts and literally simulate the whole thing. That would be the "proper" and crazy way, and the most complex too.
The simpler way would be by finding out what the test case class needs in order to run (break point & trace are always your best friends), in this particular case it turned out to be just this:
$myTest = new MyTest();
$myTest->setUp(); // Your setup will always be called prior the test.
$myTest->prepareSession(); // Specific to Selenium test case, called from `runTest` method.
$myTest->testToto();
But, even in PHPUnit_Extensions_Selenium2TestCase there is a lot of stuff that are not publicly accessible and it feels just a strike of luck. But you get the idea. Besides, simply calling a method of a test case class will result in two things: nothing happens, or you get an exception. All the fancy result tracing happens higher in the hierarchy.
I can only guess what you are trying to achieve, but probably if you ask the question about the actual problem we'd be able to help more.
Edit
exec might seem ugly indeed, but it's there for a very good reason: process isolation. There are situations when one piece of the code that is being tested changes the environment and it becomes conflicting with another piece of code, e.g., session-related, sent headers, etc. When you come across one of them, you will be praying on exec.
In your case, the easiest would be to launch the PHPUnit from the command line, but you might need to write a custom formatter to get the data in the necessary format out of it, unless you are happy with the existing ones.
Another option would be to use the the existing client for the WebDriver / Selenium and simply send commands directly to the Selenium server, I assume that's what you really need? You can find out the piece of code responsible for that in the PHPUnit extension or there's another cool project called Behat (and Mink). I believe their client is in the Behat/MinkSelenium2Driver repository. And if you don't like those, I'm sure there are other php wrappers you can find on the github, or can create your own using the existing ones as an example.
PS: Share a link to the project when it's up and running if its public.
I've recently inherited a large PHP application with NO objects/modules/namespaces...only a lot of files containing functions.
Of course, there is a LOT of dependencies (and all files and almost always included).
I'm looking for a tool that could analyse the files and generate a dependencies graph. It would then be easier to detect independent files/set of files and re-factor the whole thing.
So far the best solution I've found would be to write a CodeSniffer sniff to detect all functions calls and then use that to generate the graph.
It seems something useful for other, so I'm sure tools already exists for it.
What would you recommend ?
I think that the best solution is use a doc generat + grapviz, PHPDocumentor looks to have a Grapviz extension at https://github.com/phpDocumentor/GraphViz
This is a example made with PHPDocumentor:
http://demo.phpdoc.org/Clean/graphs/classes.svg
Too you can use a hierarchical profiler like xhprof (https://github.com/facebook/xhprof), this can draw a tree of all call to functions from a execution.
A example form xhprof draw done by Graphviz
I could recommend a lightweight project I wrote few days ago. Basically I had a 300+ files PHP project and I wanted to detect what files do these files require/include and vice-versa. Moreover, I wanted to check for each individual file what files does this file requires/includes (directly or indirectly, ie. via file inheritance) and vice-versa: what are the files that include this particular file. For any combination of these I wanted an interactive dependency graph (base on file inclusion and not on class/function calls/usage).
Check out the project's sandbox and its source code.
Note that the whole thing was written in only 2 days so don't judge it
too harsh. What's important is that it's doing its job!
For a PHP application I'm developing, I need to read the current git revision SHA which of course I can get easily by using shell_exec or backticks to execute the git command line client.
I have obviously put this call into a method of its very own, so that I can easily isolate and mock this for the rest of my unit tests. So my class looks a bit like this:
class Task_Bundle
{
public function execute()
{
// Do things
$revision = $this->git_sha();
// Do more things
}
protected function git_sha()
{
return `git rev-parse --short HEAD`;
}
}
Of course, although I can test most of the class by mocking git_sha, I'm struggling to see how to test the actual git_sha() method because I don't see a way to create a known state for it. I don't think there's any real value in a unit test that also calls git rev-parse to compare the results? I was wondering about at least asserting that the command had been run, but I can't see any way to get a history of shell commands executed by PHP - even if I specify that PHP should use BASH rather than SH the history list comes up empty, I presume because the separate backticks executions are separate terminal sessions.
I'd love to hear any suggestions for how I might test this, or is it OK to just leave that method untested and be careful with it when the app is being maintained in future?
Although you have accepted an answer, let me post something about what the way unittests should work because even if the solution posted does work and is indeed nice, it has some backdraws.
First of all you don't want to actually execute your command, as this would test an external software. From this moment you are depending on external stuff you cannot control (what happens if I want to test your software on my pc? Do I have to instal git or any git-mock?). What you really want to do is see if the command line command is correct and is executed well.
As I struggled with this before, here is what I did: I created a class which executes shell_exec. Everywhere I want to interact with the shell, I inject the class and use it. So for testing I can mock the class and see if the correct method is executed and if the correct parameters are set.
This has many benefits:
I have one point of interacting with the shell. If anything changes in the future or if I have the need to do anything before executing the command, I can add the behaviour to the class. The PHP function is encapsulated nicely.
It works on every PC, even if the shell_exec command does not work for any reason. The tests get executed on every pc (development machines, testserver, integration server) without the need to worry about external dependencies.
Of course to validate that your software works as a whole, you have to do integration tests which do not mock anything. But this is a totally different situation. Unittests should make sure your class (or method) does what it needs to do. Sometimes this is not clear cut, as you may decide to use a real class somewhere because mocking it would lead to much overhead. But, as far as I'm concerned, it's no longer a unit test by any means if it relys on external libraries/software which may not be installed or may not work for some reason.
at least you could do something like checking for the retured string to match /^[abcdef0-9]{7}$/
I suppose if you really wanted to, you could write a dummy git and prepend its location to your environment's PATH. Have it do whatever validation you need to do, and then generate output consistent with what the real thing would do. You could conceivably even have it forward stuff to the real git, if you wanted, but then you couldn't guarantee the results.
You'd have to be careful, though....You'd want do just enough so that the class can be fooled into thinking it's really git, but if you make it behave too much like the real thing does, that amounts to writing your own half-assed version of Git.
Is there a function in PHP to query the number of open files?
Sort of like memory_get_usage() but for open files.
I'm running the unit test suites for Zend Framework. The problem is that after it gets through the tests for Zend_Search_Lucene, subsequent tests start failing. But if I skip the Zend_Search_Lucene tests, all the test suites succeed.
I'd like to prove that Zend_Search_Lucene (or any other test suite) is opening too many files and not cleaning up after itself. I thought PHP might have a function to simply report how many files are open. But after 20 minutes of searching the PHP manual and Google, I can't find any such function.
There is no such function in PHP.
But there are alternatives. If you are running linux/unix/OSX, then running lsof from command line can give you that information.
This could be combined with a custom Test listener for PHPUnit - I described the approach under a different question - How to wrap PHPUnit to control command line reporting?
PHPUnit_Framework_TestListener interface has methods like startTest(), endTest(), startTestSuite(), endTestSuite(). You could execute a shell_exec call to lsof from those methods and print out the interesting numbers before and after every test/testsuite.
For open files : I'm using lsof
http://novosial.org/debug/unix/lsof/
What I'd do, is to unregister the default stream handler and then register my own in which I'd provide an implementation for stream_open that keeps an internal counter.
These two might do what you are looking for:
apd_dump_regular_resources — Return all current regular resources as an array
apd_dump_persistent_resources — Return all persistent resources as an array
Iit's a wild guess though because I never used APD (it's PECL), so it might be resources does not refer to the actual resource type. Unfortunately, I couldnt find any documentation showing output.
Is there a simple "Web interface" to running PHPUnit test suites? i.e. a PHP script that runs the test on the command line, and outputs a nicely formatted HTML result.
I develop web applications, and the day-to-day workflow usually switches between the IDE and the browser. I would like to have the unit testing in the same environment.
I'm looking for something really simple and PHP based - I am planning to get into phpUnderControl (which has the functionality I'm looking for) but not yet.
I recently discovered Visual PHPUnit which looks like a very very nice interface for everyone that doesn't want to run PHPUnit from the command line:
It seems to be the next iteration of #Matt's PHPUnit Test Report
I feel your frustration - I'm a UI guy myself. Looking at the terminal too long makes my head spin. I wrote a quick little application that you might find helpful.
(source: mattmueller.me)
You can find it here: http://mattmueller.me/blog/introducing-phpunit-test-report
Cheers!
Matt
After several hours of researching recently, the best PHPUnit web frontend I have come across was https://github.com/NSinopoli/VisualPHPUnit
You can use phing to run a PHPUnitTask and then convert the output with:
PHPUnitReport - This task transforms PHPUnit xml reports to HTML using XSLT.
Example:
<phpunitreport infile="reports/testsuites.xml"
format="frames"
todir="reports/tests"
styledir="/home/phing/etc"/>
See phpunit --help for the various output formats.
The 2.3 version of PHPUnit had a chapter on this, but it is gone for some time now. You might be able to find an old copy with Google somewhere.
Since you mention this is for phpUnderControl: if you are not fixed on that, consider using Jenkins and http://jenkins-php.org.
On a side note: unless we are talking CI servers, most people I know don't use PHPUnit through a web interface. They either just use the command line or their IDE integration.
You can use Jenkins to run any kind of tasks including PHPUnit tests. It can automatically checkout your app, run the tests, build a HTML report and even email you if the build fails.
Here's the templates you need to setup Jenkins to build a bunch of interesting reports and stats from your project.
If you don't care about reformatting the output and just want to run PHPUnit from a web page, you can do so with some PHP code like this:
<pre>
<?php
$argv[0] = "phpunit.phar";
$argv[1] = '--bootstrap';
$argv[2] = 'src/load.php';
$argv[3] = "tests/MoneyTest";
$_SERVER['argv'] = $argv;
include 'phpunit.phar';
?>
</pre>
The file src/load.php is just a bunch of includes to include the classes. The output then looks like this:
#!/usr/bin/env php
PHPUnit 4.1.2 by Sebastian Bergmann.
........................
Time: 122 ms, Memory: 3.25Mb
OK (24 tests, 43 assertions)
Just ignore that first line and you can see the results.
I'm shocked that PHPUnit does not include a basic way to do this. Some classes may be dependent on the web server. Do we just not test those? Some sites have you upload your files and don't allow command line executions.
I've never seen such a web-interface... But, as you say you are always using your IDE and your webbrowser, why not think the other way ?
i.e. a possible solution would be to launch the unittests from your IDE ;-)
Which means you should be able to click on the failing tests to "jump" to either the test method, or the reason that caused the test to fail, for instance.
In the PHP + PHPUnit world, I know that Zend Studio does that -- yes, it's not free, unfortunatly ;-(
Using Eclipse PDT, a solution would be to register PHPUnit as an external tool (see or instance this blogpost : Using PHPUnit with Eclipse PDT) -- but it's quite not sexy, and you cannot click on the results to jump the the methods/tests...
Another solution would be to develop a plugin to integrate PHPUnit into Eclipse PDT (like it's been done for Zend Studio, I suppose) -- A phpunit4eclipse was created some time ago, but it's just a start, and didn't get much succes, so the author didn't work on it after releasing that...
I found this:
I stumbeld upon a post from Parth Patil, whose solution was to create an xml-report from PHPUnit and then use this xml to create your own report.
I used his solution, made it PHPUnit 3.4 compatible and also added some Reflection to see my testcase doc-comments in the report. (Note: For the refelection i use the Zend_Framework reflection class)
Ok you said you'd prefer an independent IDE solution, but just so you know there is a recent plugin that enables executing PHPUnit simply into Eclipse, and having a nice representation (like in Zend Studio, but for free).
Here is the link, the main developper replies fast to emails too if you have a problem :
http://www.phpsrc.org/wiki/
I personnaly tested some web interface, but I have always been deceived (not really practital and stable). But this is your choice.
jframework also has a nice UI for PHPUnit. It breaks the results, and shows test coverage on all files and each file separately.
It works on both web and cli, with the cli one having the benefit of dumping every test after its done (the web-based one has to wait until everything is over).
You can always use the Maven for PHP from which you can use the surefire reports (mvn site).
More info here: http://www.php-maven.org