diff --git a/src/lib/Persistence/Legacy/Content/Type/StorageDispatcher.php b/src/lib/Persistence/Legacy/Content/Type/StorageDispatcher.php index 3cc7478a15..5f3719c7b0 100644 --- a/src/lib/Persistence/Legacy/Content/Type/StorageDispatcher.php +++ b/src/lib/Persistence/Legacy/Content/Type/StorageDispatcher.php @@ -21,16 +21,16 @@ public function __construct(StorageRegistryInterface $registry) public function storeFieldConstraintsData(FieldDefinition $fieldDefinition): void { - if ($this->registry->hasStorage($fieldDefinition->identifier)) { - $storage = $this->registry->getStorage($fieldDefinition->identifier); + if ($this->registry->hasStorage($fieldDefinition->fieldType)) { + $storage = $this->registry->getStorage($fieldDefinition->fieldType); $storage->storeFieldConstraintsData($fieldDefinition->id, $fieldDefinition->fieldTypeConstraints); } } public function loadFieldConstraintsData(FieldDefinition $fieldDefinition): void { - if ($this->registry->hasStorage($fieldDefinition->identifier)) { - $storage = $this->registry->getStorage($fieldDefinition->identifier); + if ($this->registry->hasStorage($fieldDefinition->fieldType)) { + $storage = $this->registry->getStorage($fieldDefinition->fieldType); $fieldDefinition->fieldTypeConstraints = $storage->getFieldConstraintsData($fieldDefinition->id); } diff --git a/src/lib/Persistence/Legacy/Content/Type/StorageRegistry.php b/src/lib/Persistence/Legacy/Content/Type/StorageRegistry.php index aabc5a6a96..fe0e334bd1 100644 --- a/src/lib/Persistence/Legacy/Content/Type/StorageRegistry.php +++ b/src/lib/Persistence/Legacy/Content/Type/StorageRegistry.php @@ -35,14 +35,14 @@ public function getStorage(string $fieldTypeName): FieldConstraintsStorage if ($storage === null) { throw new InvalidArgumentException( '$typeName', - sprintf('Undefined %s for "%s" field type', FieldConstraintsStorage::class, $fieldTypeName) + sprintf('Undefined %s for "%s" field type', FieldConstraintsStorage::class, $fieldTypeName) ); } return $storage; } - public function findStorage(string $needle): ?FieldConstraintsStorage + private function findStorage(string $needle): ?FieldConstraintsStorage { foreach ($this->storages as $fieldTypeName => $storage) { if ($fieldTypeName === $needle) { diff --git a/src/lib/Persistence/Legacy/Content/Type/StorageRegistryInterface.php b/src/lib/Persistence/Legacy/Content/Type/StorageRegistryInterface.php index 8cacc2e478..4b4edc2a24 100644 --- a/src/lib/Persistence/Legacy/Content/Type/StorageRegistryInterface.php +++ b/src/lib/Persistence/Legacy/Content/Type/StorageRegistryInterface.php @@ -18,6 +18,4 @@ public function hasStorage(string $fieldTypeName): bool; * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException */ public function getStorage(string $fieldTypeName): FieldConstraintsStorage; - - public function findStorage(string $needle): ?FieldConstraintsStorage; } diff --git a/tests/lib/Persistence/Legacy/Content/Type/StorageDispatcherTest.php b/tests/lib/Persistence/Legacy/Content/Type/StorageDispatcherTest.php new file mode 100644 index 0000000000..3990620254 --- /dev/null +++ b/tests/lib/Persistence/Legacy/Content/Type/StorageDispatcherTest.php @@ -0,0 +1,151 @@ +createMock(FieldTypeConstraints::class); + + $storage = $this->createMock(FieldConstraintsStorage::class); + $storage + ->expects($this->once()) + ->method('storeFieldConstraintsData') + ->with(self::EXAMPLE_FIELD_DEFINITION_ID, $constraints); + + $fieldDefinition = new FieldDefinition(); + $fieldDefinition->fieldTypeConstraints = $constraints; + $fieldDefinition->id = self::EXAMPLE_FIELD_DEFINITION_ID; + $fieldDefinition->fieldType = self::EXAMPLE_FIELD_TYPE_IDENTIFIER; + + $registry = $this->createStorageRegistryMockWithExternalStorage($storage); + + $dispatcher = new StorageDispatcher($registry); + $dispatcher->storeFieldConstraintsData($fieldDefinition); + } + + public function testStoreFieldConstraintsDataForNonSupportedFieldType(): void + { + $fieldDefinition = new FieldDefinition(); + $fieldDefinition->id = self::EXAMPLE_FIELD_DEFINITION_ID; + $fieldDefinition->fieldType = self::EXAMPLE_FIELD_TYPE_IDENTIFIER; + + $registry = $this->createStorageRegistryMockWithoutExternalStorage(); + + $dispatcher = new StorageDispatcher($registry); + $dispatcher->storeFieldConstraintsData($fieldDefinition); + } + + public function testLoadFieldConstraintsData(): void + { + $constraints = $this->createMock(FieldTypeConstraints::class); + + $storage = $this->createMock(FieldConstraintsStorage::class); + $storage + ->expects($this->once()) + ->method('getFieldConstraintsData') + ->with(self::EXAMPLE_FIELD_DEFINITION_ID) + ->willReturn($constraints); + + $fieldDefinition = new FieldDefinition(); + $fieldDefinition->id = self::EXAMPLE_FIELD_DEFINITION_ID; + $fieldDefinition->fieldType = self::EXAMPLE_FIELD_TYPE_IDENTIFIER; + + $registry = $this->createStorageRegistryMockWithExternalStorage($storage); + + $dispatcher = new StorageDispatcher($registry); + $dispatcher->loadFieldConstraintsData($fieldDefinition); + + self::assertSame( + $constraints, + $fieldDefinition->fieldTypeConstraints + ); + } + + public function testLoadFieldConstraintsDataForNonSupportedFieldType(): void + { + $constraints = $this->createMock(FieldTypeConstraints::class); + + $fieldDefinition = new FieldDefinition(); + $fieldDefinition->id = self::EXAMPLE_FIELD_DEFINITION_ID; + $fieldDefinition->fieldType = self::EXAMPLE_FIELD_TYPE_IDENTIFIER; + $fieldDefinition->fieldTypeConstraints = $constraints; + + $registry = $this->createStorageRegistryMockWithoutExternalStorage(); + + $dispatcher = new StorageDispatcher($registry); + $dispatcher->loadFieldConstraintsData($fieldDefinition); + + self::assertSame( + $constraints, + $fieldDefinition->fieldTypeConstraints + ); + } + + public function testDeleteFieldConstraintsData(): void + { + $storage = $this->createMock(FieldConstraintsStorage::class); + $storage + ->expects($this->once()) + ->method('deleteFieldConstraintsData') + ->with(self::EXAMPLE_FIELD_DEFINITION_ID); + + $registry = $this->createStorageRegistryMockWithExternalStorage($storage); + + $dispatcher = new StorageDispatcher($registry); + $dispatcher->deleteFieldConstraintsData( + self::EXAMPLE_FIELD_TYPE_IDENTIFIER, + self::EXAMPLE_FIELD_DEFINITION_ID + ); + } + + public function testDeleteFieldConstraintsDataForNonSupportedFieldType(): void + { + $registry = $this->createStorageRegistryMockWithoutExternalStorage(); + + $dispatcher = new StorageDispatcher($registry); + $dispatcher->deleteFieldConstraintsData( + self::EXAMPLE_FIELD_TYPE_IDENTIFIER, + self::EXAMPLE_FIELD_DEFINITION_ID + ); + } + + private function createStorageRegistryMockWithoutExternalStorage(): StorageRegistryInterface + { + $registry = $this->createMock(StorageRegistryInterface::class); + $registry->method('hasStorage')->with(self::EXAMPLE_FIELD_TYPE_IDENTIFIER)->willReturn(false); + $registry + ->expects($this->never()) + ->method('getStorage') + ->with(self::EXAMPLE_FIELD_TYPE_IDENTIFIER); + + return $registry; + } + + private function createStorageRegistryMockWithExternalStorage( + FieldConstraintsStorage $storage + ): StorageRegistryInterface { + $registry = $this->createMock(StorageRegistryInterface::class); + $registry->method('hasStorage')->with(self::EXAMPLE_FIELD_TYPE_IDENTIFIER)->willReturn(true); + $registry->method('getStorage')->with(self::EXAMPLE_FIELD_TYPE_IDENTIFIER)->willReturn($storage); + + return $registry; + } +} diff --git a/tests/lib/Persistence/Legacy/Content/Type/StorageRegistryTest.php b/tests/lib/Persistence/Legacy/Content/Type/StorageRegistryTest.php new file mode 100644 index 0000000000..b887e38273 --- /dev/null +++ b/tests/lib/Persistence/Legacy/Content/Type/StorageRegistryTest.php @@ -0,0 +1,55 @@ + $this->createMock(FieldConstraintsStorage::class), + 'bar' => $this->createMock(FieldConstraintsStorage::class), + ]); + + self::assertTrue($registry->hasStorage('foo')); + self::assertTrue($registry->hasStorage('bar')); + // baz field type is not supported + self::assertFalse($registry->hasStorage('baz')); + } + + public function testGetStorage(): void + { + $storages = [ + 'foo' => $this->createMock(FieldConstraintsStorage::class), + 'bar' => $this->createMock(FieldConstraintsStorage::class), + ]; + + $registry = new StorageRegistry($storages); + + self::assertSame($storages['foo'], $registry->getStorage('foo')); + self::assertSame($storages['bar'], $registry->getStorage('bar')); + } + + public function testGetStorageForNonSupportedFieldType(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectErrorMessage('Argument \'$typeName\' is invalid: Undefined Ibexa\Contracts\Core\FieldType\FieldConstraintsStorage for "baz" field type'); + + $registry = new StorageRegistry([ + 'foo' => $this->createMock(FieldConstraintsStorage::class), + 'bar' => $this->createMock(FieldConstraintsStorage::class), + ]); + $registry->getStorage('baz'); + } +}