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

ClassifyRequestTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
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 16
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\Message\ClassifyRequest;
6
use Bobbyshaw\WatsonVisualRecognition\Message\RequestInterface;
7
use Bobbyshaw\WatsonVisualRecognition\Tests\Base;
8
use GuzzleHttp\Psr7\Request;
9
10
/**
11
 * Class ClassifyRequestTest
12
 * @package Bobbyshaw\WatsonVisualRecognition\Tests\Message
13
 */
14
class ClassifyRequestTest extends Base
15
{
16
    /**
17
     * @var RequestInterface
18
     */
19
    protected $request;
20
21
    /** @var [] $container */
0 ignored issues
show
Documentation introduced by
The doc-type [] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
22
    protected $container = [];
23
24
    /**
25
     * Create a mock Guzzle Client with example response
26
     */
27
    public function setUp()
28
    {
29
        parent::setUp();
30
31
        $request = $this->getMockHttpResponse('ClassifySuccess.txt');
32
        $httpClient = $this->getMockHttpClientWithHistoryAndResponses($this->container, [$request]);
33
34
        $this->request = new ClassifyRequest($httpClient);
35
36
        $this->request->initialize(
37
            [
38
                'images_file' => 'Tests/images/hummingbird-1047836_640.jpg',
39
                'classifier_ids' => [1, 2, 3],
40
            ]
41
        );
42
    }
43
44
    /**
45
     * Check for expected image and classifier IDs
46
     */
47
    public function testGetData()
48
    {
49
        $data = $this->request->getData();
50
51
        $this->assertSame('Tests/images/hummingbird-1047836_640.jpg', $data['images_file']);
52
        $this->assertSame([1, 2, 3], $data['classifier_ids']);
53
    }
54
55
    /**
56
     * Test that the Classify POST HTTP Request is sent
57
     * and contains the correct data
58
     */
59
    public function testSendData()
60
    {
61
        $this->request->send();
62
63
        // Count number of requests sent
64
        $this->assertEquals(1, count($this->container));
65
66
        $transaction = $this->container[0];
67
68
        /** @var Request $httpRequest */
69
        $httpRequest = $transaction['request'];
70
71
        $uri = $httpRequest->getUri();
72
73
        $this->assertEquals('POST', $httpRequest->getMethod());
74
75
        // Check path
76
        $this->assertEquals('/visual-recognition-beta/api/v2/classify/', $uri->getPath());
77
78
        // Check post data
79
        $this->assertStringStartsWith('multipart/form-data', $httpRequest->getHeaderLine('Content-Type'));
80
81
        $body = $httpRequest->getBody()->getContents();
82
83
        $imageData = "Content-Disposition: form-data; name=\"images_file\"; filename=\"hummingbird-1047836_640.jpg\"";
84
        $this->assertContains($imageData, $body);
85
86
        $classifierIdsData = "{\"classifiers\":[{\"classifier_id\":1},{\"classifier_id\":2},{\"classifier_id\":3}]}";
87
        $this->assertContains($classifierIdsData, $body);
88
    }
89
}
90