1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Radowoj\Searcher;
|
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase;
|
6
|
|
|
use Radowoj\Searcher\SearchProvider\Bing;
|
7
|
|
|
use GuzzleHttp\Client as GuzzleClient;
|
8
|
|
|
|
9
|
|
|
class BingTest extends TestCase
|
10
|
|
|
{
|
11
|
|
|
|
12
|
|
|
const TEST_API_KEY = 'foo-api-key';
|
13
|
|
|
|
14
|
|
|
protected $guzzleMock = null;
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
public function setUp()
|
18
|
|
|
{
|
19
|
|
|
$this->guzzleMockBuilder = $this->getMockBuilder(GuzzleClient::class)
|
|
|
|
|
20
|
|
|
->setMethods([]);
|
21
|
|
|
}
|
22
|
|
|
|
23
|
|
|
public function testInstantiation()
|
24
|
|
|
{
|
25
|
|
|
$bing = new Bing(self::TEST_API_KEY, $this->guzzleMockBuilder->getMock());
|
|
|
|
|
26
|
|
|
}
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
|
30
|
|
|
public function providerSearchRequestParams()
|
31
|
|
|
{
|
32
|
|
|
return [
|
33
|
|
|
'just search string' => ['search string'],
|
34
|
|
|
'string with limit' => ['some search string', 42],
|
35
|
|
|
'search string with limit and offset' => ['foo search string', 36, 11]
|
36
|
|
|
];
|
37
|
|
|
}
|
38
|
|
|
|
39
|
|
|
|
40
|
|
|
protected function getResponseMock($returnValue = null)
|
41
|
|
|
{
|
42
|
|
|
if (is_null($returnValue)) {
|
43
|
|
|
$returnValue = (object)[
|
44
|
|
|
'webPages' => [
|
45
|
|
|
'value' => [],
|
46
|
|
|
'totalEstimatedMatches' => 0,
|
47
|
|
|
]
|
48
|
|
|
];
|
49
|
|
|
}
|
50
|
|
|
|
51
|
|
|
$responseMock = $this->getMockBuilder(GuzzleHttp\Psr7\Response::class)
|
52
|
|
|
->setMethods(['getBody'])
|
53
|
|
|
->getMock();
|
54
|
|
|
|
55
|
|
|
$responseMock->expects($this->once())
|
56
|
|
|
->method('getBody')
|
57
|
|
|
->willReturn(json_encode(
|
58
|
|
|
$returnValue
|
59
|
|
|
));
|
60
|
|
|
|
61
|
|
|
return $responseMock;
|
62
|
|
|
}
|
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/**
|
66
|
|
|
* @dataProvider providerSearchRequestParams
|
67
|
|
|
*/
|
68
|
|
|
public function testSearchRequest($searchString, $limit = 100, $offset = 0)
|
69
|
|
|
{
|
70
|
|
|
$encodedSearchString = urlencode($searchString);
|
71
|
|
|
|
72
|
|
|
$guzzleMock = $this->guzzleMockBuilder->setMethods(['request'])->getMock();
|
|
|
|
|
73
|
|
|
|
74
|
|
|
$responseMock = $this->getResponseMock();
|
75
|
|
|
|
76
|
|
|
$guzzleMock->expects($this->once())
|
77
|
|
|
->method('request')
|
78
|
|
|
->with(
|
79
|
|
|
'GET',
|
80
|
|
|
"https://api.cognitive.microsoft.com/bing/v5.0/search?q={$encodedSearchString}&count={$limit}&offset={$offset}"
|
81
|
|
|
)->willReturn($responseMock);
|
82
|
|
|
|
83
|
|
|
$bing = new Bing(self::TEST_API_KEY, $guzzleMock);
|
84
|
|
|
|
85
|
|
|
$bing->search($searchString, $limit, $offset);
|
86
|
|
|
}
|
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/**
|
90
|
|
|
* @expectedException InvalidArgumentException
|
91
|
|
|
* @expectedExceptionMessage Invalid Bing API response
|
92
|
|
|
*/
|
93
|
|
|
public function testExceptionOnInvalidApiResponse()
|
94
|
|
|
{
|
95
|
|
|
$guzzleMock = $this->guzzleMockBuilder->setMethods(['request'])->getMock();
|
|
|
|
|
96
|
|
|
|
97
|
|
|
$responseMock = $this->getResponseMock((object)[]);
|
98
|
|
|
|
99
|
|
|
$guzzleMock->expects($this->once())
|
100
|
|
|
->method('request')
|
101
|
|
|
->willReturn($responseMock);
|
102
|
|
|
|
103
|
|
|
$bing = new Bing(self::TEST_API_KEY, $guzzleMock);
|
104
|
|
|
|
105
|
|
|
$bing->search('foo bar');
|
106
|
|
|
}
|
107
|
|
|
|
108
|
|
|
}
|
109
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.