-
-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #407 from mertasan/method-match
Adds new `match()` method
- Loading branch information
Showing
3 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\ExpectationFailedException; | ||
|
||
beforeEach(function () { | ||
$this->matched = null; | ||
}); | ||
|
||
it('pass', function () { | ||
expect('baz') | ||
->match('foo', [ | ||
'bar' => function ($value) { | ||
$this->matched = 'bar'; | ||
|
||
return $value->toEqual('bar'); | ||
}, | ||
'foo' => function ($value) { | ||
$this->matched = 'baz'; | ||
|
||
return $value->toEqual('baz'); | ||
}, | ||
] | ||
) | ||
->toEqual($this->matched); | ||
|
||
expect(static::getCount())->toBe(2); | ||
}); | ||
|
||
it('failures', function () { | ||
expect(true) | ||
->match('foo', [ | ||
'bar' => function ($value) { | ||
return $value->toBeTrue(); | ||
}, | ||
'foo' => function ($value) { | ||
return $value->toBeFalse(); | ||
}, | ||
] | ||
); | ||
})->throws(ExpectationFailedException::class, 'true is false'); | ||
|
||
it('runs with truthy', function () { | ||
expect('foo') | ||
->match(1, [ | ||
'bar' => function ($value) { | ||
$this->matched = 'bar'; | ||
|
||
return $value->toEqual('bar'); | ||
}, | ||
true => function ($value) { | ||
$this->matched = 'foo'; | ||
|
||
return $value->toEqual('foo'); | ||
}, | ||
] | ||
) | ||
->toEqual($this->matched); | ||
|
||
expect(static::getCount())->toBe(2); | ||
}); | ||
|
||
it('runs with falsy', function () { | ||
expect('foo') | ||
->match(false, [ | ||
'bar' => function ($value) { | ||
$this->matched = 'bar'; | ||
|
||
return $value->toEqual('bar'); | ||
}, | ||
false => function ($value) { | ||
$this->matched = 'foo'; | ||
|
||
return $value->toEqual('foo'); | ||
}, | ||
] | ||
) | ||
->toEqual($this->matched); | ||
|
||
expect(static::getCount())->toBe(2); | ||
}); | ||
|
||
it('runs with truthy closure condition', function () { | ||
expect('foo') | ||
->match( | ||
function () { return '1'; }, [ | ||
'bar' => function ($value) { | ||
$this->matched = 'bar'; | ||
|
||
return $value->toEqual('bar'); | ||
}, | ||
true => function ($value) { | ||
$this->matched = 'foo'; | ||
|
||
return $value->toEqual('foo'); | ||
}, | ||
] | ||
) | ||
->toEqual($this->matched); | ||
|
||
expect(static::getCount())->toBe(2); | ||
}); | ||
|
||
it('runs with falsy closure condition', function () { | ||
expect('foo') | ||
->match( | ||
function () { return '0'; }, [ | ||
'bar' => function ($value) { | ||
$this->matched = 'bar'; | ||
|
||
return $value->toEqual('bar'); | ||
}, | ||
false => function ($value) { | ||
$this->matched = 'foo'; | ||
|
||
return $value->toEqual('foo'); | ||
}, | ||
] | ||
) | ||
->toEqual($this->matched); | ||
|
||
expect(static::getCount())->toBe(2); | ||
}); | ||
|
||
it('can be passed non-callable values', function () { | ||
expect('foo') | ||
->match('pest', [ | ||
'bar' => 'foo', | ||
'pest' => 'baz', | ||
] | ||
); | ||
})->throws(ExpectationFailedException::class, 'two strings are equal'); | ||
|
||
it('passes with empty data', function () { | ||
expect('foo') | ||
->match('bar', []) | ||
->toEqual('foo'); | ||
}); | ||
|
||
it('can be used in higher order tests') | ||
->expect(true) | ||
->match( | ||
function () { return true; }, [ | ||
false => function ($value) { | ||
return $value->toBeFalse(); | ||
}, | ||
true => function ($value) { | ||
return $value->toBeTrue(); | ||
}, | ||
] | ||
); |