|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Tests\Sylius\PayPalPlugin\Unit\Resolver; |
| 15 | + |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Sylius\PayPalPlugin\Resolver\SupportedLocaleResolver; |
| 18 | + |
| 19 | +final class SupportedLocaleResolverTest extends TestCase |
| 20 | +{ |
| 21 | + /** @dataProvider validFiveCharacterLocaleProvider */ |
| 22 | + public function test_returns_exact_match_for_five_character_locale(string $locale, array $supportedLocales): void |
| 23 | + { |
| 24 | + $resolver = new SupportedLocaleResolver($supportedLocales); |
| 25 | + |
| 26 | + $result = $resolver->resolve($locale); |
| 27 | + |
| 28 | + $this->assertSame($locale, $result); |
| 29 | + } |
| 30 | + |
| 31 | + /** @return \Generator<string, array<string|array<string>>> */ |
| 32 | + public static function validFiveCharacterLocaleProvider(): \Generator |
| 33 | + { |
| 34 | + yield 'en_US' => ['en_US', ['en_US', 'fr_FR', 'de_DE']]; |
| 35 | + yield 'fr_FR' => ['fr_FR', ['en_US', 'fr_FR', 'de_DE']]; |
| 36 | + yield 'de_DE' => ['de_DE', ['en_US', 'fr_FR', 'de_DE']]; |
| 37 | + } |
| 38 | + |
| 39 | + /** @dataProvider unsupportedFiveCharacterLocaleProvider */ |
| 40 | + public function test_throws_for_unsupported_five_character_locale(string $locale, array $supportedLocales): void |
| 41 | + { |
| 42 | + $resolver = new SupportedLocaleResolver($supportedLocales); |
| 43 | + |
| 44 | + $this->expectException(\UnexpectedValueException::class); |
| 45 | + $this->expectExceptionMessage(sprintf('Locale "%s" is not supported by PayPal.', $locale)); |
| 46 | + |
| 47 | + $resolver->resolve($locale); |
| 48 | + } |
| 49 | + |
| 50 | + /** @return \Generator<string, array<string|array<string>>> */ |
| 51 | + public static function unsupportedFiveCharacterLocaleProvider(): \Generator |
| 52 | + { |
| 53 | + yield 'es_ES not in list' => ['es_ES', ['en_US', 'fr_FR', 'de_DE']]; |
| 54 | + yield 'it_IT not in list' => ['it_IT', ['en_US', 'fr_FR']]; |
| 55 | + yield 'pt_PT not in list' => ['pt_PT', ['en_GB', 'en_US']]; |
| 56 | + } |
| 57 | + |
| 58 | + /** @dataProvider validLanguageCodeProvider */ |
| 59 | + public function test_resolves_two_character_language_code_to_supported_locale(string $languageCode, array $supportedLocales, string $expectedLocale): void |
| 60 | + { |
| 61 | + $resolver = new SupportedLocaleResolver($supportedLocales); |
| 62 | + |
| 63 | + $result = $resolver->resolve($languageCode); |
| 64 | + |
| 65 | + $this->assertSame($expectedLocale, $result); |
| 66 | + } |
| 67 | + |
| 68 | + /** @return \Generator<string, array<string|array<string>>> */ |
| 69 | + public static function validLanguageCodeProvider(): \Generator |
| 70 | + { |
| 71 | + yield 'en finds en_US' => ['en', ['en_US', 'en_GB', 'fr_FR', 'de_DE'], 'en_US']; |
| 72 | + yield 'de finds de_DE' => ['de', ['de_DE', 'de_AT', 'fr_FR'], 'de_DE']; |
| 73 | + yield 'fr finds fr_FR' => ['fr', ['en_US', 'fr_FR', 'de_DE'], 'fr_FR']; |
| 74 | + } |
| 75 | + |
| 76 | + /** @dataProvider unsupportedLanguageCodeProvider */ |
| 77 | + public function test_throws_when_language_code_not_found(string $languageCode, array $supportedLocales): void |
| 78 | + { |
| 79 | + $resolver = new SupportedLocaleResolver($supportedLocales); |
| 80 | + |
| 81 | + $this->expectException(\UnexpectedValueException::class); |
| 82 | + $this->expectExceptionMessage(sprintf('Language "%s" could not be resolved into a locale supported by PayPal.', $languageCode)); |
| 83 | + |
| 84 | + $resolver->resolve($languageCode); |
| 85 | + } |
| 86 | + |
| 87 | + /** @return \Generator<string, array<string|array<string>>> */ |
| 88 | + public static function unsupportedLanguageCodeProvider(): \Generator |
| 89 | + { |
| 90 | + yield 'es not in list' => ['es', ['en_US', 'fr_FR', 'de_DE']]; |
| 91 | + yield 'it not in list' => ['it', ['en_US', 'fr_FR']]; |
| 92 | + yield 'pt not in list' => ['pt', ['en_GB', 'en_US']]; |
| 93 | + } |
| 94 | + |
| 95 | + /** @dataProvider invalidLocaleProvider */ |
| 96 | + public function test_throws_for_invalid_locale_length(string $locale, array $supportedLocales): void |
| 97 | + { |
| 98 | + $resolver = new SupportedLocaleResolver($supportedLocales); |
| 99 | + |
| 100 | + $this->expectException(\UnexpectedValueException::class); |
| 101 | + $this->expectExceptionMessage(sprintf('Locale "%s" is not supported by PayPal.', $locale)); |
| 102 | + |
| 103 | + $resolver->resolve($locale); |
| 104 | + } |
| 105 | + |
| 106 | + /** @return \Generator<string, array<string|array<string>>> */ |
| 107 | + public static function invalidLocaleProvider(): \Generator |
| 108 | + { |
| 109 | + yield 'three character locale' => ['eng', ['en_US', 'fr_FR']]; |
| 110 | + yield 'six character locale' => ['en_US_X', ['en_US', 'fr_FR']]; |
| 111 | + yield 'four character locale' => ['enUS', ['en_US', 'fr_FR']]; |
| 112 | + } |
| 113 | + |
| 114 | + public function test_throws_for_empty_locale(): void |
| 115 | + { |
| 116 | + $resolver = new SupportedLocaleResolver(['en_US', 'fr_FR']); |
| 117 | + |
| 118 | + $this->expectException(\UnexpectedValueException::class); |
| 119 | + $this->expectExceptionMessage('Locale cannot be empty.'); |
| 120 | + |
| 121 | + $resolver->resolve(''); |
| 122 | + } |
| 123 | + |
| 124 | + public function test_constructor_removes_duplicate_locales(): void |
| 125 | + { |
| 126 | + $supportedLocales = ['en_US', 'fr_FR', 'en_GB', 'de_DE', 'fr_FR']; |
| 127 | + $resolver = new SupportedLocaleResolver($supportedLocales); |
| 128 | + |
| 129 | + $result = $resolver->resolve('en'); |
| 130 | + $this->assertSame('en_US', $result); |
| 131 | + } |
| 132 | + |
| 133 | + /** @dataProvider emptyLocaleProvider */ |
| 134 | + public function test_throws_with_empty_locale(string $locale): void |
| 135 | + { |
| 136 | + $supportedLocales = ['en_US', 'fr_FR', 'en_US', 'de_DE', 'fr_FR']; |
| 137 | + $resolver = new SupportedLocaleResolver($supportedLocales); |
| 138 | + |
| 139 | + $this->expectException(\UnexpectedValueException::class); |
| 140 | + $this->expectExceptionMessage('Locale cannot be empty.'); |
| 141 | + |
| 142 | + $resolver->resolve($locale); |
| 143 | + } |
| 144 | + |
| 145 | + /** @return \Generator<string, array<array<string>>> */ |
| 146 | + public static function emptyLocaleProvider(): \Generator |
| 147 | + { |
| 148 | + yield 'empty' => ['']; |
| 149 | + yield 'space' => [' ']; |
| 150 | + } |
| 151 | +} |
0 commit comments