|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Core\Tests\Metadata\Property\Factory; |
| 15 | + |
| 16 | +use ApiPlatform\Core\Metadata\Property\Factory\ConstructorPropertyMetadataFactory; |
| 17 | +use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
| 18 | +use ApiPlatform\Core\Metadata\Property\PropertyMetadata; |
| 19 | +use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
| 20 | +use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | +use Prophecy\Argument; |
| 23 | +use Symfony\Component\PropertyInfo\Type; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Maxime Veber <maxime.veber@nekland.fr> |
| 27 | + */ |
| 28 | +class ConstructorPropertyMetadataFactoryTest extends TestCase |
| 29 | +{ |
| 30 | + public function testItIsAnInstanceOfPropertyMetadataFactory() |
| 31 | + { |
| 32 | + $factory = new ConstructorPropertyMetadataFactory($this->getResourceMetadataFactoryMock(DummyObjectWithConstructor::class)); |
| 33 | + $this->assertInstanceOf(PropertyMetadataFactoryInterface::class, $factory); |
| 34 | + } |
| 35 | + |
| 36 | + public function testItAddsWritableForConstructorProperties() |
| 37 | + { |
| 38 | + $propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 39 | + |
| 40 | + $type = new Type(Type::BUILTIN_TYPE_STRING); |
| 41 | + $fooMetadata = new PropertyMetadata($type, 'foo', true, false, false, false, true, false, 'http://example.com/foo', null, ['foo' => 'bar']); |
| 42 | + $propertyMetadataFactory->create(DummyObjectWithConstructor::class, 'foo', Argument::any())->willReturn($fooMetadata)->shouldBeCalled(); |
| 43 | + |
| 44 | + $factory = new ConstructorPropertyMetadataFactory( |
| 45 | + $this->getResourceMetadataFactoryMock(DummyObjectWithConstructor::class), |
| 46 | + $propertyMetadataFactory->reveal() |
| 47 | + ); |
| 48 | + |
| 49 | + $fooMetadata = $factory->create(DummyObjectWithConstructor::class, 'foo', [ |
| 50 | + 'collection_operation_name' => 'hello', |
| 51 | + ]); |
| 52 | + $this->assertTrue($fooMetadata->isWritable()); |
| 53 | + } |
| 54 | + |
| 55 | + public function testItDoesntAddWritableOnWrongOperations() |
| 56 | + { |
| 57 | + $propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 58 | + |
| 59 | + $type = new Type(Type::BUILTIN_TYPE_STRING); |
| 60 | + $fooMetadata = new PropertyMetadata($type, 'foo', true, false, false, false, true, false, 'http://example.com/foo', null, ['foo' => 'bar']); |
| 61 | + $propertyMetadataFactory->create(DummyObjectWithConstructor::class, 'foo', Argument::any())->willReturn($fooMetadata)->shouldBeCalled(); |
| 62 | + |
| 63 | + $factory = new ConstructorPropertyMetadataFactory( |
| 64 | + $this->getResourceMetadataFactoryMock(DummyObjectWithConstructor::class, 'GET'), |
| 65 | + $propertyMetadataFactory->reveal() |
| 66 | + ); |
| 67 | + |
| 68 | + $fooMetadata = $factory->create(DummyObjectWithConstructor::class, 'foo', [ |
| 69 | + 'collection_operation_name' => 'hello', |
| 70 | + ]); |
| 71 | + $this->assertFalse($fooMetadata->isWritable()); |
| 72 | + } |
| 73 | + |
| 74 | + public function testItCreateAndSetRightMetadataIfNoFactoryGiven() |
| 75 | + { |
| 76 | + $factory = new ConstructorPropertyMetadataFactory($this->getResourceMetadataFactoryMock(DummyObjectWithConstructor::class)); |
| 77 | + $fooMetadata = $factory->create(DummyObjectWithConstructor::class, 'bar', [ |
| 78 | + 'collection_operation_name' => 'hello', |
| 79 | + ]); |
| 80 | + |
| 81 | + $this->assertTrue($fooMetadata->isWritable()); |
| 82 | + } |
| 83 | + |
| 84 | + public function testItDoesntThrowErrorForAnyKindOfObject() |
| 85 | + { |
| 86 | + $propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 87 | + |
| 88 | + $type = new Type(Type::BUILTIN_TYPE_STRING); |
| 89 | + $fooMetadata = new PropertyMetadata($type, 'foo', true, true, false, false, true, false, 'http://example.com/foo', null, ['foo' => 'bar']); |
| 90 | + $propertyMetadataFactory->create(DummyObjectWithoutConstructor::class, 'foo', Argument::any())->willReturn($fooMetadata)->shouldBeCalled(); |
| 91 | + |
| 92 | + $factory = new ConstructorPropertyMetadataFactory( |
| 93 | + $this->getResourceMetadataFactoryMock(DummyObjectWithoutConstructor::class), |
| 94 | + $propertyMetadataFactory->reveal() |
| 95 | + ); |
| 96 | + |
| 97 | + $fooMetadata = $factory->create(DummyObjectWithoutConstructor::class, 'foo', [ |
| 98 | + 'collection_operation_name' => 'hello', |
| 99 | + ]); |
| 100 | + $this->assertTrue($fooMetadata->isWritable()); |
| 101 | + } |
| 102 | + |
| 103 | + private function getResourceMetadataFactoryMock($resource, $method = 'post') |
| 104 | + { |
| 105 | + $resourceMetadataFactory = $this->prophesize(ResourceMetadataFactoryInterface::class); |
| 106 | + $metadata = (new ResourceMetadata())->withCollectionOperations(['hello' => ['method' => $method]]); |
| 107 | + |
| 108 | + $resourceMetadataFactory->create($resource)->willReturn($metadata); |
| 109 | + |
| 110 | + return $resourceMetadataFactory->reveal(); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +class DummyObjectWithConstructor |
| 115 | +{ |
| 116 | + private $foo; |
| 117 | + private $bar; |
| 118 | + |
| 119 | + public function __construct(string $foo, \stdClass $bar) |
| 120 | + { |
| 121 | + $this->foo = $foo; |
| 122 | + $this->bar = $bar; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +class DummyObjectWithoutConstructor |
| 127 | +{ |
| 128 | + private $foo; |
| 129 | + |
| 130 | + public function getFoo() |
| 131 | + { |
| 132 | + return $this->foo; |
| 133 | + } |
| 134 | + |
| 135 | + public function setFoo($foo) |
| 136 | + { |
| 137 | + $this->foo = $foo; |
| 138 | + } |
| 139 | +} |
0 commit comments