-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLargeFileUploadTaskTest.php
More file actions
153 lines (140 loc) · 5.97 KB
/
Copy pathLargeFileUploadTaskTest.php
File metadata and controls
153 lines (140 loc) · 5.97 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace Microsoft\Graph\Core\Test\Tasks;
use DateTime;
use GuzzleHttp\Psr7\Stream;
use Http\Promise\FulfilledPromise;
use Http\Promise\Promise;
use Microsoft\Graph\Core\Models\LargeFileUploadCreateSessionBody;
use Microsoft\Graph\Core\Models\LargeFileUploadSession;
use Microsoft\Graph\Core\Models\LargeFileUploadCreateUploadSessionBody;
use Microsoft\Graph\Core\Tasks\LargeFileUploadTask;
use Microsoft\Kiota\Abstractions\RequestAdapter;
use Microsoft\Kiota\Abstractions\Serialization\Parsable;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use SplQueue;
class LargeFileUploadTaskTest extends TestCase
{
private LargeFileUploadTask $largeFileUploadTask;
private RequestAdapter $adapter;
private StreamInterface $stream;
private Promise $promise;
private LargeFileUploadSession $session;
private Parsable $mockBody;
private SplQueue $mockQueue;
private LargeFileUploadSession $mockSession;
protected function setUp(): void {
$this->largeFileUploadTask = $this->createMock(LargeFileUploadTask::class);
$this->adapter = $this->createMock(RequestAdapter::class);
$this->stream = $this->createMock(StreamInterface::class);
$this->promise = $this->createMock(Promise::class);
$this->mockBody = $this->createMock(LargeFileUploadCreateSessionBody::class);
$this->mockQueue = $this->createMock(SplQueue::class);
$this->session = new LargeFileUploadSession();
$this->mockSession = $this->createMock(LargeFileUploadSession::class);
$this->session->setUploadUrl('https://upload.example.com/session/1');
$this->mockSession->setUploadUrl('https://upload.example.com/session/1');
}
/**
* @throws \Exception
*/
public function testUpload(): void
{
$this->session->setExpirationDateTime(new DateTime('12-12-2090'));
$this->session->setUploadUrl('https://upload.example.com/session/1');
$this->session->setNextExpectedRanges(['0-100']);
$this->stream = new Stream(fopen('php://memory', 'rb+'));
$this->stream->write(str_repeat("10101", 21));
/** @phpstan-ignore-next-line */
$this->adapter->method('sendAsync')
->willReturn($this->promise);
/** @phpstan-ignore-next-line */
$this->largeFileUploadTask->method('nextChunk')
->willReturn($this->promise);
/** @phpstan-ignore-next-line */
$this->promise->method('then')
->willReturnCallback(function ($needed){
if (!is_null($needed) && is_callable($needed)) {
call_user_func($needed, $this->session);
}
});
/** @phpstan-ignore-next-line */
$this->promise->method('wait')
->willReturn($this->session);
$this->session->setNextExpectedRanges([]);
$session = $this->promise->wait();
$lfu = new LargeFileUploadTask($session, $this->adapter, $this->stream);
$lfu->upload();
$this->assertEquals($this->session, $lfu->getUploadSession());
$this->assertEmpty([]);
$this->stream->close();
}
/**
* @throws \Exception
*/
public function testUploadWithExpiredSession(): void {
$this->expectException(RuntimeException::class);
$this->session->setExpirationDateTime(new DateTime('12-12-2020'));
$this->session->setUploadUrl('https://upload.example.com/session/1');
$this->stream = new Stream(fopen('php://memory', 'rb'));
/** @phpstan-ignore-next-line */
$this->promise->method('wait')
->willReturn($this->session);
$session = $this->promise->wait();
$lfu = new LargeFileUploadTask($session, $this->adapter, $this->stream);
$lfu->upload();
}
/**
* @throws \Exception
*/
public function testCreateUploadSession(): void {
/** @phpstan-ignore-next-line */
$this->adapter->method('sendAsync')
->willReturn($this->promise);
/** @phpstan-ignore-next-line */
$this->promise->method('wait')
->willReturn($this->session);
$session = LargeFileUploadTask::createUploadSession($this->adapter, $this->mockBody, '/session/createUploadSession')->wait();
$this->assertEquals($this->session, $session);
}
public function testGetUploadSession(): void {
$this->stream = new Stream(fopen('php://memory', 'rb'));
$lfu = new LargeFileUploadTask($this->session, $this->adapter, $this->stream);
$this->assertEquals($this->session, $lfu->getUploadSession());
}
/**
* @throws \Exception
*/
public function testCancel(): void {
$this->stream = new Stream(fopen('php://memory', 'rb+'));
$this->stream->write(str_repeat("10101", 21));
/** @phpstan-ignore-next-line */
$this->adapter->method('sendNoContentAsync')
->willReturn($this->promise);
/** @phpstan-ignore-next-line */
$this->promise->method('then')
->willReturnCallback(function ($needed){
if (!is_null($needed) && is_callable($needed)) {
call_user_func($needed, $this->session);
}
return new FulfilledPromise($this->session);
});
$lfu = new LargeFileUploadTask($this->session, $this->adapter, $this->stream);
$this->assertFalse($this->session->getIsCancelled());
$lfu->cancel();
$this->assertTrue($this->session->getIsCancelled());
}
/**
* @throws \Exception
*/
public function testResume(): void {
$this->stream = new Stream(fopen('php://memory', 'rb+'));
$this->stream->write(str_repeat("10101", 21));
$this->session->setNextExpectedRanges(['10-']);
$this->session->setExpirationDateTime(new DateTime('12-12-2090'));
$lfu = new LargeFileUploadTask($this->session, $this->adapter, $this->stream);
$lfu->resume();
$this->assertEquals('10-', $lfu->getNextRange());
}
}