Passed
Push — master ( 4c3e72...46eb59 )
by Arthur
10:39
created

HttpTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 82.76%

Importance

Changes 0
Metric Value
wmc 14
eloc 20
dl 0
loc 71
ccs 24
cts 29
cp 0.8276
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A changeTestUserRoles() 0 3 1
A setUp() 0 3 1
A httpNoAuth() 0 3 1
A sendRequest() 0 5 2
A getAuth0Service() 0 7 2
A http() 0 3 1
A getHttpUser() 0 3 1
A sendRequestWithToken() 0 5 2
A decodeHttpResponse() 0 11 3
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;
12
use Illuminate\Contracts\Auth\Authenticatable;
13
use Illuminate\Foundation\Testing\TestResponse;
14
use Modules\Auth0\Services\Auth0Service;
15
use Modules\Authorization\Entities\Role;
16
use Modules\User\Entities\User;
17
18
abstract class HttpTest extends \Foundation\Abstracts\Tests\TestCase
19
{
20
    /**
21
     * @var Auth0Service
22
     */
23
    private $auth0;
24
25
    protected $roles = Role::USER;
26
27 20
    public function setUp()
28
    {
29 20
        parent::setUp();
30 20
    }
31
32 18
    protected function getAuth0Service()
33
    {
34 18
        if ($this->auth0 === null) {
35 18
            $this->auth0 = $this->app->make(Auth0UserRepository::class);
36
        }
37
38 18
        return $this->auth0;
39
    }
40
41
    /**
42
     * @return User | Authenticatable
43
     */
44 17
    protected function getHttpUser()
45
    {
46 17
        return $this->getAuth0Service()->getTestUser($this->roles);
0 ignored issues
show
Bug introduced by
The method getTestUser() does not exist on Auth0\Login\Contract\Auth0UserRepository. It seems like you code against a sub-type of Auth0\Login\Contract\Auth0UserRepository such as Modules\Auth0\Services\Auth0Service. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        return $this->getAuth0Service()->/** @scrutinizer ignore-call */ getTestUser($this->roles);
Loading history...
47
    }
48
49 2
    protected function changeTestUserRoles($roles)
50
    {
51 2
        return $this->getAuth0Service()->getTestUser($roles);
52
    }
53
54 6
    protected function decodeHttpResponse($content, $unwrap = true)
55
    {
56 6
        if ($content instanceof TestResponse) {
57 4
            $content = $content->content();
58
        }
59
60 6
        if ($unwrap) {
61 6
            return json_decode($content, true)['data'] ?? json_decode($content, true);
62
        }
63
64
        return json_decode($content, true);
65
    }
66
67 16
    protected function http(string $method, string $route, array $payload = [])
68
    {
69 16
        return $this->sendRequest($method, $route, $payload, true);
70
    }
71
72 17
    private function sendRequest(string $method, string $route, array $payload = [], $authenticated = true): \Illuminate\Foundation\Testing\TestResponse
73
    {
74 17
        return $this->json($method, env('API_URL').'/'.$route, $payload, $authenticated ? [
75 16
            'Authorization' => 'Bearer '.$this->getAuth0Service()->getTestUserToken()->id_token,
0 ignored issues
show
Bug introduced by
The method getTestUserToken() does not exist on Auth0\Login\Contract\Auth0UserRepository. It seems like you code against a sub-type of Auth0\Login\Contract\Auth0UserRepository such as Modules\Auth0\Services\Auth0Service. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            'Authorization' => 'Bearer '.$this->getAuth0Service()->/** @scrutinizer ignore-call */ getTestUserToken()->id_token,
Loading history...
76 17
        ] : []);
77
    }
78
79
    protected function sendRequestWithToken($token, string $method, string $route, array $payload = [], $authenticated = true): \Illuminate\Foundation\Testing\TestResponse
80
    {
81
        return $this->json($method, env('API_URL').'/'.$route, $payload, $authenticated ? [
82
            'Authorization' => 'Bearer '.$token,
83
        ] : []);
84
    }
85
86 1
    protected function httpNoAuth(string $method, string $route, array $payload = [])
87
    {
88 1
        return $this->sendRequest($method, $route, $payload, false);
89
    }
90
}
91