Passed
Push — master ( e5b31a...cf340d )
by Arthur
04:48
created

HttpTest::getTestUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 2
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 Auth0\Login\Contract\Auth0UserRepository;
0 ignored issues
show
Bug introduced by
The type Auth0\Login\Contract\Auth0UserRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Cache;
13
use Foundation\Exceptions\Exception;
14
use GuzzleHttp\Client;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use GuzzleHttp\Exception\ClientException;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Exception\ClientException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Illuminate\Contracts\Auth\Authenticatable;
17
use Modules\User\Entities\User;
18
19
abstract class HttpTest extends \Foundation\Abstracts\Tests\TestCase
20
{
21
22
    /**
23
     * @var Auth0UserRepository
24
     */
25
    protected $service;
26
27 2
    public function setUp()
28
    {
29 2
        parent::setUp();
30 2
        $this->service = $this->app->make(Auth0UserRepository::class);
31 2
    }
32
33
34
    /**
35
     * @return User | Authenticatable
36
     */
37
    protected function getHttpUser()
38
    {
39
        $auth0 = \App::make('auth0');
40
        $tokenInfo = $auth0->decodeJWT($this->getUserTokenData()->id_token);
41
42
        return $this->service->getUserByDecodedJWT($tokenInfo);
43
    }
44
45
    private function getUserTokenData(): \stdClass
46
    {
47
        return Cache::remember('testing:http_access_token', 60, function () {
48
            try {
49
                $httpClient = new Client();
50
                $response = $httpClient->post(config('laravel-auth0.domain') . 'oauth/token', [
51
                    'form_params' => [
52
                        'grant_type' => 'password',
53
                        'client_id' => env('AUTH0_CLIENT_ID'),
54
                        'username' => env('AUTH0_TEST_USER_NAME'),
55
                        'password' => env('AUTH0_TEST_USER_PASS'),
56
                        'scope' => 'openid profile email offline_access',
57
                    ],
58
                ]);
59
                return json_decode($response->getBody()->getContents());
60
            } catch (ClientException $exception) {
61
                throw new Exception("Could not obtain token from Auth0 for testing.");
62
            }
63
        });
64
    }
65
66
    protected function decodeHttpContent($content) {
67
        return json_decode($content,true)['data'];
68
    }
69
70
    protected function http(string $method, string $route, array $payload = [])
71
    {
72
        return $this->sendRequest($method, $route, $payload, true);
73
    }
74
75 1
    private function sendRequest(string $method, string $route, array $payload = [], $authenticated = true): \Illuminate\Foundation\Testing\TestResponse
76
    {
77 1
        return $this->json($method, env('API_URL') . '/' . $route, $payload, $authenticated ? [
78
            'Authorization' => 'Bearer ' . $this->getUserTokenData()->id_token,
79 1
        ] : []);
80
    }
81
82 1
    protected function httpNoAuth(string $method, string $route, array $payload = [])
83
    {
84 1
        return $this->sendRequest($method, $route, $payload, false);
85
    }
86
}
87