From 61ddcc520de1f6b7d2b8dd9305c0fad78e2b5b2c Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Tue, 27 Aug 2024 11:38:55 +0200 Subject: [PATCH] Extracted common code for Binary and Media field type (#407) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For more details see https://github.com/ibexa/core/pull/407 Key changes: * Extracted redundant Binary and Media SearchField into a common base * [Tests] Extracted common base for Binary and Media ft integration tests * [Tests] Made MediaIntegrationTest::getValidFieldSettings return type more strict * [PHPStan] Aligned baseline with the changes --------- Co-authored-by: Adam Wójs Co-authored-by: Mikolaj Adamczyk --- phpstan-baseline.neon | 185 ---------- .../BinaryBase/AbstractBinarySearchField.php | 66 ++++ src/lib/FieldType/BinaryFile/SearchField.php | 65 +--- src/lib/FieldType/Media/SearchField.php | 65 +--- .../BaseBinaryFileIntegrationTestCase.php | 259 ++++++++++++++ .../FieldType/BinaryFileIntegrationTest.php | 324 ++---------------- .../FieldType/MediaIntegrationTest.php | 318 ++--------------- 7 files changed, 381 insertions(+), 901 deletions(-) create mode 100644 src/lib/FieldType/BinaryBase/AbstractBinarySearchField.php create mode 100644 tests/integration/Core/Repository/FieldType/BaseBinaryFileIntegrationTestCase.php diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index a126cb12d1..b53ddd9647 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -29310,101 +29310,6 @@ parameters: count: 1 path: tests/integration/Core/Repository/FieldType/BaseIntegrationTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:assertCopiedFieldDataLoadedCorrectly\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:assertFieldDataLoadedCorrect\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:assertUpdatedFieldDataLoadedCorrect\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:assertUpdatedFieldDataLoadedCorrect\\(\\) should return array but return statement is missing\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:checkSearchEngineSupport\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:getAdditionallyIndexedFieldData\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:getFixtureData\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:getSettingsSchema\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:getStoragePrefix\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:getValidUpdateFieldData\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:getValidUpdateFieldData\\(\\) should return array but returns Ibexa\\\\Core\\\\FieldType\\\\BinaryFile\\\\Value\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:getValidatorSchema\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:provideFromHashData\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:provideInvalidCreationFieldData\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:provideInvalidUpdateFieldData\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:provideToHashData\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:providerForTestIsEmptyValue\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:providerForTestIsNotEmptyValue\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - - - message: "#^Property Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\BinaryFileIntegrationTest\\:\\:\\$loadedBinaryFilePath has no type specified\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php - - message: "#^Cannot access property \\$value on Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Field\\|null\\.$#" count: 1 @@ -30305,96 +30210,6 @@ parameters: count: 1 path: tests/integration/Core/Repository/FieldType/MapLocationIntegrationTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:assertCopiedFieldDataLoadedCorrectly\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:assertFieldDataLoadedCorrect\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:assertUpdatedFieldDataLoadedCorrect\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:assertUpdatedFieldDataLoadedCorrect\\(\\) should return array but return statement is missing\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:getAdditionallyIndexedFieldData\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:getFixtureData\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:getSettingsSchema\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:getStoragePrefix\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:getValidUpdateFieldData\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:getValidUpdateFieldData\\(\\) should return array but returns Ibexa\\\\Core\\\\FieldType\\\\Media\\\\Value\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:getValidatorSchema\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:provideFromHashData\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:provideInvalidCreationFieldData\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:provideInvalidUpdateFieldData\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:provideToHashData\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:providerForTestIsEmptyValue\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:providerForTestIsNotEmptyValue\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - - - message: "#^Property Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\MediaIntegrationTest\\:\\:\\$loadedMediaPath has no type specified\\.$#" - count: 1 - path: tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Integration\\\\Core\\\\Repository\\\\FieldType\\\\RelationIntegrationTest\\:\\:assertCopiedFieldDataLoadedCorrectly\\(\\) has no return type specified\\.$#" count: 1 diff --git a/src/lib/FieldType/BinaryBase/AbstractBinarySearchField.php b/src/lib/FieldType/BinaryBase/AbstractBinarySearchField.php new file mode 100644 index 0000000000..f6cdf6571c --- /dev/null +++ b/src/lib/FieldType/BinaryBase/AbstractBinarySearchField.php @@ -0,0 +1,66 @@ +value->externalData['fileName'] ?? null, + new Search\FieldType\StringField() + ), + new Search\Field( + 'file_size', + $field->value->externalData['fileSize'] ?? null, + new Search\FieldType\IntegerField() + ), + new Search\Field( + 'mime_type', + $field->value->externalData['mimeType'] ?? null, + new Search\FieldType\StringField() + ), + ]; + } + + /** + * @return array + */ + public function getIndexDefinition(): array + { + return [ + 'file_name' => new Search\FieldType\StringField(), + 'file_size' => new Search\FieldType\IntegerField(), + 'mime_type' => new Search\FieldType\StringField(), + ]; + } + + public function getDefaultMatchField(): string + { + return 'file_name'; + } + + public function getDefaultSortField(): string + { + return $this->getDefaultMatchField(); + } +} diff --git a/src/lib/FieldType/BinaryFile/SearchField.php b/src/lib/FieldType/BinaryFile/SearchField.php index 8857116de7..3a8ed5a018 100644 --- a/src/lib/FieldType/BinaryFile/SearchField.php +++ b/src/lib/FieldType/BinaryFile/SearchField.php @@ -4,74 +4,15 @@ * @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\Core\FieldType\BinaryFile; -use Ibexa\Contracts\Core\FieldType\Indexable; -use Ibexa\Contracts\Core\Persistence\Content\Field; -use Ibexa\Contracts\Core\Persistence\Content\Type\FieldDefinition; -use Ibexa\Contracts\Core\Search; +use Ibexa\Core\FieldType\BinaryBase\AbstractBinarySearchField; /** * Indexable definition for BinaryFile field type. */ -class SearchField implements Indexable +class SearchField extends AbstractBinarySearchField { - public function getIndexData(Field $field, FieldDefinition $fieldDefinition) - { - return [ - new Search\Field( - 'file_name', - $field->value->externalData['fileName'] ?? null, - new Search\FieldType\StringField() - ), - new Search\Field( - 'file_size', - $field->value->externalData['fileSize'] ?? null, - new Search\FieldType\IntegerField() - ), - new Search\Field( - 'mime_type', - $field->value->externalData['mimeType'] ?? null, - new Search\FieldType\StringField() - ), - ]; - } - - public function getIndexDefinition() - { - return [ - 'file_name' => new Search\FieldType\StringField(), - 'file_size' => new Search\FieldType\IntegerField(), - 'mime_type' => new Search\FieldType\StringField(), - ]; - } - - /** - * Get name of the default field to be used for matching. - * - * As field types can index multiple fields (see MapLocation field type's - * implementation of this interface), this method is used to define default - * field for matching. Default field is typically used by Field criterion. - * - * @return string - */ - public function getDefaultMatchField() - { - return 'file_name'; - } - - /** - * Get name of the default field to be used for sorting. - * - * As field types can index multiple fields (see MapLocation field type's - * implementation of this interface), this method is used to define default - * field for sorting. Default field is typically used by Field sort clause. - * - * @return string - */ - public function getDefaultSortField() - { - return $this->getDefaultMatchField(); - } } diff --git a/src/lib/FieldType/Media/SearchField.php b/src/lib/FieldType/Media/SearchField.php index 11d8a94cce..36c91b9bef 100644 --- a/src/lib/FieldType/Media/SearchField.php +++ b/src/lib/FieldType/Media/SearchField.php @@ -4,74 +4,15 @@ * @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\Core\FieldType\Media; -use Ibexa\Contracts\Core\FieldType\Indexable; -use Ibexa\Contracts\Core\Persistence\Content\Field; -use Ibexa\Contracts\Core\Persistence\Content\Type\FieldDefinition; -use Ibexa\Contracts\Core\Search; +use Ibexa\Core\FieldType\BinaryBase\AbstractBinarySearchField; /** * Indexable definition for Media field type. */ -class SearchField implements Indexable +class SearchField extends AbstractBinarySearchField { - public function getIndexData(Field $field, FieldDefinition $fieldDefinition) - { - return [ - new Search\Field( - 'file_name', - $field->value->externalData['fileName'] ?? null, - new Search\FieldType\StringField() - ), - new Search\Field( - 'file_size', - $field->value->externalData['fileSize'] ?? null, - new Search\FieldType\IntegerField() - ), - new Search\Field( - 'mime_type', - $field->value->externalData['mimeType'] ?? null, - new Search\FieldType\StringField() - ), - ]; - } - - public function getIndexDefinition() - { - return [ - 'file_name' => new Search\FieldType\StringField(), - 'file_size' => new Search\FieldType\IntegerField(), - 'mime_type' => new Search\FieldType\StringField(), - ]; - } - - /** - * Get name of the default field to be used for matching. - * - * As field types can index multiple fields (see MapLocation field type's - * implementation of this interface), this method is used to define default - * field for matching. Default field is typically used by Field criterion. - * - * @return string - */ - public function getDefaultMatchField() - { - return 'file_name'; - } - - /** - * Get name of the default field to be used for sorting. - * - * As field types can index multiple fields (see MapLocation field type's - * implementation of this interface), this method is used to define default - * field for sorting. Default field is typically used by Field sort clause. - * - * @return string - */ - public function getDefaultSortField() - { - return $this->getDefaultMatchField(); - } } diff --git a/tests/integration/Core/Repository/FieldType/BaseBinaryFileIntegrationTestCase.php b/tests/integration/Core/Repository/FieldType/BaseBinaryFileIntegrationTestCase.php new file mode 100644 index 0000000000..2407b54b66 --- /dev/null +++ b/tests/integration/Core/Repository/FieldType/BaseBinaryFileIntegrationTestCase.php @@ -0,0 +1,259 @@ + + */ + abstract public function provideFromHashData(): array; + + /** + * @phpstan-return list + */ + abstract public function provideToHashData(): array; + + /** + * @phpstan-param TBaseBinaryFileFieldValueHash $fileFieldValueData + */ + abstract protected function buildBinaryFileValueFromFixtureData(array $fileFieldValueData): BinaryBaseValue; + + public function assertFieldDataLoadedCorrect(Field $field): void + { + $this->asserFieldValueIsCorrectInstance($field); + + $fixtureData = $this->getFixtureData(); + $this->assertCreatedUpdatedBinaryFieldDataLoadedCorrectly($fixtureData['create'], $field); + } + + public function assertUpdatedFieldDataLoadedCorrect(Field $field): void + { + $this->asserFieldValueIsCorrectInstance($field); + + $fixtureData = $this->getFixtureData(); + $this->assertCreatedUpdatedBinaryFieldDataLoadedCorrectly($fixtureData['update'], $field); + } + + public function assertCopiedFieldDataLoadedCorrectly(Field $field): void + { + $this->assertFieldDataLoadedCorrect($field); + + self::assertEquals( + self::$loadedFilePath, + $field->value->id + ); + } + + /** + * @return array + */ + public function getInvalidFieldSettings(): array + { + return [ + 'somethingUnknown' => 0, + ]; + } + + /** + * @return array> + */ + public function getValidatorSchema(): array + { + return [ + 'FileSizeValidator' => [ + 'maxFileSize' => [ + 'type' => 'int', + 'default' => false, + ], + ], + ]; + } + + /** + * @return array> + */ + public function getValidValidatorConfiguration(): array + { + return [ + 'FileSizeValidator' => [ + 'maxFileSize' => 2 * 1024 * 1024, // 2 MB + ], + ]; + } + + /** + * @return array> + */ + public function getInvalidValidatorConfiguration(): array + { + return [ + 'StringLengthValidator' => [ + 'minStringLength' => new \stdClass(), + ], + ]; + } + + public function getFieldName(): string + { + return 'Icy-Night-Flower-Binary.jpg'; + } + + /** + * @return list + */ + public function provideInvalidCreationFieldData(): array + { + return [ + [ + [ + 'id' => self::FOO_BAR_SAMPLE_FILE_PATH, + ], + InvalidArgumentValue::class, + ], + [ + $this->buildBinaryFileValue(self::FOO_BAR_SAMPLE_FILE_PATH), + InvalidArgumentValue::class, + ], + ]; + } + + public function getValidCreationFieldData(): BinaryBaseValue + { + $fixtureData = $this->getFixtureData(); + + return $this->buildBinaryFileValueFromFixtureData($fixtureData['create']); + } + + public function getValidUpdateFieldData(): BinaryBaseValue + { + $fixtureData = $this->getFixtureData(); + + return $this->buildBinaryFileValueFromFixtureData($fixtureData['update']); + } + + /** + * @return list> + */ + public function providerForTestIsNotEmptyValue(): array + { + return [ + [ + $this->getValidCreationFieldData(), + ], + ]; + } + + /** + * @return list + */ + public function provideInvalidUpdateFieldData(): array + { + return $this->provideInvalidCreationFieldData(); + } + + protected function getStoragePrefix(): string + { + $configValue = $this->getConfigValue(self::$storagePrefixConfigKey); + if (!is_string($configValue)) { + self::fail(sprintf('"%s" config key value is not a string', self::$storagePrefixConfigKey)); + } + + return $configValue; + } + + protected function getSearchTargetValueOne(): string + { + $value = $this->getValidSearchValueOne(); + + // ensure case-insensitivity + return strtoupper($value->fileName); + } + + protected function getSearchTargetValueTwo(): string + { + $value = $this->getValidSearchValueTwo(); + + // ensure case-insensitivity + return strtoupper($value->fileName); + } + + /** + * @return list> + */ + protected function getAdditionallyIndexedFieldData(): array + { + return [ + [ + 'file_size', + $this->getValidSearchValueOne()->fileSize, + $this->getValidSearchValueTwo()->fileSize, + ], + [ + 'mime_type', + // ensure case-insensitivity + 'IMAGE/JPEG', + 'IMAGE/PNG', + ], + ]; + } + + /** + * @phpstan-param TBaseBinaryFileFieldValueHash $expectedData + */ + private function assertCreatedUpdatedBinaryFieldDataLoadedCorrectly(array $expectedData, Field $field): void + { + // Will change during storage + unset($expectedData['id']); + $expectedData['inputUri'] = null; + + self::assertNotEmpty($field->value->id); + self::assertInstanceOf(BinaryBaseValue::class, $field->value); + $this->assertPropertiesCorrect( + $expectedData, + $field->value + ); + + self::assertTrue( + $this->uriExistsOnIO($field->value->uri), + "File {$field->value->uri} doesn't exist." + ); + + self::$loadedFilePath = $field->value->id; + } +} diff --git a/tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php b/tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php index 63362afc50..0d7e51ddfc 100644 --- a/tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php +++ b/tests/integration/Core/Repository/FieldType/BinaryFileIntegrationTest.php @@ -4,12 +4,13 @@ * @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\Integration\Core\Repository\FieldType; use Ibexa\Contracts\Core\Repository\Values\Content\Field; use Ibexa\Contracts\Core\Test\Repository\SetupFactory\Legacy; -use Ibexa\Core\Base\Exceptions\InvalidArgumentValue; +use Ibexa\Core\FieldType\BinaryBase\Value as BinaryBaseValue; use Ibexa\Core\FieldType\BinaryFile\Value as BinaryFileValue; /** @@ -18,31 +19,9 @@ * @group integration * @group field-type */ -class BinaryFileIntegrationTest extends FileSearchBaseIntegrationTest +final class BinaryFileIntegrationTest extends BaseBinaryFileIntegrationTestCase { - /** - * Stores the loaded image path for copy test. - */ - protected static $loadedBinaryFilePath; - - /** - * IOService storage prefix for the tested Type's files. - * - * @var string - */ - protected static $storagePrefixConfigKey = 'ibexa.io.binary_file.storage.prefix'; - - protected function getStoragePrefix() - { - return $this->getConfigValue(self::$storagePrefixConfigKey); - } - - /** - * Sets up fixture data. - * - * @return array - */ - protected function getFixtureData() + protected function getFixtureData(): array { return [ 'create' => [ @@ -51,7 +30,7 @@ protected function getFixtureData() 'fileName' => 'Icy-Night-Flower-Binary.jpg', 'fileSize' => filesize($path), 'mimeType' => 'image/jpeg', - // Left out'downloadCount' by intention (will be set to 0) + // Left out 'downloadCount' by intention (will be set to 0) ], 'update' => [ 'id' => null, @@ -75,249 +54,44 @@ public function getTypeName() } /** - * Get expected settings schema. - * - * @return array + * @return array{} */ - public function getSettingsSchema() + public function getSettingsSchema(): array { return []; } /** - * Get a valid $fieldSettings value. - * - * @return mixed + * @return array{} */ - public function getValidFieldSettings() + public function getValidFieldSettings(): array { return []; } - /** - * Get $fieldSettings value not accepted by the field type. - * - * @return mixed - */ - public function getInvalidFieldSettings() + protected function buildBinaryFileValueFromFixtureData(array $fileFieldValueData): BinaryBaseValue { - return [ - 'somethingUnknown' => 0, - ]; + return new BinaryFileValue($fileFieldValueData); } - /** - * Get expected validator schema. - * - * @return array - */ - public function getValidatorSchema() - { - return [ - 'FileSizeValidator' => [ - 'maxFileSize' => [ - 'type' => 'int', - 'default' => false, - ], - ], - ]; - } - - /** - * Get a valid $validatorConfiguration. - * - * @return mixed - */ - public function getValidValidatorConfiguration() - { - return [ - 'FileSizeValidator' => [ - 'maxFileSize' => 2 * 1024 * 1024, // 2 MB - ], - ]; - } - - /** - * Get $validatorConfiguration not accepted by the field type. - * - * @return mixed - */ - public function getInvalidValidatorConfiguration() - { - return [ - 'StringLengthValidator' => [ - 'minStringLength' => new \stdClass(), - ], - ]; - } - - /** - * Get initial field data for valid object creation. - * - * @return mixed - */ - public function getValidCreationFieldData() - { - $fixtureData = $this->getFixtureData(); - - return new BinaryFileValue($fixtureData['create']); - } - - /** - * Get name generated by the given field type (via fieldType->getName()). - * - * @return string - */ - public function getFieldName() - { - return 'Icy-Night-Flower-Binary.jpg'; - } - - /** - * Asserts that the field data was loaded correctly. - * - * Asserts that the data provided by {@link getValidCreationFieldData()} - * was stored and loaded correctly. - * - * @param \Ibexa\Contracts\Core\Repository\Values\Content\Field $field - */ - public function assertFieldDataLoadedCorrect(Field $field) + public function asserFieldValueIsCorrectInstance(Field $field): void { self::assertInstanceOf( BinaryFileValue::class, $field->value ); - - $fixtureData = $this->getFixtureData(); - $expectedData = $fixtureData['create']; - - // Will change during storage - unset($expectedData['id']); - $expectedData['inputUri'] = null; - - self::assertNotEmpty($field->value->id); - $this->assertPropertiesCorrect( - $expectedData, - $field->value - ); - - self::assertTrue( - $this->uriExistsOnIO($field->value->uri), - "File {$field->value->uri} doesn't exist" - ); - - self::$loadedBinaryFilePath = $field->value->id; } - public function provideInvalidCreationFieldData() + protected function buildBinaryFileValue(string $fileId): BinaryBaseValue { - return [ - [ - [ - 'id' => '/foo/bar/sindelfingen.pdf', - ], - InvalidArgumentValue::class, - ], + return new BinaryFileValue( [ - new BinaryFileValue( - [ - 'id' => '/foo/bar/sindelfingen.pdf', - ] - ), - InvalidArgumentValue::class, - ], - ]; - } - - /** - * Get update field externals data. - * - * @return array - */ - public function getValidUpdateFieldData() - { - $fixtureData = $this->getFixtureData(); - - return new BinaryFileValue($fixtureData['update']); - } - - /** - * Get externals updated field data values. - * - * This is a PHPUnit data provider - * - * @return array - */ - public function assertUpdatedFieldDataLoadedCorrect(Field $field) - { - self::assertInstanceOf( - BinaryFileValue::class, - $field->value - ); - - $fixtureData = $this->getFixtureData(); - $expectedData = $fixtureData['update']; - - // Will change during storage - unset($expectedData['id']); - $expectedData['inputUri'] = null; - - self::assertNotEmpty($field->value->id); - $this->assertPropertiesCorrect( - $expectedData, - $field->value - ); - - self::assertTrue( - $this->uriExistsOnIO($field->value->uri), - "File {$field->value->uri} doesn't exist." - ); - } - - public function provideInvalidUpdateFieldData() - { - return $this->provideInvalidCreationFieldData(); - } - - /** - * Asserts the the field data was loaded correctly. - * - * Asserts that the data provided by {@link getValidCreationFieldData()} - * was copied and loaded correctly. - * - * @param \Ibexa\Contracts\Core\Repository\Values\Content\Field $field - */ - public function assertCopiedFieldDataLoadedCorrectly(Field $field) - { - $this->assertFieldDataLoadedCorrect($field); - - self::assertEquals( - self::$loadedBinaryFilePath, - $field->value->id + 'id' => $fileId, + ] ); } - /** - * Get data to test to hash method. - * - * This is a PHPUnit data provider - * - * The returned records must have the the original value assigned to the - * first index and the expected hash result to the second. For example: - * - * - * array( - * array( - * new MyValue( true ), - * array( 'myValue' => true ), - * ), - * // ... - * ); - * - * - * @return array - */ - public function provideToHashData() + public function provideToHashData(): array { $fixture = $this->getFixtureData(); $expected = $fixture['create']; @@ -336,14 +110,7 @@ public function provideToHashData() ]; } - /** - * Get expectations for the fromHash call on our field value. - * - * This is a PHPUnit data provider - * - * @return array - */ - public function provideFromHashData() + public function provideFromHashData(): array { $fixture = $this->getFixtureData(); $fixture['create']['downloadCount'] = 0; @@ -360,7 +127,10 @@ public function provideFromHashData() ]; } - public function providerForTestIsEmptyValue() + /** + * @return list> + */ + public function providerForTestIsEmptyValue(): array { return [ [new BinaryFileValue()], @@ -368,16 +138,7 @@ public function providerForTestIsEmptyValue() ]; } - public function providerForTestIsNotEmptyValue() - { - return [ - [ - $this->getValidCreationFieldData(), - ], - ]; - } - - protected function getValidSearchValueOne() + protected function getValidSearchValueOne(): BinaryFileValue { return new BinaryFileValue( [ @@ -391,8 +152,10 @@ protected function getValidSearchValueOne() /** * BinaryFile field type is not searchable with Field criterion * and sort clause in Legacy search engine. + * + * @throws \ErrorException */ - protected function checkSearchEngineSupport() + protected function checkSearchEngineSupport(): void { if ($this->getSetupFactory() instanceof Legacy) { self::markTestSkipped( @@ -401,7 +164,7 @@ protected function checkSearchEngineSupport() } } - protected function getValidSearchValueTwo() + protected function getValidSearchValueTwo(): BinaryFileValue { return new BinaryFileValue( [ @@ -411,37 +174,4 @@ protected function getValidSearchValueTwo() ] ); } - - protected function getSearchTargetValueOne() - { - $value = $this->getValidSearchValueOne(); - - // ensure case-insensitivity - return strtoupper($value->fileName); - } - - protected function getSearchTargetValueTwo() - { - $value = $this->getValidSearchValueTwo(); - - // ensure case-insensitivity - return strtoupper($value->fileName); - } - - protected function getAdditionallyIndexedFieldData() - { - return [ - [ - 'file_size', - $this->getValidSearchValueOne()->fileSize, - $this->getValidSearchValueTwo()->fileSize, - ], - [ - 'mime_type', - // ensure case-insensitivity - 'IMAGE/JPEG', - 'IMAGE/PNG', - ], - ]; - } } diff --git a/tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php b/tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php index 75534b2652..4ab6d1bed9 100644 --- a/tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php +++ b/tests/integration/Core/Repository/FieldType/MediaIntegrationTest.php @@ -4,11 +4,12 @@ * @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\Integration\Core\Repository\FieldType; use Ibexa\Contracts\Core\Repository\Values\Content\Field; -use Ibexa\Core\Base\Exceptions\InvalidArgumentValue; +use Ibexa\Core\FieldType\BinaryBase\Value as BinaryBaseValue; use Ibexa\Core\FieldType\Media\Type as MediaType; use Ibexa\Core\FieldType\Media\Value as MediaValue; @@ -18,31 +19,9 @@ * @group integration * @group field-type */ -class MediaIntegrationTest extends FileSearchBaseIntegrationTest +final class MediaIntegrationTest extends BaseBinaryFileIntegrationTestCase { - /** - * Stores the loaded image path for copy test. - */ - protected static $loadedMediaPath; - - /** - * IOService storage prefix for the tested Type's files. - * - * @var string - */ - protected static $storagePrefixConfigKey = 'ibexa.io.binary_file.storage.prefix'; - - protected function getStoragePrefix() - { - return $this->getConfigValue(self::$storagePrefixConfigKey); - } - - /** - * Sets up fixture data. - * - * @return array - */ - protected function getFixtureData() + protected function getFixtureData(): array { return [ 'create' => [ @@ -80,11 +59,9 @@ public function getTypeName() } /** - * Get expected settings schema. - * - * @return array + * @return array */ - public function getSettingsSchema() + public function getSettingsSchema(): array { return [ 'mediaType' => [ @@ -95,241 +72,38 @@ public function getSettingsSchema() } /** - * Get a valid $fieldSettings value. - * - * @return mixed + * @return array */ - public function getValidFieldSettings() + public function getValidFieldSettings(): array { return [ 'mediaType' => MediaType::TYPE_FLASH, ]; } - /** - * Get $fieldSettings value not accepted by the field type. - * - * @return mixed - */ - public function getInvalidFieldSettings() - { - return [ - 'somethingUnknown' => 0, - ]; - } - - /** - * Get expected validator schema. - * - * @return array - */ - public function getValidatorSchema() - { - return [ - 'FileSizeValidator' => [ - 'maxFileSize' => [ - 'type' => 'int', - 'default' => false, - ], - ], - ]; - } - - /** - * Get a valid $validatorConfiguration. - * - * @return mixed - */ - public function getValidValidatorConfiguration() - { - return [ - 'FileSizeValidator' => [ - 'maxFileSize' => 2 * 1024 * 1024, // 2 MB - ], - ]; - } - - /** - * Get $validatorConfiguration not accepted by the field type. - * - * @return mixed - */ - public function getInvalidValidatorConfiguration() - { - return [ - 'StringLengthValidator' => [ - 'minStringLength' => new \stdClass(), - ], - ]; - } - - /** - * Get initial field data for valid object creation. - * - * @return mixed - */ - public function getValidCreationFieldData() - { - $fixtureData = $this->getFixtureData(); - - return new MediaValue($fixtureData['create']); - } - - /** - * Get name generated by the given field type (via or fieldType->getName()). - * - * @return string - */ - public function getFieldName() + protected function buildBinaryFileValueFromFixtureData(array $fileFieldValueData): BinaryBaseValue { - return 'Icy-Night-Flower-Binary.jpg'; + return new MediaValue($fileFieldValueData); } - /** - * Asserts that the field data was loaded correctly. - * - * Asserts that the data provided by {@link getValidCreationFieldData()} - * was stored and loaded correctly. - * - * @param \Ibexa\Contracts\Core\Repository\Values\Content\Field $field - */ - public function assertFieldDataLoadedCorrect(Field $field) + public function asserFieldValueIsCorrectInstance(Field $field): void { self::assertInstanceOf( MediaValue::class, $field->value ); - - $fixtureData = $this->getFixtureData(); - $expectedData = $fixtureData['create']; - - // Will change during storage - unset($expectedData['id']); - $expectedData['inputUri'] = null; - - self::assertNotEmpty($field->value->id); - $this->assertPropertiesCorrect( - $expectedData, - $field->value - ); - - self::assertTrue( - $this->uriExistsOnIO($field->value->uri), - "File {$field->value->uri} doesn't exist." - ); - - self::$loadedMediaPath = $field->value->id; } - public function provideInvalidCreationFieldData() + protected function buildBinaryFileValue(string $fileId): BinaryBaseValue { - return [ - [ - [ - 'id' => '/foo/bar/sindelfingen.pdf', - ], - InvalidArgumentValue::class, - ], + return new MediaValue( [ - new MediaValue( - [ - 'id' => '/foo/bar/sindelfingen.pdf', - ] - ), - InvalidArgumentValue::class, - ], - ]; - } - - /** - * Get update field externals data. - * - * @return array - */ - public function getValidUpdateFieldData() - { - $fixtureData = $this->getFixtureData(); - - return new MediaValue($fixtureData['update']); - } - - /** - * Get externals updated field data values. - * - * This is a PHPUnit data provider - * - * @return array - */ - public function assertUpdatedFieldDataLoadedCorrect(Field $field) - { - self::assertInstanceOf( - MediaValue::class, - $field->value - ); - - $fixtureData = $this->getFixtureData(); - $expectedData = $fixtureData['update']; - - // Will change during storage - unset($expectedData['id']); - $expectedData['inputUri'] = null; - - self::assertNotEmpty($field->value->id); - $this->assertPropertiesCorrect( - $expectedData, - $field->value - ); - - self::assertTrue( - $this->uriExistsOnIO($field->value->uri), - "File {$field->value->uri} doesn't exist." - ); - } - - public function provideInvalidUpdateFieldData() - { - return $this->provideInvalidCreationFieldData(); - } - - /** - * Asserts the the field data was loaded correctly. - * - * Asserts that the data provided by {@link getValidCreationFieldData()} - * was copied and loaded correctly. - * - * @param \Ibexa\Contracts\Core\Repository\Values\Content\Field $field - */ - public function assertCopiedFieldDataLoadedCorrectly(Field $field) - { - $this->assertFieldDataLoadedCorrect($field); - - self::assertEquals( - self::$loadedMediaPath, - $field->value->id + 'id' => $fileId, + ] ); } - /** - * Get data to test to hash method. - * - * This is a PHPUnit data provider - * - * The returned records must have the the original value assigned to the - * first index and the expected hash result to the second. For example: - * - * - * array( - * array( - * new MyValue( true ), - * array( 'myValue' => true ), - * ), - * // ... - * ); - * - * - * @return array - */ - public function provideToHashData() + public function provideToHashData(): array { $fixture = $this->getFixtureData(); $expected = $fixture['create']; @@ -355,14 +129,7 @@ public function provideToHashData() ]; } - /** - * Get expectations for the fromHash call on our field value. - * - * This is a PHPUnit data provider - * - * @return array - */ - public function provideFromHashData() + public function provideFromHashData(): array { $fixture = $this->getFixtureData(); $fixture['create']['uri'] = $fixture['create']['id']; @@ -378,23 +145,17 @@ public function provideFromHashData() ]; } - public function providerForTestIsEmptyValue() + /** + * @return list> + */ + public function providerForTestIsEmptyValue(): array { return [ [new MediaValue()], ]; } - public function providerForTestIsNotEmptyValue() - { - return [ - [ - $this->getValidCreationFieldData(), - ], - ]; - } - - protected function getValidSearchValueOne() + protected function getValidSearchValueOne(): MediaValue { return new MediaValue( [ @@ -405,7 +166,7 @@ protected function getValidSearchValueOne() ); } - protected function getValidSearchValueTwo() + protected function getValidSearchValueTwo(): MediaValue { return new MediaValue( [ @@ -415,37 +176,4 @@ protected function getValidSearchValueTwo() ] ); } - - protected function getSearchTargetValueOne() - { - $value = $this->getValidSearchValueOne(); - - // ensure case-insensitivity - return strtoupper($value->fileName); - } - - protected function getSearchTargetValueTwo() - { - $value = $this->getValidSearchValueTwo(); - - // ensure case-insensitivity - return strtoupper($value->fileName); - } - - protected function getAdditionallyIndexedFieldData() - { - return [ - [ - 'file_size', - $this->getValidSearchValueOne()->fileSize, - $this->getValidSearchValueTwo()->fileSize, - ], - [ - 'mime_type', - // ensure case-insensitivity - 'IMAGE/JPEG', - 'IMAGE/PNG', - ], - ]; - } }