Merge branch 'master' into scoped

# Conflicts:
#	src/Expectations/HigherOrderExpectation.php
#	tests/Features/Expect/HigherOrder/methods.php
This commit is contained in:
Luke Downing
2022-01-23 23:44:04 +00:00
5 changed files with 34 additions and 9 deletions

View File

@ -62,7 +62,7 @@ final class Expectation
/** /**
* Creates a new expectation with the decoded JSON value. * Creates a new expectation with the decoded JSON value.
* *
* @return self<mixed> * @return self<array<int|string, mixed>|bool>
*/ */
public function json(): Expectation public function json(): Expectation
{ {
@ -70,7 +70,10 @@ final class Expectation
InvalidExpectationValue::expected('string'); InvalidExpectationValue::expected('string');
} }
return $this->toBeJson()->and(json_decode($this->value, true)); /** @var array<int|string, mixed>|bool $value */
$value = json_decode($this->value, true);
return $this->toBeJson()->and($value);
} }
/** /**

View File

@ -95,6 +95,16 @@ final class HigherOrderExpectation
return new self($this->original, $this->original->value); return new self($this->original, $this->original->value);
} }
/**
* Creates a new expectation with the decoded JSON value.
*
* @return self<TOriginalValue, array<string|int, mixed>|bool>
*/
public function json(): self
{
return new self($this->original, $this->expectation->json()->value);
}
/** /**
* Dynamically calls methods on the class with the given arguments. * Dynamically calls methods on the class with the given arguments.
* *

View File

@ -52,9 +52,9 @@ final class HigherOrderCallables
} }
/** /**
* Tap into the test case to perform an action and return the test case. * Execute the given callable after the test has executed the setup method.
*/ */
public function tap(callable $callable): object public function defer(callable $callable): object
{ {
Reflection::bindCallableWithData($callable); Reflection::bindCallableWithData($callable);

View File

@ -91,8 +91,20 @@ it('can use the scoped method to lock into the given level for expectations', fu
); );
}); });
it('works consistently with the json expectation method', function () {
expect(new HasMethods())
->jsonString()->json()->id->toBe(1)
->jsonString()->json()->name->toBe('Has Methods')->toBeString()
->jsonString()->json()->quantity->toBe(20)->toBeInt();
});
class HasMethods class HasMethods
{ {
public function jsonString(): string
{
return '{ "id": 1, "name": "Has Methods", "quantity": 20 }';
}
public function name() public function name()
{ {
return 'Has Methods'; return 'Has Methods';

View File

@ -21,9 +21,9 @@ it('resolves expect callables correctly')
test('does not treat method names as callables') test('does not treat method names as callables')
->expect('it')->toBeString(); ->expect('it')->toBeString();
it('can tap into the test') it('can defer a method until after test setup')
->expect('foo')->toBeString() ->expect('foo')->toBeString()
->tap(function () { expect($this)->toBeInstanceOf(TestCase::class); }) ->defer(function () { expect($this)->toBeInstanceOf(TestCase::class); })
->toBe('foo') ->toBe('foo')
->and('hello world')->toBeString(); ->and('hello world')->toBeString();
@ -32,15 +32,15 @@ it('can pass datasets into the expect callables')
->expect(function (...$numbers) { return $numbers; })->toBe([1, 2, 3]) ->expect(function (...$numbers) { return $numbers; })->toBe([1, 2, 3])
->and(function (...$numbers) { return $numbers; })->toBe([1, 2, 3]); ->and(function (...$numbers) { return $numbers; })->toBe([1, 2, 3]);
it('can pass datasets into the tap callable') it('can pass datasets into the defer callable')
->with([[1, 2, 3]]) ->with([[1, 2, 3]])
->tap(function (...$numbers) { expect($numbers)->toBe([1, 2, 3]); }); ->defer(function (...$numbers) { expect($numbers)->toBe([1, 2, 3]); });
it('can pass shared datasets into callables') it('can pass shared datasets into callables')
->with('numbers.closure.wrapped') ->with('numbers.closure.wrapped')
->expect(function ($value) { return $value; }) ->expect(function ($value) { return $value; })
->and(function ($value) { return $value; }) ->and(function ($value) { return $value; })
->tap(function ($value) { expect($value)->toBeInt(); }) ->defer(function ($value) { expect($value)->toBeInt(); })
->toBeInt(); ->toBeInt();
afterEach()->assertTrue(true); afterEach()->assertTrue(true);