Completed
Push — develop ( 4ff741...64bd10 )
by Tom
14:53
created

testGetClassifiersFailedAuthResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Bobbyshaw\WatsonVisualRecognition\Tests\Message;
4
5
use Bobbyshaw\WatsonVisualRecognition\Client;
6
use Bobbyshaw\WatsonVisualRecognition\Message\GetClassifiersRequest;
7
use Bobbyshaw\WatsonVisualRecognition\Tests\Base;
8
use GuzzleHttp\Client as GuzzleClient;
9
use GuzzleHttp\Handler\MockHandler;
10
use GuzzleHttp\HandlerStack;
11
use GuzzleHttp\Psr7\Response;
12
use GuzzleHttp\Psr7\Request;
13
use GuzzleHttp\Psr7\Uri;
14
15
/**
16
 * Class GetClassifiersRequestTest
17
 * @package Bobbyshaw\WatsonVisualRecognition\Tests\Message
18
 */
19
class GetClassifiersRequestTest extends Base
20
{
21
    /** @var Client */
22
    private $client;
23
    private $config;
24
    private $username = 'username';
25
    private $password = 'password';
26
    private $version = '2015-12-02';
27
28
    public function setUp()
29
    {
30
        $this->config = array(
31
            'username' => $this->username,
32
            'password' => $this->password,
33
            'version' => $this->version
34
        );
35
36
        $this->client = new Client();
37
        $this->client->initialize($this->config);
38
    }
39
40
    /**
41
     * Test that params are as expected
42
     */
43
    public function testGetData()
44
    {
45
        /** @var GetClassifiersRequest $request */
46
        $request = $this->client->getClassifiers();
47
48
        $this->assertEquals($this->config, $request->getData());
49
    }
50
51
    /**
52
     * Test the getClassifiers function HTTP request and response
53
     */
54
    public function testGetClassifiers()
55
    {
56
        $container = [];
57
        $guzzle = $this->getMockHttpClientWithHistoryAndResponses(
58
            $container,
59
            [$this->getMockHttpResponse('GetClassifiersSuccess.txt')]
60
        );
61
62
        $this->client = new Client($guzzle);
63
        $this->client->initialize(['username' => $this->username, 'password' => $this->password]);
64
65
        /** @var GetClassifiersRequest $request */
66
        $classifiersRequest = $this->client->getClassifiers();
67
68
        $response = $classifiersRequest->send();
69
        $classifiers = $response->getClassifiers();
0 ignored issues
show
Unused Code introduced by
$classifiers is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70
71
72
        // One request should be sent
73
        $this->assertCount(1, $container);
74
75
        $transaction = $container[0];
76
77
        /** @var Request $request */
78
        $request = $transaction['request'];
79
80
        /** @var Uri $uri */
81
        $uri = $request->getUri();
82
83
        // Check method
84
        $this->assertEquals('GET', $request->getMethod());
85
86
        // Check we're talking to the right host
87
        $this->assertEquals('https', $uri->getScheme());
88
        $this->assertEquals('gateway.watsonplatform.net', $uri->getHost());
89
90
        // Check version query parameter
91
        $query = \GuzzleHttp\Psr7\parse_query($uri->getQuery());
92
        $this->assertArrayHasKey('version', $query);
93
94
        // Check path
95
        $this->assertEquals('/visual-recognition-beta/api/v2/classifiers/', $uri->getPath());
96
97
        // Check basic auth
98
        $auth = $request->getHeader('Authorization');
99
        $this->assertEquals('Basic ' . base64_encode($this->username . ':' . $this->password), $auth[0]);
100
101
        $this->assertEquals($response, $classifiersRequest->getResponse());
102
    }
103
104
    /**
105
     * Test getClassifiers failed auth is handled appropriately
106
     *
107
     * @expectedException \Bobbyshaw\WatsonVisualRecognition\Exceptions\AuthException
108
     */
109
    public function testGetClassifiersFailedAuthResponse()
110
    {
111
        $container = [];
112
        $response = $this->getMockHttpResponse('FailedAuth.txt', 401);
113
        $httpClient = $this->getMockHttpClientWithHistoryAndResponses($container, [$response]);
114
115
        $this->client = new Client($httpClient);
116
        $this->client->initialize(['username' => $this->username, 'password' => $this->password]);
117
118
        /** @var GetClassifiersRequest $request */
119
        $request = $this->client->getClassifiers();
120
        $request->send();
121
    }
122
}
123