GraphCollectionRequestTest.php
1 year ago
GraphRequestTest.php
1 year ago
GraphResponseTest.php
1 year ago
HttpTest.php
1 year ago
MockClientFactory.php
1 year ago
StreamTest.php
1 year ago
StreamTest.php
100 lines
| 1 | <?php |
| 2 | use PHPUnit\Framework\TestCase; |
| 3 | use Microsoft\Graph\Graph; |
| 4 | use Microsoft\Graph\Http\GraphRequest; |
| 5 | use Microsoft\Graph\Exception\GraphException; |
| 6 | use org\bovigo\vfs\vfsStream; |
| 7 | use org\bovigo\vfs\vfsStreamFile; |
| 8 | use org\bovigo\vfs\vfsStreamWrapper; |
| 9 | use org\bobigo\vfs\vfsStreamDirectory; |
| 10 | |
| 11 | class StreamTest extends TestCase |
| 12 | { |
| 13 | private $root; |
| 14 | private $client; |
| 15 | private $body; |
| 16 | private $container; |
| 17 | |
| 18 | public function setUp(): void |
| 19 | { |
| 20 | $this->root = vfsStream::setup('testDir'); |
| 21 | |
| 22 | $this->body = json_encode(array('body' => 'content')); |
| 23 | $stream = AmeliaGuzzleHttp\Psr7\Utils::streamFor('content'); |
| 24 | |
| 25 | $mock = new AmeliaGuzzleHttp\Handler\MockHandler([ |
| 26 | new AmeliaGuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], $this->body), |
| 27 | new AmeliaGuzzleHttp\Psr7\Response(200,['foo' => 'bar'], $stream), |
| 28 | new AmeliaGuzzleHttp\Psr7\Response(200, ['foo' => 'bar'], 'hello') |
| 29 | ]); |
| 30 | |
| 31 | $this->container = []; |
| 32 | $history = AmeliaGuzzleHttp\Middleware::history($this->container); |
| 33 | $handler = AmeliaGuzzleHttp\HandlerStack::create($mock); |
| 34 | $handler->push($history); |
| 35 | $this->client = new AmeliaGuzzleHttp\Client(['handler' => $handler]); |
| 36 | } |
| 37 | |
| 38 | public function testUpload() |
| 39 | { |
| 40 | $file = new VfsStreamFile('foo.txt'); |
| 41 | $this->root->addChild($file); |
| 42 | $file->setContent('data'); |
| 43 | |
| 44 | $request = new GraphRequest("GET", "/me", "token", "url", "v1.0"); |
| 45 | $request->upload($file->url(), $this->client); |
| 46 | |
| 47 | $this->assertEquals($this->container[0]['request']->getBody()->getContents(), $file->getContent()); |
| 48 | } |
| 49 | |
| 50 | public function testInvalidUpload() |
| 51 | { |
| 52 | $this->expectException(Microsoft\Graph\Exception\GraphException::class); |
| 53 | |
| 54 | $file = new VfsStreamFile('foo.txt', 0000); |
| 55 | $this->root->addChild($file); |
| 56 | |
| 57 | $request = new GraphRequest("GET", "/me", "token", "url", "v1.0"); |
| 58 | $request->upload($file->url(), $this->client); |
| 59 | } |
| 60 | |
| 61 | public function testDownload() |
| 62 | { |
| 63 | $request = new GraphRequest("GET", "/me", "token", "url", "v1.0"); |
| 64 | $file = new VfsStreamFile('foo.txt'); |
| 65 | $this->root->addChild($file); |
| 66 | |
| 67 | $request->download($file->url(), $this->client); |
| 68 | $this->assertEquals($this->body, $file->getContent()); |
| 69 | } |
| 70 | |
| 71 | public function testInvalidDownload() |
| 72 | { |
| 73 | set_error_handler(function() {}); |
| 74 | try { |
| 75 | $this->expectException(Microsoft\Graph\Exception\GraphException::class); |
| 76 | |
| 77 | $file = new VfsStreamFile('foo.txt', 0000); |
| 78 | $this->root->addChild($file); |
| 79 | |
| 80 | $request = new GraphRequest("GET", "/me", "token", "url", "v1.0"); |
| 81 | $request->download($file->url(), $this->client); |
| 82 | } finally { |
| 83 | restore_error_handler(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public function testSetReturnStream() |
| 88 | { |
| 89 | $request = new GraphRequest("GET", "/me", "token", "url", "v1.0"); |
| 90 | $request->setReturnType(AmeliaGuzzleHttp\Psr7\Stream::class); |
| 91 | |
| 92 | $this->assertTrue($request->getReturnsStream()); |
| 93 | |
| 94 | $response = $request->execute($this->client); |
| 95 | $this->assertInstanceOf(AmeliaGuzzleHttp\Psr7\Stream::class, $response); |
| 96 | |
| 97 | $response = $request->execute($this->client); |
| 98 | $this->assertInstanceOf(AmeliaGuzzleHttp\Psr7\Stream::class, $response); |
| 99 | } |
| 100 | } |