Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kenjis/phpunit-helper
Helpers for PHPUnit. Easy mock creation and easy private property/method testing.
https://github.com/kenjis/phpunit-helper
hacktoberfest phpunit phpunit-mock phpunit-util
Last synced: 3 months ago
JSON representation
Helpers for PHPUnit. Easy mock creation and easy private property/method testing.
- Host: GitHub
- URL: https://github.com/kenjis/phpunit-helper
- Owner: kenjis
- License: mit
- Created: 2021-02-01T06:35:37.000Z (almost 4 years ago)
- Default Branch: 1.x
- Last Pushed: 2021-09-30T04:12:03.000Z (over 3 years ago)
- Last Synced: 2024-10-08T14:50:49.980Z (3 months ago)
- Topics: hacktoberfest, phpunit, phpunit-mock, phpunit-util
- Language: PHP
- Homepage:
- Size: 94.7 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHPUnit Helper
Provides helper traits for PHPUnit.
- [TestDouble](#testdouble) ... Easy mock creation
- [ReflectionHelper](#reflectionhelper) ... Easy private property/method testing
- [DebugHelper](#debughelper) ... Helper function `dd()` and `d()`## Table of Contents
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
- [`TestDouble`](#testdouble)
- [`$this->getDouble()`](#this-getdouble)
- [`$this->verifyInvoked()`](#this-verifyinvoked)
- [`$this->verifyInvokedOnce()`](#this-verifyinvokedonce)
- [`$this->verifyInvokedMultipleTimes()`](#this-verifyinvokedmultipletimes)
- [`$this->verifyNeverInvoked()`](#this-verifyneverinvoked)
- [`ReflectionHelper`](#reflectionhelper)
- [`$this->getPrivateProperty()`](#this-getprivateproperty)
- [`$this->setPrivateProperty()`](#this-setprivateproperty)
- [`$this->getPrivateMethodInvoker()`](#this-getprivatemethodinvoker)
- [`DebugHelper`](#debughelper)
- [License](#license)## Requirements
- PHP 7.3 or later
## Installation
Run:
```sh-session
$ composer require --dev kenjis/phpunit-helper
```## 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:
```php
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.
```php
$email = $this->getMockBuilder(CI_Email::class)
->disableOriginalConstructor()
->onlyMethods(['send'])
->getMock();
$email->method('send')
->willReturn(true);
```You could write code above like below:
```php
$email = $this->getDouble(CI_Email::class, ['send' => true]);
```You can set Closure as the return value of a mocked method.
```php
$ret = function () {
throw new RuntimeException('Cannot send email!');
};
$mock = $this->getDouble(CI_Email::class, ['send' => $ret]);
```You can also set the mock itself as the return value of a mocked method with using `$this->returnSelf()`.
```php
$mock = $this->getDouble(CI_Email::class, [
'to' => $this->returnSelf(),
'subject' => $this->returnSelf(),
'send' => true,
]);
```You can create mocks with consecutive calls.
```php
$mock = $this->getMockBuilder(CI_Email::class)
->disableOriginalConstructor()
->onlyMethods(['method'])
->getMock();
$mock->expects($this->any())->method('method')
->will($this->onConsecutiveCalls('GET', 'POST' ,'DELETE'));
```You could write code above like below:
```php
$mock = $this->getDouble(
CI_Input::class,
[
['method' => ['GET', 'POST' ,'DELETE']],
]
);
```#### `$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.
```php
$loader->expects($this->atLeastOnce())
->method('view')
->with(
'shopConfirm', $this->anything(), true
);
```You could write code above like below:
```php
$this->verifyInvoked(
$loader,
'view',
[
'shopConfirm', $this->anything(), true
]
);
```#### `$this->verifyInvokedOnce()`
| param | type | description |
|---------|--------|---------------------|
|`$mock` | object | PHPUnit mock object |
|`$method`| string | method name |
|`$params`| array | arguments |Verifies that method was invoked only once.
```php
$loader->expects($this->once())
->method('view')
->with(
'shopConfirm', $this->anything(), true
);
```You could write code above like below:
```php
$this->verifyInvokedOnce(
$loader,
'view',
[
'shopConfirm', $this->anything(), true
]
);
```#### `$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.
```php
$loader->expects($this->exactly(2))
->method('view')
->withConsecutive(
['shopConfirm', $this->anything(), true],
['shopTmplCheckout', $this->anything()]
);
```You could write code above like below:
```php
$this->verifyInvokedMultipleTimes(
$loader,
'view',
2,
[
['shopConfirm', $this->anything(), true],
['shopTmplCheckout', $this->anything()]
]
);
```#### `$this->verifyNeverInvoked()`
| param | type | description |
|---------|--------|---------------------|
|`$mock` | object | PHPUnit mock object |
|`$method`| string | method name |
|`$params`| array | arguments |Verifies that method was not called.
```php
$loader->expects($this->never())
->method('view')
->with(
'shopConfirm', $this->anything(), true
);
```You could write code above like below:
```php
$this->verifyNeverInvoked(
$loader,
'view',
[
'shopConfirm', $this->anything(), true
]
);
```### `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:
```php
getPrivateProperty()`| param | type | description |
|-----------|---------------|---------------------|
|`$obj` | object/string | object / class name |
|`$property`| string | property name |`returns` (mixed) property value.
Gets private or protected property value.
~~~php
$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.
~~~php
$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.
~~~php
$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:
```php