Passed
Push — master ( 7f5941...009dcf )
by Radosław
02:23
created

BingTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 100
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Radowoj\Searcher\SearchProvider;
4
5
use PHPUnit\Framework\TestCase;
6
use Radowoj\Searcher\SearchProvider\Bing;
7
use Radowoj\Searcher\SearchProvider\ISearchProvider;
8
use GuzzleHttp\Client as GuzzleClient;
9
10
class BingTest extends TestCase
11
{
12
13
    const TEST_API_KEY = 'foo-api-key';
14
15
    protected $guzzleMockBuilder = null;
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
        $this->assertInstanceOf(ISearchProvider::class, $bing);
27
    }
28
29
30
31
    public function providerSearchRequestParams()
32
    {
33
        return [
34
            'just search string' => ['search string'],
35
            'string with limit' => ['some search string', 42],
36
            'search string with limit and offset' => ['foo search string', 36, 11]
37
        ];
38
    }
39
40
41
    protected function getResponseMock($returnValue = null)
42
    {
43
        if (is_null($returnValue)) {
44
            $returnValue = (object)[
45
                'webPages' => [
46
                    'value' => [],
47
                    'totalEstimatedMatches' => 0,
48
                ]
49
            ];
50
        }
51
52
        $responseMock = $this->getMockBuilder(GuzzleHttp\Psr7\Response::class)
53
            ->setMethods(['getBody'])
54
            ->getMock();
55
56
        $responseMock->expects($this->once())
57
            ->method('getBody')
58
            ->willReturn(json_encode(
59
                $returnValue
60
            ));
61
62
        return $responseMock;
63
    }
64
65
66
    /**
67
     * @dataProvider providerSearchRequestParams
68
     */
69
    public function testSearchRequest($searchString, $limit = 100, $offset = 0)
70
    {
71
        $encodedSearchString = urlencode($searchString);
72
73
        $guzzleMock = $this->guzzleMockBuilder->setMethods(['request'])->getMock();
74
75
        $responseMock = $this->getResponseMock();
76
77
        $guzzleMock->expects($this->once())
78
            ->method('request')
79
            ->with(
80
                'GET',
81
                "https://api.cognitive.microsoft.com/bing/v5.0/search?q={$encodedSearchString}&count={$limit}&offset={$offset}"
82
            )->willReturn($responseMock);
83
84
        $bing = new Bing(self::TEST_API_KEY, $guzzleMock);
85
86
        $bing->search($searchString, $limit, $offset);
87
    }
88
89
90
    /**
91
     * @expectedException InvalidArgumentException
92
     * @expectedExceptionMessage Invalid Bing API response
93
     */
94
    public function testExceptionOnInvalidApiResponse()
95
    {
96
        $guzzleMock = $this->guzzleMockBuilder->setMethods(['request'])->getMock();
97
98
        $responseMock = $this->getResponseMock((object)[]);
99
100
        $guzzleMock->expects($this->once())
101
            ->method('request')
102
            ->willReturn($responseMock);
103
104
        $bing = new Bing(self::TEST_API_KEY, $guzzleMock);
105
106
        $bing->search('foo bar');
107
    }
108
109
}
110