2020-06-29 01:17:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Web;
|
|
|
|
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Web;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Settings;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-07-19 02:33:01 +00:00
|
|
|
use Psr\Http\Client\ClientInterface;
|
|
|
|
use Psr\Http\Message\RequestFactoryInterface;
|
|
|
|
use Psr\Http\Message\RequestInterface;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\StreamInterface;
|
2020-06-29 01:17:58 +00:00
|
|
|
|
|
|
|
class WebServiceTest extends TestCase
|
|
|
|
{
|
2020-07-19 02:33:01 +00:00
|
|
|
protected function tearDown(): void
|
2020-06-29 01:17:58 +00:00
|
|
|
{
|
2020-07-19 02:33:01 +00:00
|
|
|
Settings::unsetHttpClient();
|
2020-06-29 01:17:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerWEBSERVICE
|
|
|
|
*/
|
2020-07-19 02:33:01 +00:00
|
|
|
public function testWEBSERVICE(string $expectedResult, string $url, ?array $responseData): void
|
2020-06-29 01:17:58 +00:00
|
|
|
{
|
2020-07-19 02:33:01 +00:00
|
|
|
if ($responseData) {
|
|
|
|
$body = $this->createMock(StreamInterface::class);
|
|
|
|
$body->expects(self::atMost(1))->method('getContents')->willReturn($responseData[1]);
|
|
|
|
|
|
|
|
$response = $this->createMock(ResponseInterface::class);
|
|
|
|
$response->expects(self::once())->method('getStatusCode')->willReturn($responseData[0]);
|
|
|
|
$response->expects(self::atMost(1))->method('getBody')->willReturn($body);
|
|
|
|
|
|
|
|
$client = $this->createMock(ClientInterface::class);
|
|
|
|
$client->expects(self::once())->method('sendRequest')->willReturn($response);
|
|
|
|
|
|
|
|
$request = $this->createMock(RequestInterface::class);
|
|
|
|
|
|
|
|
$requestFactory = $this->createMock(RequestFactoryInterface::class);
|
|
|
|
$requestFactory->expects(self::atMost(1))->method('createRequest')->willReturn($request);
|
|
|
|
|
|
|
|
Settings::setHttpClient($client, $requestFactory);
|
|
|
|
}
|
|
|
|
|
2020-06-29 01:17:58 +00:00
|
|
|
$result = Web::WEBSERVICE($url);
|
|
|
|
self::assertEquals($expectedResult, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providerWEBSERVICE(): array
|
|
|
|
{
|
|
|
|
return require 'tests/data/Calculation/Web/WEBSERVICE.php';
|
|
|
|
}
|
2020-07-19 02:33:01 +00:00
|
|
|
|
|
|
|
public function testWEBSERVICEReturnErrorWhenClientThrows(): void
|
|
|
|
{
|
|
|
|
$exception = $this->createMock(\Psr\Http\Client\ClientExceptionInterface::class);
|
|
|
|
|
|
|
|
$client = $this->createMock(ClientInterface::class);
|
|
|
|
$client->expects(self::once())->method('sendRequest')->willThrowException($exception);
|
|
|
|
|
|
|
|
$request = $this->createMock(RequestInterface::class);
|
|
|
|
|
|
|
|
$requestFactory = $this->createMock(RequestFactoryInterface::class);
|
|
|
|
$requestFactory->expects(self::atMost(1))->method('createRequest')->willReturn($request);
|
|
|
|
|
|
|
|
Settings::setHttpClient($client, $requestFactory);
|
|
|
|
|
|
|
|
$result = Web::WEBSERVICE('https://example.com');
|
|
|
|
self::assertEquals('#VALUE!', $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWEBSERVICEThrowsIfNotClientConfigured(): void
|
|
|
|
{
|
|
|
|
$this->expectExceptionMessage('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.');
|
|
|
|
Web::WEBSERVICE('https://example.com');
|
|
|
|
}
|
2020-06-29 01:17:58 +00:00
|
|
|
}
|