Passed
Push — master ( 1dc943...5f0a93 )
by Arthur
08:24 queued 35s
created

HttpTest::sendRequest()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 1
nop 4
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
    protected $service;
24
25 10
    protected $roles = Role::USER;
26 10
27 10
    public function setUp()
28
    {
29
        parent::setUp();
30
        $this->service = $this->app->make(Auth0UserRepository::class);
31
        $this->service->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

31
        $this->service->/** @scrutinizer ignore-call */ 
32
                        getTestUser($this->roles);
Loading history...
32 7
    }
33
34 7
    /**
35
     * @return User | Authenticatable
36
     */
37 2
    protected function getHttpUser()
38
    {
39 2
        return $this->service->getTestUser($this->roles);
40 2
    }
41
42
    protected function changeTestUserRoles($roles)
43
    {
44
        return $this->service->getTestUser($roles);
45
    }
46 6
47
    protected function decodeHttpContent($content, $unwrap = true)
48 6
    {
49
        if ($content instanceof TestResponse) {
50
            $content = $content->content();
51 7
        }
52
53 7
        if ($unwrap) {
54 6
            return json_decode($content, true)['data'];
55 7
        }
56
57
        return json_decode($content, true);
58
    }
59
60
    protected function http(string $method, string $route, array $payload = [])
61
    {
62
        return $this->sendRequest($method, $route, $payload, true);
63
    }
64
65 1
    private function sendRequest(string $method, string $route, array $payload = [], $authenticated = true): \Illuminate\Foundation\Testing\TestResponse
66
    {
67 1
        return $this->json($method, env('API_URL').'/'.$route, $payload, $authenticated ? [
68
            'Authorization' => 'Bearer '.$this->service->getTestUserToken()->id_token,
69
        ] : []);
70
    }
71
72
    protected function sendRequestWithToken($token, string $method, string $route, array $payload = [], $authenticated = true): \Illuminate\Foundation\Testing\TestResponse
73
    {
74
        return $this->json($method, env('API_URL').'/'.$route, $payload, $authenticated ? [
75
            'Authorization' => 'Bearer '.$token,
76
        ] : []);
77
    }
78
79
    protected function httpNoAuth(string $method, string $route, array $payload = [])
80
    {
81
        return $this->sendRequest($method, $route, $payload, false);
82
    }
83
}
84