Passed
Push — master ( 2f8437...bbe808 )
by Radosław
02:25
created

BingTest::providerSearchRequestParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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());
0 ignored issues
show
Unused Code introduced by
$bing is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
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