Skip to content

Commit

Permalink
chore: Replace pest by PHPUnit
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos committed Apr 29, 2024
1 parent 203d6b4 commit c640ff8
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 206 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ jobs:
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: Execute tests
run: vendor/bin/pest
run: vendor/bin/phpunit
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.idea
.vscode
.phpunit.result.cache
.phpunit.cache
build
composer.lock
coverage
Expand Down
16 changes: 11 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
37 changes: 21 additions & 16 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
executionOrder="depends,defects"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
displayDetailsOnTestsThatTriggerWarnings="true"
failOnRisky="true"
failOnWarning="true">
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
15 changes: 0 additions & 15 deletions tests/Pest.php

This file was deleted.

13 changes: 4 additions & 9 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
49 changes: 23 additions & 26 deletions tests/unit/CurrentPathLocaleTest.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
<?php

use ElaborateCode\JigsawLocalization\Mocks\PageMock;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;

it('returns DEFAULT_LOCALE code', function () {
expect([
current_path_locale((new PageMock)->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'],
];
}
}
28 changes: 17 additions & 11 deletions tests/unit/LocalePathTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<?php

use ElaborateCode\JigsawLocalization\Mocks\PageMock;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;

it('sets path on DEFAULT_LOCALE for partial path', function () {
$page = new PageMock;
final class LocalePathTest extends TestCase
{
#[DataProvider('providerLocalePath')]
public function testLocalePath(string $path, ?string $locale, string $expected) {
$actual = locale_path($this->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'],
];
}
}
34 changes: 19 additions & 15 deletions tests/unit/LocaleUrlTest.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
<?php

use ElaborateCode\JigsawLocalization\Mocks\PageMock;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;

it('sets URL with base path on DEFAULT_LOCALE for partial path', function () {
$this->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'],
];
}
}
40 changes: 24 additions & 16 deletions tests/unit/TransHelperTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
<?php

use ElaborateCode\JigsawLocalization\Mocks\PageMock;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;

it('translates', function () {
$page = (new PageMock)
->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'],
];
}
}
74 changes: 34 additions & 40 deletions tests/unit/TranslatePathTest.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,36 @@
<?php

use ElaborateCode\JigsawLocalization\Mocks\PageMock;

test('path es => 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'],
];
}
}
Loading

0 comments on commit c640ff8

Please sign in to comment.