PHPUnit. Error: No code coverage driver is available. (having xdebug installed)

There are 2 php.ini files in most Apache/PHP installations and definitely in WAMPServer To amend the correct php.ini used by PHP in Apache use the menus wampmanager->PHP->php.ini But for the php.ini file used by the PHP CLI you have to manually edit \wamp\bin\php\php{version}\php.ini the result of a php -v should look like this if XDEBUG … Read more

unit testing and Static methods

Static methods themselves aren’t harder to test than instance methods. The trouble arises when a method–static or otherwise–calls other static methods because you cannot isolate the method being tested. Here is a typical example method that can be difficult to test: public function findUser($id) { Assert::validIdentifier($id); Log::debug(“Looking for user $id”); // writes to a file … Read more

Mock in PHPUnit – multiple configuration of the same method with different arguments

Sadly this is not possible with the default PHPUnit Mock API. I can see two options that can get you close to something like this: Using ->at($x) $context = $this->getMockBuilder(‘Context’) ->getMock(); $context->expects($this->at(0)) ->method(‘offsetGet’) ->with(‘Matcher’) ->will($this->returnValue(new Matcher())); $context->expects($this->at(1)) ->method(‘offsetGet’) ->with(‘Logger’) ->will($this->returnValue(new Logger())); This will work fine but you are testing more than you should (mainly that … Read more

PHPunit result output on the CLI not showing test names

Use phpunit –testdox On the cli this will give you a very readable testdox format and allow you to see and fix your multiple test suites easily e.g. PHPUnit 3.7.37 by Sebastian Bergmann. Configuration read from /home/badass-project/tests/phpunit.xml AnalyticsViewers [x] test getViewersForMonth throws for no valid date [x] test getViewersForMonth limits correctly [x] test getViewersForMonth only … Read more

In PHPUnit, how do I indicate different with() on successive calls to a mocked method?

You need to use at(): $mock->expects($this->at(0)) ->method(‘foo’) ->with(‘someValue’); $mock->expects($this->at(1)) ->method(‘foo’) ->with(‘anotherValue’); $mock->foo(‘someValue’); $mock->foo(‘anotherValue’); Note that the indexes passed to at() apply across all method calls to the same mock object. If the second method call was to bar() you would not change the argument to at().