Passed
Push — master ( 20bdbe...2f8437 )
by Radosław
03:08
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;
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)
0 ignored issues
show
Bug introduced by
The property guzzleMockBuilder does not seem to exist. Did you mean guzzleMock?

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.

Loading history...
20
            ->setMethods([]);
21
    }
22
23
    public function testInstantiation()
24
    {
25
        $bing = new Bing(self::TEST_API_KEY, $this->guzzleMockBuilder->getMock());
0 ignored issues
show
Bug introduced by
The property guzzleMockBuilder does not seem to exist. Did you mean guzzleMock?

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.

Loading history...
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...
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();
0 ignored issues
show
Bug introduced by
The property guzzleMockBuilder does not seem to exist. Did you mean guzzleMock?

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.

Loading history...
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();
0 ignored issues
show
Bug introduced by
The property guzzleMockBuilder does not seem to exist. Did you mean guzzleMock?

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.

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