Passed
Push — master ( cf340d...4e06a8 )
by Arthur
06:10
created

HttpTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 63.89%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 76
ccs 23
cts 36
cp 0.6389
rs 10
c 0
b 0
f 0
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A httpNoAuth() 0 3 1
A sendRequest() 0 5 2
A getUserTokenData() 0 17 2
A http() 0 3 1
A getHttpUser() 0 6 1
A decodeHttpContent() 0 5 2
A sendRequestWithToken() 0 5 2
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 4
    public function setUp()
28
    {
29 4
        parent::setUp();
30 4
        $this->service = $this->app->make(Auth0UserRepository::class);
31 4
    }
32
33
34
    /**
35
     * @return User | Authenticatable
36
     */
37 2
    protected function getHttpUser()
38
    {
39 2
        $auth0 = \App::make('auth0');
40 2
        $tokenInfo = $auth0->decodeJWT($this->getUserTokenData()->id_token);
41
42
        return $this->service->getUserByDecodedJWT($tokenInfo);
43
    }
44
45 2
    private function getUserTokenData(): \stdClass
46
    {
47
        return Cache::remember('testing:http_access_token', 60, function () {
48
            try {
49 2
                $httpClient = new Client();
50 2
                $response = $httpClient->post(config('laravel-auth0.domain') . 'oauth/token', [
51
                    'form_params' => [
52 2
                        'grant_type' => 'password',
53 2
                        'client_id' => env('AUTH0_CLIENT_ID'),
54 2
                        'username' => env('AUTH0_TEST_USER_NAME'),
55 2
                        'password' => env('AUTH0_TEST_USER_PASS'),
56 2
                        'scope' => 'openid profile email offline_access',
57
                    ],
58
                ]);
59
                return json_decode($response->getBody()->getContents());
60 2
            } catch (ClientException $exception) {
61 2
                throw new Exception("Could not obtain token from Auth0 for testing.");
62
            }
63 2
        });
64
    }
65
66
    protected function decodeHttpContent($content, $unwrap = true)
67
    {
68
        if ($unwrap)
69
            return json_decode($content, true)['data'];
70
        return json_decode($content, true);
71
    }
72
73
    protected function http(string $method, string $route, array $payload = [])
74
    {
75
        return $this->sendRequest($method, $route, $payload, true);
76
    }
77
78 1
    private function sendRequest(string $method, string $route, array $payload = [], $authenticated = true): \Illuminate\Foundation\Testing\TestResponse
79
    {
80 1
        return $this->json($method, env('API_URL') . '/' . $route, $payload, $authenticated ? [
81
            'Authorization' => 'Bearer ' . $this->getUserTokenData()->id_token,
82 1
        ] : []);
83
    }
84
85
    protected function sendRequestWithToken($token, string $method, string $route, array $payload = [], $authenticated = true): \Illuminate\Foundation\Testing\TestResponse
86
    {
87
        return $this->json($method, env('API_URL') . '/' . $route, $payload, $authenticated ? [
88
            'Authorization' => 'Bearer ' . $token,
89
        ] : []);
90
    }
91
92 1
    protected function httpNoAuth(string $method, string $route, array $payload = [])
93
    {
94 1
        return $this->sendRequest($method, $route, $payload, false);
95
    }
96
}
97