Skip to content

Commit 609bde8

Browse files
committed
Refactor tests
1 parent 464f9e1 commit 609bde8

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed

tests/Unit/Middleware/SetLocaleTest.php

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,13 @@ public function it_sets_the_right_locale_when_accessing_localized_routes_with_om
5959
public function it_sets_the_locale_for_localized_routes_within_route_groups()
6060
{
6161
$this->setSupportedLocales(['en', 'nl']);
62+
$this->setUseLocaleMiddleware(true);
6263

63-
Route::group([
64-
'as' => 'admin.',
65-
'prefix' => 'admin',
66-
], function () {
64+
Route::group(['as' => 'admin.', 'prefix' => 'admin'], function () {
6765
Route::localized(function () {
6866
Route::get('route', function () {
6967
return App::getLocale();
70-
})->name('route.name')->middleware(['web', SetLocale::class]);
68+
});
7169
});
7270
});
7371

@@ -81,14 +79,14 @@ public function it_sets_the_locale_for_localized_routes_within_route_groups()
8179
}
8280

8381
/** @test */
84-
public function it_allows_for_localized_route_model_binding()
82+
public function it_allows_for_route_model_binding_using_a_localized_route_key()
8583
{
8684
$this->setSupportedLocales(['en', 'nl']);
85+
$this->setUseLocaleMiddleware(true);
8786

8887
Route::localized(function () {
89-
Route::get('route/{model}', function (Model $model) {
90-
return App::getLocale();
91-
})->name('route.name')->middleware(['web', SetLocale::class]);
88+
Route::get('route/{model}', function (Model $model) {})
89+
->middleware(['web']);
9290
});
9391

9492
$this->call('GET', '/en/route/en-slug')->assertOk();
@@ -98,72 +96,70 @@ public function it_allows_for_localized_route_model_binding()
9896
}
9997

10098
/** @test */
101-
public function it_does_not_detect_the_preferred_locale_with_localizer_for_localized_routes()
99+
public function it_detects_the_locale_with_localizer_for_non_localized_routes()
102100
{
101+
$this->withoutExceptionHandling();
103102
$this->setSupportedLocales(['en', 'nl']);
104103
$this->setUseLocalizer(true);
105104

106-
$localizer = Mockery::mock(Localizer::class);
107-
$localizer->shouldReceive('setSupportedLocales')->with(['en', 'nl']);
108-
$localizer->shouldNotReceive('detect');
109-
$localizer->shouldReceive('store')->with('en');
110-
105+
$localizer = Mockery::spy(Localizer::class);
106+
$localizer->shouldReceive('detect')->andReturn('en');
111107
App::instance(Localizer::class, $localizer);
112108

113-
Route::localized(function () {
114-
Route::get('localized-route', function () {})
115-
->name('localized.route')
116-
->middleware(['web', SetLocale::class]);
117-
});
109+
Route::get('non-localized-route', function () {})
110+
->middleware(['web', SetLocale::class]);
118111

119-
$this->call('GET', '/en/localized-route')->assertOk();
112+
$this->call('GET', '/non-localized-route')->assertOk();
113+
114+
$localizer->shouldHaveReceived('setSupportedLocales')->with(['en', 'nl']);
115+
$localizer->shouldHaveReceived('detect');
116+
$localizer->shouldHaveReceived('store')->with('en');
120117
}
121118

122119
/** @test */
123-
public function it_detects_the_preferred_locale_with_localizer_for_non_localized_routes()
120+
public function it_does_not_detect_the_locale_with_localizer_for_localized_routes()
124121
{
122+
$this->withoutExceptionHandling();
125123
$this->setSupportedLocales(['en', 'nl']);
126124
$this->setUseLocalizer(true);
127125

128-
$localizer = Mockery::mock(Localizer::class);
129-
$localizer->shouldReceive('setSupportedLocales')->with(['en', 'nl']);
130-
$localizer->shouldReceive('detect')->andReturn('en');
131-
$localizer->shouldReceive('store')->with('en');
132-
126+
$localizer = Mockery::spy(Localizer::class);
133127
App::instance(Localizer::class, $localizer);
134128

135-
Route::get('non-localized-route', function () {})
136-
->name('non-localized.route')
137-
->middleware(['web', SetLocale::class]);
129+
Route::localized(function () {
130+
Route::get('localized-route', function () {})
131+
->middleware(['web', SetLocale::class]);
132+
});
138133

139-
$this->call('GET', '/non-localized-route')->assertOk();
134+
$this->call('GET', '/en/localized-route')->assertOk();
135+
136+
$localizer->shouldNotHaveReceived('detect');
137+
$localizer->shouldHaveReceived('store')->with('en');
140138
}
141139

142140
/** @test */
143141
public function it_does_not_use_localizer_when_disabled()
144142
{
143+
$this->withoutExceptionHandling();
145144
$this->setSupportedLocales(['en', 'nl']);
146145
$this->setUseLocalizer(false);
147146

148-
$localizer = Mockery::mock(Localizer::class);
149-
$localizer->shouldNotReceive('setSupportedLocales');
150-
$localizer->shouldNotReceive('detect');
151-
$localizer->shouldNotReceive('store');
152-
147+
$localizer = Mockery::spy(Localizer::class);
153148
App::instance(Localizer::class, $localizer);
154149

155150
Route::localized(function () {
156151
Route::get('localized-route', function () {})
157-
->name('localized.route')
158152
->middleware(['web', SetLocale::class]);
159153
});
160154

161-
Route::get('/non-localized-route', function () {})
162-
->name('non-localized.route')
155+
Route::get('non-localized-route', function () {})
163156
->middleware(['web', SetLocale::class]);
164157

165158
$this->call('GET', '/non-localized-route')->assertOk();
166159
$this->call('GET', '/en/localized-route')->assertOk();
160+
161+
$localizer->shouldNotHaveReceived('detect');
162+
$localizer->shouldNotHaveReceived('store');
167163
}
168164

169165
/** @test */
@@ -177,11 +173,11 @@ public function it_still_sets_the_app_locale_for_localized_routes_if_localizer_i
177173
Route::localized(function () {
178174
Route::get('localized-route', function () {
179175
return App::getLocale();
180-
})->name('localized.route')->middleware(['web', SetLocale::class]);
176+
})->middleware(['web', SetLocale::class]);
181177
});
182178

183179
$response = $this->call('GET', '/en/localized-route');
184-
180+
$response->assertOk();
185181
$this->assertEquals('en', $response->original);
186182
}
187183

@@ -193,12 +189,12 @@ public function it_does_not_set_the_app_locale_for_non_localized_routes_if_local
193189

194190
App::setLocale('fr');
195191

196-
Route::get('/non-localized-route', function () {
192+
Route::get('non-localized-route', function () {
197193
return App::getLocale();
198-
})->name('non-localized.route')->middleware(['web', SetLocale::class]);
194+
})->middleware(['web', SetLocale::class]);
199195

200196
$response = $this->call('GET', '/non-localized-route');
201-
197+
$response->assertOk();
202198
$this->assertEquals('fr', $response->original);
203199
}
204200
}

0 commit comments

Comments
 (0)