|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpPp\Core\Component\Tests\Unit\Collection; |
| 6 | + |
| 7 | +use PhpPp\Core\Component\Exception\Collection\InvalidValueTypeException; |
| 8 | +use PhpPp\Core\Contract\Collection\CollectionInterface; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +abstract class AbstractConstructTest extends TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @param array<string|int, mixed> $values |
| 15 | + * @return CollectionInterface<mixed> |
| 16 | + */ |
| 17 | + abstract protected function createCollection(array $values): CollectionInterface; |
| 18 | + |
| 19 | + abstract protected function getExceptionMessage(): string; |
| 20 | + |
| 21 | + public function testString(): void |
| 22 | + { |
| 23 | + $this->expectException(InvalidValueTypeException::class); |
| 24 | + $this->expectExceptionCode(0); |
| 25 | + $this->expectExceptionMessage($this->getExceptionMessage()); |
| 26 | + |
| 27 | + $this->createCollection(['foo']); |
| 28 | + } |
| 29 | + |
| 30 | + public function testInteger(): void |
| 31 | + { |
| 32 | + $this->expectException(InvalidValueTypeException::class); |
| 33 | + $this->expectExceptionCode(0); |
| 34 | + $this->expectExceptionMessage($this->getExceptionMessage()); |
| 35 | + |
| 36 | + $this->createCollection([1]); |
| 37 | + } |
| 38 | + |
| 39 | + public function testFloat(): void |
| 40 | + { |
| 41 | + $this->expectException(InvalidValueTypeException::class); |
| 42 | + $this->expectExceptionCode(0); |
| 43 | + $this->expectExceptionMessage($this->getExceptionMessage()); |
| 44 | + |
| 45 | + $this->createCollection([1.1]); |
| 46 | + } |
| 47 | + |
| 48 | + public function testTrue(): void |
| 49 | + { |
| 50 | + $this->expectException(InvalidValueTypeException::class); |
| 51 | + $this->expectExceptionCode(0); |
| 52 | + $this->expectExceptionMessage($this->getExceptionMessage()); |
| 53 | + |
| 54 | + $this->createCollection([true]); |
| 55 | + } |
| 56 | + |
| 57 | + public function testFalse(): void |
| 58 | + { |
| 59 | + $this->expectException(InvalidValueTypeException::class); |
| 60 | + $this->expectExceptionCode(0); |
| 61 | + $this->expectExceptionMessage($this->getExceptionMessage()); |
| 62 | + |
| 63 | + $this->createCollection([false]); |
| 64 | + } |
| 65 | + |
| 66 | + public function testStdClass(): void |
| 67 | + { |
| 68 | + $this->expectException(InvalidValueTypeException::class); |
| 69 | + $this->expectExceptionCode(0); |
| 70 | + $this->expectExceptionMessage($this->getExceptionMessage()); |
| 71 | + |
| 72 | + $this->createCollection([new \stdClass()]); |
| 73 | + } |
| 74 | + |
| 75 | + public function testNull(): void |
| 76 | + { |
| 77 | + if ($this->isAllowNull()) { |
| 78 | + $collection = $this->createCollection([null]); |
| 79 | + |
| 80 | + static::assertNull($collection->toArray()[0]); |
| 81 | + } else { |
| 82 | + $this->expectException(InvalidValueTypeException::class); |
| 83 | + $this->expectExceptionCode(0); |
| 84 | + $this->expectExceptionMessage($this->getExceptionMessage()); |
| 85 | + |
| 86 | + $this->createCollection([null]); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + protected function isAllowNull(): bool |
| 91 | + { |
| 92 | + return false; |
| 93 | + } |
| 94 | +} |
0 commit comments