How can I get PHPUnit MockObjects to return different values based on a parameter?

Use a callback. e.g. (straight from PHPUnit documentation): <?php class StubTest extends PHPUnit_Framework_TestCase { public function testReturnCallbackStub() { $stub = $this->getMock( ‘SomeClass’, array(‘doSomething’) ); $stub->expects($this->any()) ->method(‘doSomething’) ->will($this->returnCallback(‘callback’)); // $stub->doSomething() returns callback(…) } } function callback() { $args = func_get_args(); // … } ?> Do whatever processing you want in the callback() and return the result … Read more

PHPUnit: assert two arrays are equal, but order of elements not important

You can use assertEqualsCanonicalizing method which was added in PHPUnit 7.5. If you compare the arrays using this method, these arrays will be sorted by PHPUnit arrays comparator itself. Code example: class ArraysTest extends \PHPUnit\Framework\TestCase { public function testEquality() { $obj1 = $this->getObject(1); $obj2 = $this->getObject(2); $obj3 = $this->getObject(3); $array1 = [$obj1, $obj2, $obj3]; $array2 … Read more

How to output in CLI during execution of PHP Unit tests?

UPDATE Just realized another way to do this that works much better than the –verbose command line option: class TestSomething extends PHPUnit_Framework_TestCase { function testSomething() { $myDebugVar = array(1, 2, 3); fwrite(STDERR, print_r($myDebugVar, TRUE)); } } This lets you dump anything to your console at any time without all the unwanted output that comes along … Read more

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.

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)