https://github.com/m50/phpunit-expect
An Expectation API for phpunit
https://github.com/m50/phpunit-expect
Last synced: 3 months ago
JSON representation
An Expectation API for phpunit
- Host: GitHub
- URL: https://github.com/m50/phpunit-expect
- Owner: m50
- License: mit
- Created: 2021-07-11T06:23:51.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-09-08T04:59:40.000Z (almost 5 years ago)
- Last Synced: 2025-02-24T07:43:55.520Z (over 1 year ago)
- Language: PHP
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# PHPUnit Expect

[](https://github.com/m50/phpunit-expect/actions/workflows/test.yml)
[](https://github.styleci.io/repos/384875482)
[](https://packagist.org/packages/m50/phpunit-expect)
[](https://packagist.org/packages/m50/phpunit-expect)
[](https://packagist.org/packages/m50/phpunit-expect)
An expectation API for PHPUnit, inspired by [Jest](https://jestjs.io) for JS, and [Pest](https://pestphp.com) for PHP.
## Install
> This requires PHP 8.
The best way to install this package is with Composer.
```sh
composer require --dev m50/phpunit-expect
```
## Usage
> For additional examples, check out the tests, every expectation is utilized in a test.
When writing tests, instead of using the standard assert, you can use expect.
```php
expect(2 + 2)->toBe(4);
}
}
```
Any assertion on your test case can be used with this API. For example:
```php
class CustomerAssertTest extends TestCase
{
use Expect;
public function assertCustomer($customer, string $message = ''): void
{
// Do some assertion
}
public function testCustomer()
{
// Populate the $customer variable with a possible Customer
$this->expect($customer)->toBeCustomer();
}
}
```
It will replace `toBe` or `to` at the beginning of the function name with `assert`, so any assertion
that has not been translated, or any custom assertion you may have, can be utilized with this.
Additionally, you can chain assertions:
```php
public function testStringChain()
{
$this->expect('Hello World')
->toLowerCase() // Converts a string all to lower case, to do case insensitive assertions.
->toStartWith('hello')
->toEndWith('world')
;
}
```
And, it also supports proxied/higher-order expectations:
```php
public function testProxiedCalls()
{
$this->expect(['first' => 1, 'second' => 2])
->first->toBe(1)
->second->toBe(2)
;
$class = new \stdClass();
$class->first = 1;
$class->second = 2;
$this->expect($class)
->first->toBe(1)
->second->toBe(2)
;
}
public function testSequence()
{
$this->expect(['first' => 1, 'second' => 2])->sequence(
first: fn (Expectation $e) => $e->toBe(1),
second: fn(Expectation $e) => $e->toBe(2),
);
$class = new \stdClass();
$class->first = 1;
$class->second = 2;
}
```
Additionally, you can `not` any expectation as well:
```php
public function testAdd()
{
$this->expect(2 + 2)->not->toEqual(4);
// Or you can use the function, if you prefer.
$this->expect(2 + 2)->not()->toEqual(4);
}
```
## License
PHPUnit-Expect is open-sourced software licensed under the [MIT license](LICENSE).