mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 02:30:06 +02:00
Allow creating paths from fixture files
We had a method that returned the full path of the fixture directory, and we frequently would concatenate that path with a file we needed. I changed it to include the file's path inside the fixture directory. That way, we avoid repeating the same patter over and over. I made the method static because we use it in data providers, which need to be static. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
8a7bc1ab7a
commit
e8fcdb661f
|
@ -14,6 +14,8 @@ use Respect\Validation\Message\Formatter;
|
||||||
use Respect\Validation\Message\Stringifier\KeepOriginalStringName;
|
use Respect\Validation\Message\Stringifier\KeepOriginalStringName;
|
||||||
use Respect\Validation\Validatable;
|
use Respect\Validation\Validatable;
|
||||||
|
|
||||||
|
use function implode;
|
||||||
|
use function ltrim;
|
||||||
use function realpath;
|
use function realpath;
|
||||||
use function Respect\Stringifier\stringify;
|
use function Respect\Stringifier\stringify;
|
||||||
use function sprintf;
|
use function sprintf;
|
||||||
|
@ -55,14 +57,6 @@ abstract class RuleTestCase extends TestCase
|
||||||
*/
|
*/
|
||||||
abstract public function providerForInvalidInput(): array;
|
abstract public function providerForInvalidInput(): array;
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the directory used to store test fixtures.
|
|
||||||
*/
|
|
||||||
public function getFixtureDirectory(): string
|
|
||||||
{
|
|
||||||
return (string) realpath(__DIR__ . '/../fixtures');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a mock of a Validatable.
|
* Create a mock of a Validatable.
|
||||||
*
|
*
|
||||||
|
@ -139,6 +133,19 @@ abstract class RuleTestCase extends TestCase
|
||||||
self::assertInvalidInput($validator, $input);
|
self::assertInvalidInput($validator, $input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the directory used to store test fixtures.
|
||||||
|
*/
|
||||||
|
public static function fixture(?string $filename = null): string
|
||||||
|
{
|
||||||
|
$parts = [(string) realpath(__DIR__ . '/../fixtures')];
|
||||||
|
if ($filename !== null) {
|
||||||
|
$parts[] = ltrim($filename, '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode('/', $parts);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $input
|
* @param mixed $input
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -33,9 +33,9 @@ final class ExtensionTest extends RuleTestCase
|
||||||
'inc' => [new Extension('inc'), 'filename.inc'],
|
'inc' => [new Extension('inc'), 'filename.inc'],
|
||||||
'bz2' => [new Extension('bz2'), 'filename.foo.bar.bz2'],
|
'bz2' => [new Extension('bz2'), 'filename.foo.bar.bz2'],
|
||||||
'php' => [new Extension('php'), new SplFileInfo(__FILE__)],
|
'php' => [new Extension('php'), new SplFileInfo(__FILE__)],
|
||||||
'png' => [new Extension('png'), $this->getFixtureDirectory() . 'valid-image.png'],
|
'png' => [new Extension('png'), self::fixture('valid-image.png')],
|
||||||
'gif' => [new Extension('gif'), $this->getFixtureDirectory() . 'valid-image.gif'],
|
'gif' => [new Extension('gif'), self::fixture('valid-image.gif')],
|
||||||
'file-invalid' => [new Extension('png'), $this->getFixtureDirectory() . 'invalid-image.png'],
|
'file-invalid' => [new Extension('png'), self::fixture('invalid-image.png')],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,8 +36,8 @@ final class FileTest extends RuleTestCase
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'filename' => [$sut, __FILE__],
|
'filename' => [$sut, __FILE__],
|
||||||
'SplFileInfo' => [$sut, new SplFileInfo($this->getFixtureDirectory() . '/valid-image.png')],
|
'SplFileInfo' => [$sut, new SplFileInfo(self::fixture('valid-image.png'))],
|
||||||
'SplFileObject' => [$sut, new SplFileObject($this->getFixtureDirectory() . '/invalid-image.png')],
|
'SplFileObject' => [$sut, new SplFileObject(self::fixture('invalid-image.png'))],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,12 +33,12 @@ final class ImageTest extends RuleTestCase
|
||||||
$rule = new Image();
|
$rule = new Image();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
[$rule, $this->getFixtureDirectory() . '/valid-image.gif'],
|
[$rule, self::fixture('valid-image.gif')],
|
||||||
[$rule, $this->getFixtureDirectory() . '/valid-image.jpg'],
|
[$rule, self::fixture('valid-image.jpg')],
|
||||||
[$rule, $this->getFixtureDirectory() . '/valid-image.png'],
|
[$rule, self::fixture('valid-image.png')],
|
||||||
[$rule, new SplFileInfo($this->getFixtureDirectory() . '/valid-image.gif')],
|
[$rule, new SplFileInfo(self::fixture('valid-image.gif'))],
|
||||||
[$rule, new SplFileInfo($this->getFixtureDirectory() . '/valid-image.jpg')],
|
[$rule, new SplFileInfo(self::fixture('valid-image.jpg'))],
|
||||||
[$rule, new SplFileObject($this->getFixtureDirectory() . '/valid-image.png')],
|
[$rule, new SplFileObject(self::fixture('valid-image.png'))],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ final class ImageTest extends RuleTestCase
|
||||||
$rule = new Image();
|
$rule = new Image();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
[$rule, $this->getFixtureDirectory() . '/invalid-image.png'],
|
[$rule, self::fixture('invalid-image.png')],
|
||||||
[$rule, 'asdf'],
|
[$rule, 'asdf'],
|
||||||
[$rule, 1],
|
[$rule, 1],
|
||||||
[$rule, true],
|
[$rule, true],
|
||||||
|
@ -62,7 +62,7 @@ final class ImageTest extends RuleTestCase
|
||||||
*/
|
*/
|
||||||
public function shouldValidateWithDefinedInstanceOfFileInfo(): void
|
public function shouldValidateWithDefinedInstanceOfFileInfo(): void
|
||||||
{
|
{
|
||||||
$input = $this->getFixtureDirectory() . '/valid-image.gif';
|
$input = self::fixture('valid-image.gif');
|
||||||
|
|
||||||
$finfo = $this->createMock(finfo::class);
|
$finfo = $this->createMock(finfo::class);
|
||||||
$finfo
|
$finfo
|
||||||
|
|
|
@ -29,7 +29,7 @@ final class ReadableTest extends RuleTestCase
|
||||||
*/
|
*/
|
||||||
public function providerForValidInput(): array
|
public function providerForValidInput(): array
|
||||||
{
|
{
|
||||||
$file = $this->getFixtureDirectory() . '/valid-image.gif';
|
$file = self::fixture('valid-image.gif');
|
||||||
$rule = new Readable();
|
$rule = new Readable();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
@ -44,7 +44,7 @@ final class ReadableTest extends RuleTestCase
|
||||||
*/
|
*/
|
||||||
public function providerForInvalidInput(): array
|
public function providerForInvalidInput(): array
|
||||||
{
|
{
|
||||||
$file = $this->getFixtureDirectory() . '/invalid-image.gif';
|
$file = self::fixture('invalid-image.gif');
|
||||||
$rule = new Readable();
|
$rule = new Readable();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
|
@ -34,11 +34,15 @@ final class WritableTest extends RuleTestCase
|
||||||
public function providerForValidInput(): array
|
public function providerForValidInput(): array
|
||||||
{
|
{
|
||||||
$sut = new Writable();
|
$sut = new Writable();
|
||||||
$filename = $this->getFixtureDirectory() . '/valid-image.png';
|
$filename = self::fixture('valid-image.png');
|
||||||
|
$directory = self::fixture();
|
||||||
|
|
||||||
|
chmod($filename, 0644);
|
||||||
|
chmod($directory, 0755);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'writable file' => [$sut, $filename],
|
'writable file' => [$sut, $filename],
|
||||||
'writable directory' => [$sut, $this->getFixtureDirectory()],
|
'writable directory' => [$sut, $directory],
|
||||||
'writable SplFileInfo file' => [$sut, new SplFileInfo($filename)],
|
'writable SplFileInfo file' => [$sut, new SplFileInfo($filename)],
|
||||||
'writable SplFileObject file' => [$sut, new SplFileObject($filename)],
|
'writable SplFileObject file' => [$sut, new SplFileObject($filename)],
|
||||||
'writable PSR-7 stream' => [$sut, $this->createPsr7Stream(true)],
|
'writable PSR-7 stream' => [$sut, $this->createPsr7Stream(true)],
|
||||||
|
@ -51,9 +55,9 @@ final class WritableTest extends RuleTestCase
|
||||||
public function providerForInvalidInput(): array
|
public function providerForInvalidInput(): array
|
||||||
{
|
{
|
||||||
$rule = new Writable();
|
$rule = new Writable();
|
||||||
$filename = $this->getFixtureDirectory() . '/non-writable';
|
$filename = self::fixture('non-writable');
|
||||||
|
|
||||||
$this->changeFileModeToUnwritable($filename);
|
chmod($filename, 0555);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'unwritable PSR-7 stream' => [$rule, $this->createPsr7Stream(false)],
|
'unwritable PSR-7 stream' => [$rule, $this->createPsr7Stream(false)],
|
||||||
|
@ -78,9 +82,4 @@ final class WritableTest extends RuleTestCase
|
||||||
|
|
||||||
return $stream;
|
return $stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function changeFileModeToUnwritable(string $filename): void
|
|
||||||
{
|
|
||||||
chmod($filename, 0555);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue