|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: arthur |
|
5
|
|
|
* Date: 09.10.18 |
|
6
|
|
|
* Time: 21:56 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Foundation\Abstracts\Tests; |
|
10
|
|
|
|
|
11
|
|
|
use Cache; |
|
12
|
|
|
use Foundation\Repositories\Auth0UserRepository; |
|
13
|
|
|
use GuzzleHttp\Client; |
|
14
|
|
|
use Modules\User\Services\UserService; |
|
15
|
|
|
use Tests\TestCase; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class HttpTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
protected function getTestUser() |
|
21
|
|
|
{ |
|
22
|
|
|
$auth0 = \App::make('auth0'); |
|
23
|
|
|
$repository = new Auth0UserRepository(new UserService()); |
|
24
|
|
|
$tokenInfo = $auth0->decodeJWT($this->getUserTokenData()->id_token); |
|
25
|
|
|
return $repository->getUserByDecodedJWT($tokenInfo); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
private function getUserTokenData(): \stdClass |
|
29
|
|
|
{ |
|
30
|
|
|
return Cache::remember('testing:http_access_token', 60, function () { |
|
31
|
|
|
$httpClient = new Client(); |
|
32
|
|
|
$response = $httpClient->post(config('laravel-auth0.domain') . 'oauth/token', [ |
|
33
|
|
|
'form_params' => [ |
|
34
|
|
|
'grant_type' => 'password', |
|
35
|
|
|
'client_id' => 'Dik7up1ZsRePpdZNjzrHIAUHe8mCb3RK', |
|
36
|
|
|
'username' => '[email protected]', |
|
37
|
|
|
'password' => 'admin', |
|
38
|
|
|
'scope' => 'openid profile email offline_access' |
|
39
|
|
|
] |
|
40
|
|
|
]); |
|
41
|
|
|
return json_decode($response->getBody()->getContents()); |
|
42
|
|
|
}); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function http(string $method, string $route, array $payload = array()) |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->sendRequest($method, $route, $payload, true); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
private function sendRequest(string $method, string $route, array $payload = array(), $authenticated = true): \Illuminate\Foundation\Testing\TestResponse |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->json($method, $route, $payload, $authenticated ? [ |
|
53
|
|
|
"Authorization" => "Bearer " . $this->getUserTokenData()->id_token |
|
54
|
|
|
] : []); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function httpNoAuth(string $method, string $route, array $payload = array()) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->sendRequest($method, $route, $payload, false); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: