How can I get XDebug to run with PHPUnit on the CLI?

The xdebug.profiler_enable setting can’t be changed at runtime but only at the start of script. Running phpunit -d foo=bar will just lead to phpunit calling ini_set(“foo”, “bar”); and that doesn’t work since the value can’t change at runtime. See: xdebug.profiler_enable Enables Xdebug’s profiler which creates files in the profile output directory. Those files can be … Read more

Autoloading classes in PHPUnit using Composer and autoload.php

Well, at first. You need to tell the autoloader where to find the php file for a class. That’s done by following the PSR-0 standard. The best way is to use namespaces. The autoloader searches for a Acme/Tests/ReturningTest.php file when you requested a Acme\Tests\ReturningTest class. There are some great namespace tutorials out there, just search … Read more

PHPUnit: how do I mock multiple method calls with multiple arguments?

In my case the answer turned out to be quite simple: $this->expects($this->at(0)) ->method(‘write’) ->with(/* first set of params */); $this->expects($this->at(1)) ->method(‘write’) ->with(/* second set of params */); The key is to use $this->at(n), with n being the Nth call of the method. I couldn’t do anything with any of the logicalOr() variants I tried.

How to test a second parameter in a PHPUnit mock object

I believe the way to do this is: $observer->expects($this->once()) ->method(‘method’) ->with($this->equalTo($arg1),$this->equalTo($arg2)); Or $observer->expects($this->once()) ->method(‘method’) ->with($arg1, $arg2); If you need to perform a different type of assertion on the 2nd arg, you can do that, too: $observer->expects($this->once()) ->method(‘method’) ->with($this->equalTo($arg1),$this->stringContains(‘some_string’)); If you need to make sure some argument passes multiple assertions, use logicalAnd() $observer->expects($this->once()) ->method(‘method’) ->with($this->logicalAnd($this->stringContains(‘a’), $this->stringContains(‘b’)));

Can I “Mock” time in PHPUnit?

I recently came up with another solution that is great if you are using PHP 5.3 namespaces. You can implement a new time() function inside your current namespace and create a shared resource where you set the return value in your tests. Then any unqualified call to time() will use your new function. For further … Read more

PHPUnit best practices to organize tests

I’ll start of by linking to the manual and then going into what I’ve seen and heard in the field. Organizing phpunit test suites Module / Test folder organization in the file system My recommended approach is combining the file system with an xml config. tests/ \ unit/ | – module1 | – module2 – … Read more