1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Containers\Localization\UI\API\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use App\Containers\Localization\Tests\ApiTestCase; |
6
|
|
|
use Illuminate\Support\Facades\Config; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class CheckLocalizationMiddlewareTest. |
10
|
|
|
* |
11
|
|
|
* @group localization |
12
|
|
|
* @group api |
13
|
|
|
*/ |
14
|
|
|
class CheckLocalizationMiddlewareTest extends ApiTestCase |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
// the endpoint to be called within this test (e.g., [email protected]/users) |
18
|
|
|
protected $endpoint = '[email protected]/localizations'; |
19
|
|
|
|
20
|
|
|
// fake some access rights |
21
|
|
|
protected $access = [ |
22
|
|
|
'permissions' => '', |
23
|
|
|
'roles' => '', |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @test |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function test_if_middleware_sets_default_app_language() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$data = []; |
32
|
|
|
$requestHeaders = []; |
33
|
|
|
|
34
|
|
|
// send the HTTP request |
35
|
|
|
$response = $this->makeCall($data, $requestHeaders); |
36
|
|
|
|
37
|
|
|
// assert the response status |
38
|
|
|
$response->assertStatus(200); |
39
|
|
|
|
40
|
|
|
$defaultLanguage = Config::get('app.locale'); |
41
|
|
|
|
42
|
|
|
// check if the header is properly set |
43
|
|
|
$response->assertHeader('content-language', $defaultLanguage); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
public function test_if_middleware_sets_custom_language() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$language = 'fr'; |
49
|
|
|
|
50
|
|
|
$data = []; |
51
|
|
|
$requestHeaders = [ |
52
|
|
|
'accept-language' => $language, |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
// send the HTTP request |
56
|
|
|
$response = $this->makeCall($data, $requestHeaders); |
57
|
|
|
|
58
|
|
|
// assert the response status |
59
|
|
|
$response->assertStatus(200); |
60
|
|
|
|
61
|
|
|
// check if the header is properly set |
62
|
|
|
$response->assertHeader('content-language', $language); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function test_if_middleware_throws_error_on_wrong_language() |
66
|
|
|
{ |
67
|
|
|
$language = 'xxx'; |
68
|
|
|
|
69
|
|
|
$data = []; |
70
|
|
|
$requestHeaders = [ |
71
|
|
|
'accept-language' => $language, |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
// send the HTTP request |
75
|
|
|
$response = $this->makeCall($data, $requestHeaders); |
76
|
|
|
|
77
|
|
|
// assert the response status |
78
|
|
|
$response->assertStatus(412); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.