|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use GroupByInc\API\AbstractBridge; |
|
4
|
|
|
use GroupByInc\API\Bridge; |
|
5
|
|
|
use GroupByInc\API\Model\RefinementsResult; |
|
6
|
|
|
use GroupByInc\API\Model\Results; |
|
7
|
|
|
use GroupByInc\API\Query; |
|
8
|
|
|
use GroupByInc\API\Util\StringUtils; |
|
9
|
|
|
use Httpful\Request; |
|
10
|
|
|
use Httpful\Response; |
|
11
|
|
|
|
|
12
|
|
|
class BridgeTest extends PHPUnit_Framework_TestCase |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
const CLIENT_KEY = 'randomkey'; |
|
15
|
|
|
const HOST = 'localhost'; |
|
16
|
|
|
const PORT = 8080; |
|
17
|
|
|
const SEARCH_URL = 'http://localhost:8080/search'; |
|
18
|
|
|
const REFINEMENT_SEARCH_URL = 'http://localhost:8080/search/refinements'; |
|
19
|
|
|
const HEADERS = "Status 200\r\nContent-Type:application/json\n"; |
|
20
|
|
|
// Must match expected bridge json |
|
21
|
|
|
const TEST_QUERY = '{"clientKey":"randomkey","sort":[],"fields":[],"orFields":[],"refinements":[],"customUrlParams":[],"skip":0,"pageSize":10,"pruneRefinements":true,"wildcardSearchEnabled":false}'; |
|
22
|
|
|
const TEST_RESPONSE = '{"query":"foobar","pageInfo":{"recordStart":14,"recordEnd":23}}'; |
|
23
|
|
|
|
|
24
|
|
View Code Duplication |
public function testErroneousStatusCode() |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
$bridge = Phake::partialMock('GroupByInc\API\Bridge', self::CLIENT_KEY, self::HOST, self::PORT); |
|
27
|
|
|
Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 0) |
|
28
|
|
|
->thenReturn(new Response('{"foo":"bar"}', 'Status 400', Request::post(''))); |
|
29
|
|
|
|
|
30
|
|
|
$query = new Query(); |
|
31
|
|
|
try { |
|
32
|
|
|
/** @var Bridge $bridge */ |
|
33
|
|
|
$bridge->search($query); |
|
34
|
|
|
$this->fail("Should have thrown exception here"); |
|
35
|
|
|
} catch (RuntimeException $e) { |
|
36
|
|
|
if (strpos($e->getMessage(), '404 Not Found') !== false) { |
|
37
|
|
|
$this->fail("Expected status code 400, found 404"); |
|
38
|
|
|
} |
|
39
|
|
|
// Should throw exception |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
View Code Duplication |
public function testErrorOnReturnBinary() |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
$bridge = Phake::partialMock('GroupByInc\API\Bridge', self::CLIENT_KEY, self::HOST, self::PORT); |
|
46
|
|
|
Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 0) |
|
47
|
|
|
->thenReturn(new Response('{"foo":"bar"}', "Status 200\r\nContent-Type:application/bson\n", Request::post(''))); |
|
48
|
|
|
|
|
49
|
|
|
$query = new Query(); |
|
50
|
|
|
try { |
|
51
|
|
|
/** @var Bridge $bridge */ |
|
52
|
|
|
$bridge->search($query); |
|
53
|
|
|
$this->fail("Should have thrown exception here"); |
|
54
|
|
|
} catch (RuntimeException $e) { |
|
55
|
|
|
// Should throw exception |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
View Code Duplication |
public function testSearch() |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
$bridge = Phake::partialMock('GroupByInc\API\Bridge', self::CLIENT_KEY, self::HOST, self::PORT); |
|
62
|
|
|
Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 0) |
|
63
|
|
|
->thenReturn(new Response('{"query":"foobar"}', self::HEADERS, Request::post(''))); |
|
64
|
|
|
|
|
65
|
|
|
$query = new Query(); |
|
66
|
|
|
/** @var Bridge $bridge */ |
|
67
|
|
|
/** @var Results $results */ |
|
68
|
|
|
$results = $bridge->search($query); |
|
|
|
|
|
|
69
|
|
|
$this->assertEquals('foobar', $results->getQuery()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
View Code Duplication |
public function testSearchCompressedResponse() |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$bridge = Phake::partialMock('GroupByInc\API\Bridge', self::CLIENT_KEY, self::HOST, self::PORT); |
|
75
|
|
|
Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 0) |
|
76
|
|
|
->thenReturn(new Response('{"query":"foobar"}', self::HEADERS . "Content-Encoding:gzip\n", Request::post(''))); |
|
77
|
|
|
|
|
78
|
|
|
$query = new Query(); |
|
79
|
|
|
/** @var Bridge $bridge */ |
|
80
|
|
|
/** @var Results $results */ |
|
81
|
|
|
$results = $bridge->search($query); |
|
|
|
|
|
|
82
|
|
|
$this->assertEquals('foobar', $results->getQuery()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
View Code Duplication |
public function testSearchRefinements() |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
|
|
$refinementsQuery = '{"originalQuery":' . self::TEST_QUERY . ',"navigationName":"height"}'; |
|
88
|
|
|
$bridge = Phake::partialMock('GroupByInc\API\Bridge', self::CLIENT_KEY, self::HOST, self::PORT); |
|
89
|
|
|
Phake::when($bridge)->execute(self::REFINEMENT_SEARCH_URL, $refinementsQuery, 0) |
|
90
|
|
|
->thenReturn(new Response('{"navigation":{"name":"foobar"}}', self::HEADERS, Request::post(''))); |
|
91
|
|
|
|
|
92
|
|
|
$query = new Query(); |
|
93
|
|
|
/** @var Bridge $bridge */ |
|
94
|
|
|
/** @var RefinementsResult $results */ |
|
95
|
|
|
$results = $bridge->refinements($query, "height"); |
|
|
|
|
|
|
96
|
|
|
$this->assertEquals('foobar', $results->getNavigation()->getName()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
View Code Duplication |
public function testMultipleFailingCalls() |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
|
|
$bridge = Phake::partialMock('GroupByInc\API\Bridge', self::CLIENT_KEY, self::HOST, self::PORT); |
|
102
|
|
|
Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 0) |
|
103
|
|
|
->thenThrow(new Exception('the request failed!')); |
|
104
|
|
|
Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 1) |
|
105
|
|
|
->thenThrow(new Exception('the request failed!')); |
|
106
|
|
|
Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 2) |
|
107
|
|
|
->thenThrow(new Exception('the request failed!')); |
|
108
|
|
|
|
|
109
|
|
|
$query = new Query(); |
|
110
|
|
|
/** @var Bridge $bridge */ |
|
111
|
|
|
try { |
|
112
|
|
|
$bridge->search($query); |
|
113
|
|
|
$this->fail('expected a "retry failed" exceptions but got none'); |
|
114
|
|
|
} catch (Exception $e) { |
|
115
|
|
|
$this->assertTrue(StringUtils::endsWith($e->getMessage(), 'failed after ' . AbstractBridge::MAX_TRIES . ' tries')); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
View Code Duplication |
public function testSomeFailingCalls() |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
$bridge = Phake::partialMock('GroupByInc\API\Bridge', self::CLIENT_KEY, self::HOST, self::PORT); |
|
122
|
|
|
Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 0) |
|
123
|
|
|
->thenThrow(new Exception('the request failed!')); |
|
124
|
|
|
Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 1) |
|
125
|
|
|
->thenThrow(new Exception('the request failed!')); |
|
126
|
|
|
Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 2) |
|
127
|
|
|
->thenReturn(new Response('{"query":"foobar"}', self::HEADERS, Request::post(''))); |
|
128
|
|
|
|
|
129
|
|
|
$query = new Query(); |
|
130
|
|
|
/** @var Bridge $bridge */ |
|
131
|
|
|
/** @var Results $results */ |
|
132
|
|
|
$results = $bridge->search($query); |
|
|
|
|
|
|
133
|
|
|
$this->assertNotNull($results); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// public function testClusterSearch() |
|
|
|
|
|
|
137
|
|
|
// { |
|
138
|
|
|
// $http = Mockery::mock('API\Util\AbstractRequest'); |
|
139
|
|
|
// $http->shouldReceive('setOption'); |
|
140
|
|
|
// $http->shouldReceive('getHttpStatusCode')->andReturn(200); |
|
141
|
|
|
// $http->shouldReceive('execute')->andReturn(true); |
|
142
|
|
|
// $http->shouldReceive('getHttpContentType')->andReturn('application/json'); |
|
143
|
|
|
// //create fake bridge with empty query, assign the mock http session, try to search, should not get exception |
|
144
|
|
|
// $myBridge = new Bridge('randomkey', 'somewhere', 9050); |
|
145
|
|
|
// $myQuery = new Query(); |
|
146
|
|
|
// /** @noinspection PhpParamsInspection */ |
|
147
|
|
|
// $myBridge->setSessionCluster($http); |
|
148
|
|
|
// $myBridge->searchCluster($myQuery); |
|
149
|
|
|
// } |
|
150
|
|
|
} |
|
151
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.