diff --git a/.travis.yml b/.travis.yml index 5a5c8fad6..9a6052d82 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,6 +27,10 @@ matrix: - name: "[PHP 7.2] PHP Unit tests" php: 7.2 env: TEST_CONFIG="phpunit.xml.dist" + # 7.4 + - name: "[PHP 7.4] PHP Unit tests" + php: 7.4 + env: TEST_CONFIG="phpunit.xml.dist" # test only master (+ Pull requests) branches: diff --git a/composer.json b/composer.json index fd68675c5..86e7ddfdc 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { - "name": "ezsystems/repository-forms", "description": "Use Symfony forms with eZ Platform repository objects", + "name": "ezsystems/repository-forms", "license": "GPL-2.0-only", "type": "ezplatform-bundle", "authors": [ @@ -16,8 +16,8 @@ "jms/translation-bundle": "^1.3" }, "require-dev": { - "phpunit/phpunit": "^5.7|^6.0", - "matthiasnoback/symfony-dependency-injection-test": "~1.0|~2.0", + "phpunit/phpunit": "^7.5", + "matthiasnoback/symfony-dependency-injection-test": "~3.1", "behat/behat": "^3.0", "friendsofphp/php-cs-fixer": "^2.7" }, diff --git a/lib/FieldType/DataTransformer/SingleCountryValueTransformer.php b/lib/FieldType/DataTransformer/SingleCountryValueTransformer.php index 1b1cbf646..9d35fd437 100644 --- a/lib/FieldType/DataTransformer/SingleCountryValueTransformer.php +++ b/lib/FieldType/DataTransformer/SingleCountryValueTransformer.php @@ -10,6 +10,7 @@ use eZ\Publish\Core\FieldType\Country\Value; use Symfony\Component\Form\DataTransformerInterface; +use Symfony\Component\Form\Exception\TransformationFailedException; /** * DataTransformer for Country\Value to be used with form type handling only single selection. @@ -36,7 +37,16 @@ public function transform($value) return null; } - return current($value->countries)['Alpha2']; + if (empty($value->countries)) { + return null; + } + + $country = current($value->countries); + if (!isset($country['Alpha2'])) { + throw new TransformationFailedException('Missing Alpha2 key'); + } + + return $country['Alpha2']; } public function reverseTransform($value) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index fcde46be2..94fb5a4f6 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -5,8 +5,7 @@ convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" - beStrictAboutTestsThatDoNotTestAnything="false" - syntaxCheck="true"> + beStrictAboutTestsThatDoNotTestAnything="false"> diff --git a/tests/RepositoryForms/FieldType/DataTransformer/SingleCountryValueTransformerTest.php b/tests/RepositoryForms/FieldType/DataTransformer/SingleCountryValueTransformerTest.php new file mode 100644 index 000000000..8e979ee52 --- /dev/null +++ b/tests/RepositoryForms/FieldType/DataTransformer/SingleCountryValueTransformerTest.php @@ -0,0 +1,103 @@ + ['Name' => 'Afghanistan', 'Alpha2' => 'AF', 'Alpha3' => 'AFG', 'IDC' => '93'], + 'AX' => ['Name' => 'Åland', 'Alpha2' => 'AX', 'Alpha3' => 'ALA', 'IDC' => '358'], + 'AQ' => ['Name' => 'Antarctica', 'Alpha2' => 'AQ', 'Alpha3' => 'ATA', 'IDC' => '672'], + 'AG' => ['Name' => 'Antigua and Barbuda', 'Alpha2' => 'AG', 'Alpha3' => 'ATG', 'IDC' => '1268'], + 'AM' => ['Name' => 'Armenia', 'Alpha2' => 'AM', 'Alpha3' => 'ARM', 'IDC' => '374'], + 'BB' => ['Name' => 'Barbados', 'Alpha2' => 'BB', 'Alpha3' => 'BRB', 'IDC' => '1246'], + 'BJ' => ['Name' => 'Benin', 'Alpha2' => 'BJ', 'Alpha3' => 'BEN', 'IDC' => '229'], + 'BM' => ['Name' => 'Bermuda', 'Alpha2' => 'BM', 'Alpha3' => 'BMU', 'IDC' => '1441'], + 'BT' => ['Name' => 'Bhutan', 'Alpha2' => 'BT', 'Alpha3' => 'BTN', 'IDC' => '975'], + 'BA' => ['Name' => 'Bosnia and Herzegovina', 'Alpha2' => 'BA', 'Alpha3' => 'BIH', 'IDC' => '387'], + 'BW' => ['Name' => 'Botswana', 'Alpha2' => 'BW', 'Alpha3' => 'BWA', 'IDC' => '267'], + 'BV' => ['Name' => 'Bouvet Island', 'Alpha2' => 'BV', 'Alpha3' => 'BVT', 'IDC' => '47'], + 'IO' => ['Name' => 'British Indian Ocean Territory', 'Alpha2' => 'IO', 'Alpha3' => 'IOT', 'IDC' => '246'], + 'BN' => ['Name' => 'Brunei Darussalam', 'Alpha2' => 'BN', 'Alpha3' => 'BRN', 'IDC' => '673'], + 'BF' => ['Name' => 'Burkina Faso', 'Alpha2' => 'BF', 'Alpha3' => 'BFA', 'IDC' => '226'], + 'KH' => ['Name' => 'Cambodia', 'Alpha2' => 'KH', 'Alpha3' => 'KHM', 'IDC' => '855'], + 'CV' => ['Name' => 'Cape Verde', 'Alpha2' => 'CV', 'Alpha3' => 'CPV', 'IDC' => '238'], + 'CF' => ['Name' => 'Central African Republic', 'Alpha2' => 'CF', 'Alpha3' => 'CAF', 'IDC' => '236'], + 'CN' => ['Name' => 'China', 'Alpha2' => 'CN', 'Alpha3' => 'CHN', 'IDC' => '86'], + 'CC' => ['Name' => 'Cocos (Keeling) Islands', 'Alpha2' => 'CC', 'Alpha3' => 'CCK', 'IDC' => '61'], + 'BL' => ['Name' => 'Saint Barthélemy', 'Alpha2' => 'BL', 'Alpha3' => 'BLM', 'IDC' => '590'], + 'GS' => ['Name' => 'South Georgia and The South Sandwich Islands', 'Alpha2' => 'GS', 'Alpha3' => 'SGS', 'IDC' => '500'], + 'TW' => ['Name' => 'Taiwan', 'Alpha2' => 'TW', 'Alpha3' => 'TWN', 'IDC' => '886'], + 'ZW' => ['Name' => 'Zimbabwe', 'Alpha2' => 'ZW', 'Alpha3' => 'ZWE', 'IDC' => '263'], + ]; + + /** @var \EzSystems\RepositoryForms\FieldType\DataTransformer\SingleCountryValueTransformer */ + private $transformer; + + protected function setUp(): void + { + $this->transformer = new SingleCountryValueTransformer(self::COUNTRIES_INFO); + } + + /** + * @dataProvider dataProviderForTransform + */ + public function testTransform(?Value $value, ?string $expectedValue): void + { + $this->assertEquals($expectedValue, $this->transformer->transform($value)); + } + + public function dataProviderForTransform(): iterable + { + yield 'country' => [ + new Value(['ZW' => self::COUNTRIES_INFO['ZW']]), + 'ZW', + ]; + + yield 'empty_array' => [ + new Value([]), + null, + ]; + + yield 'null' => [null, null]; + } + + public function testTransformThrowsTransformationFailedException(): void + { + $this->expectException(TransformationFailedException::class); + $this->expectExceptionMessage('Missing Alpha2 key'); + + $this->transformer->transform(new Value([ + 'AF' => ['Name' => 'Afghanistan', 'Alpha3' => 'AFG', 'IDC' => '93'], + ])); + } + + /** + * @dataProvider dataProviderForReverseTransform + */ + public function testReverseTransform($value, ?Value $expectedValue): void + { + $this->assertEquals($expectedValue, $this->transformer->reverseTransform($value)); + } + + public function dataProviderForReverseTransform(): iterable + { + yield 'country' => [ + 'ZW', + new Value(['ZW' => self::COUNTRIES_INFO['ZW']]), + ]; + + yield 'null' => [null, null]; + } +}