1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Radowoj\Searcher\SearchProvider;
|
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 $guzzleMockBuilder = null;
|
15
|
|
|
|
16
|
|
|
public function setUp()
|
17
|
|
|
{
|
18
|
|
|
$this->guzzleMockBuilder = $this->getMockBuilder(GuzzleClient::class)
|
19
|
|
|
->setMethods([]);
|
20
|
|
|
}
|
21
|
|
|
|
22
|
|
|
public function testInstantiation()
|
23
|
|
|
{
|
24
|
|
|
$bing = new Bing(self::TEST_API_KEY, $this->guzzleMockBuilder->getMock());
|
|
|
|
|
25
|
|
|
}
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
public function providerSearchRequestParams()
|
30
|
|
|
{
|
31
|
|
|
return [
|
32
|
|
|
'just search string' => ['search string'],
|
33
|
|
|
'string with limit' => ['some search string', 42],
|
34
|
|
|
'search string with limit and offset' => ['foo search string', 36, 11]
|
35
|
|
|
];
|
36
|
|
|
}
|
37
|
|
|
|
38
|
|
|
|
39
|
|
|
protected function getResponseMock($returnValue = null)
|
40
|
|
|
{
|
41
|
|
|
if (is_null($returnValue)) {
|
42
|
|
|
$returnValue = (object)[
|
43
|
|
|
'webPages' => [
|
44
|
|
|
'value' => [],
|
45
|
|
|
'totalEstimatedMatches' => 0,
|
46
|
|
|
]
|
47
|
|
|
];
|
48
|
|
|
}
|
49
|
|
|
|
50
|
|
|
$responseMock = $this->getMockBuilder(GuzzleHttp\Psr7\Response::class)
|
51
|
|
|
->setMethods(['getBody'])
|
52
|
|
|
->getMock();
|
53
|
|
|
|
54
|
|
|
$responseMock->expects($this->once())
|
55
|
|
|
->method('getBody')
|
56
|
|
|
->willReturn(json_encode(
|
57
|
|
|
$returnValue
|
58
|
|
|
));
|
59
|
|
|
|
60
|
|
|
return $responseMock;
|
61
|
|
|
}
|
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/**
|
65
|
|
|
* @dataProvider providerSearchRequestParams
|
66
|
|
|
*/
|
67
|
|
|
public function testSearchRequest($searchString, $limit = 100, $offset = 0)
|
68
|
|
|
{
|
69
|
|
|
$encodedSearchString = urlencode($searchString);
|
70
|
|
|
|
71
|
|
|
$guzzleMock = $this->guzzleMockBuilder->setMethods(['request'])->getMock();
|
72
|
|
|
|
73
|
|
|
$responseMock = $this->getResponseMock();
|
74
|
|
|
|
75
|
|
|
$guzzleMock->expects($this->once())
|
76
|
|
|
->method('request')
|
77
|
|
|
->with(
|
78
|
|
|
'GET',
|
79
|
|
|
"https://api.cognitive.microsoft.com/bing/v5.0/search?q={$encodedSearchString}&count={$limit}&offset={$offset}"
|
80
|
|
|
)->willReturn($responseMock);
|
81
|
|
|
|
82
|
|
|
$bing = new Bing(self::TEST_API_KEY, $guzzleMock);
|
83
|
|
|
|
84
|
|
|
$bing->search($searchString, $limit, $offset);
|
85
|
|
|
}
|
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/**
|
89
|
|
|
* @expectedException InvalidArgumentException
|
90
|
|
|
* @expectedExceptionMessage Invalid Bing API response
|
91
|
|
|
*/
|
92
|
|
|
public function testExceptionOnInvalidApiResponse()
|
93
|
|
|
{
|
94
|
|
|
$guzzleMock = $this->guzzleMockBuilder->setMethods(['request'])->getMock();
|
95
|
|
|
|
96
|
|
|
$responseMock = $this->getResponseMock((object)[]);
|
97
|
|
|
|
98
|
|
|
$guzzleMock->expects($this->once())
|
99
|
|
|
->method('request')
|
100
|
|
|
->willReturn($responseMock);
|
101
|
|
|
|
102
|
|
|
$bing = new Bing(self::TEST_API_KEY, $guzzleMock);
|
103
|
|
|
|
104
|
|
|
$bing->search('foo bar');
|
105
|
|
|
}
|
106
|
|
|
|
107
|
|
|
}
|
108
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.