Compare commits

...

23 Commits

Author SHA1 Message Date
1680613e12 docs: update changelog 2021-06-19 14:42:29 +01:00
f6d3ce41bc chore: updates funding 2021-06-18 22:17:44 +01:00
d16a48bf0f chore: cs 2021-06-18 22:16:54 +01:00
9459ce4030 Merge pull request #324 from pestphp/nested-higher-order-expectations
Adds Nested Higher Order Expectations
2021-06-18 22:12:17 +01:00
c773d1cd57 chore: type checks scripts folder 2021-06-18 22:09:56 +01:00
22a1aac84a Fixes types 2021-06-18 22:08:47 +01:00
3a20696da4 CS 2021-06-18 22:03:51 +01:00
99bcf98617 Rebuilds snapshots 2021-06-18 22:03:01 +01:00
09d9bae988 Merge branch 'master' into nested-higher-order-expectations
# Conflicts:
#	tests/.snapshots/success.txt
2021-06-18 22:01:30 +01:00
27de6106ab Adds type hinting 2021-06-18 22:01:16 +01:00
aeded0a356 refacto: coding style updates 2021-06-18 22:01:14 +01:00
afef1d56e8 Merge pull request #323 from pestphp/non-callable-sequence
Sequenced expectations can now be passed as non-callable values
2021-06-18 21:53:04 +01:00
4b55de27f1 Adds generics 2021-06-18 21:52:24 +01:00
3d7b6426a1 Updates snapshots 2021-06-18 21:49:38 +01:00
1d415eb7fb Merge branch 'master' into nested-higher-order-expectations
# Conflicts:
#	tests/.snapshots/success.txt
2021-06-18 21:49:05 +01:00
8a384a6d65 Merge fixes 2021-06-18 21:46:57 +01:00
7c4dd2f2e7 Merge branch 'master' into non-callable-sequence
# Conflicts:
#	tests/.snapshots/success.txt
2021-06-18 21:45:02 +01:00
b6f0496c3c chore: updates funding 2021-06-18 12:39:05 +01:00
d96a2485b6 Updates snapshot 2021-06-17 00:18:12 +01:00
9e9d1cc8cc Merge branch 'master' into nested-higher-order-expectations
# Conflicts:
#	src/Expectation.php
#	tests/.snapshots/success.txt
2021-06-17 00:17:25 +01:00
579bb1b90c Updates DocBlock type hinting 2021-06-17 00:15:42 +01:00
7ff64540a6 Adds nested Higher Order Expectations. 2021-06-16 20:48:23 +01:00
241d4cf94c Reimplements non-callable sequence values. 2021-06-16 20:34:17 +01:00
15 changed files with 182 additions and 64 deletions

3
.github/FUNDING.yml vendored
View File

@ -1,5 +1,4 @@
# These are supported funding model platforms
github: [nunomaduro,owenvoke,olivernybroe,octoper]
github: [nunomaduro,owenvoke,olivernybroe,octoper,lukeraymonddowning]
patreon: nunomaduro
custom: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=66BYDWAT92N6L

View File

@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [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)
### Added
- Adds a new `json` expectation method to improve testing with JSON strings ([#325](https://github.com/pestphp/pest/pull/325))

View File

@ -7,6 +7,7 @@ parameters:
level: max
paths:
- src
- scripts
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: false

View File

@ -15,6 +15,7 @@ $globalsFilePath = implode(DIRECTORY_SEPARATOR, [
$compiledFilePath = implode(DIRECTORY_SEPARATOR, [dirname(__DIR__), 'compiled', 'globals.php']);
/* @phpstan-ignore-next-line */
@unlink($compiledFilePath);
$replace = function ($contents, $string, $by) {

View File

@ -12,9 +12,13 @@ use Pest\Expectation;
trait Expectable
{
/**
* @template TValue
*
* Creates a new expectation.
*
* @param mixed $value
* @param TValue $value
*
* @return Expectation<TValue>
*/
public function expect($value): Expectation
{

View File

@ -4,8 +4,8 @@ declare(strict_types=1);
namespace Pest\Concerns;
use BadMethodCallException;
use Closure;
use Pest\HigherOrderExpectation;
/**
* @internal
@ -43,7 +43,7 @@ trait Extendable
public function __call(string $method, array $parameters)
{
if (!static::hasExtend($method)) {
return new HigherOrderExpectation($this, $method, $parameters);
throw new BadMethodCallException("$method is not a callable method name.");
}
/** @var Closure $extend */

View 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;
}
}

View File

@ -6,6 +6,7 @@ namespace Pest;
use BadMethodCallException;
use Pest\Concerns\Extendable;
use Pest\Concerns\RetrievesValues;
use Pest\Support\Arr;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\Constraint;
@ -15,12 +16,17 @@ use SebastianBergmann\Exporter\Exporter;
/**
* @internal
*
* @template TValue
*
* @property Expectation $not Creates the opposite expectation.
* @property Each $each Creates an expectation on each element on the traversable value.
*/
final class Expectation
{
use Extendable;
use Extendable {
__call as __extendsCall;
}
use RetrievesValues;
/**
* The expectation value.
@ -43,7 +49,7 @@ final class Expectation
/**
* Creates a new expectation.
*
* @param mixed $value
* @param TValue $value
*/
public function __construct($value)
{
@ -53,7 +59,9 @@ final class Expectation
/**
* Creates a new expectation.
*
* @param mixed $value
* @param TValue $value
*
* @return Expectation<TValue>
*/
public function and($value): Expectation
{
@ -120,7 +128,7 @@ final class Expectation
if (is_callable($callback)) {
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".
*
* @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)) {
throw new BadMethodCallException('Expectation value is not iterable.');
@ -148,7 +160,12 @@ final class Expectation
}
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;
@ -712,6 +729,24 @@ final class Expectation
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
* or creates a new higher order expectation.
@ -721,7 +756,7 @@ final class Expectation
public function __get(string $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 */

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pest;
use Pest\Concerns\Expectable;
use Pest\Concerns\RetrievesValues;
/**
* @internal
@ -14,6 +15,7 @@ use Pest\Concerns\Expectable;
final class HigherOrderExpectation
{
use Expectable;
use RetrievesValues;
/**
* @var Expectation
@ -30,6 +32,11 @@ final class HigherOrderExpectation
*/
private $opposite = false;
/**
* @var bool
*/
private $shouldReset = false;
/**
* @var string
*/
@ -38,45 +45,12 @@ final class HigherOrderExpectation
/**
* Creates a new higher order expectation.
*
* @param array<int|string, mixed>|null $parameters
* @phpstan-ignore-next-line
* @param mixed $value
*/
public function __construct(Expectation $original, string $name, ?array $parameters = null)
public function __construct(Expectation $original, $value)
{
$this->original = $original;
$this->name = $name;
$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);
$this->expectation = $this->expect($value);
}
/**
@ -92,12 +66,13 @@ final class HigherOrderExpectation
/**
* 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
{
if (!$this->originalHasMethod($name)) {
return new self($this->original, $name, $arguments);
if (!$this->expectationHasMethod($name)) {
/* @phpstan-ignore-next-line */
return new self($this->original, $this->getValue()->$name(...$arguments));
}
return $this->performAssertion($name, $arguments);
@ -112,8 +87,8 @@ final class HigherOrderExpectation
return $this->not();
}
if (!$this->originalHasMethod($name)) {
return new self($this->original, $name);
if (!$this->expectationHasMethod($name)) {
return new self($this->original, $this->retrieve($name, $this->getValue()));
}
return $this->performAssertion($name, []);
@ -122,25 +97,33 @@ final class HigherOrderExpectation
/**
* 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);
}
/**
* 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.
*
* @param array<int|string, mixed> $arguments
* @param array<int, mixed> $arguments
*/
private function performAssertion(string $name, array $arguments): self
{
$expectation = $this->opposite
? $this->expectation->not()
: $this->expectation;
$this->expectation = $expectation->{$name}(...$arguments); // @phpstan-ignore-line
/* @phpstan-ignore-next-line */
$this->expectation = ($this->opposite ? $this->expectation->not() : $this->expectation)->{$name}(...$arguments);
$this->opposite = false;
$this->shouldReset = true;
return $this;
}

View File

@ -6,7 +6,7 @@ namespace Pest;
function version(): string
{
return '1.6.0';
return '1.7.0';
}
function testDirectory(string $file = ''): string

View File

@ -111,9 +111,11 @@
✓ it works inside of each
✓ it works with sequence
✓ it can compose complex expectations
✓ it can handle nested method calls
PASS Tests\Features\Expect\HigherOrder\methodsAndProperties
✓ it can access methods and properties
✓ it can handle nested methods and properties
PASS Tests\Features\Expect\HigherOrder\properties
✓ it allows properties to be accessed from the value
@ -124,6 +126,7 @@
✓ it works with sequence
✓ it can compose complex expectations
✓ it works with objects
✓ it works with nested properties
PASS Tests\Features\Expect\each
✓ an exception is thrown if the the type is not iterable
@ -156,6 +159,8 @@
✓ 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 with associative arrays
✓ it can be passed non-callable values
✓ it can be passed a mixture of value types
PASS Tests\Features\Expect\toBe
✓ strict comparisons
@ -573,5 +578,5 @@
✓ it is a test
✓ it uses correct parent class
Tests: 4 incompleted, 7 skipped, 357 passed
Tests: 4 incompleted, 7 skipped, 362 passed

View File

@ -59,6 +59,14 @@ 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();
});
class HasMethods
{
public function name()
@ -97,4 +105,9 @@ class HasMethods
],
];
}
public function newInstance()
{
return new static();
}
}

View File

@ -14,10 +14,20 @@ 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();
});
class HasMethodsAndProperties
{
public $name = 'Has Methods and Properties';
public $meta = ['foo' => ['bar' => 'baz']];
public $posts = [
[
'is_published' => true,
@ -47,4 +57,9 @@ class HasMethodsAndProperties
{
return $x * $y;
}
public function newInstance()
{
return new static();
}
}

View File

@ -58,6 +58,12 @@ it('works with objects', function () {
);
});
it('works with nested properties', function () {
expect(new HasProperties())
->nested->foo->bar->toBeString()->toEqual('baz')
->posts->toBeArray()->toHaveCount(2);
});
class HasProperties
{
public $name = 'foo';
@ -72,4 +78,8 @@ class HasProperties
'title' => 'Bar',
],
];
public $nested = [
'foo' => ['bar' => 'baz'],
];
}

View File

@ -44,3 +44,19 @@ test('it works with associative arrays', function () {
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);
});