Test Failed
Push — master ( 93ef3c...0c96ba )
by Arthur
09:20
created

HttpTest::http()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Foundation\Abstracts\Tests\TestCase. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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