From da6acd9a9b5fad0732d62371e65827bc36072771 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Thu, 11 Jul 2024 16:49:25 +0200 Subject: [PATCH] [Tests] Extracted common base for Binary and Media ft integration tests --- .../BaseBinaryFileIntegrationTestCase.php | 259 ++++++++++++++ .../FieldType/BinaryFileIntegrationTest.php | 324 ++---------------- .../FieldType/MediaIntegrationTest.php | 318 ++--------------- 3 files changed, 309 insertions(+), 592 deletions(-) create mode 100644 tests/integration/Core/Repository/FieldType/BaseBinaryFileIntegrationTestCase.php 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..84c9d22ae4 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', - ], - ]; - } }