1. Go to this page and download the library: Download jasny/phpunit-extension library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jasny / phpunit-extension example snippets
use Jasny\PHPUnit\CallbackMockTrait;
use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
use CallbackMockTrait;
public function testSingleCall()
{
$callback = $this->createCallbackMock($this->once(), ['abc'], 10);
function_that_invokes($callback);
}
public function testConsecutiveCalls()
{
$callback = $this->createCallbackMock(
$this->exactly(2),
function(InvocationMocker $invoke) {
$invoke
->withConsecutive(['abc'], ['xyz'])
->willReturnOnConsecutiveCalls(10, 42);
}
);
function_that_invokes($callback);
}
}
use Jasny\PHPUnit\SafeMocksTrait;
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
use SafeMocksTrait;
public function test()
{
$foo = $this->createMock(Foo::class);
$foo->expects($this->once())->method('hello');
$foo->hello();
$foo->bye();
}
}
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
public function test()
{
$this->expectDeprecation();
$result = my_old_func(0.42); // <-- Will exit on deprecation
$this->assertEquals(89, $result); // This should fail, but is never executed
}
}
use Jasny\PHPUnit\ExpectWarningTrait;
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
use ExpectWarningTrait;
public function test()
{
$this->expectDeprecation();
$result = my_old_func(0.42); // <-- Will NOT exit
$this->assertEquals(89, $result); // Will fail
}
}
use Jasny\PHPUnit\InContextOfTrait;
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
use InContextOfTrait;
public function testCallPrivateMethod()
{
$object = new MyObject();
$result = $this->inContextOf($object, fn($object) => $object->privateMethod('foo', 'bar'));
$this->assertEquals($result, 'foo-bar');
}
public function testGetPrivateProperty()
{
$value = $this->inContextOf($object, fn($object) => $object->privateProperty);
$this->assertEquals($value, 999);
}
/** Alternatively, do the assertion in the closure */
public function testAssertPrivateProperty()
{
$this->inContextOf($object, fn($object) => $this->assertEquals($object->privateProperty, 999));
}
public function testSetPrivateProperty()
{
$this->inContextOf($object, fn($object) => $object->privateProperty = 42);
}
}
use Jasny\PHPUnit\ConsecutiveTrait;
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
use ConsecutiveTrait;
public function test()
{
$mock = $this->createMock(MyClass::class);
$mock->expects($this->exactly(2))
->method('foo')
->with(...$this->consecutive(
['a', 1],
['b', 2],
))
->willReturnOnConsecutiveCalls(10, 42);
$this->assertEquals(10, $mock->foo('a', 1));
$this->assertEquals(42, $mock->foo('b', 2));
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.