Skip to content

Commit bc5090a

Browse files
committed
Refactor tests
1 parent d344747 commit bc5090a

File tree

3 files changed

+90
-92
lines changed

3 files changed

+90
-92
lines changed

tests/Feature/SetLocaleTest.php

Lines changed: 84 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use CodeZero\BrowserLocale\BrowserLocale;
66
use CodeZero\Localizer\Middleware\SetLocale;
77
use CodeZero\Localizer\Tests\TestCase;
8-
use Illuminate\Foundation\Testing\TestResponse;
98
use Illuminate\Support\Facades\App;
109
use Illuminate\Support\Facades\Config;
1110
use Illuminate\Support\Facades\Crypt;
@@ -14,137 +13,162 @@
1413

1514
class SetLocaleTest extends TestCase
1615
{
16+
protected $sessionKey;
17+
protected $cookieName;
18+
19+
/**
20+
* Setup the test environment.
21+
*
22+
* @return void
23+
*/
1724
protected function setUp()
1825
{
1926
parent::setUp();
2027

21-
TestResponse::macro('assertAppLocale', function ($locale) {
22-
return $this->assertSee('The locale is: ' . $locale);
23-
});
24-
25-
$this->withoutExceptionHandling();
26-
$this->setSupportedLocales(['en', 'nl', 'fr', 'de', 'es', 'it']);
28+
$this->sessionKey = Config::get('localizer.session-key');
29+
$this->cookieName = Config::get('localizer.cookie-name');
2730
}
2831

2932
/** @test */
3033
public function it_looks_for_a_locale_in_the_url_first()
3134
{
32-
$this->registerRoute('nl/some/route');
33-
35+
$this->setSupportedLocales(['en', 'nl', 'fr', 'de', 'es', 'it']);
3436
$this->setSessionLocale('fr');
3537
$this->setBrowserLocales('it');
3638
$this->setAppLocale('en');
37-
3839
$cookie = [$this->cookieName => Crypt::encrypt('de')];
3940

40-
$this->call('GET', 'nl/some/route', [], $cookie)
41-
->assertSessionHas($this->sessionKey, 'nl')
42-
->assertCookie($this->cookieName, 'nl')
43-
->assertAppLocale('nl');
41+
Route::get('nl/some/route', function () {
42+
return App::getLocale();
43+
})->middleware(['web', SetLocale::class]);
44+
45+
$response = $this->call('GET', 'nl/some/route', [], $cookie);
46+
47+
$response->assertSessionHas($this->sessionKey, 'nl');
48+
$response->assertCookie($this->cookieName, 'nl');
49+
$this->assertEquals('nl', $response->original);
4450
}
4551

4652
/** @test */
4753
public function you_can_configure_which_segment_to_use_as_locale()
4854
{
49-
$this->registerRoute('some/nl/route');
50-
51-
Config::set('localizer.url-segment', 2);
52-
55+
$this->setSupportedLocales(['en', 'nl', 'fr', 'de', 'es', 'it']);
5356
$this->setSessionLocale('fr');
5457
$this->setBrowserLocales('it');
5558
$this->setAppLocale('en');
56-
5759
$cookie = [$this->cookieName => Crypt::encrypt('de')];
5860

59-
$this->call('GET', 'some/nl/route', [], $cookie)
60-
->assertSessionHas($this->sessionKey, 'nl')
61-
->assertCookie($this->cookieName, 'nl')
62-
->assertAppLocale('nl');
61+
Config::set('localizer.url-segment', 2);
62+
63+
Route::get('some/nl/route', function () {
64+
return App::getLocale();
65+
})->middleware(['web', SetLocale::class]);
66+
67+
$response = $this->call('GET', 'some/nl/route', [], $cookie);
68+
69+
$response->assertSessionHas($this->sessionKey, 'nl');
70+
$response->assertCookie($this->cookieName, 'nl');
71+
$this->assertEquals('nl', $response->original);
6372
}
6473

6574
/** @test */
6675
public function it_looks_for_a_locale_in_the_session_if_not_found_in_the_url()
6776
{
68-
$this->registerRoute('some/route');
69-
77+
$this->setSupportedLocales(['en', 'nl', 'fr', 'de', 'es', 'it']);
7078
$this->setSessionLocale('fr');
7179
$this->setBrowserLocales('it');
7280
$this->setAppLocale('en');
73-
7481
$cookie = [$this->cookieName => Crypt::encrypt('de')];
7582

76-
$this->call('GET', 'some/route', [], $cookie)
77-
->assertSessionHas($this->sessionKey, 'fr')
78-
->assertCookie($this->cookieName, 'fr')
79-
->assertAppLocale('fr');
83+
Route::get('some/route', function () {
84+
return App::getLocale();
85+
})->middleware(['web', SetLocale::class]);
86+
87+
$response = $this->call('GET', 'some/route', [], $cookie);
88+
89+
$response->assertSessionHas($this->sessionKey, 'fr');
90+
$response->assertCookie($this->cookieName, 'fr');
91+
$this->assertEquals('fr', $response->original);
8092
}
8193

8294
/** @test */
8395
public function it_looks_for_a_locale_in_a_cookie_if_not_found_in_the_url_or_session()
8496
{
85-
$this->registerRoute('some/route');
86-
97+
$this->setSupportedLocales(['en', 'nl', 'fr', 'de', 'es', 'it']);
8798
$this->setSessionLocale(null);
8899
$this->setBrowserLocales('it');
89100
$this->setAppLocale('en');
90-
91101
$cookie = [$this->cookieName => Crypt::encrypt('de')];
92102

93-
$this->call('GET', 'some/route', [], $cookie)
94-
->assertSessionHas($this->sessionKey, 'de')
95-
->assertCookie($this->cookieName, 'de')
96-
->assertAppLocale('de');
103+
Route::get('some/route', function () {
104+
return App::getLocale();
105+
})->middleware(['web', SetLocale::class]);
106+
107+
$response = $this->call('GET', 'some/route', [], $cookie);
108+
109+
$response->assertSessionHas($this->sessionKey, 'de');
110+
$response->assertCookie($this->cookieName, 'de');
111+
$this->assertEquals('de', $response->original);
97112
}
98113

99114
/** @test */
100115
public function it_looks_for_a_locale_in_the_browser_if_not_found_in_the_url_or_session_or_cookie()
101116
{
102-
$this->registerRoute('some/route');
103-
117+
$this->setSupportedLocales(['en', 'nl', 'fr', 'de', 'es', 'it']);
104118
$this->setSessionLocale(null);
105119
$this->setBrowserLocales('it');
106120
$this->setAppLocale('en');
107-
108121
$cookie = [];
109122

110-
$this->call('GET', 'some/route', [], $cookie)
111-
->assertSessionHas($this->sessionKey, 'it')
112-
->assertCookie($this->cookieName, 'it')
113-
->assertAppLocale('it');
123+
Route::get('some/route', function () {
124+
return App::getLocale();
125+
})->middleware(['web', SetLocale::class]);
126+
127+
$response = $this->call('GET', 'some/route', [], $cookie);
128+
129+
$response->assertSessionHas($this->sessionKey, 'it');
130+
$response->assertCookie($this->cookieName, 'it');
131+
$this->assertEquals('it', $response->original);
114132
}
115133

116134
/** @test */
117135
public function it_returns_the_best_match_when_a_browser_locale_is_used()
118136
{
119-
$this->registerRoute('some/route');
120-
137+
$this->setSupportedLocales(['en', 'nl', 'fr', 'de', 'es', 'it']);
121138
$this->setSessionLocale(null);
122-
$this->setBrowserLocales('cs,it-IT;q=0.8,es;q=0.4');
139+
$this->setBrowserLocales('cs,it-IT;q=0.4,es;q=0.8');
123140
$this->setAppLocale('en');
124-
125141
$cookie = [];
126142

127-
$this->call('GET', 'some/route', [], $cookie)
128-
->assertSessionHas($this->sessionKey, 'it')
129-
->assertCookie($this->cookieName, 'it')
130-
->assertAppLocale('it');
143+
Route::get('some/route', function () {
144+
return App::getLocale();
145+
})->middleware(['web', SetLocale::class]);
146+
147+
$response = $this->call('GET', 'some/route', [], $cookie);
148+
149+
$response->assertSessionHas($this->sessionKey, 'es');
150+
$response->assertCookie($this->cookieName, 'es');
151+
$this->assertEquals('es', $response->original);
131152
}
132153

133154
/** @test */
134155
public function it_defaults_to_the_current_app_locale()
135156
{
136-
$this->registerRoute('some/route');
137-
157+
$this->setSupportedLocales(['en', 'nl', 'fr', 'de', 'es', 'it']);
138158
$this->setSessionLocale(null);
139159
$this->setBrowserLocales(null);
140160
$this->setAppLocale('en');
141-
142161
$cookie = [];
143162

144-
$this->call('GET', 'some/route', [], $cookie)
145-
->assertSessionHas($this->sessionKey, 'en')
146-
->assertCookie($this->cookieName, 'en')
147-
->assertAppLocale('en');
163+
Route::get('some/route', function () {
164+
return App::getLocale();
165+
})->middleware(['web', SetLocale::class]);
166+
167+
$response = $this->call('GET', 'some/route', [], $cookie);
168+
169+
$response->assertSessionHas($this->sessionKey, 'en');
170+
$response->assertCookie($this->cookieName, 'en');
171+
$this->assertEquals('en', $response->original);
148172
}
149173

150174
/**
@@ -170,7 +194,7 @@ protected function setAppLocale($locale)
170194
*/
171195
protected function setSupportedLocales(array $locales)
172196
{
173-
Config::set($this->localesKey, $locales);
197+
Config::set('localizer.supported-locales', $locales);
174198

175199
return $this;
176200
}
@@ -204,22 +228,4 @@ protected function setBrowserLocales($locales)
204228

205229
return $this;
206230
}
207-
208-
/**
209-
* Register a route.
210-
*
211-
* @param string $url
212-
*
213-
* @return $this
214-
*/
215-
protected function registerRoute($url)
216-
{
217-
Route::getRoutes()->add(
218-
Route::get($url, function () {
219-
return 'The locale is: ' . App::getLocale();
220-
})->middleware(['web', SetLocale::class])
221-
);
222-
223-
return $this;
224-
}
225231
}

tests/TestCase.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88

99
abstract class TestCase extends BaseTestCase
1010
{
11-
protected $localesKey;
12-
protected $sessionKey;
13-
protected $cookieName;
14-
1511
/**
1612
* Setup the test environment.
1713
*
@@ -21,10 +17,6 @@ protected function setUp()
2117
{
2218
parent::setUp();
2319

24-
$this->localesKey = 'localizer.supported-locales';
25-
$this->sessionKey = Config::get('localizer.session-key');
26-
$this->cookieName = Config::get('localizer.cookie-name');
27-
2820
Config::set('app.key', str_random(32));
2921
}
3022

tests/Unit/LocalizerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class LocalizerTest extends TestCase
1414
/** @test */
1515
public function it_loops_through_the_detectors_and_returns_the_first_supported_locale()
1616
{
17-
$locales = ['en', 'nl'];
17+
$supportedLocales = ['en', 'nl'];
1818
$detectors = [
1919
Mockery::mock(Detector::class)->allows()->detect()->andReturns(false)->getMock(),
2020
Mockery::mock(Detector::class)->allows()->detect()->andReturns(null)->getMock(),
@@ -23,28 +23,28 @@ public function it_loops_through_the_detectors_and_returns_the_first_supported_l
2323
Mockery::mock(Detector::class)->allows()->detect()->andReturns('en')->getMock(),
2424
];
2525

26-
$localizer = new Localizer($locales, $detectors);
26+
$localizer = new Localizer($supportedLocales, $detectors);
2727

2828
$this->assertEquals('nl', $localizer->detect());
2929
}
3030

3131
/** @test */
3232
public function it_returns_the_best_match_if_an_array_of_locales_is_detected()
3333
{
34-
$locales = ['en', 'nl'];
34+
$supportedLocales = ['en', 'nl'];
3535
$detectors = [
3636
Mockery::mock(Detector::class)->allows()->detect()->andReturns(['de', 'nl', 'en'])->getMock(),
3737
];
3838

39-
$localizer = new Localizer($locales, $detectors);
39+
$localizer = new Localizer($supportedLocales, $detectors);
4040

4141
$this->assertEquals('nl', $localizer->detect());
4242
}
4343

4444
/** @test */
4545
public function it_returns_false_if_no_supported_locale_could_be_detected()
4646
{
47-
$locales = ['en'];
47+
$supportedLocales = ['en'];
4848
$detectors = [
4949
Mockery::mock(Detector::class)->allows()->detect()->andReturns(false)->getMock(),
5050
Mockery::mock(Detector::class)->allows()->detect()->andReturns(null)->getMock(),
@@ -53,7 +53,7 @@ public function it_returns_false_if_no_supported_locale_could_be_detected()
5353
Mockery::mock(Detector::class)->allows()->detect()->andReturns('fr')->getMock(),
5454
];
5555

56-
$localizer = new Localizer($locales, $detectors);
56+
$localizer = new Localizer($supportedLocales, $detectors);
5757

5858
$this->assertFalse($localizer->detect());
5959
}

0 commit comments

Comments
 (0)