respect-validation/tests/library/Stubs/StreamStub.php
Henrique Moody 07c4095f11
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>
2023-04-03 16:28:37 +02:00

139 lines
2.1 KiB
PHP

<?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 '';
}
}