Passed
Push — master ( f51c1b...16736d )
by Arthur
05:36
created

HttpTest::getAuth0Service()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
ccs 2
cts 2
cp 1
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;
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 10
    private $auth0;
24
25 10
    protected $roles = Role::USER;
26 10
27 10
    public function setUp()
28
    {
29
        parent::setUp();
30
    }
31
32 7
    protected function getAuth0Service()
33
    {
34 7
        if ($this->auth0 === null)
35
            $this->auth0 = $this->app->make(Auth0UserRepository::class);
36
        return $this->auth0;
37 2
    }
38
39 2
    /**
40 2
     * @return User | Authenticatable
41
     */
42
    protected function getHttpUser()
43
    {
44
        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

44
        return $this->getAuth0Service()->/** @scrutinizer ignore-call */ getTestUser($this->roles);
Loading history...
45
    }
46 6
47
    protected function changeTestUserRoles($roles)
48 6
    {
49
        return $this->getAuth0Service()->getTestUser($roles);
50
    }
51 7
52
    protected function decodeHttpContent($content, $unwrap = true)
53 7
    {
54 6
        if ($content instanceof TestResponse) {
55 7
            $content = $content->content();
56
        }
57
58
        if ($unwrap) {
59
            return json_decode($content, true)['data'] ?? json_decode($content, true);
60
        }
61
62
        return json_decode($content, true);
63
    }
64
65 1
    protected function http(string $method, string $route, array $payload = [])
66
    {
67 1
        return $this->sendRequest($method, $route, $payload, true);
68
    }
69
70
    private function sendRequest(string $method, string $route, array $payload = [], $authenticated = true): \Illuminate\Foundation\Testing\TestResponse
71
    {
72
        return $this->json($method, env('API_URL') . '/' . $route, $payload, $authenticated ? [
73
            '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

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