|
5 | 5 | namespace Testo\Assert\Internal\Assertion\Traits; |
6 | 6 |
|
7 | 7 | use Testo\Assert\State\AssertException; |
| 8 | +use Testo\Assert\StaticState; |
| 9 | +use Testo\Assert\Support; |
8 | 10 |
|
9 | 11 | /** |
10 | | - * Contains methods for comparing numeric values |
| 12 | + * Contains assertion methods for iterable values. |
| 13 | + * |
| 14 | + * @property iterable $value |
11 | 15 | */ |
12 | 16 | trait IterableTrait |
13 | 17 | { |
14 | | - /** |
15 | | - * Asserts that the iterable contains the given needle. |
16 | | - * @param mixed $needle The value to look for within the iterable. |
17 | | - * @param string $message Optional message for the assertion. |
18 | | - * @throws AssertException when the assertion fails. |
19 | | - */ |
| 18 | + #[\Override] |
20 | 19 | public function contains(mixed $needle, string $message = ''): self |
21 | 20 | { |
22 | | - throw new \LogicException('Not implemented yet'); |
| 21 | + foreach ($this->value as $item) { |
| 22 | + if ($item === $needle) { |
| 23 | + StaticState::log('Assert contains: ' . Support::stringify($needle) . '.'); |
| 24 | + return new self($this->value); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + StaticState::fail( |
| 29 | + AssertException::fail( |
| 30 | + \sprintf( |
| 31 | + 'Failed to assert that %s contains %s.', |
| 32 | + Support::stringify($this->value), |
| 33 | + Support::stringify($needle), |
| 34 | + ), |
| 35 | + ), |
| 36 | + ); |
23 | 37 | } |
24 | 38 |
|
25 | | - /** |
26 | | - * Asserts that the iterable has the same number of elements as the expected iterable. |
27 | | - * @param iterable $expected The iterable to compare size against. |
28 | | - * @param string $message Optional message for the assertion. |
29 | | - * @throws AssertException when the assertion fails. |
30 | | - */ |
| 39 | + #[\Override] |
31 | 40 | public function sameSizeAs(iterable $expected, string $message = ''): self |
32 | 41 | { |
33 | | - throw new \LogicException('Not implemented yet'); |
| 42 | + if (self::countIterable($this->value) === self::countIterable($expected)) { |
| 43 | + StaticState::log('Assert same size as: ' . Support::stringify($expected) . '.'); |
| 44 | + return new self($this->value); |
| 45 | + } |
| 46 | + |
| 47 | + StaticState::fail( |
| 48 | + AssertException::fail( |
| 49 | + \sprintf( |
| 50 | + 'Failed to assert that iterable %s has the same number of elements as %s.', |
| 51 | + Support::stringify($this->value), |
| 52 | + Support::stringify($expected), |
| 53 | + ), |
| 54 | + ), |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + #[\Override] |
| 59 | + public function allOf(string $type, string $message = ''): self |
| 60 | + { |
| 61 | + $type = \strtolower($type); |
| 62 | + $type = match ($type) { |
| 63 | + 'integer' => 'int', |
| 64 | + 'double' => 'float', |
| 65 | + 'boolean' => 'bool', |
| 66 | + default => $type, |
| 67 | + }; |
| 68 | + foreach ($this->value as $element) { |
| 69 | + $actualType = \strtolower(\get_debug_type($element)); |
| 70 | + $actualType === $type or StaticState::fail( |
| 71 | + AssertException::fail( |
| 72 | + \sprintf( |
| 73 | + 'Failed to assert that all elements of iterable %s have type %s (found %s instead).', |
| 74 | + Support::stringify($this->value), |
| 75 | + Support::stringify($type), |
| 76 | + Support::stringify($actualType), |
| 77 | + ), |
| 78 | + ), |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + StaticState::log( |
| 83 | + \sprintf( |
| 84 | + 'Assert all elements are of type %s.', |
| 85 | + Support::stringify($type), |
| 86 | + ), |
| 87 | + ); |
| 88 | + return new self($this->value); |
34 | 89 | } |
35 | 90 |
|
36 | | - public function allOf(string $type, string $message = ''): \Testo\Assert\Api\Builtin\IterableType |
| 91 | + #[\Override] |
| 92 | + public function hasCount(int $expected): self |
37 | 93 | { |
38 | | - throw new \LogicException('Not implemented yet'); |
| 94 | + $count = self::countIterable($this->value); |
| 95 | + if ($count === $expected) { |
| 96 | + StaticState::log("Assert count: {$count}."); |
| 97 | + return new self($this->value); |
| 98 | + } |
| 99 | + |
| 100 | + StaticState::fail( |
| 101 | + AssertException::fail( |
| 102 | + \sprintf( |
| 103 | + 'Failed to assert that %s has %d elements (found %d instead).', |
| 104 | + Support::stringify($this->value), |
| 105 | + $expected, |
| 106 | + $count, |
| 107 | + ), |
| 108 | + ), |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Counts the number of elements in the given iterable. |
| 114 | + */ |
| 115 | + private static function countIterable(iterable $value): int |
| 116 | + { |
| 117 | + // if Countable |
| 118 | + if (\is_array($value) || $value instanceof \Countable) { |
| 119 | + return \count($value); |
| 120 | + } |
| 121 | + |
| 122 | + // if Traversable |
| 123 | + $count = 0; |
| 124 | + foreach ($value as $_) { |
| 125 | + $count++; |
| 126 | + } |
| 127 | + |
| 128 | + return $count; |
39 | 129 | } |
40 | 130 | } |
0 commit comments