diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 928ed06..a5de793 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -38,4 +38,4 @@ jobs: composer update --${{ matrix.stability }} --prefer-dist --no-interaction - name: Execute tests - run: vendor/bin/pest + run: vendor/bin/phpunit diff --git a/.gitignore b/.gitignore index 83c9b9f..76ab5ca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ .idea +.vscode .phpunit.result.cache +.phpunit.cache build composer.lock coverage diff --git a/composer.json b/composer.json index 1a9a51f..ab4a1f4 100644 --- a/composer.json +++ b/composer.json @@ -14,8 +14,8 @@ "elaborate-code/php-json-tongue": "^1.0" }, "require-dev": { + "bamarni/composer-bin-plugin": "^1.8", "laravel/pint": "^1.2", - "pestphp/pest": "^1.22", "phpstan/extension-installer": "^1.1", "phpstan/phpstan-deprecation-rules": "^1.0", "phpstan/phpstan-phpunit": "^1.0", @@ -36,16 +36,22 @@ } }, "scripts": { + "bin": "echo 'bin not installed'", "analyse": "vendor/bin/phpstan analyse", - "test": "vendor/bin/pest", - "test-coverage": "vendor/bin/pest --coverage", + "test": "vendor/bin/phpunit --colors=always", "format": "vendor/bin/pint" }, "config": { "sort-packages": true, "allow-plugins": { - "pestphp/pest-plugin": true, - "phpstan/extension-installer": true + "phpstan/extension-installer": true, + "bamarni/composer-bin-plugin": true + } + }, + "extra": { + "bamarni-bin": { + "bin-links": true, + "forward-command": true } }, "minimum-stability": "stable", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 8f4b58c..3d66d16 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,18 +1,23 @@ - - - - ./tests - - - - - ./app - ./src - - + + + + ./tests + + + + + ./src + + diff --git a/tests/Pest.php b/tests/Pest.php deleted file mode 100644 index 2f77bf4..0000000 --- a/tests/Pest.php +++ /dev/null @@ -1,15 +0,0 @@ -in('unit/LocaleUrlTest.php'); -uses(Tests\TestCase::class)->in('unit/TranslateUrlTest.php'); diff --git a/tests/TestCase.php b/tests/TestCase.php index 8b9f315..aeb46ca 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,25 +2,20 @@ namespace Tests; +use ElaborateCode\JigsawLocalization\Mocks\PageMock; use PHPUnit\Framework\TestCase as BaseTestCase; use TightenCo\Jigsaw\Container; -// use TightenCo\Jigsaw\File\ConfigFile; - class TestCase extends BaseTestCase { + public PageMock $pageData; public $app; protected function setUp(): void { parent::setUp(); - $this->app = new Container; - - // $this->app->bind('config', function ($c) use ($cachePath) { - // $config = (new ConfigFile($c['cwd'] . '/config.php', $c['cwd'] . '/helpers.php'))->config; - // $config->put('view.compiled', $cachePath); - // return $config; - // }); + $this->pageData = new PageMock(); + $this->app = Container::getInstance(); } } diff --git a/tests/unit/CurrentPathLocaleTest.php b/tests/unit/CurrentPathLocaleTest.php index 135fd74..a1e9f3e 100644 --- a/tests/unit/CurrentPathLocaleTest.php +++ b/tests/unit/CurrentPathLocaleTest.php @@ -1,30 +1,27 @@ setPath('/')), - current_path_locale((new PageMock)->setPath('/blog')), - ]) - ->each - ->toBe(packageDefaultLocale()); -}); +final class CurrentPathLocaleTest extends TestCase +{ + #[DataProvider('providerLanguageCode')] + public function testLanguageCode($path, $expected): void + { + $this->pageData->setPath($path); + $actual = current_path_locale($this->pageData); + $this->assertEquals($expected, $actual); + } -it('returns language code', function () { - expect([ - current_path_locale((new PageMock)->setPath('/es')), - current_path_locale((new PageMock)->setPath('/es/blog')), - ]) - ->each - ->toBe('es'); -}); - -it('returns language+region code', function () { - expect([ - current_path_locale((new PageMock)->setPath('/haw-US')), - current_path_locale((new PageMock)->setPath('/haw-US/blog')), - ]) - ->each - ->toBe('haw-US'); -}); + public static function providerLanguageCode(): array + { + return [ + ['/', packageDefaultLocale()], + ['/blog', packageDefaultLocale()], + ['/es', 'es'], + ['/es/blog', 'es'], + ['/raw-US', 'raw-US'], + ['/raw-US/blog', 'raw-US'], + ]; + } +} diff --git a/tests/unit/LocalePathTest.php b/tests/unit/LocalePathTest.php index d29b49c..59ad31e 100644 --- a/tests/unit/LocalePathTest.php +++ b/tests/unit/LocalePathTest.php @@ -1,15 +1,21 @@ pageData, $path, $locale); + $this->assertEquals($expected, $actual); + } - expect(locale_path($page, 'blog'))->toBe('/blog'); -}); - -it('sets path on locale for partial path', function () { - $page = new PageMock; - - expect(locale_path($page, 'blog', 'ar'))->toBe('/ar/blog'); -}); + public static function providerLocalePath(): array + { + return [ + ['blog', null, '/blog'], + ['blog', 'ar', '/ar/blog'], + ]; + } +} diff --git a/tests/unit/LocaleUrlTest.php b/tests/unit/LocaleUrlTest.php index 24aa34b..067f044 100644 --- a/tests/unit/LocaleUrlTest.php +++ b/tests/unit/LocaleUrlTest.php @@ -1,19 +1,23 @@ app->config = collect(['baseUrl' => 'https://elaboratecode.com/packages']); +final class LocaleUrlTest extends TestCase +{ + #[DataProvider('providerLocaleUrl')] + public function testLocaleUrl(string $path, ?string $locale, string $expected): void + { + $this->app->config = collect(['baseUrl' => 'https://elaboratecode.com/packages']); + $actual = locale_url($this->pageData, $path, $locale); + $this->assertEquals($expected, $actual); + } - $page = new PageMock; - - expect(locale_url($page, 'blog'))->toBe('https://elaboratecode.com/packages/blog'); -}); - -it('sets URL with base path on locale for partial path', function () { - $this->app->config = collect(['baseUrl' => 'https://elaboratecode.com/packages']); - - $page = new PageMock; - - expect(locale_url($page, 'blog', 'ar'))->toBe('https://elaboratecode.com/packages/ar/blog'); -}); + public static function providerLocaleUrl(): array + { + return [ + ['blog', null, 'https://elaboratecode.com/packages/blog'], + ['blog', 'ar', 'https://elaboratecode.com/packages/ar/blog'], + ]; + } +} diff --git a/tests/unit/TransHelperTest.php b/tests/unit/TransHelperTest.php index 948fd60..75328b5 100644 --- a/tests/unit/TransHelperTest.php +++ b/tests/unit/TransHelperTest.php @@ -1,20 +1,28 @@ setLocalization([ - 'es' => [ - 'Cat' => 'Gato', - ], - ]); +final class TransHelperTest extends TestCase +{ + #[DataProvider('provider__')] + public function test__(string $text, ?string $locale, string $expected): void + { + $this->pageData + ->setLocalization([ + 'es' => [ + 'Cat' => 'Gato', + ], + ]); + $actual = __($this->pageData, $text, $locale); + $this->assertEquals($expected, $actual); + } - expect(__($page, 'Cat', 'es'))->toBe('Gato'); -}); - -it('returns the given text when no translation is found', function () { - $page = (new PageMock); - - expect(__($page, 'Dog', 'es'))->toBe('Dog'); -}); + public static function provider__(): array + { + return [ + ['Cat', 'es', 'Gato'], + ['Dog', 'es', 'Dog'], + ]; + } +} diff --git a/tests/unit/TranslatePathTest.php b/tests/unit/TranslatePathTest.php index b05255e..168ff71 100644 --- a/tests/unit/TranslatePathTest.php +++ b/tests/unit/TranslatePathTest.php @@ -1,42 +1,36 @@ ar', function () { - $page = (new PageMock)->setPath('/es/blog'); - - expect(translate_path($page, 'ar'))->toBe('/ar/blog'); -}); - -test('path ar <=> fr-CA', function () { - expect(translate_path((new PageMock)->setPath('/ar/blog'), 'fr-CA'))->toBe('/fr-CA/blog'); - expect(translate_path((new PageMock)->setPath('/fr-CA/blog'), 'fr-CA'))->toBe('/fr-CA/blog'); -}); - -test('path ar <=> haw-US', function () { - expect(translate_path((new PageMock)->setPath('/ar/blog'), 'haw-US'))->toBe('/haw-US/blog'); - expect(translate_path((new PageMock)->setPath('/haw-US/blog'), 'haw-US'))->toBe('/haw-US/blog'); -}); - -test('path haw-US <=> fr-CA', function () { - expect(translate_path((new PageMock)->setPath('/fr-CA/blog'), 'haw-US'))->toBe('/haw-US/blog'); - expect(translate_path((new PageMock)->setPath('/haw-US/blog'), 'fr-CA'))->toBe('/fr-CA/blog'); -}); - -test('path from DEFAULT_LOCALE', function () { - expect(translate_path((new PageMock)->setPath('/blog'), 'ar'))->toBe('/ar/blog'); - expect(translate_path((new PageMock)->setPath('/blog'), 'en-UK'))->toBe('/en-UK/blog'); - expect(translate_path((new PageMock)->setPath('/blog'), 'haw-US'))->toBe('/haw-US/blog'); - expect(translate_path((new PageMock)->setPath('/blog'), packageDefaultLocale()))->toBe('/blog'); -}); - -test('path to DEFAULT_LOCALE', function () { - expect([ - translate_path((new PageMock)->setPath('/ar/blog')), - translate_path((new PageMock)->setPath('/en-UK/blog')), - translate_path((new PageMock)->setPath('/haw-US/blog')), - translate_path((new PageMock)->setPath('/blog')), - ]) - ->each - ->toBe('/blog'); -}); +use PHPUnit\Framework\Attributes\DataProvider; +use Tests\TestCase; + +final class TranslatePathTest extends TestCase +{ + #[DataProvider('providerTranslatePath')] + public function testTranslatePath(string $path, ?string $locale, string $expected): void + { + $this->pageData->setPath($path); + $actual = translate_path($this->pageData, $locale); + $this->assertEquals($expected, $actual); + } + + public static function providerTranslatePath(): array + { + return [ + ['/es/blog', 'ar', '/ar/blog'], + ['/ar/blog', 'fr-CA', '/fr-CA/blog'], + ['/fr-CA/blog', 'fr-CA', '/fr-CA/blog'], + ['/ar/blog', 'haw-US', '/haw-US/blog'], + ['/haw-US/blog', 'haw-US', '/haw-US/blog'], + ['/fr-CA/blog', 'haw-US', '/haw-US/blog'], + ['/haw-US/blog', 'fr-CA', '/fr-CA/blog'], + ['/blog', 'ar', '/ar/blog'], + ['/blog', 'en-UK', '/en-UK/blog'], + ['/blog', 'haw-US', '/haw-US/blog'], + ['/blog', packageDefaultLocale(), '/blog'], + ['/ar/blog', null, '/blog'], + ['/en-UK/blog', null, '/blog'], + ['/haw-US/blog', null, '/blog'], + ['/blog', null, '/blog'], + ]; + } +} diff --git a/tests/unit/TranslateUrlTest.php b/tests/unit/TranslateUrlTest.php index 540ca09..7a2918a 100644 --- a/tests/unit/TranslateUrlTest.php +++ b/tests/unit/TranslateUrlTest.php @@ -1,54 +1,36 @@ ar', function () { - $this->app->config = collect(['baseUrl' => 'https://elaboratecode.com/packages']); - - $page = (new PageMock)->setPath('/es/blog'); - - expect(translate_url($page, 'ar'))->toBe('https://elaboratecode.com/packages/ar/blog'); -}); - -test('URL with base path ar <=> fr-CA', function () { - $this->app->config = collect(['baseUrl' => 'https://elaboratecode.com/packages']); - - expect(translate_url((new PageMock)->setPath('/ar/blog'), 'fr-CA'))->toBe('https://elaboratecode.com/packages/fr-CA/blog'); - expect(translate_url((new PageMock)->setPath('/fr-CA/blog'), 'fr-CA'))->toBe('https://elaboratecode.com/packages/fr-CA/blog'); -}); - -test('URL with base path ar <=> haw-US', function () { - $this->app->config = collect(['baseUrl' => 'https://elaboratecode.com/packages']); - - expect(translate_url((new PageMock)->setPath('/ar/blog'), 'haw-US'))->toBe('https://elaboratecode.com/packages/haw-US/blog'); - expect(translate_url((new PageMock)->setPath('/haw-US/blog'), 'haw-US'))->toBe('https://elaboratecode.com/packages/haw-US/blog'); -}); - -test('URL with base path haw-US <=> fr-CA', function () { - $this->app->config = collect(['baseUrl' => 'https://elaboratecode.com/packages']); - - expect(translate_url((new PageMock)->setPath('/fr-CA/blog'), 'haw-US'))->toBe('https://elaboratecode.com/packages/haw-US/blog'); - expect(translate_url((new PageMock)->setPath('/haw-US/blog'), 'fr-CA'))->toBe('https://elaboratecode.com/packages/fr-CA/blog'); -}); - -test('URL with base path from DEFAULT_LOCALE', function () { - $this->app->config = collect(['baseUrl' => 'https://elaboratecode.com/packages']); - - expect(translate_url((new PageMock)->setPath('/blog'), 'ar'))->toBe('https://elaboratecode.com/packages/ar/blog'); - expect(translate_url((new PageMock)->setPath('/blog'), 'en-UK'))->toBe('https://elaboratecode.com/packages/en-UK/blog'); - expect(translate_url((new PageMock)->setPath('/blog'), 'haw-US'))->toBe('https://elaboratecode.com/packages/haw-US/blog'); - expect(translate_url((new PageMock)->setPath('/blog'), packageDefaultLocale()))->toBe('https://elaboratecode.com/packages/blog'); -}); - -test('URL with base path to DEFAULT_LOCALE', function () { - $this->app->config = collect(['baseUrl' => 'https://elaboratecode.com/packages']); - - expect([ - translate_url((new PageMock)->setPath('/ar/blog')), - translate_url((new PageMock)->setPath('/en-UK/blog')), - translate_url((new PageMock)->setPath('/haw-US/blog')), - translate_url((new PageMock)->setPath('/blog')), - ]) - ->each - ->toBe('https://elaboratecode.com/packages/blog'); -}); +use PHPUnit\Framework\Attributes\DataProvider; +use Tests\TestCase; + +final class TranslateUrlTest extends TestCase +{ + #[DataProvider('providerTranslateUrl')] + public function testTranslateUrl(string $path, ?string $locale, string $expected): void + { + $this->pageData->setPath($path); + $actual = translate_url($this->pageData, $locale); + $this->assertEquals($expected, $actual); + } + + public static function providerTranslateUrl(): array + { + return [ + ['/es/blog', 'ar', 'https://elaboratecode.com/packages/ar/blog'], + ['/ar/blog', 'fr-CA', 'https://elaboratecode.com/packages/fr-CA/blog'], + ['/fr-CA/blog', 'fr-CA', 'https://elaboratecode.com/packages/fr-CA/blog'], + ['/ar/blog', 'haw-US', 'https://elaboratecode.com/packages/haw-US/blog'], + ['/haw-US/blog', 'haw-US', 'https://elaboratecode.com/packages/haw-US/blog'], + ['/fr-CA/blog', 'haw-US', 'https://elaboratecode.com/packages/haw-US/blog'], + ['/haw-US/blog', 'fr-CA', 'https://elaboratecode.com/packages/fr-CA/blog'], + ['/blog', 'ar', 'https://elaboratecode.com/packages/ar/blog'], + ['/blog', 'en-UK', 'https://elaboratecode.com/packages/en-UK/blog'], + ['/blog', 'haw-US', 'https://elaboratecode.com/packages/haw-US/blog'], + ['/blog', packageDefaultLocale(), 'https://elaboratecode.com/packages/blog'], + ['/ar/blog', null, 'https://elaboratecode.com/packages/blog'], + ['/en-UK/blog', null, 'https://elaboratecode.com/packages/blog'], + ['/haw-US/blog', null, 'https://elaboratecode.com/packages/blog'], + ['/blog', null, 'https://elaboratecode.com/packages/blog'], + ]; + } +} diff --git a/vendor-bin/phpunit/composer.json b/vendor-bin/phpunit/composer.json new file mode 100644 index 0000000..74d6bf5 --- /dev/null +++ b/vendor-bin/phpunit/composer.json @@ -0,0 +1,12 @@ +{ + "autoload-dev": { + "psr-4": { + "Tests\\": "tests", + "ElaborateCode\\JigsawLocalization\\Mocks\\": "tests/_Mocks" + } + }, + "require-dev": { + "phpunit/phpunit": "^11.1", + "mikey179/vfsstream": "^1.6" + } +}