-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionFindOperationTest.php
More file actions
98 lines (80 loc) · 3.84 KB
/
CollectionFindOperationTest.php
File metadata and controls
98 lines (80 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
declare(strict_types=1);
namespace Test\TinyBlocks\Collection\Operations\Retrieve;
use PHPUnit\Framework\TestCase;
use Test\TinyBlocks\Collection\Models\CryptoCurrency;
use TinyBlocks\Collection\Collection;
final class CollectionFindOperationTest extends TestCase
{
public function testFindByInEmptyCollection(): void
{
/** @Given an empty collection */
$collection = Collection::createFromEmpty();
/** @When attempting to find any element */
$actual = $collection->findBy(predicates: static fn(mixed $element): bool => true);
/** @Then the result should be null */
self::assertNull($actual);
}
public function testFindByReturnsNullWhenNoMatch(): void
{
/** @Given a collection with elements */
$collection = Collection::createFrom(elements: [
new CryptoCurrency(name: 'Bitcoin', price: 60000.0, symbol: 'BTC'),
new CryptoCurrency(name: 'Ethereum', price: 40000.0, symbol: 'ETH'),
new CryptoCurrency(name: 'Binance Coin', price: 1500.0, symbol: 'BNB')
]);
/** @When attempting to find an element that doesn't match any condition */
$actual = $collection->findBy(predicates: static fn(CryptoCurrency $element): bool => $element->symbol === 'XRP'
);
/** @Then the result should be null */
self::assertNull($actual);
}
public function testFindByWithMultiplePredicates(): void
{
/** @Given a collection with elements */
$elements = [
new CryptoCurrency(name: 'Bitcoin', price: 60000.0, symbol: 'BTC'),
new CryptoCurrency(name: 'Ethereum', price: 40000.0, symbol: 'ETH'),
new CryptoCurrency(name: 'Binance Coin', price: 1500.0, symbol: 'BNB')
];
$collection = Collection::createFrom(elements: $elements);
/** @When attempting to find the first element matching multiple predicates */
$actual = $collection->findBy(
static fn(CryptoCurrency $element): bool => $element->symbol === 'BNB',
static fn(CryptoCurrency $element): bool => $element->price < 2000.0
);
/** @Then the result should be the expected element */
self::assertSame($elements[2], $actual);
}
public function testFindByReturnsFirstMatchingElement(): void
{
/** @Given a collection with elements */
$elements = [
new CryptoCurrency(name: 'Bitcoin', price: 60000.0, symbol: 'BTC'),
new CryptoCurrency(name: 'Ethereum', price: 40000.0, symbol: 'ETH'),
new CryptoCurrency(name: 'Binance Coin', price: 1500.0, symbol: 'BNB')
];
$collection = Collection::createFrom(elements: $elements);
/** @When attempting to find the first matching element */
$actual = $collection->findBy(predicates: static fn(CryptoCurrency $element): bool => $element->symbol === 'ETH'
);
/** @Then the result should be the expected element */
self::assertSame($elements[1], $actual);
}
public function testFindByWithMultiplePredicatesReturnsNullWhenNoMatch(): void
{
/** @Given a collection with elements */
$collection = Collection::createFrom(elements: [
new CryptoCurrency(name: 'Bitcoin', price: 60000.0, symbol: 'BTC'),
new CryptoCurrency(name: 'Ethereum', price: 40000.0, symbol: 'ETH'),
new CryptoCurrency(name: 'Binance Coin', price: 1500.0, symbol: 'BNB')
]);
/** @When attempting to find an element matching multiple predicates that do not match */
$actual = $collection->findBy(
static fn(CryptoCurrency $element): bool => $element->symbol === 'XRP',
static fn(CryptoCurrency $element): bool => $element->price < 1000.0
);
/** @Then the result should be null */
self::assertNull($actual);
}
}