-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added tests for `ImageServerPresenter` - added test methods in `ImageStorageExtensionTest` - added `NoImageResolverTest` - fixed PHPStan
- Loading branch information
Showing
15 changed files
with
446 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
tests/Bridge/Nette/Application/ImageServerPresenterTest.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SixtyEightPublishers\ImageStorage\Tests\Bridge\Nette\Application; | ||
|
||
use Mockery; | ||
use Tester\Assert; | ||
use Tester\TestCase; | ||
use Psr\Log\LogLevel; | ||
use Nette\Http\UrlScript; | ||
use Psr\Log\Test\TestLogger; | ||
use Nette\Http\Request as NetteRequest; | ||
use Nette\Application\Request as ApplicationRequest; | ||
use SixtyEightPublishers\FileStorage\FileStorageInterface; | ||
use SixtyEightPublishers\ImageStorage\ImageStorageInterface; | ||
use SixtyEightPublishers\FileStorage\FileStorageProviderInterface; | ||
use SixtyEightPublishers\ImageStorage\Exception\ResponseException; | ||
use SixtyEightPublishers\ImageStorage\Exception\InvalidStateException; | ||
use SixtyEightPublishers\ImageStorage\Bridge\Nette\ImageServer\ErrorResponse; | ||
use SixtyEightPublishers\ImageStorage\Bridge\Nette\ImageServer\ImageResponse; | ||
use SixtyEightPublishers\ImageStorage\Bridge\Nette\Application\ImageServerPresenter; | ||
use SixtyEightPublishers\ImageStorage\Bridge\Nette\ImageServer\Request as ImageServerRequest; | ||
|
||
require __DIR__ . '/../../../bootstrap.php'; | ||
|
||
final class ImageServerPresenterTest extends TestCase | ||
{ | ||
public function testExceptionShouldBeThrownIfStorageIsNotInstanceOfImageStorageInterface(): void | ||
{ | ||
$netteRequest = new NetteRequest( | ||
new UrlScript('https://www.example.com/images/test/w:100/image.png?_v=123') | ||
); | ||
$fileStorageProvider = Mockery::mock(FileStorageProviderInterface::class); | ||
$fileStorage = Mockery::mock(FileStorageInterface::class); | ||
|
||
$fileStorage->shouldReceive('getName') | ||
->once() | ||
->withNoArgs() | ||
->andReturn('default'); | ||
|
||
$fileStorageProvider->shouldReceive('get') | ||
->once() | ||
->with('default') | ||
->andReturn($fileStorage); | ||
|
||
$applicationRequest = new ApplicationRequest('ImageStorage:ImageServer:default', 'GET', [ | ||
'__storageName' => 'default', | ||
]); | ||
|
||
$presenter = new ImageServerPresenter($netteRequest, $fileStorageProvider); | ||
|
||
Assert::exception( | ||
static fn () => $presenter->run($applicationRequest), | ||
InvalidStateException::class, | ||
'File storage "default" must be implementor of an interface SixtyEightPublishers\ImageStorage\ImageStorageInterface.' | ||
); | ||
} | ||
|
||
public function testImageResponseShouldBeReturned(): void | ||
{ | ||
$netteRequest = new NetteRequest( | ||
new UrlScript('https://www.example.com/images/test/w:100/image.png?_v=123') | ||
); | ||
$fileStorageProvider = Mockery::mock(FileStorageProviderInterface::class); | ||
$imageStorage = Mockery::mock(ImageStorageInterface::class); | ||
$imageResponse = Mockery::mock(ImageResponse::class); | ||
|
||
$fileStorageProvider->shouldReceive('get') | ||
->once() | ||
->with('default') | ||
->andReturn($imageStorage); | ||
|
||
$imageStorage->shouldReceive('getImageResponse') | ||
->once() | ||
->with(Mockery::type(ImageServerRequest::class)) | ||
->andReturnUsing(static function (ImageServerRequest $request) use ($netteRequest, $imageResponse): ImageResponse { | ||
Assert::same($netteRequest, $request->getOriginalRequest()); | ||
|
||
return $imageResponse; | ||
}); | ||
|
||
$applicationRequest = new ApplicationRequest('ImageStorage:ImageServer:default', 'GET', [ | ||
'__storageName' => 'default', | ||
]); | ||
|
||
$presenter = new ImageServerPresenter($netteRequest, $fileStorageProvider); | ||
|
||
Assert::same($imageResponse, $presenter->run($applicationRequest)); | ||
} | ||
|
||
public function testErrorResponseShouldBeReturned(): void | ||
{ | ||
$netteRequest = new NetteRequest( | ||
new UrlScript('https://www.example.com/images/test/w:100/image.png?_v=123') | ||
); | ||
$fileStorageProvider = Mockery::mock(FileStorageProviderInterface::class); | ||
$imageStorage = Mockery::mock(ImageStorageInterface::class); | ||
$errorResponse = Mockery::mock(ErrorResponse::class); | ||
|
||
$fileStorageProvider->shouldReceive('get') | ||
->once() | ||
->with('default') | ||
->andReturn($imageStorage); | ||
|
||
$imageStorage->shouldReceive('getImageResponse') | ||
->once() | ||
->with(Mockery::type(ImageServerRequest::class)) | ||
->andReturnUsing(static function (ImageServerRequest $request) use ($netteRequest, $errorResponse): ErrorResponse { | ||
Assert::same($netteRequest, $request->getOriginalRequest()); | ||
|
||
return $errorResponse; | ||
}); | ||
|
||
$applicationRequest = new ApplicationRequest('ImageStorage:ImageServer:default', 'GET', [ | ||
'__storageName' => 'default', | ||
]); | ||
|
||
$presenter = new ImageServerPresenter($netteRequest, $fileStorageProvider); | ||
|
||
Assert::same($errorResponse, $presenter->run($applicationRequest)); | ||
} | ||
|
||
public function testErrorResponseShouldBeReturnedAndLogged(): void | ||
{ | ||
$netteRequest = new NetteRequest( | ||
new UrlScript('https://www.example.com/images/test/w:100/image.png?_v=123') | ||
); | ||
$fileStorageProvider = Mockery::mock(FileStorageProviderInterface::class); | ||
$imageStorage = Mockery::mock(ImageStorageInterface::class); | ||
$errorResponse = Mockery::mock(ErrorResponse::class); | ||
$exception = new ResponseException('File not found.', 404); | ||
$logger = new TestLogger(); | ||
|
||
$fileStorageProvider->shouldReceive('get') | ||
->once() | ||
->with('default') | ||
->andReturn($imageStorage); | ||
|
||
$imageStorage->shouldReceive('getImageResponse') | ||
->once() | ||
->with(Mockery::type(ImageServerRequest::class)) | ||
->andReturnUsing(static function (ImageServerRequest $request) use ($netteRequest, $errorResponse): ErrorResponse { | ||
Assert::same($netteRequest, $request->getOriginalRequest()); | ||
|
||
return $errorResponse; | ||
}); | ||
|
||
$errorResponse->shouldReceive('getException') | ||
->once() | ||
->withNoArgs() | ||
->andReturn($exception); | ||
|
||
$applicationRequest = new ApplicationRequest('ImageStorage:ImageServer:default', 'GET', [ | ||
'__storageName' => 'default', | ||
]); | ||
|
||
$presenter = new ImageServerPresenter($netteRequest, $fileStorageProvider, $logger); | ||
|
||
Assert::same($errorResponse, $presenter->run($applicationRequest)); | ||
Assert::same([ | ||
[ | ||
'level' => LogLevel::ERROR, | ||
'message' => 'File not found.', | ||
'context' => [ | ||
'exception' => $exception, | ||
], | ||
], | ||
], $logger->records); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
Mockery::close(); | ||
} | ||
} | ||
|
||
(new ImageServerPresenterTest())->run(); |
Oops, something went wrong.