Completed
Push — master ( 9146c8...39ecb4 )
by Nicolas
04:52
created

NullTransportTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica\Test\Transport;
4
5
use Elastica\Connection;
6
use Elastica\Query;
7
use Elastica\Request;
8
use Elastica\Response;
9
use Elastica\Test\Base as BaseTest;
10
use Elastica\Transport\NullTransport;
11
12
/**
13
 * Elastica Null Transport Test.
14
 *
15
 * @author James Boehmer <[email protected]>
16
 * @author Jan Domanski <[email protected]>
17
 *
18
 * @internal
19
 */
20
class NullTransportTest extends BaseTest
21
{
22
    /** @var NullTransport NullTransport */
23
    protected $transport;
24
25
    protected function setUp(): void
26
    {
27
        parent::setUp();
28
        $this->transport = new NullTransport();
29
    }
30
31
    /**
32
     * @group functional
33
     */
34
    public function testEmptyResult(): void
35
    {
36
        // Creates a client with any destination, and verify it returns a response object when executed
37
        $client = $this->_getClient();
38
        $connection = new Connection(['transport' => 'NullTransport']);
39
        $client->setConnections([$connection]);
40
41
        $index = $client->getIndex('elasticaNullTransportTest1');
42
43
        $resultSet = $index->search(new Query());
44
        $this->assertNotNull($resultSet);
45
46
        $response = $resultSet->getResponse();
47
        $this->assertNotNull($response);
48
49
        // Validate most of the expected fields in the response data.  Consumers of the response
50
        // object have a reasonable expectation of finding "hits", "took", etc
51
        $responseData = $response->getData();
52
53
        $this->assertArrayHasKey('took', $responseData);
54
        $this->assertEquals(0, $responseData['took']);
55
        $this->assertContains('_shards', $responseData);
56
        $this->assertArrayHasKey('hits', $responseData);
57
        $this->assertArrayHasKey('total', $responseData['hits']);
58
        $this->assertEquals(0, $responseData['hits']['total']['value']);
59
        $this->assertArrayHasKey('params', $responseData);
60
61
        $took = $response->getEngineTime();
62
        $this->assertEquals(0, $took);
63
64
        $errorString = $response->getError();
65
        $this->assertEmpty($errorString);
66
67
        $shards = $response->getShardsStatistics();
68
        $this->assertArrayHasKey('total', $shards);
69
        $this->assertEquals(0, $shards['total']);
70
        $this->assertArrayHasKey('successful', $shards);
71
        $this->assertEquals(0, $shards['successful']);
72
        $this->assertArrayHasKey('failed', $shards);
73
        $this->assertEquals(0, $shards['failed']);
74
    }
75
76
    /**
77
     * @group functional
78
     */
79
    public function testExec(): void
80
    {
81
        $request = new Request('/test');
82
        $params = ['name' => 'ruflin'];
83
        $transport = new NullTransport();
84
        $response = $transport->exec($request, $params);
85
86
        $this->assertInstanceOf(Response::class, $response);
87
88
        $data = $response->getData();
89
        $this->assertEquals($params, $data['params']);
90
    }
91
92
    /**
93
     * @group unit
94
     */
95
    public function testResponse(): void
96
    {
97
        $resposeString = '';
98
        $response = new Response($resposeString);
99
        $this->transport->setResponse($response);
100
        $this->assertEquals($response, $this->transport->getResponse());
101
    }
102
103
    /**
104
     * @group unit
105
     */
106
    public function testGenerateDefaultResponse(): void
107
    {
108
        $params = ['blah' => 123];
109
        $response = $this->transport->generateDefaultResponse($params);
110
        $this->assertEquals([], $response->getTransferInfo());
111
112
        $responseData = $response->getData();
113
        $this->assertContains('params', $responseData);
114
        $this->assertEquals($params, $responseData['params']);
115
    }
116
}
117