Skip to content

Commit 81b3ab9

Browse files
author
AlexVakhovski
committed
Add unit tests
1 parent 203db88 commit 81b3ab9

9 files changed

+256
-12
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
"illuminate/routing": "^10.0|^11.0|^12.0"
2222
},
2323
"require-dev": {
24-
"pestphp/pest": "^2.28|^3.5",
25-
"phpstan/extension-installer": "^1.3",
26-
"laravel/pint": "^1.22"
24+
"laravel/pint": "^1.22",
25+
"mockery/mockery": "^1.6",
26+
"pestphp/pest": "^3.8",
27+
"phpstan/extension-installer": "^1.3"
2728
},
2829
"conflict": {
2930
"symfony/console": "<6.4"

src/LocalizeUrlGenerator.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,20 @@
1212

1313
namespace Alexwaha\Localize;
1414

15-
use Illuminate\Contracts\Config\Repository as Config;
15+
use Illuminate\Contracts\Config\Repository as ConfigContract;
1616
use Illuminate\Contracts\Routing\UrlGenerator as UrlGeneratorContract;
17-
use Illuminate\Routing\UrlGenerator;
1817
use Illuminate\Support\Str;
1918

2019
class LocalizeUrlGenerator
2120
{
22-
public Config $config;
23-
24-
/**
25-
* @var UrlGenerator
26-
*/
2721
public UrlGeneratorContract $urlGenerator;
2822

29-
public function __construct(Config $config, UrlGeneratorContract $urlGenerator)
23+
public ConfigContract $config;
24+
25+
public function __construct(UrlGeneratorContract $urlGenerator, ConfigContract $config)
3026
{
31-
$this->config = $config;
3227
$this->urlGenerator = $urlGenerator;
28+
$this->config = $config;
3329
}
3430

3531
public function generate(string $name, array $parameters = [], bool $paginated = false): string

src/Middleware/PaginatedMiddleware.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function handle(Request $request, Closure $next): Response
5454
unset($routeParams['page']);
5555

5656
$queryParams = $request->query();
57+
unset($queryParams['page']);
5758

5859
$url = $this->urlGenerator->route($canonicalRouteName, $routeParams);
5960

tests/Pest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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(Tests\TestCase::class)
15+
// ->use(Illuminate\Foundation\Testing\RefreshDatabase::class)
16+
// ->in('Feature');
17+
18+
/*
19+
|--------------------------------------------------------------------------
20+
| Expectations
21+
|--------------------------------------------------------------------------
22+
|
23+
| When you're writing tests, you often need to check that values meet certain conditions. The
24+
| "expect()" function gives you access to a set of "expectations" methods that you can use
25+
| to assert different things. Of course, you may extend the Expectation API at any time.
26+
|
27+
*/
28+
29+
expect()->extend('toBeOne', function () {
30+
return $this->toBe(1);
31+
});
32+
33+
/*
34+
|--------------------------------------------------------------------------
35+
| Functions
36+
|--------------------------------------------------------------------------
37+
|
38+
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
39+
| project that you don't want to repeat in every file. Here you can also expose helpers as
40+
| global functions to help you to reduce the number of lines of code in your test files.
41+
|
42+
*/
43+
44+
function something()
45+
{
46+
// ..
47+
}

tests/TestCase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use PHPUnit\Framework\TestCase as BaseTestCase;
6+
7+
abstract class TestCase extends BaseTestCase
8+
{
9+
// Nothing extra needed
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Alexwaha\Localize\LanguageProvider;
4+
use Illuminate\Config\Repository as Config;
5+
6+
it('can instantiate LanguageProvider and return languages', function () {
7+
$config = new Config([
8+
'app' => [
9+
'fallback_locale' => 'en',
10+
],
11+
]);
12+
13+
$languages = [
14+
['locale' => 'en', 'slug' => 'en'],
15+
['locale' => 'es', 'slug' => 'es'],
16+
];
17+
18+
$provider = new LanguageProvider($config, $languages);
19+
20+
$langs = $provider->getLanguages();
21+
22+
expect($langs)
23+
->toHaveCount(2)
24+
->and($langs[0]->getLocale())
25+
->toBe('en')
26+
->and($langs[1]->getSlug())
27+
->toBe('es');
28+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
use Alexwaha\Localize\LocalizeUrlGenerator;
4+
use Illuminate\Config\Repository;
5+
use Illuminate\Http\Request;
6+
use Illuminate\Routing\Route;
7+
use Illuminate\Routing\RouteCollection;
8+
use Illuminate\Routing\UrlGenerator;
9+
10+
it('can generate a localized URL for non-paginated route', function () {
11+
$routes = new RouteCollection;
12+
13+
$request = Request::create('/');
14+
$urlGenerator = new UrlGenerator($routes, $request);
15+
16+
$config = new Repository([
17+
'app' => [
18+
'locale' => 'en',
19+
],
20+
]);
21+
22+
$route = new Route(['GET'], '/en/home', ['as' => 'en.home']);
23+
$routes->add($route);
24+
25+
$localizer = new LocalizeUrlGenerator($urlGenerator, $config);
26+
27+
$url = $localizer->generate('home');
28+
29+
expect($url)->toBe('http://localhost/en/home');
30+
});
31+
32+
it('can generate a localized paginated URL when page > 1', function () {
33+
$routes = new RouteCollection;
34+
35+
$request = Request::create('/');
36+
$urlGenerator = new UrlGenerator($routes, $request);
37+
38+
$config = new Repository([
39+
'app' => [
40+
'locale' => 'en',
41+
],
42+
]);
43+
44+
$routes->add(new Route(['GET'], '/en/blog', ['as' => 'en.blog.index']));
45+
$routes->add(new Route(['GET'], '/en/blog/page/{page}', ['as' => 'en.blog.paginated']));
46+
47+
$localizer = new LocalizeUrlGenerator($urlGenerator, $config);
48+
49+
$url = $localizer->generate('blog.index', ['page' => 2]);
50+
51+
expect($url)->toBe('http://localhost/en/blog/page/2');
52+
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
use Alexwaha\Localize\Middleware\PaginatedMiddleware;
4+
use Illuminate\Http\Request;
5+
use Illuminate\Routing\Redirector;
6+
use Illuminate\Routing\Route;
7+
use Illuminate\Routing\RouteCollection;
8+
use Illuminate\Routing\UrlGenerator;
9+
use Symfony\Component\HttpFoundation\Response;
10+
11+
it('redirects to index route if page is 1 on paginated route', function () {
12+
$routes = new RouteCollection;
13+
14+
$routes->add(new Route(['GET'], '/blog', ['as' => 'blog.index']));
15+
$routes->add($paginatedRoute = new Route(['GET'], '/blog/page/{page?}', ['as' => 'blog.paginated']));
16+
17+
$request = Request::create('/blog/page/1', 'GET', ['page' => 1]);
18+
19+
$paginatedRoute->bind($request);
20+
$request->setRouteResolver(fn () => $paginatedRoute);
21+
22+
$urlGenerator = new UrlGenerator($routes, $request);
23+
$redirector = new Redirector($urlGenerator);
24+
25+
$middleware = new PaginatedMiddleware($urlGenerator, $redirector);
26+
27+
$response = $middleware->handle($request, function () {
28+
return new Response('HTTP_OK');
29+
});
30+
31+
expect($response->isRedirect())
32+
->toBeTrue()
33+
->and($response->getTargetUrl())
34+
->toBe('http://localhost/blog');
35+
});
36+
37+
it('does not redirect when page is greater than 1', function () {
38+
39+
$routes = new RouteCollection;
40+
41+
$routes->add(new Route(['GET'], '/blog', ['as' => 'blog.index']));
42+
$routes->add($paginatedRoute = new Route(['GET'], '/blog/page/{page?}', ['as' => 'blog.paginated']));
43+
44+
$request = Request::create('/blog/page/2', 'GET', ['page' => 2]);
45+
46+
$urlGenerator = new UrlGenerator($routes, $request);
47+
$redirector = new Redirector($urlGenerator);
48+
49+
$paginatedRoute->bind($request);
50+
$request->setRouteResolver(fn () => $paginatedRoute);
51+
52+
$middleware = new PaginatedMiddleware($urlGenerator, $redirector);
53+
54+
$response = $middleware->handle($request, function () {
55+
return new Response('HTTP_OK');
56+
});
57+
58+
expect($response->isOk())->toBeTrue();
59+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use Alexwaha\Localize\LanguageProvider;
4+
use Alexwaha\Localize\Middleware\SetLocaleMiddleware;
5+
use Illuminate\Config\Repository;
6+
use Illuminate\Foundation\Application;
7+
use Illuminate\Http\Request;
8+
use Symfony\Component\HttpFoundation\Response;
9+
10+
it('sets the locale based on URL segment using real Application', function () {
11+
$app = new Application(__DIR__);
12+
13+
$configArray = [
14+
'app' => [
15+
'fallback_locale' => 'en',
16+
'locale' => 'en',
17+
],
18+
];
19+
20+
$config = new Repository($configArray);
21+
22+
$app->instance('config', $config);
23+
24+
$app->instance('translator', new class
25+
{
26+
public function setLocale($locale) {}
27+
28+
public function getLocale(): string
29+
{
30+
return 'en';
31+
}
32+
});
33+
34+
$languages = [
35+
['locale' => 'en', 'slug' => 'en'],
36+
['locale' => 'es', 'slug' => 'es'],
37+
];
38+
39+
$languageProvider = new LanguageProvider($config, $languages);
40+
41+
$middleware = new SetLocaleMiddleware($app, $languageProvider);
42+
43+
$request = Request::create('/es/some-page', 'GET');
44+
45+
$middleware->handle($request, function () {
46+
return new Response('HTTP_OK');
47+
});
48+
49+
expect($app->getLocale())->toBe('es');
50+
});

0 commit comments

Comments
 (0)