|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of php-fast-forward/config. |
| 7 | + * |
| 8 | + * This source file is subject to the license bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + * |
| 11 | + * @link https://github.com/php-fast-forward/config |
| 12 | + * @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <[email protected]> |
| 13 | + * @license https://opensource.org/licenses/MIT MIT License |
| 14 | + */ |
| 15 | + |
| 16 | +use FastForward\Config\ArrayAccessConfigTrait; |
| 17 | +use PHPUnit\Framework\Attributes\CoversTrait; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 20 | + |
| 21 | +/** |
| 22 | + * @internal |
| 23 | + */ |
| 24 | +#[CoversTrait(ArrayAccessConfigTrait::class)] |
| 25 | +final class ArrayAccessConfigTraitTest extends TestCase |
| 26 | +{ |
| 27 | + use ProphecyTrait; |
| 28 | + |
| 29 | + public function testOffsetExistsWillCallHasMethod(): void |
| 30 | + { |
| 31 | + $object = $this->createTraitInstance(has: true); |
| 32 | + self::assertTrue($object->offsetExists('key')); |
| 33 | + |
| 34 | + $object = $this->createTraitInstance(has: false); |
| 35 | + self::assertFalse($object->offsetExists('key')); |
| 36 | + } |
| 37 | + |
| 38 | + public function testOffsetGetWillCallGetMethod(): void |
| 39 | + { |
| 40 | + $object = $this->createTraitInstance(get: 'foo'); |
| 41 | + self::assertSame('foo', $object->offsetGet('bar')); |
| 42 | + } |
| 43 | + |
| 44 | + public function testOffsetSetWillCallSetMethod(): void |
| 45 | + { |
| 46 | + $object = $this->createTraitInstance(); |
| 47 | + $object->offsetSet('alpha', 'beta'); |
| 48 | + |
| 49 | + self::assertSame(['alpha' => 'beta'], $object->getSetCalls()); |
| 50 | + } |
| 51 | + |
| 52 | + public function testOffsetUnsetWillCallRemoveMethod(): void |
| 53 | + { |
| 54 | + $object = $this->createTraitInstance(); |
| 55 | + $object->offsetUnset('delta'); |
| 56 | + |
| 57 | + self::assertSame(['delta'], $object->getRemoveCalls()); |
| 58 | + } |
| 59 | + |
| 60 | + private function createTraitInstance(bool $has = false, mixed $get = null): ArrayAccess |
| 61 | + { |
| 62 | + return new class($has, $get) implements ArrayAccess { |
| 63 | + use ArrayAccessConfigTrait; |
| 64 | + |
| 65 | + private array $setCalls = []; |
| 66 | + |
| 67 | + private array $removeCalls = []; |
| 68 | + |
| 69 | + private bool $hasReturn; |
| 70 | + |
| 71 | + private mixed $getReturn; |
| 72 | + |
| 73 | + public function __construct(bool $has, mixed $get) |
| 74 | + { |
| 75 | + $this->hasReturn = $has; |
| 76 | + $this->getReturn = $get; |
| 77 | + } |
| 78 | + |
| 79 | + public function has(mixed $offset): bool |
| 80 | + { |
| 81 | + return $this->hasReturn; |
| 82 | + } |
| 83 | + |
| 84 | + public function get(mixed $offset): mixed |
| 85 | + { |
| 86 | + return $this->getReturn; |
| 87 | + } |
| 88 | + |
| 89 | + public function set(mixed $offset, mixed $value): void |
| 90 | + { |
| 91 | + $this->setCalls[$offset] = $value; |
| 92 | + } |
| 93 | + |
| 94 | + public function remove(mixed $offset): void |
| 95 | + { |
| 96 | + $this->removeCalls[] = $offset; |
| 97 | + } |
| 98 | + |
| 99 | + public function getSetCalls(): array |
| 100 | + { |
| 101 | + return $this->setCalls; |
| 102 | + } |
| 103 | + |
| 104 | + public function getRemoveCalls(): array |
| 105 | + { |
| 106 | + return $this->removeCalls; |
| 107 | + } |
| 108 | + }; |
| 109 | + } |
| 110 | +} |
0 commit comments