Passed
Push — master ( 5555ce...f15488 )
by Arthur
04:40
created

HttpTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 67.5%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 81
ccs 27
cts 40
cp 0.675
rs 10
c 0
b 0
f 0
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A getHttpUser() 0 6 1
A httpNoAuth() 0 3 1
A sendRequest() 0 5 2
A getUserTokenData() 0 22 2
A http() 0 3 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
                $domain = 'https://astral.eu.auth0.com/';
51 2
                $clientId = env('AUTH0_CLIENT_ID');
52 2
                $username = env('AUTH0_TEST_USER_NAME');
53 2
                $password = env('AUTH0_TEST_USER_PASS');
54
55 2
                $response = $httpClient->post($domain . 'oauth/token', [
56
                    'form_params' => [
57 2
                        'grant_type' => 'password',
58 2
                        'client_id' => $clientId,
59 2
                        'username' => $username,
60 2
                        'password' => $password,
61 2
                        'scope' => 'openid profile email offline_access',
62
                    ],
63
                ]);
64
                return json_decode($response->getBody()->getContents());
65 2
            } catch (ClientException $exception) {
66 2
                throw new Exception("Could not obtain token from Auth0 for testing from $domain $clientId $username $password" . $exception->getMessage());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $clientId does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $domain does not seem to be defined for all execution paths leading up to this point.
Loading history...
67
            }
68 2
        });
69
    }
70
71
    protected function decodeHttpContent($content, $unwrap = true)
72
    {
73
        if ($unwrap)
74
            return json_decode($content, true)['data'];
75
        return json_decode($content, true);
76
    }
77
78
    protected function http(string $method, string $route, array $payload = [])
79
    {
80
        return $this->sendRequest($method, $route, $payload, true);
81
    }
82
83 1
    private function sendRequest(string $method, string $route, array $payload = [], $authenticated = true): \Illuminate\Foundation\Testing\TestResponse
84
    {
85 1
        return $this->json($method, env('API_URL') . '/' . $route, $payload, $authenticated ? [
86
            'Authorization' => 'Bearer ' . $this->getUserTokenData()->id_token,
87 1
        ] : []);
88
    }
89
90
    protected function sendRequestWithToken($token, string $method, string $route, array $payload = [], $authenticated = true): \Illuminate\Foundation\Testing\TestResponse
91
    {
92
        return $this->json($method, env('API_URL') . '/' . $route, $payload, $authenticated ? [
93
            'Authorization' => 'Bearer ' . $token,
94
        ] : []);
95
    }
96
97 1
    protected function httpNoAuth(string $method, string $route, array $payload = [])
98
    {
99 1
        return $this->sendRequest($method, $route, $payload, false);
100
    }
101
}
102