forked from microsoftgraph/msgraph-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionWrapperTest.php
More file actions
91 lines (76 loc) · 3.9 KB
/
Copy pathExceptionWrapperTest.php
File metadata and controls
91 lines (76 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Microsoft\Graph\Core\ExceptionWrapper;
use PHPUnit\Framework\TestCase;
class ExceptionWrapperTest extends TestCase
{
protected $responseBodies;
protected $autoBadResponseExceptions;
protected $manualBadResponseExceptions;
public function setUp(): void
{
$this->responseBodies = array(
'short' => json_encode(array('body' => 'content')), // not truncated by Guzzle
'long' => json_encode(array('body' => base64_encode(random_bytes(120)) . '.')), // truncated by Guzzle
);
$this->autoBadResponseExceptions = array();
$this->manualBadResponseExceptions = array();
foreach ($this->responseBodies as $name => $responseBody) {
$autoBadResponseException = GuzzleHttp\Exception\RequestException::create(new Request("GET", "/endpoint"), new Response(400, [], $responseBody));
assert($autoBadResponseException instanceof BadResponseException);
$this->autoBadResponseExceptions[$name] = $autoBadResponseException;
$manualBadResponseException = new BadResponseException("Error: API returned 400", new Request("GET", "/endpoint"), new Response(400, [], $responseBody));
$this->manualBadResponseExceptions[$name] = $manualBadResponseException;
}
}
public function testWrapBadResponseExceptionReturnsInstanceOfSameClass()
{
$name = 'short';
$ex = $this->autoBadResponseExceptions[$name];
$wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException($ex);
$this->assertInstanceOf(get_class($ex), $wrappedException);
$ex = $this->manualBadResponseExceptions[$name];
$wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException($ex);
$this->assertInstanceOf(get_class($ex), $wrappedException);
$name = 'long';
$ex = $this->autoBadResponseExceptions[$name];
$wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException($ex);
$this->assertInstanceOf(get_class($ex), $wrappedException);
$ex = $this->manualBadResponseExceptions[$name];
$wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException($ex);
$this->assertInstanceOf(get_class($ex), $wrappedException);
}
public function testWrapAutoBadResponseExceptionHasResponseBody()
{
$name = 'short';
$responseBody = $this->responseBodies[$name];
$ex = $this->autoBadResponseExceptions[$name];
$wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException($ex);
$this->assertStringContainsString($responseBody, $wrappedException->getMessage());
$name = 'long';
$responseBody = $this->responseBodies[$name];
$ex = $this->autoBadResponseExceptions[$name];
$wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException($ex);
$this->assertStringContainsString($responseBody, $wrappedException->getMessage());
}
public function testWrapManualBadResponseExceptionHasNotResponseBody()
{
$name = 'short';
$responseBody = $this->responseBodies[$name];
$ex = $this->manualBadResponseExceptions[$name];
$wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException($ex);
$this->assertStringNotContainsString($responseBody, $wrappedException->getMessage());
$name = 'long';
$responseBody = $this->responseBodies[$name];
$ex = $this->manualBadResponseExceptions[$name];
$wrappedException = ExceptionWrapper::wrapGuzzleBadResponseException($ex);
$this->assertStringNotContainsString($responseBody, $wrappedException->getMessage());
}
public function testWrapBadResponseExceptionWithInvalidInput()
{
$this->expectException(TypeError::class);
ExceptionWrapper::wrapGuzzleBadResponseException(null);
}
}