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 <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2023-04-02 15:04:06 +02:00
parent e8fcdb661f
commit 07c4095f11
No known key found for this signature in database
GPG key ID: 221E9281655813A6
6 changed files with 217 additions and 47 deletions

View file

@ -0,0 +1,138 @@
<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* 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 '';
}
}

View file

@ -0,0 +1,65 @@
<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* 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;
}
}

View file

@ -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;
}
}

View file

@ -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)],
];
}

View file

@ -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()],
];
}

View file

@ -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;
}
}