|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Shopware\Tests\Unit\Core\Framework\Struct; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use function PHPStan\Testing\assertType; |
| 7 | + |
| 8 | +/** |
| 9 | + * @internal |
| 10 | + */ |
| 11 | +class CollectionTest extends TestCase |
| 12 | +{ |
| 13 | + public function testFromAssociative(): void |
| 14 | + { |
| 15 | + $data = [ |
| 16 | + null, |
| 17 | + 0, |
| 18 | + 'some-string', |
| 19 | + new \stdClass(), |
| 20 | + ['some' => 'value'], |
| 21 | + ]; |
| 22 | + |
| 23 | + $collection = (new TestCollection())->assignRecursive($data); |
| 24 | + |
| 25 | + static::assertCount(5, $collection); |
| 26 | + |
| 27 | + assertType("array{null, 0, 'some-string', stdClass, array{some: 'value'}}", $data); |
| 28 | + static::assertSame($data[0], $collection->get(0)); |
| 29 | + assertType("array{null, 0, 'some-string', stdClass, array{some: 'value'}}", $data); |
| 30 | + static::assertSame($data[1], $collection->get(1)); |
| 31 | + assertType("array{null, 0, 'some-string', stdClass, array{some: 'value'}}", $data); |
| 32 | + static::assertSame($data[2], $collection->get(2)); |
| 33 | + assertType("array{null, 0, 'some-string', stdClass, array{some: 'value'}}", $data); |
| 34 | + static::assertSame($data[3], $collection->get(3)); |
| 35 | + assertType("array{null, 0, 'some-string', stdClass, array{some: 'value'}}", $data); |
| 36 | + static::assertSame($data[4], $collection->get(4)); |
| 37 | + assertType("array{null, 0, 'some-string', stdClass, array{some: 'value'}}", $data); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * @template TElement |
| 43 | + * |
| 44 | + * @extends Collection<TElement> |
| 45 | + */ |
| 46 | +class TestCollection extends Collection |
| 47 | +{ |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * @template TElement |
| 52 | + * |
| 53 | + * @implements \IteratorAggregate<array-key, TElement> |
| 54 | + */ |
| 55 | +abstract class Collection implements \IteratorAggregate, \Countable |
| 56 | +{ |
| 57 | + /** |
| 58 | + * @var array<array-key, TElement> |
| 59 | + */ |
| 60 | + protected array $elements = []; |
| 61 | + |
| 62 | + /** |
| 63 | + * @param iterable<TElement> $elements |
| 64 | + */ |
| 65 | + public function __construct(iterable $elements = []) |
| 66 | + { |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param array-key $key |
| 71 | + * |
| 72 | + * @return TElement|null |
| 73 | + */ |
| 74 | + public function get($key) |
| 75 | + { |
| 76 | + return $this->elements[$key] ?? null; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @phpstan-impure |
| 81 | + */ |
| 82 | + public function count(): int |
| 83 | + { |
| 84 | + return \count($this->elements); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @return \Traversable<TElement> |
| 89 | + */ |
| 90 | + public function getIterator(): \Traversable |
| 91 | + { |
| 92 | + yield from $this->elements; |
| 93 | + } |
| 94 | + |
| 95 | + public function assignRecursive(array $options): static |
| 96 | + { |
| 97 | + return $this; |
| 98 | + } |
| 99 | +} |
0 commit comments