Failed Conditions
Push — master ( 165034...7cb488 )
by Adrien
09:17
created

testWEBSERVICEThrowsIfNotClientConfigured()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Web;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Web;
6
use PhpOffice\PhpSpreadsheet\Settings;
7
use PHPUnit\Framework\TestCase;
8
use Psr\Http\Client\ClientInterface;
9
use Psr\Http\Message\RequestFactoryInterface;
10
use Psr\Http\Message\RequestInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\StreamInterface;
13
14
class WebServiceTest extends TestCase
15
{
16
    protected function tearDown(): void
17
    {
18
        Settings::unsetHttpClient();
19
    }
20
21
    /**
22
     * @dataProvider providerWEBSERVICE
23
     */
24
    public function testWEBSERVICE(string $expectedResult, string $url, ?array $responseData): void
25
    {
26
        if ($responseData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $responseData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
27
            $body = $this->createMock(StreamInterface::class);
28
            $body->expects(self::atMost(1))->method('getContents')->willReturn($responseData[1]);
29
30
            $response = $this->createMock(ResponseInterface::class);
31
            $response->expects(self::once())->method('getStatusCode')->willReturn($responseData[0]);
32
            $response->expects(self::atMost(1))->method('getBody')->willReturn($body);
33
34
            $client = $this->createMock(ClientInterface::class);
35
            $client->expects(self::once())->method('sendRequest')->willReturn($response);
36
37
            $request = $this->createMock(RequestInterface::class);
38
39
            $requestFactory = $this->createMock(RequestFactoryInterface::class);
40
            $requestFactory->expects(self::atMost(1))->method('createRequest')->willReturn($request);
41
42
            Settings::setHttpClient($client, $requestFactory);
43
        }
44
45
        $result = Web::WEBSERVICE($url);
46
        self::assertEquals($expectedResult, $result);
47
    }
48
49
    public function providerWEBSERVICE(): array
50
    {
51
        return require 'tests/data/Calculation/Web/WEBSERVICE.php';
52
    }
53
54
    public function testWEBSERVICEReturnErrorWhenClientThrows(): void
55
    {
56
        $exception = $this->createMock(\Psr\Http\Client\ClientExceptionInterface::class);
57
58
        $client = $this->createMock(ClientInterface::class);
59
        $client->expects(self::once())->method('sendRequest')->willThrowException($exception);
60
61
        $request = $this->createMock(RequestInterface::class);
62
63
        $requestFactory = $this->createMock(RequestFactoryInterface::class);
64
        $requestFactory->expects(self::atMost(1))->method('createRequest')->willReturn($request);
65
66
        Settings::setHttpClient($client, $requestFactory);
67
68
        $result = Web::WEBSERVICE('https://example.com');
69
        self::assertEquals('#VALUE!', $result);
70
    }
71
72
    public function testWEBSERVICEThrowsIfNotClientConfigured(): void
73
    {
74
        $this->expectExceptionMessage('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.');
75
        Web::WEBSERVICE('https://example.com');
76
    }
77
}
78