|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\UX\TwigComponent\Tests\Unit; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Symfony\UX\TwigComponent\ComputedPropertiesProxy; |
| 7 | + |
| 8 | +/** |
| 9 | + * @author Kevin Bond <[email protected]> |
| 10 | + */ |
| 11 | +final class ComputedPropertiesProxyTest extends TestCase |
| 12 | +{ |
| 13 | + public function testProxyCachesGetMethodReturns(): void |
| 14 | + { |
| 15 | + $proxy = new ComputedPropertiesProxy(new class() { |
| 16 | + private int $count = 0; |
| 17 | + |
| 18 | + public function getCount(): int |
| 19 | + { |
| 20 | + return ++$this->count; |
| 21 | + } |
| 22 | + }); |
| 23 | + |
| 24 | + $this->assertSame(1, $proxy->getCount()); |
| 25 | + $this->assertSame(1, $proxy->getCount()); |
| 26 | + $this->assertSame(1, $proxy->count()); |
| 27 | + } |
| 28 | + |
| 29 | + public function testProxyCachesIsMethodReturns(): void |
| 30 | + { |
| 31 | + $proxy = new ComputedPropertiesProxy(new class() { |
| 32 | + private int $count = 0; |
| 33 | + |
| 34 | + public function isCount(): int |
| 35 | + { |
| 36 | + return ++$this->count; |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + $this->assertSame(1, $proxy->isCount()); |
| 41 | + $this->assertSame(1, $proxy->isCount()); |
| 42 | + $this->assertSame(1, $proxy->count()); |
| 43 | + } |
| 44 | + |
| 45 | + public function testProxyCachesHasMethodReturns(): void |
| 46 | + { |
| 47 | + $proxy = new ComputedPropertiesProxy(new class() { |
| 48 | + private int $count = 0; |
| 49 | + |
| 50 | + public function hasCount(): int |
| 51 | + { |
| 52 | + return ++$this->count; |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + $this->assertSame(1, $proxy->hasCount()); |
| 57 | + $this->assertSame(1, $proxy->hasCount()); |
| 58 | + $this->assertSame(1, $proxy->count()); |
| 59 | + } |
| 60 | + |
| 61 | + public function testCanProxyPublicProperties(): void |
| 62 | + { |
| 63 | + $proxy = new ComputedPropertiesProxy(new class() { |
| 64 | + public $foo = 'bar'; |
| 65 | + }); |
| 66 | + |
| 67 | + $this->assertSame('bar', $proxy->foo()); |
| 68 | + } |
| 69 | + |
| 70 | + public function testCanProxyArrayAccess(): void |
| 71 | + { |
| 72 | + $proxy = new ComputedPropertiesProxy(new class() implements \ArrayAccess { |
| 73 | + private $array = ['foo' => 'bar']; |
| 74 | + |
| 75 | + public function offsetExists(mixed $offset): bool |
| 76 | + { |
| 77 | + return isset($this->array[$offset]); |
| 78 | + } |
| 79 | + |
| 80 | + public function offsetGet(mixed $offset): mixed |
| 81 | + { |
| 82 | + return $this->array[$offset]; |
| 83 | + } |
| 84 | + |
| 85 | + public function offsetSet(mixed $offset, mixed $value): void |
| 86 | + { |
| 87 | + } |
| 88 | + |
| 89 | + public function offsetUnset(mixed $offset): void |
| 90 | + { |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + $this->assertSame('bar', $proxy->foo()); |
| 95 | + } |
| 96 | + |
| 97 | + public function testCannotProxyMethodsThatDoNotExist(): void |
| 98 | + { |
| 99 | + $proxy = new ComputedPropertiesProxy(new class() {}); |
| 100 | + |
| 101 | + $this->expectException(\InvalidArgumentException::class); |
| 102 | + |
| 103 | + $proxy->getSomething(); |
| 104 | + } |
| 105 | + |
| 106 | + public function testCannotPassArgumentsToProxiedMethods(): void |
| 107 | + { |
| 108 | + $proxy = new ComputedPropertiesProxy(new class() {}); |
| 109 | + |
| 110 | + $this->expectException(\InvalidArgumentException::class); |
| 111 | + |
| 112 | + $proxy->getSomething('foo'); |
| 113 | + } |
| 114 | + |
| 115 | + public function testCannotProxyMethodsWithRequiredArguments(): void |
| 116 | + { |
| 117 | + $proxy = new ComputedPropertiesProxy(new class() { |
| 118 | + public function getValue(int $value): int |
| 119 | + { |
| 120 | + return $value; |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + $this->expectException(\LogicException::class); |
| 125 | + |
| 126 | + $proxy->getValue(); |
| 127 | + } |
| 128 | +} |
0 commit comments