Skip to content

Commit

Permalink
fixup! IBX-1580: External storage for field type settings
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs committed Dec 2, 2021
1 parent b8192d1 commit 4f961de
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/lib/Persistence/Legacy/Content/Type/StorageDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Persistence/Legacy/Content/Type/StorageRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
151 changes: 151 additions & 0 deletions tests/lib/Persistence/Legacy/Content/Type/StorageDispatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Core\Persistence\Legacy\Content\Type;

use Ibexa\Contracts\Core\FieldType\FieldConstraintsStorage;
use Ibexa\Contracts\Core\Persistence\Content\FieldTypeConstraints;
use Ibexa\Contracts\Core\Persistence\Content\Type\FieldDefinition;
use Ibexa\Core\Persistence\Legacy\Content\Type\StorageDispatcher;
use Ibexa\Core\Persistence\Legacy\Content\Type\StorageRegistryInterface;
use PHPUnit\Framework\TestCase;

final class StorageDispatcherTest extends TestCase
{
private const EXAMPLE_FIELD_DEFINITION_ID = 1;
private const EXAMPLE_FIELD_TYPE_IDENTIFIER = 'example_ft';

public function testStoreFieldConstraintsData(): void
{
$constraints = $this->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;
}
}
55 changes: 55 additions & 0 deletions tests/lib/Persistence/Legacy/Content/Type/StorageRegistryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Core\Persistence\Legacy\Content\Type;

use Ibexa\Contracts\Core\FieldType\FieldConstraintsStorage;
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
use Ibexa\Core\Persistence\Legacy\Content\Type\StorageRegistry;
use PHPUnit\Framework\TestCase;

final class StorageRegistryTest extends TestCase
{
public function testHasStorage(): void
{
$registry = new StorageRegistry([
'foo' => $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');
}
}

0 comments on commit 4f961de

Please sign in to comment.