Download the PHP package kenjis/phpunit-helper without Composer
On this page you can find all versions of the php package kenjis/phpunit-helper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kenjis/phpunit-helper
More information about kenjis/phpunit-helper
Files in kenjis/phpunit-helper
Package phpunit-helper
Short Description Provides helper traits for PHPUnit
License MIT
Homepage https://github.com/kenjis/phpunit-helper
Informations about the package phpunit-helper
PHPUnit Helper
Provides helper traits for PHPUnit.
- TestDouble ... Easy mock creation
- ReflectionHelper ... Easy private property/method testing
- DebugHelper ... Helper function
dd()
andd()
Table of Contents
- Requirements
- Installation
- Usage
TestDouble
$this->getDouble()
$this->verifyInvoked()
$this->verifyInvokedOnce()
$this->verifyInvokedMultipleTimes()
$this->verifyNeverInvoked()
ReflectionHelper
$this->getPrivateProperty()
$this->setPrivateProperty()
$this->getPrivateMethodInvoker()
DebugHelper
- License
Requirements
- PHP 7.3 or later
Installation
Run:
Usage
TestDouble
This trait provides helper methods to create mock objects and to verify invocations.
Import the Kenjis\PhpUnitHelper\TestDouble
trait into your test class:
$this->getDouble()
param | type | description |
---|---|---|
$classname |
string | class name |
$params |
array | [method_name => return_value] |
[[method_name => [return_value1, return_value2 [, ...]]] | ||
$constructor_params |
false/array | false: disable constructor / array: constructor params |
returns
(object) PHPUnit mock object.
Gets PHPUnit mock object.
You could write code above like below:
You can set Closure as the return value of a mocked method.
You can also set the mock itself as the return value of a mocked method with using $this->returnSelf()
.
You can create mocks with consecutive calls.
You could write code above like below:
$this->verifyInvoked()
param | type | description |
---|---|---|
$mock |
object | PHPUnit mock object |
$method |
string | method name |
$params |
array | arguments |
Verifies a method was invoked at least once.
You could write code above like below:
$this->verifyInvokedOnce()
param | type | description |
---|---|---|
$mock |
object | PHPUnit mock object |
$method |
string | method name |
$params |
array | arguments |
Verifies that method was invoked only once.
You could write code above like below:
$this->verifyInvokedMultipleTimes()
param | type | description |
---|---|---|
$mock |
object | PHPUnit mock object |
$method |
string | method name |
$times |
int | times |
$params |
array | arguments |
Verifies that method was called exactly $times times.
You could write code above like below:
$this->verifyNeverInvoked()
param | type | description |
---|---|---|
$mock |
object | PHPUnit mock object |
$method |
string | method name |
$params |
array | arguments |
Verifies that method was not called.
You could write code above like below:
ReflectionHelper
This trait provides helper methods to access private or protected properties and methods.
But generally it is not recommended testing non-public properties or methods, so think twice before you use the methods in this trait.
Import the Kenjis\PhpUnitHelper\ReflectionHelper
trait into your test class:
$this->getPrivateProperty()
param | type | description |
---|---|---|
$obj |
object/string | object / class name |
$property |
string | property name |
returns
(mixed) property value.
Gets private or protected property value.
$obj = new SomeClass();
$private_propery = $this->getPrivateProperty(
$obj,
'privatePropery'
);
$this->setPrivateProperty()
param | type | description |
---|---|---|
$obj |
object/string | object / class name |
$property |
string | property name |
$value |
mixed | value |
Sets private or protected property value.
$obj = new SomeClass();
$this->setPrivateProperty(
$obj,
'privatePropery',
'new value'
);
$this->getPrivateMethodInvoker()
param | type | description |
---|---|---|
$obj |
object/string | object / class name |
$method |
string | method name |
returns
(closure) method invoker.
Gets private or protected method invoker.
$obj = new SomeClass();
$method = $this->getPrivateMethodInvoker(
$obj, 'privateMethod'
);
$this->assertEquals(
'return value of the privateMethod() method', $method()
);
DebugHelper
This trait provides helper functions, dd()
and d()
to dump variables.
Import the Kenjis\PhpUnitHelper\DebugHelper
trait into your test class:
License
This package is licensed using the MIT License.
Please have a look at LICENSE
.