Skip to content

Commit

Permalink
Adding some context to testing, and both parameter and return types
Browse files Browse the repository at this point in the history
  • Loading branch information
boboudreau committed Jun 16, 2020
1 parent e79f7a7 commit a9ef530
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 47 deletions.
107 changes: 61 additions & 46 deletions specs/Endpoint/media.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,75 @@
$this->mediaEndpoint = new Media($this->wpClient->reveal());
});

xit('should throw a Runtime Exception if a remote file is not found', function () {
$imgUrl = 'http://example.com/img/baby-squirrel-eating-pizza.jpg';
$contentType = 'image/jpeg';
set_error_handler(function (int $errno, string $errstr, string $errfile) {
expect($errno)->to->equal(E_WARNING);
expect(strpos($errstr, '404 Not Found'))->to->not->equal(false);
context('when the file to be uploaded is not found', function () {
xit('should throw a Runtime Exception for a missing remote file', function () {
$imgUrl = 'http://example.com/img/baby-squirrel-eating-pizza.jpg';
$contentType = 'image/jpeg';
set_error_handler(function (int $errno, string $errstr, string $errfile) {
expect($errno)->to->equal(E_WARNING);
expect(strpos($errstr, '404 Not Found'))->to->not->equal(false);
});

expect(
[
$this->mediaEndpoint,
'upload'
]
)
->with($imgUrl, [], $contentType)
->to->throw(RuntimeException::class);

restore_error_handler();
});
try {
$response = $this->mediaEndpoint->upload($imgUrl, [], $contentType);
} catch(Exception $e) {
expect($e)->to->be->instanceof(RuntimeException::class);
}
restore_error_handler();
});

it('should throw a Runtime Exception if the file is not found', function () {
$filename = realpath(dirname('../')) . '/file-does-not-exist.txt';
set_error_handler(function (int $errno, string $errstr, string $errfile) {
expect($errno)->to->equal(E_WARNING);
expect(strpos($errstr, ' No such file or directory'))->to->not->equal(false);
it('should throw a Runtime Exception for a missing local file', function () {
$filename = realpath(dirname('../')) . '/file-does-not-exist.txt';

set_error_handler(function (int $errno, string $errstr, string $errfile) {
expect($errno)->to->equal(E_WARNING);
expect(strpos($errstr, ' No such file or directory'))->to->not->equal(false);
});

expect(
[
$this->mediaEndpoint,
'upload'
]
)
->with($filename)
->to->throw(RuntimeException::class);

restore_error_handler();
});
try {
$response = $this->mediaEndpoint->upload($filename);
} catch(Exception $e) {
expect($e)->to->be->instanceof(RuntimeException::class);
}
restore_error_handler();
});

it('should attempt to create a new media item using a POST request', function () {
// mocking the response...
$streamResponse = $this->getProphet()->prophesize(StreamInterface::class);
$streamResponse->getContents()->willReturn(json_encode(['id' => 32, 'date' => (new DateTime())->format('c')]))
->shouldBeCalled();
context('when the file to be upload exists', function () {
it('should attempt to create a new media item using a POST request', function () {
// mocking the response...
$streamResponse = $this->getProphet()->prophesize(StreamInterface::class);
$streamResponse->getContents()->willReturn(json_encode(['id' => 32, 'date' => (new DateTime())->format('c')]))
->shouldBeCalled();

$response = $this->getProphet()->prophesize(ResponseInterface::class);
$response->hasHeader('Content-Type')->willReturn(true)->shouldBeCalled();
$response->getHeader('Content-Type')->willReturn(['application/json'])->shouldBeCalled();
$response->getBody()->willReturn($streamResponse->reveal());
$response = $this->getProphet()->prophesize(ResponseInterface::class);
$response->hasHeader('Content-Type')->willReturn(true)->shouldBeCalled();
$response->getHeader('Content-Type')->willReturn(['application/json'])->shouldBeCalled();
$response->getBody()->willReturn($streamResponse->reveal());

$this->wpClient
->send(Argument::that(function($arg) {
return ($arg instanceof Request) &&
$arg->getHeader('Content-Type') == ['text/plain'] &&
$arg->getHeader('Content-Disposition') == ['attachment; filename="README.md"'] &&
$arg->getMethod() == 'POST'
;
}))
->willReturn($response->reveal())
->shouldBeCalled();
$this->wpClient
->send(Argument::that(function($arg) {
return ($arg instanceof Request) &&
$arg->getHeader('Content-Type') == ['text/plain'] &&
$arg->getHeader('Content-Disposition') == ['attachment; filename="README.md"'] &&
$arg->getMethod() == 'POST'
;
}))
->willReturn($response->reveal())
->shouldBeCalled();

$filename = realpath(dirname('../')) . '/README.md';
$response = $this->mediaEndpoint->upload($filename);
expect($response['id'])->to->equal(32);
$filename = realpath(dirname('../')) . '/README.md';
$response = $this->mediaEndpoint->upload($filename);
expect($response['id'])->to->equal(32);
});
});

afterEach(function () {
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoint/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected function getEndpoint()
* @throws \RuntimeException
* @return array
*/
public function upload($filePath, $data = [], $mimeType = null)
public function upload(string $filePath, array $data = [], $mimeType = null) : array
{
$url = $this->getEndpoint();

Expand Down

0 comments on commit a9ef530

Please sign in to comment.