-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBatchRequestContentTest.php
More file actions
132 lines (111 loc) · 5.28 KB
/
Copy pathBatchRequestContentTest.php
File metadata and controls
132 lines (111 loc) · 5.28 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Microsoft\Graph\Core\Test\Requests;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\Psr7\Utils;
use Microsoft\Graph\Core\Requests\BatchRequestContent;
use Microsoft\Graph\Core\Requests\BatchRequestItem;
use Microsoft\Kiota\Abstractions\RequestAdapter;
use Microsoft\Kiota\Abstractions\RequestInformation;
use Microsoft\Kiota\Serialization\Json\JsonSerializationWriter;
use Microsoft\Kiota\Serialization\Json\JsonSerializationWriterFactory;
use PHPUnit\Framework\TestCase;
class BatchRequestContentTest extends TestCase
{
private array $requests;
private RequestInformation $requestInformation;
private RequestAdapter $mockRequestAdapter;
protected function setUp(): void
{
$this->requestInformation = new RequestInformation();
$this->requestInformation->httpMethod = "POST";
$this->requestInformation->setUri(new Uri("/v1/users"));
$this->requestInformation->setStreamContent(Utils::streamFor("Hello world!"));
$this->requests = [$this->requestInformation, $this->requestInformation, $this->requestInformation];
$this->mockRequestAdapter = $this->createStub(RequestAdapter::class);
$this->mockRequestAdapter->method('getSerializationWriterFactory')->willReturn((new JsonSerializationWriterFactory()));
}
public function testConstructor()
{
$requestContent = new BatchRequestContent([$this->requestInformation, $this->requestInformation]);
$this->assertInstanceOf(BatchRequestContent::class, $requestContent);
}
public function testMaximumNumberOfRequests()
{
$this->expectException(\InvalidArgumentException::class);
new BatchRequestContent(array_map(fn ($index) => $this->requestInformation, range(0, 22)));
}
public function testAddRequest()
{
$requestContent = new BatchRequestContent();
$requestContent->addRequest(new BatchRequestItem($this->requestInformation));
$this->assertEquals(1, sizeof($requestContent->getRequests()));
}
public function testAddRequestInformation()
{
$requestContent = new BatchRequestContent();
$requestContent->addRequestInformation($this->requestInformation);
$this->assertEquals(1, sizeof($requestContent->getRequests()));
}
public function testAddPsrRequest()
{
$requestContent = new BatchRequestContent();
$requestContent->addPsrRequest(new Request("POST", "/v1/users", [], null));
$this->assertEquals(1, sizeof($requestContent->getRequests()));
}
public function testRemoveByRequestId()
{
$requestItem = new BatchRequestItem($this->requestInformation);
$requestContext = new BatchRequestContent([$requestItem]);
$this->assertNotEmpty($requestContext->getRequests());
$requestContext->remove($requestItem->getId());
$this->assertEmpty($requestContext->getRequests());
}
public function testRemoveBatchRequestItem()
{
$requestItem = new BatchRequestItem($this->requestInformation);
$requestContext = new BatchRequestContent([$requestItem]);
$this->assertNotEmpty($requestContext->getRequests());
$requestContext->removeBatchRequestItem($requestItem);
$this->assertEmpty($requestContext->getRequests());
}
public function testSerializationWithNonJsonBody()
{
$this->requestInformation->addHeaders(['accept' => 'application/json']);
$batchRequestContent = new BatchRequestContent([$this->requestInformation]);
$serializationWriter = new JsonSerializationWriter();
$serializationWriter->writeObjectValue(null, $batchRequestContent);
$expectedJson = json_encode([
'requests' => [
[
"id" => $batchRequestContent->getRequests()[0]->getId(),
"method" => $this->requestInformation->httpMethod,
"url" => '/v1/users',
'headers' => ['content-type' => 'application/octet-stream', 'accept' => 'application/json'],
"body" => base64_encode("Hello world!")
]
]
], JSON_UNESCAPED_SLASHES);
$this->assertEquals($expectedJson, $serializationWriter->getSerializedContent()->getContents());
}
public function testSerializationWithJsonBody()
{
$this->requestInformation->removeHeader('content-type');
$this->requestInformation->setContentFromParsable($this->mockRequestAdapter, 'application/json', new TestUserModel('1', '1'));
$batchRequestContent = new BatchRequestContent([$this->requestInformation]);
$serializationWriter = new JsonSerializationWriter();
$serializationWriter->writeObjectValue(null, $batchRequestContent);
$expectedJson = json_encode([
'requests' => [
[
"id" => $batchRequestContent->getRequests()[0]->getId(),
"method" => $this->requestInformation->httpMethod,
"url" => '/v1/users',
'headers' => ['content-type' => 'application/json'],
"body" => ["id" => "1", "name" => "1"]
]
]
], JSON_UNESCAPED_SLASHES);
$this->assertEquals($expectedJson, $serializationWriter->getSerializedContent()->getContents());
}
}