|
15 | 15 | $this->mediaEndpoint = new Media($this->wpClient->reveal()); |
16 | 16 | }); |
17 | 17 |
|
18 | | - xit('should throw a Runtime Exception if a remote file is not found', function () { |
19 | | - $imgUrl = 'http://example.com/img/baby-squirrel-eating-pizza.jpg'; |
20 | | - $contentType = 'image/jpeg'; |
21 | | - set_error_handler(function (int $errno, string $errstr, string $errfile) { |
22 | | - expect($errno)->to->equal(E_WARNING); |
23 | | - expect(strpos($errstr, '404 Not Found'))->to->not->equal(false); |
| 18 | + context('when the file to be uploaded is not found', function () { |
| 19 | + xit('should throw a Runtime Exception for a missing remote file', function () { |
| 20 | + $imgUrl = 'http://example.com/img/baby-squirrel-eating-pizza.jpg'; |
| 21 | + $contentType = 'image/jpeg'; |
| 22 | + set_error_handler(function (int $errno, string $errstr, string $errfile) { |
| 23 | + expect($errno)->to->equal(E_WARNING); |
| 24 | + expect(strpos($errstr, '404 Not Found'))->to->not->equal(false); |
| 25 | + }); |
| 26 | + |
| 27 | + expect( |
| 28 | + [ |
| 29 | + $this->mediaEndpoint, |
| 30 | + 'upload' |
| 31 | + ] |
| 32 | + ) |
| 33 | + ->with($imgUrl, [], $contentType) |
| 34 | + ->to->throw(RuntimeException::class); |
| 35 | + |
| 36 | + restore_error_handler(); |
24 | 37 | }); |
25 | | - try { |
26 | | - $response = $this->mediaEndpoint->upload($imgUrl, [], $contentType); |
27 | | - } catch(Exception $e) { |
28 | | - expect($e)->to->be->instanceof(RuntimeException::class); |
29 | | - } |
30 | | - restore_error_handler(); |
31 | | - }); |
32 | 38 |
|
33 | | - it('should throw a Runtime Exception if the file is not found', function () { |
34 | | - $filename = realpath(dirname('../')) . '/file-does-not-exist.txt'; |
35 | | - set_error_handler(function (int $errno, string $errstr, string $errfile) { |
36 | | - expect($errno)->to->equal(E_WARNING); |
37 | | - expect(strpos($errstr, ' No such file or directory'))->to->not->equal(false); |
| 39 | + it('should throw a Runtime Exception for a missing local file', function () { |
| 40 | + $filename = realpath(dirname('../')) . '/file-does-not-exist.txt'; |
| 41 | + |
| 42 | + set_error_handler(function (int $errno, string $errstr, string $errfile) { |
| 43 | + expect($errno)->to->equal(E_WARNING); |
| 44 | + expect(strpos($errstr, ' No such file or directory'))->to->not->equal(false); |
| 45 | + }); |
| 46 | + |
| 47 | + expect( |
| 48 | + [ |
| 49 | + $this->mediaEndpoint, |
| 50 | + 'upload' |
| 51 | + ] |
| 52 | + ) |
| 53 | + ->with($filename) |
| 54 | + ->to->throw(RuntimeException::class); |
| 55 | + |
| 56 | + restore_error_handler(); |
38 | 57 | }); |
39 | | - try { |
40 | | - $response = $this->mediaEndpoint->upload($filename); |
41 | | - } catch(Exception $e) { |
42 | | - expect($e)->to->be->instanceof(RuntimeException::class); |
43 | | - } |
44 | | - restore_error_handler(); |
45 | 58 | }); |
46 | 59 |
|
47 | | - it('should attempt to create a new media item using a POST request', function () { |
48 | | - // mocking the response... |
49 | | - $streamResponse = $this->getProphet()->prophesize(StreamInterface::class); |
50 | | - $streamResponse->getContents()->willReturn(json_encode(['id' => 32, 'date' => (new DateTime())->format('c')])) |
51 | | - ->shouldBeCalled(); |
| 60 | + context('when the file to be upload exists', function () { |
| 61 | + it('should attempt to create a new media item using a POST request', function () { |
| 62 | + // mocking the response... |
| 63 | + $streamResponse = $this->getProphet()->prophesize(StreamInterface::class); |
| 64 | + $streamResponse->getContents()->willReturn(json_encode(['id' => 32, 'date' => (new DateTime())->format('c')])) |
| 65 | + ->shouldBeCalled(); |
52 | 66 |
|
53 | | - $response = $this->getProphet()->prophesize(ResponseInterface::class); |
54 | | - $response->hasHeader('Content-Type')->willReturn(true)->shouldBeCalled(); |
55 | | - $response->getHeader('Content-Type')->willReturn(['application/json'])->shouldBeCalled(); |
56 | | - $response->getBody()->willReturn($streamResponse->reveal()); |
| 67 | + $response = $this->getProphet()->prophesize(ResponseInterface::class); |
| 68 | + $response->hasHeader('Content-Type')->willReturn(true)->shouldBeCalled(); |
| 69 | + $response->getHeader('Content-Type')->willReturn(['application/json'])->shouldBeCalled(); |
| 70 | + $response->getBody()->willReturn($streamResponse->reveal()); |
57 | 71 |
|
58 | | - $this->wpClient |
59 | | - ->send(Argument::that(function($arg) { |
60 | | - return ($arg instanceof Request) && |
61 | | - $arg->getHeader('Content-Type') == ['text/plain'] && |
62 | | - $arg->getHeader('Content-Disposition') == ['attachment; filename="README.md"'] && |
63 | | - $arg->getMethod() == 'POST' |
64 | | - ; |
65 | | - })) |
66 | | - ->willReturn($response->reveal()) |
67 | | - ->shouldBeCalled(); |
| 72 | + $this->wpClient |
| 73 | + ->send(Argument::that(function($arg) { |
| 74 | + return ($arg instanceof Request) && |
| 75 | + $arg->getHeader('Content-Type') == ['text/plain'] && |
| 76 | + $arg->getHeader('Content-Disposition') == ['attachment; filename="README.md"'] && |
| 77 | + $arg->getMethod() == 'POST' |
| 78 | + ; |
| 79 | + })) |
| 80 | + ->willReturn($response->reveal()) |
| 81 | + ->shouldBeCalled(); |
68 | 82 |
|
69 | | - $filename = realpath(dirname('../')) . '/README.md'; |
70 | | - $response = $this->mediaEndpoint->upload($filename); |
71 | | - expect($response['id'])->to->equal(32); |
| 83 | + $filename = realpath(dirname('../')) . '/README.md'; |
| 84 | + $response = $this->mediaEndpoint->upload($filename); |
| 85 | + expect($response['id'])->to->equal(32); |
| 86 | + }); |
72 | 87 | }); |
73 | 88 |
|
74 | 89 | afterEach(function () { |
|
0 commit comments