-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! IBX-1580: External storage for field type settings
- Loading branch information
Showing
5 changed files
with
212 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
tests/lib/Persistence/Legacy/Content/Type/StorageDispatcherTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
55
tests/lib/Persistence/Legacy/Content/Type/StorageRegistryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |