diff --git a/.gitattributes b/.gitattributes index 7ccb1f3..22f739a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -7,6 +7,5 @@ .gitattributes export-ignore .gitignore export-ignore .scrutinizer.yml export-ignore -.travis.yml export-ignore phpunit.xml.dist export-ignore CONTRIBUTING.md export-ignore diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..92c6321 --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,46 @@ +name: run-tests + +on: [push] + +jobs: + tests: + runs-on: ubuntu-latest + + strategy: + fail-fast: true + matrix: + php: [7.2, 7.3, 7.4] + laravel: [6.*] + dependency-version: [prefer-lowest, prefer-stable] + + name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }} + + steps: + - name: Checkout code + uses: actions/checkout@v1 + + - name: Cache dependencies + uses: actions/cache@v1 + with: + path: ~/.composer/cache/files + key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + + - name: Setup PHP + uses: shivammathur/setup-php@v1 + with: + php-version: ${{ matrix.php }} + extensions: curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv, intl + coverage: xdebug + + - name: Install dependencies + run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest + + - name: Execute tests + run: | + mkdir -p build/logs + vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover + + - name: Scrutinizer CI + run: | + wget https://scrutinizer-ci.com/ocular.phar + php ocular.phar code-coverage:upload --format=php-clover coverage.clover diff --git a/.scrutinizer.yml b/.scrutinizer.yml index ba44fae..8a9285f 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -21,8 +21,8 @@ checks: tools: external_code_coverage: - timeout: 1200 - runs: 2 + timeout: 600 + runs: 6 php_code_sniffer: enabled: true config: diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index f6ee926..0000000 --- a/.travis.yml +++ /dev/null @@ -1,23 +0,0 @@ -language: php - -php: - - 7.2 - - 7.3 - - nightly - -matrix: - allow_failures: - - php: nightly - -before_script: - - travis_retry composer self-update - - travis_retry composer install --prefer-dist --no-interaction - -script: - - composer validate - - mkdir -p build/logs - - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover - -after_script: - - if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi - - if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi diff --git a/README.md b/README.md index 32c6ead..f55bce1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Localization [![Packagist License][badge_license]](LICENSE.md) [![For Laravel][badge_laravel]][link-github-repo] -[![Travis Status][badge_build]][link-travis] +[![Github Workflow Status][badge_build]][link-github-status] [![Coverage Status][badge_coverage]][link-scrutinizer] [![Scrutinizer Code Quality][badge_quality]][link-scrutinizer] [![SensioLabs Insight][badge_insight]][link-insight] @@ -53,7 +53,7 @@ If you discover any security related issues, please email arcanedev.maroc@gmail. [badge_license]: http://img.shields.io/packagist/l/arcanedev/localization.svg?style=flat-square [badge_laravel]: https://img.shields.io/badge/Laravel-5.x%20|%206.x-orange.svg?style=flat-square -[badge_build]: http://img.shields.io/travis/ARCANEDEV/Localization.svg?style=flat-square +[badge_build]: https://img.shields.io/github/workflow/status/ARCANEDEV/Localization/run-tests?style=flat-square [badge_coverage]: https://img.shields.io/scrutinizer/coverage/g/ARCANEDEV/Localization.svg?style=flat-square [badge_quality]: https://img.shields.io/scrutinizer/g/ARCANEDEV/Localization.svg?style=flat-square [badge_insight]: https://img.shields.io/sensiolabs/i/30a231b2-295e-466a-8733-fe95cf2bc48d.svg?style=flat-square @@ -64,9 +64,9 @@ If you discover any security related issues, please email arcanedev.maroc@gmail. [link-author]: https://github.com/arcanedev-maroc [link-github-repo]: https://github.com/ARCANEDEV/Localization +[link-github-status]: https://github.com/ARCANEDEV/localization/actions [link-github-issues]: https://github.com/ARCANEDEV/Localization/issues [link-contributors]: https://github.com/ARCANEDEV/Localization/graphs/contributors [link-packagist]: https://packagist.org/packages/arcanedev/localization -[link-travis]: https://travis-ci.org/ARCANEDEV/Localization [link-scrutinizer]: https://scrutinizer-ci.com/g/ARCANEDEV/Localization/?branch=master [link-insight]: https://insight.sensiolabs.com/projects/30a231b2-295e-466a-8733-fe95cf2bc48d diff --git a/composer.json b/composer.json index 997f2ef..0f77ba0 100644 --- a/composer.json +++ b/composer.json @@ -16,11 +16,12 @@ "require": { "php": ">=7.2.0", "ext-json": "*", - "arcanedev/support": "^5.0" + "arcanedev/support": "^5.1" }, "require-dev": { + "ext-intl": "*", "orchestra/testbench": "^4.0", - "mockery/mockery": "^1.0", + "mockery/mockery": "^1.3.1", "phpunit/phpcov": "^6.0", "phpunit/phpunit": "^8.0" }, diff --git a/helpers.php b/helpers.php index 829ca1d..037d31c 100644 --- a/helpers.php +++ b/helpers.php @@ -1,14 +1,18 @@ */ -class UntranslatableAttributeException extends LocalizationException +class UntranslatableAttributeException extends LocalizationException { + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- + */ + + /** + * @param string $key + * @param array $translatableAttributes + * + * @return static + */ public static function make(string $key, array $translatableAttributes) { $translatableAttributes = implode(', ', $translatableAttributes); diff --git a/src/Localization.php b/src/Localization.php index 3238a95..175c6bd 100644 --- a/src/Localization.php +++ b/src/Localization.php @@ -1,4 +1,8 @@ -singleton(LocalizationContract::class, Localization::class); } @@ -66,7 +70,7 @@ private function registerLocalization() /** * Register RouteTranslator utility. */ - private function registerRouteTranslator() + private function registerRouteTranslator(): void { $this->singleton(RouteTranslatorContract::class, RouteTranslator::class); } @@ -74,7 +78,7 @@ private function registerRouteTranslator() /** * Register LocalesManager utility. */ - private function registerLocalesManager() + private function registerLocalesManager(): void { $this->singleton(LocalesManagerContract::class, LocalesManager::class); } @@ -82,7 +86,7 @@ private function registerLocalesManager() /** * Register LocaleNegotiator utility. */ - private function registerLocaleNegotiator() + private function registerLocaleNegotiator(): void { $this->bind(NegotiatorContract::class, function ($app) { /** @var \Arcanedev\Localization\Contracts\LocalesManager $manager */ diff --git a/src/Providers/RoutingServiceProvider.php b/src/Providers/RoutingServiceProvider.php index 0534b29..eaf2b8f 100644 --- a/src/Providers/RoutingServiceProvider.php +++ b/src/Providers/RoutingServiceProvider.php @@ -1,4 +1,8 @@ -app['router']->mixin(new Router); diff --git a/src/Routing/Router.php b/src/Routing/Router.php index bac612a..2221e73 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -1,4 +1,8 @@ -get( @@ -57,7 +61,7 @@ public function transGet() * * @return \Closure|\Illuminate\Routing\Route */ - public function transPost() + public function transPost(): Closure { return function($trans, $action) { return $this->post( @@ -71,7 +75,7 @@ public function transPost() * * @return \Closure|\Illuminate\Routing\Route */ - public function transPut() + public function transPut(): Closure { return function($trans, $action) { return $this->put( @@ -85,7 +89,7 @@ public function transPut() * * @return \Closure|\Illuminate\Routing\Route */ - public function transPatch() + public function transPatch(): Closure { return function($trans, $action) { return $this->patch( @@ -99,7 +103,7 @@ public function transPatch() * * @return \Closure|\Illuminate\Routing\Route */ - public function transDelete() + public function transDelete(): Closure { return function($trans, $action) { return $this->delete( @@ -113,7 +117,7 @@ public function transDelete() * * @return \Closure|\Illuminate\Routing\Route */ - public function transOptions() + public function transOptions(): Closure { return function($trans, $action) { return $this->options( @@ -127,7 +131,7 @@ public function transOptions() * * @return \Closure|\Illuminate\Routing\Route */ - public function transAny() + public function transAny(): Closure { return function($trans, $action) { return $this->any( diff --git a/src/Traits/HasTranslations.php b/src/Traits/HasTranslations.php index b6b12d9..376761e 100644 --- a/src/Traits/HasTranslations.php +++ b/src/Traits/HasTranslations.php @@ -1,4 +1,8 @@ -locales->loadFromArray($this->getConfig('locales')); $this->setSupportedLocales($this->getConfig('supported-locales')); @@ -132,7 +136,7 @@ public function getDefaultLocale() * * @param string $defaultLocale * - * @return self + * @return $this */ public function setDefaultLocale($defaultLocale = null) { @@ -167,7 +171,7 @@ public function getCurrentLocale() * * @param string $currentLocale * - * @return self + * @return $this */ public function setCurrentLocale($currentLocale) { @@ -221,7 +225,7 @@ public function getSupportedLocalesKeys() * * @param array $supportedLocales * - * @return self + * @return $this * * @throws \Arcanedev\Localization\Exceptions\UndefinedSupportedLocalesException */ @@ -359,7 +363,7 @@ public function getCurrentOrDefaultLocale() * * @return \Arcanedev\Localization\Entities\LocaleCollection */ - private function filterLocales(array $supportedLocales) + private function filterLocales(array $supportedLocales): LocaleCollection { return $this->locales->filter(function(Locale $locale) use ($supportedLocales) { return in_array($locale->key(), $supportedLocales); @@ -369,7 +373,7 @@ private function filterLocales(array $supportedLocales) /** * Update locale regional. */ - private function updateRegional() + private function updateRegional(): void { $currentLocale = $this->getCurrentLocaleEntity(); diff --git a/src/Utilities/Negotiator.php b/src/Utilities/Negotiator.php index 29f30e2..2a3705c 100644 --- a/src/Utilities/Negotiator.php +++ b/src/Utilities/Negotiator.php @@ -1,4 +1,8 @@ -getMatchesFromAcceptedLanguages(); @@ -131,9 +135,9 @@ private function getFromAcceptedLanguagesHeader() /** * Get locale from http accepted languages server. * - * @return null|string + * @return string|null */ - private function getFromHttpAcceptedLanguagesServer() + private function getFromHttpAcceptedLanguagesServer(): ?string { $httpAcceptLanguage = $this->request->server('HTTP_ACCEPT_LANGUAGE'); @@ -151,9 +155,9 @@ private function getFromHttpAcceptedLanguagesServer() /** * Get locale from remote host server. * - * @return null|string + * @return string|null */ - private function getFromRemoteHostServer() + private function getFromRemoteHostServer(): ?string { if (empty($remoteHost = $this->request->server('REMOTE_HOST'))) return null; @@ -174,9 +178,9 @@ private function getFromRemoteHostServer() * * @param array $matches * - * @return null|string + * @return string|null */ - private function inSupportedLocales(array $matches) + private function inSupportedLocales(array $matches): ?string { foreach (array_keys($matches) as $locale) { if ($this->isSupported($locale)) return $locale; @@ -198,7 +202,7 @@ private function inSupportedLocales(array $matches) * * @return bool */ - private function isSupported($locale) + private function isSupported($locale): bool { return $this->supportedLocales->has($locale); } @@ -213,7 +217,7 @@ private function isSupported($locale) * * @return array - Matches from the header field Accept-Languages */ - private function getMatchesFromAcceptedLanguages() + private function getMatchesFromAcceptedLanguages(): array { $matches = []; @@ -239,7 +243,7 @@ private function getMatchesFromAcceptedLanguages() * * @return array */ - private function retrieveGenericMatches($acceptLanguages, &$matches) + private function retrieveGenericMatches($acceptLanguages, &$matches): array { $genericMatches = []; @@ -281,7 +285,7 @@ private function retrieveGenericMatches($acceptLanguages, &$matches) * * @return float|null */ - private function getQualityFactor($locale, $option) + private function getQualityFactor($locale, $option): ?float { if (isset($option[1])) return (float) str_replace('q=', '', $option[1]); diff --git a/src/Utilities/RouteTranslator.php b/src/Utilities/RouteTranslator.php index 7d9e90b..5b7b573 100644 --- a/src/Utilities/RouteTranslator.php +++ b/src/Utilities/RouteTranslator.php @@ -1,4 +1,8 @@ -translator->getLocale(); diff --git a/src/Utilities/Url.php b/src/Utilities/Url.php index 4dbadaf..7e8eaa4 100644 --- a/src/Utilities/Url.php +++ b/src/Utilities/Url.php @@ -1,4 +1,8 @@ -getRoutes()); + return self::extractAttributesFromRoutes($url, app('router')->getRoutes()); } /** @@ -220,7 +222,9 @@ private static function checkParsedUrl(array $parsed): array */ private static function getUrl(array $parsed): string { - return strlen($parsed['scheme']) ? $parsed['scheme'].':'.self::getHierPart($parsed) : ''; + return strlen((string) $parsed['scheme']) + ? $parsed['scheme'].':'.self::getHierPart($parsed) + : ''; } /** @@ -260,7 +264,9 @@ private static function getAuthority(array $parsed): string */ private static function getUserInfo(array $parsed): string { - return strlen($parsed['pass']) ? $parsed['user'].':'.$parsed['pass'] : ''; + return strlen((string) $parsed['pass']) + ? $parsed['user'].':'.$parsed['pass'] + : ''; } /** @@ -286,7 +292,9 @@ private static function getHost(array $parsed): string */ private static function getQuery(array $parsed): string { - return strlen($parsed['query']) ? '?'.$parsed['query'] : ''; + return strlen((string) $parsed['query']) + ? '?'.$parsed['query'] + : ''; } /** @@ -298,6 +306,8 @@ private static function getQuery(array $parsed): string */ private static function getFragment(array $parsed): string { - return strlen($parsed['fragment']) ? '#'.$parsed['fragment'] : ''; + return strlen((string) $parsed['fragment']) + ? '#'.$parsed['fragment'] + : ''; } } diff --git a/tests/Entities/LocaleCollectionTest.php b/tests/Entities/LocaleCollectionTest.php index 8391a8a..087b90c 100644 --- a/tests/Entities/LocaleCollectionTest.php +++ b/tests/Entities/LocaleCollectionTest.php @@ -1,7 +1,10 @@ -locales->loadFromArray($data); @@ -73,7 +76,7 @@ public function it_can_get_all_locales() } /** @test */ - public function it_can_get_supported_locales() + public function it_can_get_supported_locales(): void { $this->locales ->loadFromArray(config('localization.locales', [])) @@ -97,7 +100,7 @@ public function it_can_get_supported_locales() } /** @test */ - public function it_can_transform_locales_to_native_names() + public function it_can_transform_locales_to_native_names(): void { $this->locales ->loadFromArray(config('localization.locales', [])) @@ -118,7 +121,7 @@ public function it_can_transform_locales_to_native_names() } /** @test */ - public function it_can_load_locales_from_config() + public function it_can_load_locales_from_config(): void { $this->locales->loadFromConfig(); @@ -137,7 +140,7 @@ public function it_can_load_locales_from_config() } /** @test */ - public function it_can_get_first_locale() + public function it_can_get_first_locale(): void { $this->locales->loadFromConfig(); diff --git a/tests/Entities/LocaleTest.php b/tests/Entities/LocaleTest.php index f975d76..0349d10 100644 --- a/tests/Entities/LocaleTest.php +++ b/tests/Entities/LocaleTest.php @@ -1,4 +1,8 @@ -locale = $this->makeLocale('en'); @@ -55,7 +59,7 @@ public function it_can_be_instantiated() } /** @test */ - public function it_must_lower_direction_case() + public function it_must_lower_direction_case(): void { $key = 'en'; $data = $this->getLocale($key); @@ -66,7 +70,7 @@ public function it_must_lower_direction_case() } /** @test */ - public function it_can_get_direction_if_empty() + public function it_can_get_direction_if_empty(): void { $key = 'en'; $data = $this->getLocale($key); @@ -77,7 +81,7 @@ public function it_can_get_direction_if_empty() } /** @test */ - public function it_can_convert_entity_to_array() + public function it_can_convert_entity_to_array(): void { $this->locale = $this->makeLocale('en'); @@ -85,7 +89,7 @@ public function it_can_convert_entity_to_array() } /** @test */ - public function it_can_convert_entity_to_json() + public function it_can_convert_entity_to_json(): void { $this->locale = $this->makeLocale('en'); @@ -105,7 +109,7 @@ public function it_can_convert_entity_to_json() * * @return \Arcanedev\Localization\Entities\Locale */ - private function makeLocale($key) + private function makeLocale($key): Locale { return Locale::make($key, $this->getLocale($key)); } @@ -117,7 +121,7 @@ private function makeLocale($key) * * @return array */ - private function getLocale($key) + private function getLocale($key): array { return Arr::get([ 'ar' => [ diff --git a/tests/LocalizationServiceProviderTest.php b/tests/LocalizationServiceProviderTest.php index 306a1a6..364e1ed 100644 --- a/tests/LocalizationServiceProviderTest.php +++ b/tests/LocalizationServiceProviderTest.php @@ -1,4 +1,8 @@ -expectException(\Arcanedev\Localization\Exceptions\UnsupportedLocaleException::class); $this->expectExceptionMessage('Laravel default locale [jp] is not in the `supported-locales` array.'); @@ -38,7 +42,7 @@ public function it_must_throw_unsupported_locale_exception_on_default_locale() } /** @test */ - public function it_can_set_and_get_supported_locales() + public function it_can_set_and_get_supported_locales(): void { $supportedLocales = localization()->getSupportedLocales(); @@ -65,7 +69,7 @@ public function it_can_set_and_get_supported_locales() } /** @test */ - public function it_must_throw_undefined_supported_locales_exception_on_set_supported_locales_with_empty_array() + public function it_must_throw_undefined_supported_locales_exception_on_set_supported_locales_with_empty_array(): void { $this->expectException(\Arcanedev\Localization\Exceptions\UndefinedSupportedLocalesException::class); @@ -73,7 +77,7 @@ public function it_must_throw_undefined_supported_locales_exception_on_set_suppo } /** @test */ - public function it_can_get_supported_locales_keys() + public function it_can_get_supported_locales_keys(): void { static::assertSame( $this->supportedLocales, @@ -82,7 +86,7 @@ public function it_can_get_supported_locales_keys() } /** @test */ - public function it_can_set_locale() + public function it_can_set_locale(): void { static::assertSame(route('about'), 'http://localhost/about'); @@ -102,7 +106,7 @@ public function it_can_set_locale() } /** @test */ - public function it_can_get_default_locale() + public function it_can_get_default_locale(): void { static::assertSame('en', localization()->getDefaultLocale()); @@ -113,7 +117,7 @@ public function it_can_get_default_locale() } /** @test */ - public function it_can_get_current_locale() + public function it_can_get_current_locale(): void { static::assertSame('en', localization()->getCurrentLocale()); static::assertNotEquals('es', localization()->getCurrentLocale()); @@ -135,7 +139,7 @@ public function it_can_get_current_locale() } /** @test */ - public function it_can_localize_url() + public function it_can_localize_url(): void { static::assertSame( $this->testUrlOne.localization()->getCurrentLocale(), @@ -189,7 +193,7 @@ public function it_can_localize_url() } /** @test */ - public function it_can_get_localized_url() + public function it_can_get_localized_url(): void { static::assertSame( $this->testUrlOne.localization()->getCurrentLocale(), @@ -268,7 +272,7 @@ public function it_can_get_localized_url() * * @dataProvider getLocalizedURLDataProvider */ - public function it_can_get_localized_url_with_specific_format($hideDefault, $forceDefault, $locale, $url, $expected) + public function it_can_get_localized_url_with_specific_format($hideDefault, $forceDefault, $locale, $url, $expected): void { $this->app['config']->set('localization.hide-default-in-url', $hideDefault); @@ -279,7 +283,7 @@ public function it_can_get_localized_url_with_specific_format($hideDefault, $for } /** @test */ - public function it_can_get_url_from_route_name_translated() + public function it_can_get_url_from_route_name_translated(): void { static::assertSame( $this->testUrlOne.'es/acerca', @@ -330,7 +334,7 @@ public function it_can_get_url_from_route_name_translated() } /** @test */ - public function it_must_throw_an_exception_on_unsupported_locale() + public function it_must_throw_an_exception_on_unsupported_locale(): void { $this->expectException(\Arcanedev\Localization\Exceptions\UnsupportedLocaleException::class); $this->expectExceptionMessage("Locale 'jp' is not in the list of supported locales."); @@ -339,7 +343,7 @@ public function it_must_throw_an_exception_on_unsupported_locale() } /** @test */ - public function it_can_get_non_localized_url() + public function it_can_get_non_localized_url(): void { static::assertSame( $this->testUrlOne, @@ -360,7 +364,7 @@ public function it_can_get_non_localized_url() } /** @test */ - public function it_can_get_current_locale_name() + public function it_can_get_current_locale_name(): void { $locales = [ 'en' => 'English', @@ -376,7 +380,7 @@ public function it_can_get_current_locale_name() } /** @test */ - public function it_can_get_current_locale_script() + public function it_can_get_current_locale_script(): void { foreach ($this->supportedLocales as $locale) { localization()->setLocale($locale); @@ -387,7 +391,7 @@ public function it_can_get_current_locale_script() } /** @test */ - public function it_can_get_current_locale_direction() + public function it_can_get_current_locale_direction(): void { foreach ($this->supportedLocales as $locale) { $this->refreshApplication($locale); @@ -397,7 +401,7 @@ public function it_can_get_current_locale_direction() } /** @test */ - public function it_can_get_current_locale_native() + public function it_can_get_current_locale_native(): void { $locales = [ 'en' => 'English', @@ -413,7 +417,7 @@ public function it_can_get_current_locale_native() } /** @test */ - public function testGetCurrentLocaleRegional() + public function testGetCurrentLocaleRegional(): void { $locales = [ 'en' => 'en_GB', @@ -429,7 +433,7 @@ public function testGetCurrentLocaleRegional() } /** @test */ - public function it_can_create_url_from_uri() + public function it_can_create_url_from_uri(): void { static::assertSame( 'http://localhost/view/1', @@ -446,7 +450,7 @@ public function it_can_create_url_from_uri() } /** @test */ - public function it_can_render_locales_navigation_bar() + public function it_can_render_locales_navigation_bar(): void { $navbar = localization()->localesNavbar(); @@ -458,7 +462,7 @@ public function it_can_render_locales_navigation_bar() } /** @test */ - public function it_can_get_all_locales() + public function it_can_get_all_locales(): void { $locales = localization()->getAllLocales(); @@ -469,7 +473,7 @@ public function it_can_get_all_locales() } /** @test */ - public function it_can_get_localized_url_with_relative_urls() + public function it_can_get_localized_url_with_relative_urls(): void { static::assertSame($this->testUrlOne.'en', localization()->LocalizeURL('/')); @@ -489,7 +493,7 @@ public function it_can_get_localized_url_with_relative_urls() } /** @test */ - public function it_can_get_localized_url_with_route_name_from_lang() + public function it_can_get_localized_url_with_route_name_from_lang(): void { static::assertSame( 'http://localhost/en/view/en-view-slug', @@ -523,7 +527,7 @@ public function it_can_get_localized_url_with_route_name_from_lang() * @param string $uri * @param array $server * - * @return \Illuminate\Http\Response + * @return \Illuminate\Foundation\Testing\TestResponse */ public function makeCall($uri, array $server = []) { @@ -539,7 +543,7 @@ public function makeCall($uri, array $server = []) * * @return array */ - public function getLocalizedURLDataProvider() + public function getLocalizedURLDataProvider(): array { $url = 'http://localhost/'; diff --git a/tests/Middleware/LocaleCookieRedirectTest.php b/tests/Middleware/LocaleCookieRedirectTest.php index cd6a553..75c629a 100644 --- a/tests/Middleware/LocaleCookieRedirectTest.php +++ b/tests/Middleware/LocaleCookieRedirectTest.php @@ -1,4 +1,8 @@ -refreshApplication(false, false, true); @@ -35,7 +39,7 @@ public function it_can_redirect_with_locale_cookie() } /** @test */ - public function it_can_pass_redirect_without_cookie() + public function it_can_pass_redirect_without_cookie(): void { $this->refreshApplication(false, true); session()->put('locale', null); diff --git a/tests/Middleware/LocaleSessionRedirectTest.php b/tests/Middleware/LocaleSessionRedirectTest.php index 4632ea5..90a126b 100644 --- a/tests/Middleware/LocaleSessionRedirectTest.php +++ b/tests/Middleware/LocaleSessionRedirectTest.php @@ -1,4 +1,8 @@ -refreshApplication(false, true); session()->put('locale', 'fr'); @@ -38,7 +42,7 @@ public function it_can_redirect_with_locale_session() } /** @test */ - public function it_can_pass_redirect_without_session() + public function it_can_pass_redirect_without_session(): void { $this->refreshApplication(false, true); session()->put('locale', null); diff --git a/tests/Providers/DeferredServicesProviderTest.php b/tests/Providers/DeferredServicesProviderTest.php index 903a5f7..d575c94 100644 --- a/tests/Providers/DeferredServicesProviderTest.php +++ b/tests/Providers/DeferredServicesProviderTest.php @@ -1,4 +1,8 @@ -router); static::assertInstanceOf(Router::class, $this->router); } /** @test */ - public function it_can_get_and_check_all_routes() + public function it_can_get_and_check_all_routes(): void { $routes = $this->router->getRoutes(); $routeNames = $this->routeRegistrar->getRouteNames(); diff --git a/tests/Stubs/Http/Controllers/BarController.php b/tests/Stubs/Http/Controllers/BarController.php index 999d4bc..ae77c5e 100644 --- a/tests/Stubs/Http/Controllers/BarController.php +++ b/tests/Stubs/Http/Controllers/BarController.php @@ -1,4 +1,8 @@ -singleton(\Illuminate\Contracts\Http\Kernel::class, Stubs\Http\Kernel::class); } @@ -63,11 +67,9 @@ protected function resolveApplicationHttpKernel($app) /** * Define environment setup. * - * @param \Illuminate\Foundation\Application $app - * - * @return void + * @param \Illuminate\Foundation\Application $app */ - protected function getEnvironmentSetUp($app) + protected function getEnvironmentSetUp($app): void { /** * @var \Illuminate\Contracts\Config\Repository $config @@ -109,7 +111,7 @@ protected function getEnvironmentSetUp($app) * @param bool $session * @param bool $cookie */ - protected function refreshApplication($locale = false, $session = false, $cookie = false) + protected function refreshApplication($locale = false, $session = false, $cookie = false): void { parent::refreshApplication(); @@ -134,7 +136,7 @@ protected function refreshApplication($locale = false, $session = false, $cookie * * @param string|bool $locale */ - protected function setRoutes($locale = false) + protected function setRoutes($locale = false): void { $this->routeRegistrar = new Stubs\Http\RouteRegistrar; diff --git a/tests/TranslatableModelTest.php b/tests/TranslatableModelTest.php index 1fa4673..201a523 100644 --- a/tests/TranslatableModelTest.php +++ b/tests/TranslatableModelTest.php @@ -1,4 +1,8 @@ -create([ @@ -52,7 +56,7 @@ public function it_can_set_translated_values_when_creating_a_model() } /** @test */ - public function it_can_save_a_translated_attribute() + public function it_can_save_a_translated_attribute(): void { $this->model->setTranslation('name', 'en', 'Name') ->save(); @@ -61,7 +65,7 @@ public function it_can_save_a_translated_attribute() } /** @test */ - public function it_can_translate_attribute_with_a_fallback_locale() + public function it_can_translate_attribute_with_a_fallback_locale(): void { $this->model->setTranslation('name', 'en', 'Name') ->save(); @@ -73,7 +77,7 @@ public function it_can_translate_attribute_with_a_fallback_locale() } /** @test */ - public function it_must_return_empty_string_when_attribute_does_not_have_translation_and_fallback_locale_is_disabled() + public function it_must_return_empty_string_when_attribute_does_not_have_translation_and_fallback_locale_is_disabled(): void { $this->model->setTranslation('name', 'en', 'Name') ->save(); @@ -82,7 +86,7 @@ public function it_must_return_empty_string_when_attribute_does_not_have_transla } /** @test */ - public function it_must_return_empty_string_when_attribute_does_not_have_translation_and_fallback_locale_is_not_translatable_and_available() + public function it_must_return_empty_string_when_attribute_does_not_have_translation_and_fallback_locale_is_not_translatable_and_available(): void { $this->setFallbackLocale('ar'); @@ -93,7 +97,7 @@ public function it_must_return_empty_string_when_attribute_does_not_have_transla } /** @test */ - public function it_must_return_empty_string_when_attribute_does_not_have_translation_and_fallback_locale_is_not_translatable_and_disabled() + public function it_must_return_empty_string_when_attribute_does_not_have_translation_and_fallback_locale_is_not_translatable_and_disabled(): void { $this->setFallbackLocale('ar'); @@ -104,7 +108,7 @@ public function it_must_return_empty_string_when_attribute_does_not_have_transla } /** @test */ - public function it_can_save_multiple_translations() + public function it_can_save_multiple_translations(): void { $this->model->setTranslation('name', 'en', 'Name') ->setTranslation('name', 'fr', 'Nom') @@ -116,7 +120,7 @@ public function it_can_save_multiple_translations() } /** @test */ - public function it_can_translate_the_attribute_with_the_current_app_locale() + public function it_can_translate_the_attribute_with_the_current_app_locale(): void { $this->model->setTranslation('name', 'en', 'Name') ->setTranslation('name', 'fr', 'Nom') @@ -132,7 +136,7 @@ public function it_can_translate_the_attribute_with_the_current_app_locale() } /** @test */ - public function it_can_get_all_translations_for_a_specific_attribute() + public function it_can_get_all_translations_for_a_specific_attribute(): void { $this->model->setTranslation('name', 'en', 'Name') ->setTranslation('name', 'fr', 'Nom') @@ -145,7 +149,7 @@ public function it_can_get_all_translations_for_a_specific_attribute() } /** @test */ - public function it_handle_null_value_from_database() + public function it_handle_null_value_from_database(): void { $testModel = (new class() extends TranslatableModel { public function setAttributesExternally(array $attributes) @@ -164,7 +168,7 @@ public function setAttributesExternally(array $attributes) } /** @test */ - public function it_can_get_all_translations() + public function it_can_get_all_translations(): void { $translations = ['fr' => 'Salut', 'en' => 'Hello']; @@ -179,7 +183,7 @@ public function it_can_get_all_translations() } /** @test */ - public function it_can_get_all_translations_for_all_translatable_attributes() + public function it_can_get_all_translations_for_all_translatable_attributes(): void { $this->model->setTranslation('name', 'en', 'Name') ->setTranslation('name', 'fr', 'Nom') @@ -200,7 +204,7 @@ public function it_can_get_all_translations_for_all_translatable_attributes() } /** @test */ - public function it_can_get_all_locales_available_in_the_translated_attribute() + public function it_can_get_all_locales_available_in_the_translated_attribute(): void { $this->model->setTranslation('name', 'en', 'Name') ->setTranslation('name', 'fr', 'Nom') @@ -210,7 +214,7 @@ public function it_can_get_all_locales_available_in_the_translated_attribute() } /** @test */ - public function it_can_forget_a_translation() + public function it_can_forget_a_translation(): void { $this->model->setTranslation('name', 'en', 'Name') ->setTranslation('name', 'fr', 'Nom') @@ -227,7 +231,7 @@ public function it_can_forget_a_translation() } /** @test */ - public function it_can_forget_all_translations() + public function it_can_forget_all_translations(): void { $this->model->setTranslation('name', 'en', 'Name') ->setTranslation('name', 'fr', 'Nom') @@ -257,7 +261,7 @@ public function it_can_forget_all_translations() } /** @test */ - public function it_will_throw_an_exception_when_trying_to_translate_an_untranslatable_attribute() + public function it_will_throw_an_exception_when_trying_to_translate_an_untranslatable_attribute(): void { $this->expectException(\Arcanedev\Localization\Exceptions\UntranslatableAttributeException::class); $this->expectExceptionMessage("The attribute `untranslated` is untranslatable because it's not available in the translatable array: `name, slug`"); @@ -266,7 +270,7 @@ public function it_will_throw_an_exception_when_trying_to_translate_an_untransla } /** @test */ - public function it_can_use_accessors_on_translated_attributes() + public function it_can_use_accessors_on_translated_attributes(): void { $this->model->setTranslation('name', 'en', 'name'); @@ -274,7 +278,7 @@ public function it_can_use_accessors_on_translated_attributes() } /** @test */ - public function it_can_use_mutators_on_translated_attributes() + public function it_can_use_mutators_on_translated_attributes(): void { $this->model->setTranslation('slug', 'en', 'This is a mutated slug'); @@ -282,7 +286,7 @@ public function it_can_use_mutators_on_translated_attributes() } /** @test */ - public function it_can_set_multiple_translations_at_once() + public function it_can_set_multiple_translations_at_once(): void { $this->model->setTranslations('name', $translations = [ 'nl' => 'hallo', @@ -295,14 +299,14 @@ public function it_can_set_multiple_translations_at_once() } /** @test */ - public function it_can_check_if_an_attribute_is_translatable() + public function it_can_check_if_an_attribute_is_translatable(): void { static::assertTrue($this->model->isTranslatableAttribute('name')); static::assertFalse($this->model->isTranslatableAttribute('untranslated')); } /** @test */ - public function it_can_check_if_an_attribute_has_translation() + public function it_can_check_if_an_attribute_has_translation(): void { $this->model->setTranslation('name', 'en', 'Hello') ->setTranslation('name', 'fr', 'Salut') @@ -317,7 +321,7 @@ public function it_can_check_if_an_attribute_has_translation() } /** @test */ - public function it_will_fire_an_event_when_a_translation_has_been_set() + public function it_will_fire_an_event_when_a_translation_has_been_set(): void { $this->expectsEvents([TranslationHasBeenSet::class]); @@ -327,7 +331,7 @@ public function it_will_fire_an_event_when_a_translation_has_been_set() } /** @test */ - public function it_will_return_fallback_locale_translation_when_getting_an_empty_translation_from_the_locale() + public function it_will_return_fallback_locale_translation_when_getting_an_empty_translation_from_the_locale(): void { $this->setFallbackLocale('en'); @@ -339,7 +343,7 @@ public function it_will_return_fallback_locale_translation_when_getting_an_empty } /** @test */ - public function it_will_return_correct_translation_value_if_value_is_set_to_zero() + public function it_will_return_correct_translation_value_if_value_is_set_to_zero(): void { $this->model->setTranslation('name', 'en', '0') ->save(); @@ -348,7 +352,7 @@ public function it_will_return_correct_translation_value_if_value_is_set_to_zero } /** @test */ - public function it_will_not_return_fallback_value_if_value_is_set_to_zero() + public function it_will_not_return_fallback_value_if_value_is_set_to_zero(): void { $this->setFallbackLocale('en'); @@ -360,7 +364,7 @@ public function it_will_not_return_fallback_value_if_value_is_set_to_zero() } /** @test */ - public function it_will_not_remove_zero_value_of_other_locale_in_database() + public function it_will_not_remove_zero_value_of_other_locale_in_database(): void { $this->setFallbackLocale('en'); @@ -381,7 +385,7 @@ public function it_will_not_remove_zero_value_of_other_locale_in_database() * * @param string $locale */ - private function setFallbackLocale($locale) + private function setFallbackLocale($locale): void { $this->app['config']->set('app.fallback_locale', $locale); } diff --git a/tests/Utilities/LocalesManagerTest.php b/tests/Utilities/LocalesManagerTest.php index c279af2..92ac5a3 100644 --- a/tests/Utilities/LocalesManagerTest.php +++ b/tests/Utilities/LocalesManagerTest.php @@ -1,4 +1,8 @@ -localesManager); } /** @test */ - public function it_can_set_and_get_current_locale() + public function it_can_set_and_get_current_locale(): void { foreach ($this->supportedLocales as $locale) { $this->localesManager->setCurrentLocale($locale); @@ -63,7 +67,7 @@ public function it_can_set_and_get_current_locale() } /** @test */ - public function it_can_get_current_locale_entity() + public function it_can_get_current_locale_entity(): void { foreach ($this->supportedLocales as $locale) { $this->localesManager->setCurrentLocale($locale); @@ -76,7 +80,7 @@ public function it_can_get_current_locale_entity() } /** @test */ - public function it_can_get_all_locales() + public function it_can_get_all_locales(): void { $locales = $this->localesManager->getAllLocales(); @@ -89,7 +93,7 @@ public function it_can_get_all_locales() } /** @test */ - public function it_can_get_supported_locales() + public function it_can_get_supported_locales(): void { $supportedLocales = $this->localesManager->getSupportedLocales(); @@ -102,7 +106,7 @@ public function it_can_get_supported_locales() } /** @test */ - public function it_can_set_and_get_supported_locales() + public function it_can_set_and_get_supported_locales(): void { $supported = ['en', 'fr']; @@ -120,7 +124,7 @@ public function it_can_set_and_get_supported_locales() } /** @test */ - public function it_can_get_supported_locales_keys() + public function it_can_get_supported_locales_keys(): void { $supportedKeys = $this->localesManager->getSupportedLocalesKeys(); @@ -129,7 +133,7 @@ public function it_can_get_supported_locales_keys() } /** @test */ - public function it_can_get_current_locale_without_negotiator() + public function it_can_get_current_locale_without_negotiator(): void { $this->app['config']->set('localization.accept-language-header', false); @@ -143,7 +147,7 @@ public function it_can_get_current_locale_without_negotiator() } /** @test */ - public function it_can_get_default_or_current_locale() + public function it_can_get_default_or_current_locale(): void { $this->app['config']->set('localization.hide-default-in-url', false); @@ -165,7 +169,7 @@ public function it_can_get_default_or_current_locale() } /** @test */ - public function it_can_set_and_get_default_locale() + public function it_can_set_and_get_default_locale(): void { foreach ($this->supportedLocales as $locale) { $this->localesManager->setDefaultLocale($locale); @@ -175,7 +179,7 @@ public function it_can_set_and_get_default_locale() } /** @test */ - public function it_must_throw_unsupported_locale_exception_on_set_default_locale() + public function it_must_throw_unsupported_locale_exception_on_set_default_locale(): void { $this->expectException(\Arcanedev\Localization\Exceptions\UnsupportedLocaleException::class); $this->expectExceptionMessage("Laravel default locale [jp] is not in the `supported-locales` array."); @@ -184,7 +188,7 @@ public function it_must_throw_unsupported_locale_exception_on_set_default_locale } /** @test */ - public function it_must_throw_undefined_supported_locales_exception_on_set_with_empty_array() + public function it_must_throw_undefined_supported_locales_exception_on_set_with_empty_array(): void { $this->expectException(\Arcanedev\Localization\Exceptions\UndefinedSupportedLocalesException::class); diff --git a/tests/Utilities/NegotiatorTest.php b/tests/Utilities/NegotiatorTest.php index 8068310..e836797 100644 --- a/tests/Utilities/NegotiatorTest.php +++ b/tests/Utilities/NegotiatorTest.php @@ -1,9 +1,12 @@ -negotiator); } - /** @test */ - public function it_can_negotiate_supported_accepted_languages_header() + /** + * @test + * + * @dataProvider provideAcceptedLanguages + * + * @param string $locale + * @param string $acceptLanguages + */ + public function it_can_negotiate_supported_accepted_languages_header(string $locale, string $acceptLanguages): void + { + $request = $this->mockRequestWithAcceptLanguage($acceptLanguages); + + static::assertSame($locale, $this->negotiator->negotiate($request)); + } + + public function provideAcceptedLanguages(): array { - $languages = [ + return [ ['en', 'en-us, en; q=0.5'], ['fr', 'en; q=0.5, fr; q=1.0'], ['es', 'es; q=0.6, en; q=0.5, fr; q=0.5'], ]; - - foreach ($languages as $language) { - /** @var Request $request */ - $request = $this->mockRequestWithAcceptLanguage($language[1])->reveal(); - - static::assertSame($language[0], $this->negotiator->negotiate($request)); - } } /** @test */ - public function it_can_negotiate_any_accepted_languages_header() + public function it_can_negotiate_any_accepted_languages_header(): void { /** @var Request $request */ - $request = $this->mockRequestWithAcceptLanguage('*')->reveal(); + $request = $this->mockRequestWithAcceptLanguage('*'); static::assertSame('en', $this->negotiator->negotiate($request)); } /** @test */ - public function it_can_negotiate_supported_http_accepted_languages_server() + public function it_can_negotiate_supported_http_accepted_languages_server(): void { /** @var Request $request */ - $request = $this->mockRequestWithHttpAcceptLanguage('fr;q=0.8,en;q=0.4', 'jp; q=1.0')->reveal(); + $request = $this->mockRequestWithHttpAcceptLanguage('fr;q=0.8,en;q=0.4', 'jp; q=1.0'); static::assertSame('fr', $this->negotiator->negotiate($request)); - $request = $this->mockRequestWithHttpAcceptLanguage('fr;q=0.8,en;q=0.4', '*/*')->reveal(); + $request = $this->mockRequestWithHttpAcceptLanguage('fr;q=0.8,en;q=0.4', '*/*'); static::assertSame('fr', $this->negotiator->negotiate($request)); } /** @test */ - public function it_can_negotiate_supported_remote_host_server() + public function it_can_negotiate_supported_remote_host_server(): void { /** @var Request $request */ $request = $this->mockRequestWithRemoteHostServer( 'http://www.omelette-au-fromage.fr', 'ar;q=0.8,sv;q=0.4', 'jp; q=1.0' - )->reveal(); + ); static::assertSame('fr', $this->negotiator->negotiate($request)); } /** @test */ - public function it_can_negotiate_unsupported_remote_host_server() + public function it_can_negotiate_unsupported_remote_host_server(): void { /** @var Request $request */ $request = $this->mockRequestWithRemoteHostServer( 'http://www.sushi.jp', 'ar;q=0.8,sv;q=0.4', 'jp; q=1.0' - )->reveal(); + ); static::assertSame('en', $this->negotiator->negotiate($request)); } /** @test */ - public function it_can_negotiate_undefined_remote_host_server() + public function it_can_negotiate_undefined_remote_host_server(): void { /** @var Request $request */ $request = $this->mockRequestWithRemoteHostServer( null, 'ar;q=0.8,sv;q=0.4', 'jp; q=1.0' - )->reveal(); + ); static::assertSame('en', $this->negotiator->negotiate($request)); } @@ -137,13 +147,11 @@ public function it_can_negotiate_undefined_remote_host_server() /** * Mock request. * - * @return ObjectProphecy + * @return object */ private function mockRequest() { - $request = $this->prophesize(Request::class); - - return $request; + return $this->mock(Request::class); } /** @@ -151,35 +159,38 @@ private function mockRequest() * * @param string $acceptLanguages * - * @return ObjectProphecy + * @return object */ private function mockRequestWithAcceptLanguage($acceptLanguages) { - $request = $this->mockRequest(); - - $request->header('Accept-Language') - ->willReturn($acceptLanguages) - ->shouldBeCalled(); - - return $request; + return tap( + $this->mockRequest(), + function ($request) use ($acceptLanguages) { + $request->shouldReceive('header') + ->withArgs(['Accept-Language']) + ->andReturn($acceptLanguages); + } + ); } /** * Mock request with HTTP Accept Language server. * * @param string $acceptLanguages + * @param string $httpAcceptLanguages * - * @return ObjectProphecy + * @return object */ private function mockRequestWithHttpAcceptLanguage($httpAcceptLanguages, $acceptLanguages) { - $request = $this->mockRequestWithAcceptLanguage($acceptLanguages); - - $request->server('HTTP_ACCEPT_LANGUAGE') - ->willReturn($httpAcceptLanguages) - ->shouldBeCalled(); - - return $request; + return tap( + $this->mockRequestWithAcceptLanguage($acceptLanguages), + function ($request) use ($httpAcceptLanguages) { + $request->shouldReceive('server') + ->withArgs(['HTTP_ACCEPT_LANGUAGE']) + ->andReturn($httpAcceptLanguages); + } + ); } /** @@ -189,16 +200,17 @@ private function mockRequestWithHttpAcceptLanguage($httpAcceptLanguages, $accept * @param string $httpAcceptLanguages * @param string $acceptLanguages * - * @return ObjectProphecy + * @return object */ private function mockRequestWithRemoteHostServer($remoteHost, $httpAcceptLanguages, $acceptLanguages) { - $request = $this->mockRequestWithHttpAcceptLanguage($httpAcceptLanguages, $acceptLanguages); - - $request->server('REMOTE_HOST') - ->willReturn($remoteHost) - ->shouldBeCalled(); - - return $request; + return tap( + $this->mockRequestWithHttpAcceptLanguage($httpAcceptLanguages, $acceptLanguages), + function ($request) use ($remoteHost) { + $request->shouldReceive('server') + ->withArgs(['REMOTE_HOST']) + ->andReturn($remoteHost); + } + ); } } diff --git a/tests/Utilities/UrlTest.php b/tests/Utilities/UrlTest.php index 322c6a8..1f1dc0b 100644 --- a/tests/Utilities/UrlTest.php +++ b/tests/Utilities/UrlTest.php @@ -1,4 +1,8 @@ - new \Arcanedev\Localization\Tests\Stubs\User('admin'),