Skip to content

Commit a9ef530

Browse files
committed
Adding some context to testing, and both parameter and return types
1 parent e79f7a7 commit a9ef530

File tree

2 files changed

+62
-47
lines changed

2 files changed

+62
-47
lines changed

specs/Endpoint/media.spec.php

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,75 @@
1515
$this->mediaEndpoint = new Media($this->wpClient->reveal());
1616
});
1717

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();
2437
});
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-
});
3238

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();
3857
});
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();
4558
});
4659

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();
5266

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());
5771

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();
6882

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+
});
7287
});
7388

7489
afterEach(function () {

src/Endpoint/Media.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected function getEndpoint()
2626
* @throws \RuntimeException
2727
* @return array
2828
*/
29-
public function upload($filePath, $data = [], $mimeType = null)
29+
public function upload(string $filePath, array $data = [], $mimeType = null) : array
3030
{
3131
$url = $this->getEndpoint();
3232

0 commit comments

Comments
 (0)