SimpleTest vs PHPunit

This question is quite dated but as it is still getting traffic and answers I though I state my point here again even so I already did it on some other (newer) questions. I’m really really baffled that SimpleTest still is considered an alternative to phpunit. Maybe i’m just misinformed but as far as I’ve … 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

How to run single test method with phpunit?

The following command runs the test on a single method: phpunit –filter testSaveAndDrop EscalationGroupTest escalation/EscalationGroupTest.php phpunit –filter methodName ClassName path/to/file.php For newer versions of phpunit, it is just: phpunit –filter methodName path/to/file.php

Can’t install PHPUnit via PEAR, requires PEAR Installer >= 1.9.2, can’t upgrade PEAR from 1.9.0

You basically have 2 pear installations on your machine, and the “pear upgrade” command updates the other one, not itself. I assume that the pear version you’re running has been installed via ubuntu’s apt. Find out where it has been installed with apt (on Debian/Ubuntu): $ dpkg -L php-pear … /usr/share/php/PEAR.php … /usr/bin/pear Now let’s … Read more

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

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 Mock Objects and Static Methods

Sebastian Bergmann, the author of PHPUnit, recently had a blog post about Stubbing and Mocking Static Methods. With PHPUnit 3.5 and PHP 5.3 as well as consistent use of late static binding, you can do $class::staticExpects($this->any()) ->method(‘helper’) ->will($this->returnValue(‘bar’)); Update: staticExpects is deprecated as of PHPUnit 3.8 and will be removed completely with later versions.

Best practices to test protected methods with PHPUnit

If you’re using PHP5 (>= 5.3.2) with PHPUnit, you can test your private and protected methods by using reflection to set them to be public prior to running your tests: protected static function getMethod($name) { $class = new ReflectionClass(‘MyClass’); $method = $class->getMethod($name); $method->setAccessible(true); return $method; } public function testFoo() { $foo = self::getMethod(‘foo’); $obj = … Read more

PHPUnit assert that an exception was thrown?

<?php require_once ‘PHPUnit/Framework.php’; class ExceptionTest extends PHPUnit_Framework_TestCase { public function testException() { $this->expectException(InvalidArgumentException::class); // or for PHPUnit < 5.2 // $this->setExpectedException(InvalidArgumentException::class); //…and then add your test code that generates the exception exampleMethod($anInvalidArgument); } } expectException() PHPUnit documentation PHPUnit author article provides detailed explanation on testing exceptions best practices.