1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Scriptotek\Alma; |
4
|
|
|
|
5
|
|
|
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement; |
6
|
|
|
use function GuzzleHttp\Psr7\stream_for; |
7
|
|
|
use Http\Client\Common\Exception\ClientErrorException; |
8
|
|
|
use Http\Factory\Guzzle\RequestFactory; |
9
|
|
|
use Http\Factory\Guzzle\ResponseFactory; |
10
|
|
|
use Prophecy\Argument; |
11
|
|
|
|
12
|
|
|
class SpecHelper |
13
|
|
|
{ |
14
|
|
|
public static function getDummyData($filename, $parse = true) |
15
|
|
|
{ |
16
|
|
|
$data = file_get_contents(__DIR__ . '/data/' . $filename); |
17
|
|
|
|
18
|
|
|
if (!$parse) { |
19
|
|
|
return $data; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
if (strpos($filename, '.xml')) { |
23
|
|
|
return QuiteSimpleXMLElement::make($data); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return json_decode($data); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function expectNoRequests($client) |
30
|
|
|
{ |
31
|
|
|
$client->getJSON(Argument::any(), Argument::any()) |
32
|
|
|
->shouldNotBeCalled(); |
33
|
|
|
|
34
|
|
|
$client->getXML(Argument::any(), Argument::any()) |
35
|
|
|
->shouldNotBeCalled(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function makeExceptionResponse( |
39
|
|
|
$body, |
40
|
|
|
$code = 400, |
41
|
|
|
$contentType = 'application/json;charset=utf-8', |
42
|
|
|
$cls = ClientErrorException::class |
43
|
|
|
) |
44
|
|
|
{ |
45
|
|
|
$requestFactory = new RequestFactory(); |
46
|
|
|
$responseFactory = new ResponseFactory(); |
47
|
|
|
|
48
|
|
|
return new $cls( |
49
|
|
|
'Error 400', |
50
|
|
|
$requestFactory->createRequest('GET', ''), |
51
|
|
|
$responseFactory->createResponse($code, 'Bad Request') |
52
|
|
|
->withHeader('Content-Type', $contentType) |
53
|
|
|
->withBody(stream_for($body)) |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|