mirror of
https://github.com/pestphp/pest.git
synced 2026-03-11 10:17:23 +01:00
feat: move Expectations API out of external plugin
This commit is contained in:
100
tests/Features/Expect/HigherOrder/methods.php
Normal file
100
tests/Features/Expect/HigherOrder/methods.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
it('can access methods', function () {
|
||||
expect(new HasMethods())
|
||||
->name()->toBeString()->toEqual('Has Methods');
|
||||
});
|
||||
|
||||
it('can access multiple methods', function () {
|
||||
expect(new HasMethods())
|
||||
->name()->toBeString()->toEqual('Has Methods')
|
||||
->quantity()->toBeInt()->toEqual(20);
|
||||
});
|
||||
|
||||
it('works with not', function () {
|
||||
expect(new HasMethods())
|
||||
->name()->not->toEqual('world')->toEqual('Has Methods')
|
||||
->quantity()->toEqual(20)->not()->toEqual('bar')->not->toBeNull;
|
||||
});
|
||||
|
||||
it('can accept arguments', function () {
|
||||
expect(new HasMethods())
|
||||
->multiply(5, 4)->toBeInt->toEqual(20);
|
||||
});
|
||||
|
||||
it('works with each', function () {
|
||||
expect(new HasMethods())
|
||||
->attributes()->toBeArray->each->not()->toBeNull
|
||||
->attributes()->each(function ($attribute) {
|
||||
$attribute->not->toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it('works inside of each', function () {
|
||||
expect(new HasMethods())
|
||||
->books()->each(function ($book) {
|
||||
$book->title->not->toBeNull->cost->toBeGreaterThan(19);
|
||||
});
|
||||
});
|
||||
|
||||
it('works with sequence', function () {
|
||||
expect(new HasMethods())
|
||||
->books()->sequence(
|
||||
function ($book) { $book->title->toEqual('Foo')->cost->toEqual(20); },
|
||||
function ($book) { $book->title->toEqual('Bar')->cost->toEqual(30); },
|
||||
);
|
||||
});
|
||||
|
||||
it('can compose complex expectations', function () {
|
||||
expect(new HasMethods())
|
||||
->toBeObject()
|
||||
->name()->toEqual('Has Methods')->not()->toEqual('bar')
|
||||
->quantity()->not->toEqual('world')->toEqual(20)->toBeInt
|
||||
->multiply(3, 4)->not->toBeString->toEqual(12)
|
||||
->attributes()->toBeArray()
|
||||
->books()->toBeArray->each->not->toBeEmpty
|
||||
->books()->sequence(
|
||||
function ($book) { $book->title->toEqual('Foo')->cost->toEqual(20); },
|
||||
function ($book) { $book->title->toEqual('Bar')->cost->toEqual(30); },
|
||||
);
|
||||
});
|
||||
|
||||
class HasMethods
|
||||
{
|
||||
public function name()
|
||||
{
|
||||
return 'Has Methods';
|
||||
}
|
||||
|
||||
public function quantity()
|
||||
{
|
||||
return 20;
|
||||
}
|
||||
|
||||
public function multiply($x, $y)
|
||||
{
|
||||
return $x * $y;
|
||||
}
|
||||
|
||||
public function attributes()
|
||||
{
|
||||
return [
|
||||
'name' => $this->name(),
|
||||
'quantity' => $this->quantity(),
|
||||
];
|
||||
}
|
||||
|
||||
public function books()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'title' => 'Foo',
|
||||
'cost' => 20,
|
||||
],
|
||||
[
|
||||
'title' => 'Bar',
|
||||
'cost' => 30,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
50
tests/Features/Expect/HigherOrder/methodsAndProperties.php
Normal file
50
tests/Features/Expect/HigherOrder/methodsAndProperties.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
it('can access methods and properties', function () {
|
||||
expect(new HasMethodsAndProperties())
|
||||
->name->toEqual('Has Methods and Properties')->not()->toEqual('bar')
|
||||
->multiply(3, 4)->not->toBeString->toEqual(12)
|
||||
->posts->each(function ($post) {
|
||||
$post->is_published->toBeTrue;
|
||||
})->books()->toBeArray()
|
||||
->posts->toBeArray->each->not->toBeEmpty
|
||||
->books()->sequence(
|
||||
function ($book) { $book->title->toEqual('Foo')->cost->toEqual(20); },
|
||||
function ($book) { $book->title->toEqual('Bar')->cost->toEqual(30); },
|
||||
);
|
||||
});
|
||||
|
||||
class HasMethodsAndProperties
|
||||
{
|
||||
public $name = 'Has Methods and Properties';
|
||||
|
||||
public $posts = [
|
||||
[
|
||||
'is_published' => true,
|
||||
'title' => 'Foo',
|
||||
],
|
||||
[
|
||||
'is_published' => true,
|
||||
'title' => 'Bar',
|
||||
],
|
||||
];
|
||||
|
||||
public function books()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'title' => 'Foo',
|
||||
'cost' => 20,
|
||||
],
|
||||
[
|
||||
'title' => 'Bar',
|
||||
'cost' => 30,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function multiply($x, $y)
|
||||
{
|
||||
return $x * $y;
|
||||
}
|
||||
}
|
||||
75
tests/Features/Expect/HigherOrder/properties.php
Normal file
75
tests/Features/Expect/HigherOrder/properties.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
it('allows properties to be accessed from the value', function () {
|
||||
expect(['foo' => 1])->foo->toBeInt()->toEqual(1);
|
||||
});
|
||||
|
||||
it('can access multiple properties from the value', function () {
|
||||
expect(['foo' => 'bar', 'hello' => 'world'])
|
||||
->foo->toBeString()->toEqual('bar')
|
||||
->hello->toBeString()->toEqual('world');
|
||||
});
|
||||
|
||||
it('works with not', function () {
|
||||
expect(['foo' => 'bar', 'hello' => 'world'])
|
||||
->foo->not->not->toEqual('bar')
|
||||
->foo->not->toEqual('world')->toEqual('bar')
|
||||
->hello->toEqual('world')->not()->toEqual('bar')->not->toBeNull;
|
||||
});
|
||||
|
||||
it('works with each', function () {
|
||||
expect(['numbers' => [1, 2, 3, 4], 'words' => ['hey', 'there']])
|
||||
->numbers->toEqual([1, 2, 3, 4])->each->toBeInt->toBeLessThan(5)
|
||||
->words->each(function ($word) {
|
||||
$word->toBeString()->not->toBeInt();
|
||||
});
|
||||
});
|
||||
|
||||
it('works inside of each', function () {
|
||||
expect(['books' => [['title' => 'Foo', 'cost' => 20], ['title' => 'Bar', 'cost' => 30]]])
|
||||
->books->each(function ($book) {
|
||||
$book->title->not->toBeNull->cost->toBeGreaterThan(19);
|
||||
});
|
||||
});
|
||||
|
||||
it('works with sequence', function () {
|
||||
expect(['books' => [['title' => 'Foo', 'cost' => 20], ['title' => 'Bar', 'cost' => 30]]])
|
||||
->books->sequence(
|
||||
function ($book) { $book->title->toEqual('Foo')->cost->toEqual(20); },
|
||||
function ($book) { $book->title->toEqual('Bar')->cost->toEqual(30); },
|
||||
);
|
||||
});
|
||||
|
||||
it('can compose complex expectations', function () {
|
||||
expect(['foo' => 'bar', 'numbers' => [1, 2, 3, 4]])
|
||||
->toContain('bar')->toBeArray()
|
||||
->numbers->toEqual([1, 2, 3, 4])->not()->toEqual('bar')->each->toBeInt
|
||||
->foo->not->toEqual('world')->toEqual('bar')
|
||||
->numbers->toBeArray();
|
||||
});
|
||||
|
||||
it('works with objects', function () {
|
||||
expect(new HasProperties())
|
||||
->name->toEqual('foo')->not->toEqual('world')
|
||||
->posts->toHaveCount(2)->each(function ($post) { $post->is_published->toBeTrue(); })
|
||||
->posts->sequence(
|
||||
function ($post) { $post->title->toEqual('Foo'); },
|
||||
function ($post) { $post->title->toEqual('Bar'); },
|
||||
);
|
||||
});
|
||||
|
||||
class HasProperties
|
||||
{
|
||||
public $name = 'foo';
|
||||
|
||||
public $posts = [
|
||||
[
|
||||
'is_published' => true,
|
||||
'title' => 'Foo',
|
||||
],
|
||||
[
|
||||
'is_published' => true,
|
||||
'title' => 'Bar',
|
||||
],
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user