Passed
Push — master ( b94ee9...848980 )
by Arthur
08:58 queued 04:24
created

HttpTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

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

8 Methods

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