GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ClientTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 153
Duplicated Lines 35.29 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 54
loc 153
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A can_instantiate_client() 0 8 1
B can_retrieve_an_access_token() 0 24 1
B can_predict_the_content_of_an_image_by_passing_an_url() 0 27 1
B use_general_model_by_default_when_predicting_the_content_of_an_image() 27 27 1
B can_predict_the_content_of_an_image_by_passing_an_url_and_a_specific_model() 27 27 1
A getHttpClientMock() 0 4 1
A sendRequest() 0 22 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Fab\Clarifai\Tests;
4
5
use Fab\Clarifai\Client;
6
use Fab\Clarifai\Models;
7
use GuzzleHttp\Client as HttpClient;
8
use Mockery;
9
10
class ClientTest extends TestCase
11
{
12
    /** @test */
13
    function can_instantiate_client()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
14
    {
15
        $httpClient = $this->getHttpClientMock();
16
17
        $client = new Client($httpClient, 'client-id', 'client-secret');
18
19
        $this->assertInstanceOf(Client::class, $client);
20
    }
21
22
    /** @test */
23
    function can_retrieve_an_access_token()
24
    {
25
        $httpClient = $this->getHttpClientMock();
26
        $httpClient
27
            ->shouldReceive('request')
28
            ->with('POST', 'https://api.clarifai.com/v2/token', [
29
                'headers' => [
30
                    'Accept' => 'application/json',
31
                    'Content-Type' => 'application/json',
32
                ],
33
                'auth' => ['client-id', 'client-secret'],
34
            ])
35
            ->andReturn($httpClient);
36
37
            $httpClient
38
                ->shouldReceive('getBody')
39
                ->andReturn(json_encode(['foo' => 'bar']));
40
41
        $client = new Client($httpClient, 'client-id', 'client-secret');
42
43
        $response = $client->accessToken();
44
45
        $this->assertEquals(['foo' => 'bar'], $response);
46
    }
47
48
    /** @test */
49
    function can_predict_the_content_of_an_image_by_passing_an_url()
50
    {
51
        $httpClient = $this->sendRequest(
52
            $this->getHttpClientMock(),
53
            'https://api.clarifai.com/v2/models/aaa03c23b3724a16a56b629203edc62c/outputs',
54
            [
55
                [
56
                    'data' => [
57
                        'image' => [
58
                            'url' => 'https://samples.clarifai.com/metro-north.jpg',
59
                        ]
60
                    ]
61
                ]
62
            ]
63
        );
64
65
        $client = new Client($httpClient, 'client-id', 'client-secret');
66
67
        $response = $client->withAccessToken('access-token')->predict([
68
            'https://samples.clarifai.com/metro-north.jpg',
69
        ]);
70
71
        $this->assertTrue(is_array($response));
72
        $this->assertEquals([
73
            'foo' => 'bar'
74
        ], $response);
75
    }
76
77
    /** @test */
78 View Code Duplication
    function use_general_model_by_default_when_predicting_the_content_of_an_image()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        $httpClient = $this->sendRequest(
81
            $this->getHttpClientMock(),
82
            'https://api.clarifai.com/v2/models/'.Models::GENERAL.'/outputs',
83
            [
84
                [
85
                    'data' => [
86
                        'image' => [
87
                            'url' => 'https://samples.clarifai.com/metro-north.jpg',
88
                        ]
89
                    ]
90
                ]
91
            ]
92
        );
93
94
        $client = new Client($httpClient, 'client-id', 'client-secret');
95
96
        $response = $client->withAccessToken('access-token')->predict([
97
            'https://samples.clarifai.com/metro-north.jpg',
98
        ]);
99
100
        $this->assertTrue(is_array($response));
101
        $this->assertEquals([
102
            'foo' => 'bar'
103
        ], $response);
104
    }
105
106
    /** @test */
107 View Code Duplication
    function can_predict_the_content_of_an_image_by_passing_an_url_and_a_specific_model()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        $httpClient = $this->sendRequest(
110
            $this->getHttpClientMock(),
111
            'https://api.clarifai.com/v2/models/'.Models::FOOD.'/outputs',
112
            [
113
                [
114
                    'data' => [
115
                        'image' => [
116
                            'url' => 'https://samples.clarifai.com/metro-north.jpg',
117
                        ]
118
                    ]
119
                ]
120
            ]
121
        );
122
123
        $client = new Client($httpClient, 'client-id', 'client-secret');
124
125
        $response = $client->withAccessToken('access-token')->predict([
126
            'https://samples.clarifai.com/metro-north.jpg',
127
        ], Models::FOOD);
128
129
        $this->assertTrue(is_array($response));
130
        $this->assertEquals([
131
            'foo' => 'bar'
132
        ], $response);
133
    }
134
135
    private function getHttpClientMock()
136
    {
137
        return Mockery::mock(HttpClient::class);
138
    }
139
140
    private function sendRequest($httpClient, $url, $inputs)
141
    {
142
        $httpClient
143
            ->shouldReceive('request')
144
            ->with('POST', $url, [
145
                'headers' => [
146
                    'Accept' => 'application/json',
147
                    'Content-Type' => 'application/json',
148
                    'Authorization' => 'Bearer access-token',
149
                ],
150
                'json' => [
151
                    'inputs' => $inputs
152
                ]
153
            ])
154
            ->andReturn($httpClient);
155
156
        $httpClient
157
            ->shouldReceive('getBody')
158
            ->andReturn(json_encode(['foo' => 'bar']));
159
160
        return $httpClient;
161
    }
162
}
163