Skip to content

Commit 3e70283

Browse files
Tests added
1 parent d542676 commit 3e70283

File tree

10 files changed

+172
-505
lines changed

10 files changed

+172
-505
lines changed

composer.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@
5353
},
5454
"require": {
5555
"php": "^8.2",
56+
"illuminate/config": "*",
5657
"illuminate/console": "^11.0|^12.0",
5758
"illuminate/filesystem": "^11.0|^12.0",
58-
"illuminate/support": "^11.0|^12.0",
59-
"laravel/framework": "^11.0|^12.0",
6059
"tedivm/jshrink": "~1.0"
6160
},
6261
"require-dev": {
63-
"phpunit/phpunit": "^11.0"
62+
"pestphp/pest": "^3.7",
63+
"orchestra/testbench": "^9.0|^10.0"
6464
},
6565
"autoload": {
6666
"psr-4": {
@@ -79,5 +79,10 @@
7979
"Mariuzzo\\LaravelJsLocalization\\LaravelJsLocalizationServiceProvider"
8080
]
8181
}
82+
},
83+
"config": {
84+
"allow-plugins": {
85+
"pestphp/pest-plugin": true
86+
}
8287
}
8388
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
* 'forum/thread',
1212
* ],
1313
*/
14-
'messages' => [
15-
16-
],
14+
'messages' => [],
1715

1816
/*
1917
* The default path to use for the generated javascript.

phpunit.xml

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit
3-
backupGlobals="false"
4-
backupStaticAttributes="false"
5-
bootstrap="vendor/autoload.php"
6-
colors="true"
7-
convertErrorsToExceptions="true"
8-
convertNoticesToExceptions="true"
9-
convertWarningsToExceptions="true"
10-
processIsolation="false"
11-
stopOnFailure="false"
12-
verbose="true">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
>
137
<testsuites>
14-
<testsuite name="Package">
15-
<directory suffix="Test.php">./tests/specs</directory>
8+
<testsuite name="Test Suite">
9+
<directory suffix="Test.php">./tests</directory>
1610
</testsuite>
1711
</testsuites>
18-
<filter>
19-
<whitelist>
12+
<source>
13+
<include>
2014
<directory suffix=".php">./src</directory>
21-
</whitelist>
22-
</filter>
15+
</include>
16+
</source>
2317
</phpunit>

src/LaravelJsLocalizationServiceProvider.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function boot() {
3535
*/
3636
public function register() {
3737
$this->registerConfig();
38-
$this->registerLangJsCommand();
38+
$this->registerLangJsGenerator();
3939
}
4040

4141
protected function registerConfig(): void {
@@ -54,17 +54,15 @@ protected function publishConfigs(): void {
5454
], 'localization-js-config');
5555
}
5656

57-
protected function registerLangJsCommand(): void {
58-
$this->app->singleton(LangJsCommand::class, function ($app) {
57+
protected function registerLangJsGenerator(): void {
58+
$this->app->singleton(LangJsGenerator::class, function ($app) {
5959
$app = $this->app;
6060

6161
$files = $app['files'];
6262
$langs = $app['path.base'] . '/lang';
6363

6464
$messages = $app['config']->get('localization-js.messages');
65-
$generator = new LangJsGenerator($files, $langs, $messages);
66-
67-
return new LangJsCommand($generator);
65+
return new LangJsGenerator($files, $langs, $messages);
6866
});
6967
}
7068

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
beforeEach(function () {
4+
$this->testPath = __DIR__ . '/..';
5+
$this->rootPath = __DIR__ . '/../..';
6+
$this->outputFilePath = "$this->testPath/output/lang.js";
7+
$this->langPath = "$this->testPath/fixtures/lang";
8+
});
9+
10+
test('it generates the JS language file successfully', function () {
11+
$this->artisan('lang:js', ['target' => $this->outputFilePath])
12+
->expectsOutputToContain('Created:')
13+
->assertExitCode(0);
14+
15+
$this->assertFileExists($this->outputFilePath);
16+
17+
$template = "$this->rootPath/src/Generators/Templates/langjs_with_messages.js";
18+
$this->assertFileExists($template);
19+
$this->assertFileNotEquals($template, $this->outputFilePath);
20+
21+
$this->cleanupOutputDirectory($this->testPath);
22+
});
23+
24+
test('template should have handlebars', function () {
25+
$template = "$this->rootPath/src/Generators/Templates/langjs_with_messages.js";
26+
$this->assertFileExists($template);
27+
28+
$contents = file_get_contents($template);
29+
$this->assertNotEmpty($contents);
30+
$this->assertHasHandlebars('messages', $contents);
31+
$this->assertHasHandlebars('langjs', $contents);
32+
});
33+
34+
test('output should not have handlebars', function () {
35+
$this->artisan('lang:js', ['target' => $this->outputFilePath])
36+
->expectsOutputToContain('Created:')
37+
->assertExitCode(0);
38+
39+
$this->assertFileExists($this->outputFilePath);
40+
41+
$contents = file_get_contents($this->outputFilePath);
42+
$this->assertNotEmpty($contents);
43+
$this->assertHasNotHandlebars('messages', $contents);
44+
$this->assertHasNotHandlebars('langjs', $contents);
45+
46+
$this->cleanupOutputDirectory($this->testPath);
47+
});

tests/Pest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Test Case
6+
|--------------------------------------------------------------------------
7+
|
8+
| The closure you provide to your test functions is always bound to a specific PHPUnit test
9+
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
10+
| need to change it using the "pest()" function to bind a different classes or traits.
11+
|
12+
*/
13+
14+
pest()->extend(Mariuzzo\LaravelJsLocalization\Tests\TestCase::class);
15+
16+
/*
17+
|--------------------------------------------------------------------------
18+
| Functions
19+
|--------------------------------------------------------------------------
20+
|
21+
*/

tests/TestCase.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Mariuzzo\LaravelJsLocalization\Tests;
4+
5+
use Illuminate\Support\Facades\File;
6+
use Mariuzzo\LaravelJsLocalization\LaravelJsLocalizationServiceProvider;
7+
use Orchestra\Testbench\TestCase as Orchestra;
8+
9+
class TestCase extends Orchestra {
10+
11+
protected function getPackageProviders($app) {
12+
return [
13+
LaravelJsLocalizationServiceProvider::class,
14+
];
15+
}
16+
17+
/**
18+
* @param string $handle
19+
* @param string $contents
20+
*/
21+
protected function assertHasHandlebars($handle, $contents) {
22+
$this->assertEquals(1, preg_match('/\'\{(\s)' . preg_quote($handle) . '(\s)\}\'/', $contents));
23+
}
24+
25+
/**
26+
* @param string $handle
27+
* @param string $contents
28+
*/
29+
protected function assertHasNotHandlebars($handle, $contents) {
30+
$this->assertEquals(0, preg_match('/\'\{(\s)' . preg_quote($handle) . '(\s)\}\'/', $contents));
31+
}
32+
33+
/**
34+
* Cleanup output directory after tests.
35+
*/
36+
protected function cleanupOutputDirectory($testpath) {
37+
$files = File::files("{$testpath}/output");
38+
foreach ($files as $file) {
39+
File::delete($file);
40+
}
41+
}
42+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Config;
4+
use Mariuzzo\LaravelJsLocalization\Commands\LangJsCommand;
5+
use Mariuzzo\LaravelJsLocalization\Generators\LangJsGenerator;
6+
use Mariuzzo\LaravelJsLocalization\LaravelJsLocalizationServiceProvider;
7+
8+
it('merges configuration correctly', function () {
9+
expect(Config::has('localization-js'))->toBeTrue();
10+
});
11+
12+
it('registers the LangJsGenerator service', function () {
13+
expect(app()->bound(LangJsGenerator::class))->toBeTrue()
14+
->and(app(LangJsGenerator::class))->toBeInstanceOf(LangJsGenerator::class);
15+
});
16+
17+
it('registers the command', function () {
18+
$this->app->singleton(LangJsCommand::class, function ($app) {
19+
return new LangJsCommand();
20+
});
21+
22+
expect(app()->bound(LangJsCommand::class))->toBeTrue()
23+
->and(app(LangJsCommand::class))->toBeInstanceOf(LangJsCommand::class);
24+
});
25+
26+
it('provides correct services', function () {
27+
$provider = app()->getProvider(LaravelJsLocalizationServiceProvider::class);
28+
29+
expect($provider->provides())->toContain(LangJsCommand::class);
30+
});
31+
32+
it('publishes configuration file', function () {
33+
$provider = new LaravelJsLocalizationServiceProvider(app());
34+
$provider->boot();
35+
36+
expect(LaravelJsLocalizationServiceProvider::$publishGroups)
37+
->toHaveKey('localization-js-config');
38+
});

0 commit comments

Comments
 (0)