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