From 07c4095f11d2a06741a1175b72ed1a08b659ce41 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Sun, 2 Apr 2023 15:04:06 +0200 Subject: [PATCH] Create stubs for PSR-7 interfaces When we write tests requiring those interfaces, we create mocks. Those new stubs will make those tests easier to read and allow us to reduce the number of mocks we write with PHPUnit, making the code in the tests a bit less complex. Signed-off-by: Henrique Moody --- tests/library/Stubs/StreamStub.php | 138 +++++++++++++++++++++++ tests/library/Stubs/UploadedFileStub.php | 65 +++++++++++ tests/unit/Rules/ReadableTest.php | 14 +-- tests/unit/Rules/SizeTest.php | 29 +---- tests/unit/Rules/UploadedTest.php | 4 +- tests/unit/Rules/WritableTest.php | 14 +-- 6 files changed, 217 insertions(+), 47 deletions(-) create mode 100644 tests/library/Stubs/StreamStub.php create mode 100644 tests/library/Stubs/UploadedFileStub.php diff --git a/tests/library/Stubs/StreamStub.php b/tests/library/Stubs/StreamStub.php new file mode 100644 index 00000000..b5cda16c --- /dev/null +++ b/tests/library/Stubs/StreamStub.php @@ -0,0 +1,138 @@ + + * SPDX-License-Identifier: MIT + */ + +declare(strict_types=1); + +namespace Respect\Validation\Test\Stubs; + +use Psr\Http\Message\StreamInterface; + +use const SEEK_SET; + +final class StreamStub implements StreamInterface +{ + private bool $isReadable = true; + + private bool $isWritable = true; + + private ?int $size = null; + + public static function create(): self + { + return new self(); + } + + public static function createUnwritable(): self + { + $stream = new self(); + $stream->isWritable = false; + + return $stream; + } + + public static function createUnreadable(): self + { + $stream = new self(); + $stream->isReadable = false; + + return $stream; + } + + public static function createWithSize(int $size): self + { + $stream = new self(); + $stream->size = $size; + + return $stream; + } + + public function close(): void + { + } + + /** + * {@inheritDoc} + */ + public function detach() + { + return null; + } + + public function getSize(): ?int + { + return $this->size; + } + + public function tell(): int + { + return 0; + } + + public function eof(): bool + { + return true; + } + + public function isSeekable(): bool + { + return false; + } + + /** + * {@inheritDoc} + */ + public function seek($offset, $whence = SEEK_SET): void + { + } + + public function rewind(): void + { + } + + public function isWritable(): bool + { + return $this->isWritable; + } + + /** + * {@inheritDoc} + */ + public function write($string): int + { + return 0; + } + + public function isReadable(): bool + { + return $this->isReadable; + } + + /** + * {@inheritDoc} + */ + public function read($length): string + { + return ''; + } + + public function getContents(): string + { + return ''; + } + + /** + * {@inheritDoc} + */ + public function getMetadata($key = null): void + { + } + + public function __toString(): string + { + return ''; + } +} diff --git a/tests/library/Stubs/UploadedFileStub.php b/tests/library/Stubs/UploadedFileStub.php new file mode 100644 index 00000000..cdb3586a --- /dev/null +++ b/tests/library/Stubs/UploadedFileStub.php @@ -0,0 +1,65 @@ + + * SPDX-License-Identifier: MIT + */ + +declare(strict_types=1); + +namespace Respect\Validation\Test\Stubs; + +use Psr\Http\Message\StreamInterface; +use Psr\Http\Message\UploadedFileInterface; + +use const UPLOAD_ERR_OK; + +final class UploadedFileStub implements UploadedFileInterface +{ + private ?int $size = null; + + public static function create(): self + { + return new self(); + } + + public static function createWithSize(int $size): self + { + $stub = new self(); + $stub->size = $size; + + return $stub; + } + + public function getStream(): StreamInterface + { + return StreamStub::create(); + } + + /** + * {@inheritDoc} + */ + public function moveTo($targetPath): void + { + } + + public function getSize(): ?int + { + return $this->size; + } + + public function getError(): int + { + return UPLOAD_ERR_OK; + } + + public function getClientFilename(): ?string + { + return null; + } + + public function getClientMediaType(): ?string + { + return null; + } +} diff --git a/tests/unit/Rules/ReadableTest.php b/tests/unit/Rules/ReadableTest.php index 497fbfad..1a275575 100644 --- a/tests/unit/Rules/ReadableTest.php +++ b/tests/unit/Rules/ReadableTest.php @@ -9,8 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -use Psr\Http\Message\StreamInterface; use Respect\Validation\Test\RuleTestCase; +use Respect\Validation\Test\Stubs\StreamStub; use SplFileInfo; use stdClass; @@ -35,7 +35,7 @@ final class ReadableTest extends RuleTestCase return [ [$rule, $file], [$rule, new SplFileInfo($file)], - [$rule, $this->createPsr7Stream(true)], + [$rule, StreamStub::create()], ]; } @@ -51,15 +51,7 @@ final class ReadableTest extends RuleTestCase [$rule, $file], [$rule, new SplFileInfo($file)], [$rule, new stdClass()], - [$rule, $this->createPsr7Stream(false)], + [$rule, StreamStub::createUnreadable()], ]; } - - private function createPsr7Stream(bool $isReadable): StreamInterface - { - $stream = $this->createMock(StreamInterface::class); - $stream->expects(self::any())->method('isReadable')->willReturn($isReadable); - - return $stream; - } } diff --git a/tests/unit/Rules/SizeTest.php b/tests/unit/Rules/SizeTest.php index f0c1e5bd..197ba906 100644 --- a/tests/unit/Rules/SizeTest.php +++ b/tests/unit/Rules/SizeTest.php @@ -11,11 +11,10 @@ namespace Respect\Validation\Rules; use org\bovigo\vfs\content\LargeFileContent; use org\bovigo\vfs\vfsStream; -use PHPUnit\Framework\MockObject\MockObject; -use Psr\Http\Message\StreamInterface; -use Psr\Http\Message\UploadedFileInterface; use Respect\Validation\Exceptions\ComponentException; use Respect\Validation\Test\RuleTestCase; +use Respect\Validation\Test\Stubs\StreamStub; +use Respect\Validation\Test\Stubs\UploadedFileStub; use SplFileInfo; /** @@ -42,14 +41,6 @@ final class SizeTest extends RuleTestCase ->withContent(LargeFileContent::withMegabytes(2)) ->at($root); - /** @var MockObject $psr7Stream1Mb */ - $psr7Stream1Mb = $this->createMock(StreamInterface::class); - $psr7Stream1Mb->expects(self::once())->method('getSize')->willReturn(1024); - - /** @var MockObject $psr7UploadedFileMb */ - $psr7UploadedFileMb = $this->createMock(UploadedFileInterface::class); - $psr7UploadedFileMb->expects(self::once())->method('getSize')->willReturn(1024); - return [ 'file with at least 1kb' => [new Size('1kb', null), $file2Kb->url()], 'file with at least 2k' => [new Size('2kb', null), $file2Kb->url()], @@ -62,8 +53,8 @@ final class SizeTest extends RuleTestCase 'file with up to 3mb' => [new Size(null, '3mb'), $file2Mb->url()], 'file between 1mb and 3mb' => [new Size('1mb', '3mb'), $file2Mb->url()], 'SplFileInfo instance' => [new Size('1mb', '3mb'), new SplFileInfo($file2Mb->url())], - 'PSR-7 stream' => [new Size('1kb', '2kb'), $psr7Stream1Mb], - 'PSR-7 UploadedFile' => [new Size('1kb', '2kb'), $psr7UploadedFileMb], + 'PSR-7 stream' => [new Size('1kb', '2kb'), StreamStub::createWithSize(1024)], + 'PSR-7 UploadedFile' => [new Size('1kb', '2kb'), UploadedFileStub::createWithSize(1024)], ]; } @@ -80,14 +71,6 @@ final class SizeTest extends RuleTestCase ->withContent(LargeFileContent::withMegabytes(2)) ->at($root); - /** @var MockObject $psr7Stream1Mb */ - $psr7Stream1Mb = $this->createMock(StreamInterface::class); - $psr7Stream1Mb->expects(self::once())->method('getSize')->willReturn(1024); - - /** @var MockObject $psr7UploadedFileMb */ - $psr7UploadedFileMb = $this->createMock(UploadedFileInterface::class); - $psr7UploadedFileMb->expects(self::once())->method('getSize')->willReturn(1024); - return [ 'file with at least 3kb' => [new Size('3kb', null), $file2Kb->url()], 'file with up to 1kb' => [new Size(null, '1kb'), $file2Kb->url()], @@ -98,8 +81,8 @@ final class SizeTest extends RuleTestCase 'file between 1pb and 3pb' => [new Size('1pb', '3pb'), $file2Mb->url()], 'SplFileInfo instancia' => [new Size('1pb', '3pb'), new SplFileInfo($file2Mb->url())], 'parameter invalid' => [new Size('1pb', '3pb'), []], - 'PSR-7 stream' => [new Size('1MB', '1.1MB'), $psr7Stream1Mb], - 'PSR-7 UploadedFile' => [new Size('1MB', '1.1MB'), $psr7UploadedFileMb], + 'PSR-7 stream' => [new Size('1MB', '1.1MB'), StreamStub::createWithSize(1024)], + 'PSR-7 UploadedFile' => [new Size('1MB', '1.1MB'), UploadedFileStub::createWithSize(1024)], ]; } diff --git a/tests/unit/Rules/UploadedTest.php b/tests/unit/Rules/UploadedTest.php index 37ba1ca4..892bb0b7 100644 --- a/tests/unit/Rules/UploadedTest.php +++ b/tests/unit/Rules/UploadedTest.php @@ -10,8 +10,8 @@ declare(strict_types=1); namespace Respect\Validation\Rules; use PHPUnit\Framework\SkippedTestError; -use Psr\Http\Message\UploadedFileInterface; use Respect\Validation\Test\RuleTestCase; +use Respect\Validation\Test\Stubs\UploadedFileStub; use SplFileInfo; use stdClass; @@ -41,7 +41,7 @@ final class UploadedTest extends RuleTestCase return [ [$rule, self::UPLOADED_FILENAME], [$rule, new SplFileInfo(self::UPLOADED_FILENAME)], - [$rule, $this->createMock(UploadedFileInterface::class)], + [$rule, UploadedFileStub::create()], ]; } diff --git a/tests/unit/Rules/WritableTest.php b/tests/unit/Rules/WritableTest.php index 3974088f..d84431dd 100644 --- a/tests/unit/Rules/WritableTest.php +++ b/tests/unit/Rules/WritableTest.php @@ -9,8 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -use Psr\Http\Message\StreamInterface; use Respect\Validation\Test\RuleTestCase; +use Respect\Validation\Test\Stubs\StreamStub; use SplFileInfo; use SplFileObject; use stdClass; @@ -45,7 +45,7 @@ final class WritableTest extends RuleTestCase 'writable directory' => [$sut, $directory], 'writable SplFileInfo file' => [$sut, new SplFileInfo($filename)], 'writable SplFileObject file' => [$sut, new SplFileObject($filename)], - 'writable PSR-7 stream' => [$sut, $this->createPsr7Stream(true)], + 'writable PSR-7 stream' => [$sut, StreamStub::create()], ]; } @@ -60,7 +60,7 @@ final class WritableTest extends RuleTestCase chmod($filename, 0555); return [ - 'unwritable PSR-7 stream' => [$rule, $this->createPsr7Stream(false)], + 'unwritable PSR-7 stream' => [$rule, StreamStub::createUnwritable()], 'unwritable filename' => [$rule, $filename], 'unwritable SplFileInfo file' => [$rule, new SplFileInfo($filename)], 'unwritable SplFileObject file' => [$rule, new SplFileObject($filename)], @@ -74,12 +74,4 @@ final class WritableTest extends RuleTestCase 'array' => [$rule, []], ]; } - - private function createPsr7Stream(bool $isWritable): StreamInterface - { - $stream = $this->createMock(StreamInterface::class); - $stream->expects(self::any())->method('isWritable')->willReturn($isWritable); - - return $stream; - } }