|
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 | + * @property iterable $value |
11 | 14 | */ |
12 | 15 | trait IterableTrait |
13 | 16 | { |
14 | 17 | /** |
15 | | - * Asserts that the iterable contains the given needle. |
| 18 | + * Asserts that the iterable contains the given needle (strict comparison). |
16 | 19 | * @param mixed $needle The value to look for within the iterable. |
17 | 20 | * @param string $message Optional message for the assertion. |
| 21 | + * |
18 | 22 | * @throws AssertException when the assertion fails. |
19 | 23 | */ |
20 | 24 | public function contains(mixed $needle, string $message = ''): self |
21 | 25 | { |
22 | | - throw new \LogicException('Not implemented yet'); |
| 26 | + foreach ($this->value as $item) { |
| 27 | + if ($item === $needle) { |
| 28 | + StaticState::log( |
| 29 | + 'Assert that iterable: ' . Support::stringify($this->value) . ' contains ' . Support::stringify($needle), |
| 30 | + ); |
| 31 | + return new self($this->value); |
| 32 | + } |
| 33 | + } |
| 34 | + StaticState::fail( |
| 35 | + AssertException::fail( |
| 36 | + 'Failed to assert that iterable ' . Support::stringify($this->value) . ' contains ' . Support::stringify($needle), |
| 37 | + ), |
| 38 | + ); |
23 | 39 | } |
24 | 40 |
|
25 | 41 | /** |
26 | 42 | * Asserts that the iterable has the same number of elements as the expected iterable. |
| 43 | + * |
27 | 44 | * @param iterable $expected The iterable to compare size against. |
28 | 45 | * @param string $message Optional message for the assertion. |
29 | | - * @throws AssertException when the assertion fails. |
| 46 | + * |
| 47 | + * @throws AssertException When the iterables do not have the same size. |
30 | 48 | */ |
31 | 49 | public function sameSizeAs(iterable $expected, string $message = ''): self |
32 | 50 | { |
33 | | - throw new \LogicException('Not implemented yet'); |
| 51 | + if (Support::countIterable($this->value) === Support::countIterable($expected)) { |
| 52 | + StaticState::log( |
| 53 | + 'Assert that iterable: ' . Support::stringify($this->value) . ' has the same number of elements as ' . Support::stringify($expected), |
| 54 | + ); |
| 55 | + return new self($this->value); |
| 56 | + } |
| 57 | + StaticState::fail( |
| 58 | + AssertException::fail( |
| 59 | + 'Failed to assert that iterable ' . Support::stringify($this->value) . ' has the same number of elements as ' . Support::stringify($expected), |
| 60 | + ), |
| 61 | + ); |
34 | 62 | } |
35 | 63 |
|
36 | | - public function allOf(string $type, string $message = ''): \Testo\Assert\Api\Builtin\IterableType |
| 64 | + /** |
| 65 | + * Asserts that all elements of the iterable have the given PHP type. |
| 66 | + * |
| 67 | + * The $type parameter uses PHP internal type names (compatible with gettype()), |
| 68 | + * e.g.: "integer", "double", "boolean", "string", "array", "object", "resource", "null". |
| 69 | + * |
| 70 | + * @param non-empty-string $type Expected PHP type name for all elements. |
| 71 | + * @param string $message Optional message for the assertion. |
| 72 | + * |
| 73 | + * @throws AssertException When at least one element has a different type. |
| 74 | + */ |
| 75 | + public function allOf(string $type, string $message = ''): self |
37 | 76 | { |
38 | | - throw new \LogicException('Not implemented yet'); |
| 77 | + foreach ($this->value as $element) { |
| 78 | + $actualType = \gettype($element); |
| 79 | + if ($actualType !== $type) { |
| 80 | + StaticState::fail( |
| 81 | + AssertException::fail( |
| 82 | + 'Failed to assert that all elements of iterable ' . Support::stringify($this->value) . ' have type ' . Support::stringify($type) . |
| 83 | + ' (found ' . Support::stringify($actualType) . ' instead)', |
| 84 | + ), |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | + StaticState::log( |
| 89 | + 'Assert that all elements of iterable ' . Support::stringify($this->value) . ' have type ' . Support::stringify($type), |
| 90 | + ); |
| 91 | + return new self($this->value); |
39 | 92 | } |
40 | 93 | } |
0 commit comments