1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Web; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
7
|
|
|
use GuzzleHttp\Exception\ConnectException; |
8
|
|
|
use GuzzleHttp\Handler\MockHandler; |
9
|
|
|
use GuzzleHttp\HandlerStack; |
10
|
|
|
use GuzzleHttp\Psr7\Request; |
11
|
|
|
use GuzzleHttp\Psr7\Response; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
13
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Web; |
14
|
|
|
use PhpOffice\PhpSpreadsheet\Settings; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
class WebServiceTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
protected static $client; |
20
|
|
|
|
21
|
|
|
public static function setUpBeforeClass(): void |
22
|
|
|
{ |
23
|
|
|
// Prevent URL requests being sent out |
24
|
|
|
$mock = new MockHandler([ |
25
|
|
|
new ClientException('This is not a valid URL', new Request('GET', 'test'), new Response()), |
26
|
|
|
new ConnectException('This is a 404 error', new Request('GET', 'test')), |
27
|
|
|
new Response('200', [], str_repeat('a', 40000)), |
28
|
|
|
new Response('200', [], 'This is a test'), |
29
|
|
|
]); |
30
|
|
|
|
31
|
|
|
$handlerStack = HandlerStack::create($mock); |
32
|
|
|
self::$client = new Client(['handler' => $handlerStack]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function setUp(): void |
36
|
|
|
{ |
37
|
|
|
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @dataProvider providerWEBSERVICE |
42
|
|
|
*/ |
43
|
|
|
public function testWEBSERVICE(string $expectedResult, string $url): void |
44
|
|
|
{ |
45
|
|
|
Settings::setHttpClient(self::$client); |
46
|
|
|
$result = Web::WEBSERVICE($url); |
47
|
|
|
self::assertEquals($expectedResult, $result); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function providerWEBSERVICE(): array |
51
|
|
|
{ |
52
|
|
|
return require 'tests/data/Calculation/Web/WEBSERVICE.php'; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|