mirror of
https://github.com/pestphp/pest.git
synced 2026-03-07 00:07:22 +01:00
Compare commits
57 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d90ddf889c | |||
| 5907164749 | |||
| eb6de433b7 | |||
| 32f72cdf55 | |||
| c160d97428 | |||
| 2a8de0565f | |||
| 5ab3c6e2d7 | |||
| 50ece576a7 | |||
| 9c202fa2d7 | |||
| e4e9cb09e4 | |||
| 6a7597c01a | |||
| dd05452edd | |||
| 99ea9f42e5 | |||
| 7d6a86adc7 | |||
| 7fbd2661c8 | |||
| 6ce678d1c2 | |||
| 5049b996db | |||
| d838456caa | |||
| e45c4ff4f1 | |||
| fa3959db17 | |||
| b97e206f7a | |||
| 7e9edecc7f | |||
| c290909eb3 | |||
| f2e56da2da | |||
| e6b258534a | |||
| acef002a2d | |||
| 11ebe014fb | |||
| 2d13c6e219 | |||
| fbcb492c79 | |||
| 4f67eff619 | |||
| 3c2c767e09 | |||
| ff527baa1d | |||
| 621718d4b1 | |||
| 59adc57344 | |||
| 1680613e12 | |||
| f6d3ce41bc | |||
| d16a48bf0f | |||
| 9459ce4030 | |||
| c773d1cd57 | |||
| 22a1aac84a | |||
| 3a20696da4 | |||
| 99bcf98617 | |||
| 09d9bae988 | |||
| 27de6106ab | |||
| aeded0a356 | |||
| afef1d56e8 | |||
| 4b55de27f1 | |||
| 3d7b6426a1 | |||
| 1d415eb7fb | |||
| 8a384a6d65 | |||
| 7c4dd2f2e7 | |||
| b6f0496c3c | |||
| d96a2485b6 | |||
| 9e9d1cc8cc | |||
| 579bb1b90c | |||
| 7ff64540a6 | |||
| 241d4cf94c |
3
.github/FUNDING.yml
vendored
3
.github/FUNDING.yml
vendored
@ -1,5 +1,4 @@
|
|||||||
# These are supported funding model platforms
|
# These are supported funding model platforms
|
||||||
|
|
||||||
github: [nunomaduro,owenvoke,olivernybroe,octoper]
|
github: [nunomaduro,owenvoke,olivernybroe,octoper,lukeraymonddowning]
|
||||||
patreon: nunomaduro
|
patreon: nunomaduro
|
||||||
custom: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=66BYDWAT92N6L
|
|
||||||
|
|||||||
26
CHANGELOG.md
26
CHANGELOG.md
@ -4,6 +4,32 @@ All notable changes to this project will be documented in this file.
|
|||||||
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||||
|
|
||||||
|
## [v1.10.0 (2021-07-12)](https://github.com/pestphp/pest/compare/v1.9.1...v1.10.0)
|
||||||
|
### Added
|
||||||
|
- The ability to use higher order expectations inside higher order tests ([#341](https://github.com/pestphp/pest/pull/341))
|
||||||
|
|
||||||
|
## [v1.9.1 (2021-07-11)](https://github.com/pestphp/pest/compare/v1.9.0...v1.9.1)
|
||||||
|
### Fixed
|
||||||
|
- Callable `expect` values in higher order tests failing if the value was an existing method name ([#334](https://github.com/pestphp/pest/pull/344))
|
||||||
|
|
||||||
|
## [v1.9.0 (2021-07-09)](https://github.com/pestphp/pest/compare/v1.8.0...v1.9.0)
|
||||||
|
### Changed
|
||||||
|
- You may now pass just an exception message when using the `throws` method ([#339](https://github.com/pestphp/pest/pull/339))
|
||||||
|
|
||||||
|
## [v1.8.0 (2021-07-08)](https://github.com/pestphp/pest/compare/v1.7.1...v1.8.0)
|
||||||
|
### Added
|
||||||
|
- A new `tap` and test case aware `expect` methods for higher order tests ([#331](https://github.com/pestphp/pest/pull/331))
|
||||||
|
- Access to test case methods and properties when using `skip` ([#338](https://github.com/pestphp/pest/pull/338))
|
||||||
|
|
||||||
|
## [v1.7.1 (2021-06-24)](https://github.com/pestphp/pest/compare/v1.7.0...v1.7.1)
|
||||||
|
### Fixed
|
||||||
|
- The `and` method not being usable in Higher Order expectations ([#330](https://github.com/pestphp/pest/pull/330))
|
||||||
|
|
||||||
|
## [v1.7.0 (2021-06-19)](https://github.com/pestphp/pest/compare/v1.6.0...v1.7.0)
|
||||||
|
### Added
|
||||||
|
- Support for non-callable values in the sequence method, which will be passed as `toEqual` ([#323](https://github.com/pestphp/pest/pull/323))
|
||||||
|
- Support for nested Higher Order Expectations ([#324](https://github.com/pestphp/pest/pull/324))
|
||||||
|
|
||||||
## [v1.6.0 (2021-06-18)](https://github.com/pestphp/pest/compare/v1.5.0...v1.6.0)
|
## [v1.6.0 (2021-06-18)](https://github.com/pestphp/pest/compare/v1.5.0...v1.6.0)
|
||||||
### Added
|
### Added
|
||||||
- Adds a new `json` expectation method to improve testing with JSON strings ([#325](https://github.com/pestphp/pest/pull/325))
|
- Adds a new `json` expectation method to improve testing with JSON strings ([#325](https://github.com/pestphp/pest/pull/325))
|
||||||
|
|||||||
@ -20,7 +20,7 @@
|
|||||||
"php": "^7.3 || ^8.0",
|
"php": "^7.3 || ^8.0",
|
||||||
"nunomaduro/collision": "^5.4.0",
|
"nunomaduro/collision": "^5.4.0",
|
||||||
"pestphp/pest-plugin": "^1.0.0",
|
"pestphp/pest-plugin": "^1.0.0",
|
||||||
"phpunit/phpunit": ">= 9.3.7 <= 9.5.5"
|
"phpunit/phpunit": ">= 9.3.7 <= 9.5.6"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|||||||
@ -7,6 +7,7 @@ parameters:
|
|||||||
level: max
|
level: max
|
||||||
paths:
|
paths:
|
||||||
- src
|
- src
|
||||||
|
- scripts
|
||||||
|
|
||||||
checkMissingIterableValueType: true
|
checkMissingIterableValueType: true
|
||||||
checkGenericClassInNonGenericObjectType: false
|
checkGenericClassInNonGenericObjectType: false
|
||||||
|
|||||||
@ -15,6 +15,7 @@ $globalsFilePath = implode(DIRECTORY_SEPARATOR, [
|
|||||||
|
|
||||||
$compiledFilePath = implode(DIRECTORY_SEPARATOR, [dirname(__DIR__), 'compiled', 'globals.php']);
|
$compiledFilePath = implode(DIRECTORY_SEPARATOR, [dirname(__DIR__), 'compiled', 'globals.php']);
|
||||||
|
|
||||||
|
/* @phpstan-ignore-next-line */
|
||||||
@unlink($compiledFilePath);
|
@unlink($compiledFilePath);
|
||||||
|
|
||||||
$replace = function ($contents, $string, $by) {
|
$replace = function ($contents, $string, $by) {
|
||||||
|
|||||||
@ -12,9 +12,13 @@ use Pest\Expectation;
|
|||||||
trait Expectable
|
trait Expectable
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
* @template TValue
|
||||||
|
*
|
||||||
* Creates a new expectation.
|
* Creates a new expectation.
|
||||||
*
|
*
|
||||||
* @param mixed $value
|
* @param TValue $value
|
||||||
|
*
|
||||||
|
* @return Expectation<TValue>
|
||||||
*/
|
*/
|
||||||
public function expect($value): Expectation
|
public function expect($value): Expectation
|
||||||
{
|
{
|
||||||
|
|||||||
@ -4,8 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest\Concerns;
|
namespace Pest\Concerns;
|
||||||
|
|
||||||
|
use BadMethodCallException;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Pest\HigherOrderExpectation;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
@ -43,7 +43,7 @@ trait Extendable
|
|||||||
public function __call(string $method, array $parameters)
|
public function __call(string $method, array $parameters)
|
||||||
{
|
{
|
||||||
if (!static::hasExtend($method)) {
|
if (!static::hasExtend($method)) {
|
||||||
return new HigherOrderExpectation($this, $method, $parameters);
|
throw new BadMethodCallException("$method is not a callable method name.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var Closure $extend */
|
/** @var Closure $extend */
|
||||||
|
|||||||
31
src/Concerns/RetrievesValues.php
Normal file
31
src/Concerns/RetrievesValues.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pest\Concerns;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
trait RetrievesValues
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @template TRetrievableValue
|
||||||
|
*
|
||||||
|
* Safely retrieve the value at the given key from an object or array.
|
||||||
|
*
|
||||||
|
* @param array<string, TRetrievableValue>|object $value
|
||||||
|
* @param TRetrievableValue|null $default
|
||||||
|
*
|
||||||
|
* @return TRetrievableValue|null
|
||||||
|
*/
|
||||||
|
private function retrieve(string $key, $value, $default = null)
|
||||||
|
{
|
||||||
|
if (is_array($value)) {
|
||||||
|
return $value[$key] ?? $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @phpstan-ignore-next-line
|
||||||
|
return $value->$key ?? $default;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -6,6 +6,7 @@ namespace Pest;
|
|||||||
|
|
||||||
use BadMethodCallException;
|
use BadMethodCallException;
|
||||||
use Pest\Concerns\Extendable;
|
use Pest\Concerns\Extendable;
|
||||||
|
use Pest\Concerns\RetrievesValues;
|
||||||
use Pest\Support\Arr;
|
use Pest\Support\Arr;
|
||||||
use PHPUnit\Framework\Assert;
|
use PHPUnit\Framework\Assert;
|
||||||
use PHPUnit\Framework\Constraint\Constraint;
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
@ -15,12 +16,17 @@ use SebastianBergmann\Exporter\Exporter;
|
|||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*
|
*
|
||||||
|
* @template TValue
|
||||||
|
*
|
||||||
* @property Expectation $not Creates the opposite expectation.
|
* @property Expectation $not Creates the opposite expectation.
|
||||||
* @property Each $each Creates an expectation on each element on the traversable value.
|
* @property Each $each Creates an expectation on each element on the traversable value.
|
||||||
*/
|
*/
|
||||||
final class Expectation
|
final class Expectation
|
||||||
{
|
{
|
||||||
use Extendable;
|
use Extendable {
|
||||||
|
__call as __extendsCall;
|
||||||
|
}
|
||||||
|
use RetrievesValues;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The expectation value.
|
* The expectation value.
|
||||||
@ -43,7 +49,7 @@ final class Expectation
|
|||||||
/**
|
/**
|
||||||
* Creates a new expectation.
|
* Creates a new expectation.
|
||||||
*
|
*
|
||||||
* @param mixed $value
|
* @param TValue $value
|
||||||
*/
|
*/
|
||||||
public function __construct($value)
|
public function __construct($value)
|
||||||
{
|
{
|
||||||
@ -53,7 +59,9 @@ final class Expectation
|
|||||||
/**
|
/**
|
||||||
* Creates a new expectation.
|
* Creates a new expectation.
|
||||||
*
|
*
|
||||||
* @param mixed $value
|
* @param TValue $value
|
||||||
|
*
|
||||||
|
* @return Expectation<TValue>
|
||||||
*/
|
*/
|
||||||
public function and($value): Expectation
|
public function and($value): Expectation
|
||||||
{
|
{
|
||||||
@ -120,7 +128,7 @@ final class Expectation
|
|||||||
|
|
||||||
if (is_callable($callback)) {
|
if (is_callable($callback)) {
|
||||||
foreach ($this->value as $item) {
|
foreach ($this->value as $item) {
|
||||||
$callback(expect($item));
|
$callback(new self($item));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,8 +137,12 @@ final class Expectation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows you to specify a sequential set of expectations for each item in a iterable "value".
|
* Allows you to specify a sequential set of expectations for each item in a iterable "value".
|
||||||
|
*
|
||||||
|
* @template TSequenceValue
|
||||||
|
*
|
||||||
|
* @param callable(self, self): void|TSequenceValue ...$callbacks
|
||||||
*/
|
*/
|
||||||
public function sequence(callable ...$callbacks): Expectation
|
public function sequence(...$callbacks): Expectation
|
||||||
{
|
{
|
||||||
if (!is_iterable($this->value)) {
|
if (!is_iterable($this->value)) {
|
||||||
throw new BadMethodCallException('Expectation value is not iterable.');
|
throw new BadMethodCallException('Expectation value is not iterable.');
|
||||||
@ -148,7 +160,12 @@ final class Expectation
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($values as $key => $item) {
|
foreach ($values as $key => $item) {
|
||||||
call_user_func($callbacks[$key], expect($item), expect($keys[$key]));
|
if (is_callable($callbacks[$key])) {
|
||||||
|
call_user_func($callbacks[$key], new self($item), new self($keys[$key]));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
(new self($item))->toEqual($callbacks[$key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@ -712,6 +729,24 @@ final class Expectation
|
|||||||
return $this->exporter->export($value);
|
return $this->exporter->export($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamically handle calls to the class or
|
||||||
|
* creates a new higher order expectation.
|
||||||
|
*
|
||||||
|
* @param array<int, mixed> $parameters
|
||||||
|
*
|
||||||
|
* @return HigherOrderExpectation|mixed
|
||||||
|
*/
|
||||||
|
public function __call(string $method, array $parameters)
|
||||||
|
{
|
||||||
|
if (!static::hasExtend($method)) {
|
||||||
|
/* @phpstan-ignore-next-line */
|
||||||
|
return new HigherOrderExpectation($this, $this->value->$method(...$parameters));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->__extendsCall($method, $parameters);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dynamically calls methods on the class without any arguments
|
* Dynamically calls methods on the class without any arguments
|
||||||
* or creates a new higher order expectation.
|
* or creates a new higher order expectation.
|
||||||
@ -721,7 +756,7 @@ final class Expectation
|
|||||||
public function __get(string $name)
|
public function __get(string $name)
|
||||||
{
|
{
|
||||||
if (!method_exists($this, $name) && !static::hasExtend($name)) {
|
if (!method_exists($this, $name) && !static::hasExtend($name)) {
|
||||||
return new HigherOrderExpectation($this, $name);
|
return new HigherOrderExpectation($this, $this->retrieve($name, $this->value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @phpstan-ignore-next-line */
|
/* @phpstan-ignore-next-line */
|
||||||
|
|||||||
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace Pest;
|
namespace Pest;
|
||||||
|
|
||||||
use Pest\Concerns\Expectable;
|
use Pest\Concerns\Expectable;
|
||||||
|
use Pest\Concerns\RetrievesValues;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
@ -14,6 +15,7 @@ use Pest\Concerns\Expectable;
|
|||||||
final class HigherOrderExpectation
|
final class HigherOrderExpectation
|
||||||
{
|
{
|
||||||
use Expectable;
|
use Expectable;
|
||||||
|
use RetrievesValues;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Expectation
|
* @var Expectation
|
||||||
@ -30,6 +32,11 @@ final class HigherOrderExpectation
|
|||||||
*/
|
*/
|
||||||
private $opposite = false;
|
private $opposite = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $shouldReset = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
@ -38,45 +45,12 @@ final class HigherOrderExpectation
|
|||||||
/**
|
/**
|
||||||
* Creates a new higher order expectation.
|
* Creates a new higher order expectation.
|
||||||
*
|
*
|
||||||
* @param array<int|string, mixed>|null $parameters
|
* @param mixed $value
|
||||||
* @phpstan-ignore-next-line
|
|
||||||
*/
|
*/
|
||||||
public function __construct(Expectation $original, string $name, ?array $parameters = null)
|
public function __construct(Expectation $original, $value)
|
||||||
{
|
{
|
||||||
$this->original = $original;
|
$this->original = $original;
|
||||||
$this->name = $name;
|
$this->expectation = $this->expect($value);
|
||||||
|
|
||||||
$this->expectation = $this->expect(
|
|
||||||
is_null($parameters) ? $this->getPropertyValue() : $this->getMethodValue($parameters)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the property value from the original expectation.
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
private function getPropertyValue()
|
|
||||||
{
|
|
||||||
if (is_array($this->original->value)) {
|
|
||||||
return $this->original->value[$this->name];
|
|
||||||
}
|
|
||||||
|
|
||||||
// @phpstan-ignore-next-line
|
|
||||||
return $this->original->value->{$this->name};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the value of the method from the original expectation.
|
|
||||||
*
|
|
||||||
* @param array<int|string, mixed> $arguments
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
private function getMethodValue(array $arguments)
|
|
||||||
{
|
|
||||||
// @phpstan-ignore-next-line
|
|
||||||
return $this->original->value->{$this->name}(...$arguments);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -89,15 +63,30 @@ final class HigherOrderExpectation
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new expectation.
|
||||||
|
*
|
||||||
|
* @template TValue
|
||||||
|
*
|
||||||
|
* @param TValue $value
|
||||||
|
*
|
||||||
|
* @return Expectation<TValue>
|
||||||
|
*/
|
||||||
|
public function and($value): Expectation
|
||||||
|
{
|
||||||
|
return $this->expect($value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dynamically calls methods on the class with the given arguments.
|
* Dynamically calls methods on the class with the given arguments.
|
||||||
*
|
*
|
||||||
* @param array<int|string, mixed> $arguments
|
* @param array<int, mixed> $arguments
|
||||||
*/
|
*/
|
||||||
public function __call(string $name, array $arguments): self
|
public function __call(string $name, array $arguments): self
|
||||||
{
|
{
|
||||||
if (!$this->originalHasMethod($name)) {
|
if (!$this->expectationHasMethod($name)) {
|
||||||
return new self($this->original, $name, $arguments);
|
/* @phpstan-ignore-next-line */
|
||||||
|
return new self($this->original, $this->getValue()->$name(...$arguments));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->performAssertion($name, $arguments);
|
return $this->performAssertion($name, $arguments);
|
||||||
@ -112,8 +101,8 @@ final class HigherOrderExpectation
|
|||||||
return $this->not();
|
return $this->not();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->originalHasMethod($name)) {
|
if (!$this->expectationHasMethod($name)) {
|
||||||
return new self($this->original, $name);
|
return new self($this->original, $this->retrieve($name, $this->getValue()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->performAssertion($name, []);
|
return $this->performAssertion($name, []);
|
||||||
@ -122,25 +111,33 @@ final class HigherOrderExpectation
|
|||||||
/**
|
/**
|
||||||
* Determines if the original expectation has the given method name.
|
* Determines if the original expectation has the given method name.
|
||||||
*/
|
*/
|
||||||
private function originalHasMethod(string $name): bool
|
private function expectationHasMethod(string $name): bool
|
||||||
{
|
{
|
||||||
return method_exists($this->original, $name) || $this->original::hasExtend($name);
|
return method_exists($this->original, $name) || $this->original::hasExtend($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the applicable value based on the current reset condition.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private function getValue()
|
||||||
|
{
|
||||||
|
return $this->shouldReset ? $this->original->value : $this->expectation->value;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs the given assertion with the current expectation.
|
* Performs the given assertion with the current expectation.
|
||||||
*
|
*
|
||||||
* @param array<int|string, mixed> $arguments
|
* @param array<int, mixed> $arguments
|
||||||
*/
|
*/
|
||||||
private function performAssertion(string $name, array $arguments): self
|
private function performAssertion(string $name, array $arguments): self
|
||||||
{
|
{
|
||||||
$expectation = $this->opposite
|
/* @phpstan-ignore-next-line */
|
||||||
? $this->expectation->not()
|
$this->expectation = ($this->opposite ? $this->expectation->not() : $this->expectation)->{$name}(...$arguments);
|
||||||
: $this->expectation;
|
|
||||||
|
|
||||||
$this->expectation = $expectation->{$name}(...$arguments); // @phpstan-ignore-line
|
$this->opposite = false;
|
||||||
|
$this->shouldReset = true;
|
||||||
$this->opposite = false;
|
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,14 +7,15 @@ namespace Pest\PendingObjects;
|
|||||||
use Closure;
|
use Closure;
|
||||||
use Pest\Factories\TestCaseFactory;
|
use Pest\Factories\TestCaseFactory;
|
||||||
use Pest\Support\Backtrace;
|
use Pest\Support\Backtrace;
|
||||||
|
use Pest\Support\HigherOrderCallables;
|
||||||
use Pest\Support\NullClosure;
|
use Pest\Support\NullClosure;
|
||||||
use Pest\TestSuite;
|
use Pest\TestSuite;
|
||||||
use SebastianBergmann\Exporter\Exporter;
|
use SebastianBergmann\Exporter\Exporter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @method \Pest\Expectations\Expectation expect(mixed $value)
|
|
||||||
*
|
|
||||||
* @internal
|
* @internal
|
||||||
|
*
|
||||||
|
* @mixin HigherOrderCallables
|
||||||
*/
|
*/
|
||||||
final class TestCall
|
final class TestCall
|
||||||
{
|
{
|
||||||
@ -58,11 +59,15 @@ final class TestCall
|
|||||||
/**
|
/**
|
||||||
* Asserts that the test throws the given `$exceptionClass` when called.
|
* Asserts that the test throws the given `$exceptionClass` when called.
|
||||||
*/
|
*/
|
||||||
public function throws(string $exceptionClass, string $exceptionMessage = null): TestCall
|
public function throws(string $exception, string $exceptionMessage = null): TestCall
|
||||||
{
|
{
|
||||||
$this->testCaseFactory
|
if (class_exists($exception)) {
|
||||||
->proxies
|
$this->testCaseFactory
|
||||||
->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exceptionClass]);
|
->proxies
|
||||||
|
->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exception]);
|
||||||
|
} else {
|
||||||
|
$exceptionMessage = $exception;
|
||||||
|
}
|
||||||
|
|
||||||
if (is_string($exceptionMessage)) {
|
if (is_string($exceptionMessage)) {
|
||||||
$this->testCaseFactory
|
$this->testCaseFactory
|
||||||
@ -143,21 +148,37 @@ final class TestCall
|
|||||||
? $conditionOrMessage
|
? $conditionOrMessage
|
||||||
: $message;
|
: $message;
|
||||||
|
|
||||||
if ($condition() !== false) {
|
$this->testCaseFactory
|
||||||
$this->testCaseFactory
|
->chains
|
||||||
->chains
|
->addWhen($condition, Backtrace::file(), Backtrace::line(), 'markTestSkipped', [$message]);
|
||||||
->add(Backtrace::file(), Backtrace::line(), 'markTestSkipped', [$message]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the property accessors to be used on the target.
|
||||||
|
*/
|
||||||
|
public function __get(string $name): self
|
||||||
|
{
|
||||||
|
return $this->addChain($name);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves the calls to be used on the target.
|
* Saves the calls to be used on the target.
|
||||||
*
|
*
|
||||||
* @param array<int, mixed> $arguments
|
* @param array<int, mixed> $arguments
|
||||||
*/
|
*/
|
||||||
public function __call(string $name, array $arguments): self
|
public function __call(string $name, array $arguments): self
|
||||||
|
{
|
||||||
|
return $this->addChain($name, $arguments);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a chain to the test case factory. Omitting the arguments will treat it as a property accessor.
|
||||||
|
*
|
||||||
|
* @param array<int, mixed>|null $arguments
|
||||||
|
*/
|
||||||
|
private function addChain(string $name, array $arguments = null): self
|
||||||
{
|
{
|
||||||
$this->testCaseFactory
|
$this->testCaseFactory
|
||||||
->chains
|
->chains
|
||||||
@ -168,7 +189,9 @@ final class TestCall
|
|||||||
if ($this->testCaseFactory->description !== null) {
|
if ($this->testCaseFactory->description !== null) {
|
||||||
$this->testCaseFactory->description .= ' → ';
|
$this->testCaseFactory->description .= ' → ';
|
||||||
}
|
}
|
||||||
$this->testCaseFactory->description .= sprintf('%s %s', $name, $exporter->shortenedRecursiveExport($arguments));
|
$this->testCaseFactory->description .= $arguments === null
|
||||||
|
? $name
|
||||||
|
: sprintf('%s %s', $name, $exporter->shortenedRecursiveExport($arguments));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|||||||
@ -6,7 +6,7 @@ namespace Pest;
|
|||||||
|
|
||||||
function version(): string
|
function version(): string
|
||||||
{
|
{
|
||||||
return '1.6.0';
|
return '1.10.0';
|
||||||
}
|
}
|
||||||
|
|
||||||
function testDirectory(string $file = ''): string
|
function testDirectory(string $file = ''): string
|
||||||
|
|||||||
@ -81,7 +81,6 @@ final class Coverage implements AddsOutput, HandlesArguments
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($input->getOption(self::MIN_OPTION) !== null) {
|
if ($input->getOption(self::MIN_OPTION) !== null) {
|
||||||
/* @phpstan-ignore-next-line */
|
|
||||||
$this->coverageMin = (float) $input->getOption(self::MIN_OPTION);
|
$this->coverageMin = (float) $input->getOption(self::MIN_OPTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
66
src/Support/HigherOrderCallables.php
Normal file
66
src/Support/HigherOrderCallables.php
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pest\Support;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Pest\Expectation;
|
||||||
|
use Pest\PendingObjects\TestCall;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
final class HigherOrderCallables
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var object
|
||||||
|
*/
|
||||||
|
private $target;
|
||||||
|
|
||||||
|
public function __construct(object $target)
|
||||||
|
{
|
||||||
|
$this->target = $target;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template TValue
|
||||||
|
*
|
||||||
|
* Create a new expectation. Callable values will be executed prior to returning the new expectation.
|
||||||
|
*
|
||||||
|
* @param callable|TValue $value
|
||||||
|
*
|
||||||
|
* @return Expectation<TValue>
|
||||||
|
*/
|
||||||
|
public function expect($value)
|
||||||
|
{
|
||||||
|
return new Expectation($value instanceof Closure ? Reflection::bindCallable($value) : $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template TValue
|
||||||
|
*
|
||||||
|
* Create a new expectation. Callable values will be executed prior to returning the new expectation.
|
||||||
|
*
|
||||||
|
* @param callable|TValue $value
|
||||||
|
*
|
||||||
|
* @return Expectation<TValue>
|
||||||
|
*/
|
||||||
|
public function and($value)
|
||||||
|
{
|
||||||
|
return $this->expect($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tap into the test case to perform an action and return the test case.
|
||||||
|
*
|
||||||
|
* @return TestCall|TestCase|object
|
||||||
|
*/
|
||||||
|
public function tap(callable $callable)
|
||||||
|
{
|
||||||
|
Reflection::bindCallable($callable);
|
||||||
|
|
||||||
|
return $this->target;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest\Support;
|
namespace Pest\Support;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
@ -33,33 +34,40 @@ final class HigherOrderMessage
|
|||||||
public $line;
|
public $line;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The method name.
|
* The method or property name to access.
|
||||||
*
|
*
|
||||||
* @readonly
|
* @readonly
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $methodName;
|
public $name;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The arguments.
|
* The arguments.
|
||||||
*
|
*
|
||||||
* @var array<int, mixed>
|
* @var array<int, mixed>|null
|
||||||
*
|
*
|
||||||
* @readonly
|
* @readonly
|
||||||
*/
|
*/
|
||||||
public $arguments;
|
public $arguments;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An optional condition that will determine if the message will be executed.
|
||||||
|
*
|
||||||
|
* @var callable(): bool|null
|
||||||
|
*/
|
||||||
|
public $condition = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new higher order message.
|
* Creates a new higher order message.
|
||||||
*
|
*
|
||||||
* @param array<int, mixed> $arguments
|
* @param array<int, mixed>|null $arguments
|
||||||
*/
|
*/
|
||||||
public function __construct(string $filename, int $line, string $methodName, array $arguments)
|
public function __construct(string $filename, int $line, string $methodName, $arguments)
|
||||||
{
|
{
|
||||||
$this->filename = $filename;
|
$this->filename = $filename;
|
||||||
$this->line = $line;
|
$this->line = $line;
|
||||||
$this->methodName = $methodName;
|
$this->name = $methodName;
|
||||||
$this->arguments = $arguments;
|
$this->arguments = $arguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,24 +78,58 @@ final class HigherOrderMessage
|
|||||||
*/
|
*/
|
||||||
public function call(object $target)
|
public function call(object $target)
|
||||||
{
|
{
|
||||||
|
/* @phpstan-ignore-next-line */
|
||||||
|
if (is_callable($this->condition) && call_user_func(Closure::bind($this->condition, $target)) === false) {
|
||||||
|
return $target;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->hasHigherOrderCallable()) {
|
||||||
|
/* @phpstan-ignore-next-line */
|
||||||
|
return (new HigherOrderCallables($target))->{$this->name}(...$this->arguments);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return Reflection::call($target, $this->methodName, $this->arguments);
|
return is_array($this->arguments)
|
||||||
|
? Reflection::call($target, $this->name, $this->arguments)
|
||||||
|
: $target->{$this->name}; /* @phpstan-ignore-line */
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
Reflection::setPropertyValue($throwable, 'file', $this->filename);
|
Reflection::setPropertyValue($throwable, 'file', $this->filename);
|
||||||
Reflection::setPropertyValue($throwable, 'line', $this->line);
|
Reflection::setPropertyValue($throwable, 'line', $this->line);
|
||||||
|
|
||||||
if ($throwable->getMessage() === self::getUndefinedMethodMessage($target, $this->methodName)) {
|
if ($throwable->getMessage() === self::getUndefinedMethodMessage($target, $this->name)) {
|
||||||
/** @var ReflectionClass $reflection */
|
/** @var ReflectionClass $reflection */
|
||||||
$reflection = new ReflectionClass($target);
|
$reflection = new ReflectionClass($target);
|
||||||
/* @phpstan-ignore-next-line */
|
/* @phpstan-ignore-next-line */
|
||||||
$reflection = $reflection->getParentClass() ?: $reflection;
|
$reflection = $reflection->getParentClass() ?: $reflection;
|
||||||
Reflection::setPropertyValue($throwable, 'message', sprintf('Call to undefined method %s::%s()', $reflection->getName(), $this->methodName));
|
Reflection::setPropertyValue($throwable, 'message', sprintf('Call to undefined method %s::%s()', $reflection->getName(), $this->name));
|
||||||
}
|
}
|
||||||
|
|
||||||
throw $throwable;
|
throw $throwable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates that this message should only be called when the given condition is true.
|
||||||
|
*
|
||||||
|
* @param callable(): bool $condition
|
||||||
|
*/
|
||||||
|
public function when(callable $condition): self
|
||||||
|
{
|
||||||
|
$this->condition = $condition;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether or not there exists a higher order callable with the message name.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function hasHigherOrderCallable()
|
||||||
|
{
|
||||||
|
return in_array($this->name, get_class_methods(HigherOrderCallables::class), true);
|
||||||
|
}
|
||||||
|
|
||||||
private static function getUndefinedMethodMessage(object $target, string $methodName): string
|
private static function getUndefinedMethodMessage(object $target, string $methodName): string
|
||||||
{
|
{
|
||||||
if (\PHP_MAJOR_VERSION >= 8) {
|
if (\PHP_MAJOR_VERSION >= 8) {
|
||||||
|
|||||||
@ -17,11 +17,21 @@ final class HigherOrderMessageCollection
|
|||||||
/**
|
/**
|
||||||
* Adds a new higher order message to the collection.
|
* Adds a new higher order message to the collection.
|
||||||
*
|
*
|
||||||
* @param array<int, mixed> $arguments
|
* @param array<int, mixed>|null $arguments
|
||||||
*/
|
*/
|
||||||
public function add(string $filename, int $line, string $methodName, array $arguments): void
|
public function add(string $filename, int $line, string $name, array $arguments = null): void
|
||||||
{
|
{
|
||||||
$this->messages[] = new HigherOrderMessage($filename, $line, $methodName, $arguments);
|
$this->messages[] = new HigherOrderMessage($filename, $line, $name, $arguments);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a new higher order message to the collection if the callable condition is does not return false.
|
||||||
|
*
|
||||||
|
* @param array<int, mixed>|null $arguments
|
||||||
|
*/
|
||||||
|
public function addWhen(callable $condition, string $filename, int $line, string $name, array $arguments = null): void
|
||||||
|
{
|
||||||
|
$this->messages[] = (new HigherOrderMessage($filename, $line, $name, $arguments))->when($condition);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest\Support;
|
namespace Pest\Support;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
@ -17,16 +18,14 @@ final class HigherOrderTapProxy
|
|||||||
/**
|
/**
|
||||||
* The target being tapped.
|
* The target being tapped.
|
||||||
*
|
*
|
||||||
* @var mixed
|
* @var TestCase
|
||||||
*/
|
*/
|
||||||
public $target;
|
public $target;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new tap proxy instance.
|
* Create a new tap proxy instance.
|
||||||
*
|
|
||||||
* @param mixed $target
|
|
||||||
*/
|
*/
|
||||||
public function __construct($target)
|
public function __construct(TestCase $target)
|
||||||
{
|
{
|
||||||
$this->target = $target;
|
$this->target = $target;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -41,15 +41,25 @@ final class Reflection
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (is_callable($method)) {
|
if (is_callable($method)) {
|
||||||
return Closure::fromCallable($method)->bindTo(
|
return static::bindCallable($method, $args);
|
||||||
TestSuite::getInstance()->test
|
|
||||||
)(...$args);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw $exception;
|
throw $exception;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind a callable to the TestCase and return the result.
|
||||||
|
*
|
||||||
|
* @param array<int, mixed> $args
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function bindCallable(callable $callable, array $args = [])
|
||||||
|
{
|
||||||
|
return Closure::fromCallable($callable)->bindTo(TestSuite::getInstance()->test)(...$args);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Infers the file name from the given closure.
|
* Infers the file name from the given closure.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -111,9 +111,12 @@
|
|||||||
✓ it works inside of each
|
✓ it works inside of each
|
||||||
✓ it works with sequence
|
✓ it works with sequence
|
||||||
✓ it can compose complex expectations
|
✓ it can compose complex expectations
|
||||||
|
✓ it can handle nested method calls
|
||||||
|
|
||||||
PASS Tests\Features\Expect\HigherOrder\methodsAndProperties
|
PASS Tests\Features\Expect\HigherOrder\methodsAndProperties
|
||||||
✓ it can access methods and properties
|
✓ it can access methods and properties
|
||||||
|
✓ it can handle nested methods and properties
|
||||||
|
✓ it can start a new higher order expectation using the and syntax
|
||||||
|
|
||||||
PASS Tests\Features\Expect\HigherOrder\properties
|
PASS Tests\Features\Expect\HigherOrder\properties
|
||||||
✓ it allows properties to be accessed from the value
|
✓ it allows properties to be accessed from the value
|
||||||
@ -124,6 +127,7 @@
|
|||||||
✓ it works with sequence
|
✓ it works with sequence
|
||||||
✓ it can compose complex expectations
|
✓ it can compose complex expectations
|
||||||
✓ it works with objects
|
✓ it works with objects
|
||||||
|
✓ it works with nested properties
|
||||||
|
|
||||||
PASS Tests\Features\Expect\each
|
PASS Tests\Features\Expect\each
|
||||||
✓ an exception is thrown if the the type is not iterable
|
✓ an exception is thrown if the the type is not iterable
|
||||||
@ -156,6 +160,8 @@
|
|||||||
✓ loops back to the start if it runs out of sequence items
|
✓ loops back to the start if it runs out of sequence items
|
||||||
✓ it works if the number of items in the iterable is smaller than the number of expectations
|
✓ it works if the number of items in the iterable is smaller than the number of expectations
|
||||||
✓ it works with associative arrays
|
✓ it works with associative arrays
|
||||||
|
✓ it can be passed non-callable values
|
||||||
|
✓ it can be passed a mixture of value types
|
||||||
|
|
||||||
PASS Tests\Features\Expect\toBe
|
PASS Tests\Features\Expect\toBe
|
||||||
✓ strict comparisons
|
✓ strict comparisons
|
||||||
@ -404,6 +410,8 @@
|
|||||||
PASS Tests\Features\HigherOrderTests
|
PASS Tests\Features\HigherOrderTests
|
||||||
✓ it proxies calls to object
|
✓ it proxies calls to object
|
||||||
✓ it is capable doing multiple assertions
|
✓ it is capable doing multiple assertions
|
||||||
|
✓ it resolves expect callables correctly
|
||||||
|
✓ it can tap into the test
|
||||||
|
|
||||||
WARN Tests\Features\Incompleted
|
WARN Tests\Features\Incompleted
|
||||||
… incompleted
|
… incompleted
|
||||||
@ -573,5 +581,5 @@
|
|||||||
✓ it is a test
|
✓ it is a test
|
||||||
✓ it uses correct parent class
|
✓ it uses correct parent class
|
||||||
|
|
||||||
Tests: 4 incompleted, 7 skipped, 357 passed
|
Tests: 4 incompleted, 7 skipped, 365 passed
|
||||||
|
|
||||||
@ -13,3 +13,7 @@ it('catch exceptions', function () {
|
|||||||
it('catch exceptions and messages', function () {
|
it('catch exceptions and messages', function () {
|
||||||
throw new Exception('Something bad happened');
|
throw new Exception('Something bad happened');
|
||||||
})->throws(Exception::class, 'Something bad happened');
|
})->throws(Exception::class, 'Something bad happened');
|
||||||
|
|
||||||
|
it('can just define the message', function () {
|
||||||
|
throw new Exception('Something bad happened');
|
||||||
|
})->throws('Something bad happened');
|
||||||
|
|||||||
@ -59,6 +59,21 @@ it('can compose complex expectations', function () {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can handle nested method calls', function () {
|
||||||
|
expect(new HasMethods())
|
||||||
|
->newInstance()->newInstance()->name()->toEqual('Has Methods')->toBeString()
|
||||||
|
->newInstance()->name()->toEqual('Has Methods')->not->toBeInt
|
||||||
|
->name()->toEqual('Has Methods')
|
||||||
|
->books()->each->toBeArray();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works with higher order tests')
|
||||||
|
->expect(new HasMethods())
|
||||||
|
->newInstance()->newInstance()->name()->toEqual('Has Methods')->toBeString()
|
||||||
|
->newInstance()->name()->toEqual('Has Methods')->not->toBeArray
|
||||||
|
->name()->toEqual('Has Methods')
|
||||||
|
->books()->each->toBeArray;
|
||||||
|
|
||||||
class HasMethods
|
class HasMethods
|
||||||
{
|
{
|
||||||
public function name()
|
public function name()
|
||||||
@ -97,4 +112,9 @@ class HasMethods
|
|||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function newInstance()
|
||||||
|
{
|
||||||
|
return new static();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,10 +14,46 @@ it('can access methods and properties', function () {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can handle nested methods and properties', function () {
|
||||||
|
expect(new HasMethodsAndProperties())
|
||||||
|
->meta->foo->bar->toBeString()->toEqual('baz')->not->toBeInt
|
||||||
|
->newInstance()->meta->foo->toBeArray()
|
||||||
|
->newInstance()->multiply(2, 2)->toEqual(4)->not->toEqual(5)
|
||||||
|
->newInstance()->books()->toBeArray();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works with higher order tests')
|
||||||
|
->expect(new HasMethodsAndProperties())
|
||||||
|
->meta->foo->bar->toBeString()->toEqual('baz')->not->toBeInt
|
||||||
|
->newInstance()->meta->foo->toBeArray
|
||||||
|
->newInstance()->multiply(2, 2)->toEqual(4)->not->toEqual(5)
|
||||||
|
->newInstance()->books()->toBeArray();
|
||||||
|
|
||||||
|
it('can start a new higher order expectation using the and syntax', function () {
|
||||||
|
expect(new HasMethodsAndProperties())
|
||||||
|
->toBeInstanceOf(HasMethodsAndProperties::class)
|
||||||
|
->meta->toBeArray
|
||||||
|
->and(['foo' => 'bar'])
|
||||||
|
->toBeArray()
|
||||||
|
->foo->toEqual('bar');
|
||||||
|
|
||||||
|
expect(static::getCount())->toEqual(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can start a new higher order expectation using the and syntax in higher order tests')
|
||||||
|
->expect(new HasMethodsAndProperties())
|
||||||
|
->toBeInstanceOf(HasMethodsAndProperties::class)
|
||||||
|
->meta->toBeArray
|
||||||
|
->and(['foo' => 'bar'])
|
||||||
|
->toBeArray()
|
||||||
|
->foo->toEqual('bar');
|
||||||
|
|
||||||
class HasMethodsAndProperties
|
class HasMethodsAndProperties
|
||||||
{
|
{
|
||||||
public $name = 'Has Methods and Properties';
|
public $name = 'Has Methods and Properties';
|
||||||
|
|
||||||
|
public $meta = ['foo' => ['bar' => 'baz']];
|
||||||
|
|
||||||
public $posts = [
|
public $posts = [
|
||||||
[
|
[
|
||||||
'is_published' => true,
|
'is_published' => true,
|
||||||
@ -47,4 +83,9 @@ class HasMethodsAndProperties
|
|||||||
{
|
{
|
||||||
return $x * $y;
|
return $x * $y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function newInstance()
|
||||||
|
{
|
||||||
|
return new static();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -58,6 +58,17 @@ it('works with objects', function () {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('works with nested properties', function () {
|
||||||
|
expect(new HasProperties())
|
||||||
|
->nested->foo->bar->toBeString()->toEqual('baz')
|
||||||
|
->posts->toBeArray()->toHaveCount(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works with higher order tests')
|
||||||
|
->expect(new HasProperties())
|
||||||
|
->nested->foo->bar->toBeString()->toEqual('baz')
|
||||||
|
->posts->toBeArray()->toHaveCount(2);
|
||||||
|
|
||||||
class HasProperties
|
class HasProperties
|
||||||
{
|
{
|
||||||
public $name = 'foo';
|
public $name = 'foo';
|
||||||
@ -72,4 +83,8 @@ class HasProperties
|
|||||||
'title' => 'Bar',
|
'title' => 'Bar',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public $nested = [
|
||||||
|
'foo' => ['bar' => 'baz'],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,3 +44,19 @@ test('it works with associative arrays', function () {
|
|||||||
function ($expectation, $key) { $expectation->toEqual('boom'); $key->toEqual('baz'); },
|
function ($expectation, $key) { $expectation->toEqual('boom'); $key->toEqual('baz'); },
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('it can be passed non-callable values', function () {
|
||||||
|
expect(['foo', 'bar', 'baz'])->sequence('foo', 'bar', 'baz');
|
||||||
|
|
||||||
|
expect(static::getCount())->toBe(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('it can be passed a mixture of value types', function () {
|
||||||
|
expect(['foo', 'bar', 'baz'])->sequence(
|
||||||
|
'foo',
|
||||||
|
function ($expectation) { $expectation->toEqual('bar')->toBeString(); },
|
||||||
|
'baz'
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(static::getCount())->toBe(4);
|
||||||
|
});
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
beforeEach()->assertTrue(true);
|
beforeEach()->assertTrue(true);
|
||||||
|
|
||||||
it('proxies calls to object')->assertTrue(true);
|
it('proxies calls to object')->assertTrue(true);
|
||||||
@ -8,4 +10,21 @@ it('is capable doing multiple assertions')
|
|||||||
->assertTrue(true)
|
->assertTrue(true)
|
||||||
->assertFalse(false);
|
->assertFalse(false);
|
||||||
|
|
||||||
|
it('resolves expect callables correctly')
|
||||||
|
->expect(function () { return 'foo'; })
|
||||||
|
->toBeString()
|
||||||
|
->toBe('foo')
|
||||||
|
->and('bar')
|
||||||
|
->toBeString()
|
||||||
|
->toBe('bar');
|
||||||
|
|
||||||
|
test('does not treat method names as callables')
|
||||||
|
->expect('it')->toBeString();
|
||||||
|
|
||||||
|
it('can tap into the test')
|
||||||
|
->expect('foo')->toBeString()
|
||||||
|
->tap(function () { expect($this)->toBeInstanceOf(TestCase::class); })
|
||||||
|
->toBe('foo')
|
||||||
|
->and('hello world')->toBeString();
|
||||||
|
|
||||||
afterEach()->assertTrue(true);
|
afterEach()->assertTrue(true);
|
||||||
|
|||||||
@ -1,5 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->shouldSkip = true;
|
||||||
|
});
|
||||||
|
|
||||||
it('do not skips')
|
it('do not skips')
|
||||||
->skip(false)
|
->skip(false)
|
||||||
->assertTrue(true);
|
->assertTrue(true);
|
||||||
@ -31,3 +35,12 @@ it('skips with condition and message')
|
|||||||
it('skips when skip after assertion')
|
it('skips when skip after assertion')
|
||||||
->assertTrue(true)
|
->assertTrue(true)
|
||||||
->skip();
|
->skip();
|
||||||
|
|
||||||
|
it('can use something in the test case as a condition')
|
||||||
|
->skip(function () { return $this->shouldSkip; }, 'This test was skipped')
|
||||||
|
->assertTrue(false);
|
||||||
|
|
||||||
|
it('can user higher order callables and skip')
|
||||||
|
->skip(function () { return $this->shouldSkip; })
|
||||||
|
->expect(function () { return $this->shouldSkip; })
|
||||||
|
->toBeFalse();
|
||||||
|
|||||||
Reference in New Issue
Block a user