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:
Henrique Moody 2023-04-02 14:33:29 +02:00
parent 8a7bc1ab7a
commit e8fcdb661f
No known key found for this signature in database
GPG key ID: 221E9281655813A6
6 changed files with 38 additions and 32 deletions

View file

@ -14,6 +14,8 @@ use Respect\Validation\Message\Formatter;
use Respect\Validation\Message\Stringifier\KeepOriginalStringName;
use Respect\Validation\Validatable;
use function implode;
use function ltrim;
use function realpath;
use function Respect\Stringifier\stringify;
use function sprintf;
@ -55,14 +57,6 @@ abstract class RuleTestCase extends TestCase
*/
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.
*
@ -139,6 +133,19 @@ abstract class RuleTestCase extends TestCase
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
*/

View file

@ -33,9 +33,9 @@ final class ExtensionTest extends RuleTestCase
'inc' => [new Extension('inc'), 'filename.inc'],
'bz2' => [new Extension('bz2'), 'filename.foo.bar.bz2'],
'php' => [new Extension('php'), new SplFileInfo(__FILE__)],
'png' => [new Extension('png'), $this->getFixtureDirectory() . 'valid-image.png'],
'gif' => [new Extension('gif'), $this->getFixtureDirectory() . 'valid-image.gif'],
'file-invalid' => [new Extension('png'), $this->getFixtureDirectory() . 'invalid-image.png'],
'png' => [new Extension('png'), self::fixture('valid-image.png')],
'gif' => [new Extension('gif'), self::fixture('valid-image.gif')],
'file-invalid' => [new Extension('png'), self::fixture('invalid-image.png')],
];
}

View file

@ -36,8 +36,8 @@ final class FileTest extends RuleTestCase
return [
'filename' => [$sut, __FILE__],
'SplFileInfo' => [$sut, new SplFileInfo($this->getFixtureDirectory() . '/valid-image.png')],
'SplFileObject' => [$sut, new SplFileObject($this->getFixtureDirectory() . '/invalid-image.png')],
'SplFileInfo' => [$sut, new SplFileInfo(self::fixture('valid-image.png'))],
'SplFileObject' => [$sut, new SplFileObject(self::fixture('invalid-image.png'))],
];
}

View file

@ -33,12 +33,12 @@ final class ImageTest extends RuleTestCase
$rule = new Image();
return [
[$rule, $this->getFixtureDirectory() . '/valid-image.gif'],
[$rule, $this->getFixtureDirectory() . '/valid-image.jpg'],
[$rule, $this->getFixtureDirectory() . '/valid-image.png'],
[$rule, new SplFileInfo($this->getFixtureDirectory() . '/valid-image.gif')],
[$rule, new SplFileInfo($this->getFixtureDirectory() . '/valid-image.jpg')],
[$rule, new SplFileObject($this->getFixtureDirectory() . '/valid-image.png')],
[$rule, self::fixture('valid-image.gif')],
[$rule, self::fixture('valid-image.jpg')],
[$rule, self::fixture('valid-image.png')],
[$rule, new SplFileInfo(self::fixture('valid-image.gif'))],
[$rule, new SplFileInfo(self::fixture('valid-image.jpg'))],
[$rule, new SplFileObject(self::fixture('valid-image.png'))],
];
}
@ -50,7 +50,7 @@ final class ImageTest extends RuleTestCase
$rule = new Image();
return [
[$rule, $this->getFixtureDirectory() . '/invalid-image.png'],
[$rule, self::fixture('invalid-image.png')],
[$rule, 'asdf'],
[$rule, 1],
[$rule, true],
@ -62,7 +62,7 @@ final class ImageTest extends RuleTestCase
*/
public function shouldValidateWithDefinedInstanceOfFileInfo(): void
{
$input = $this->getFixtureDirectory() . '/valid-image.gif';
$input = self::fixture('valid-image.gif');
$finfo = $this->createMock(finfo::class);
$finfo

View file

@ -29,7 +29,7 @@ final class ReadableTest extends RuleTestCase
*/
public function providerForValidInput(): array
{
$file = $this->getFixtureDirectory() . '/valid-image.gif';
$file = self::fixture('valid-image.gif');
$rule = new Readable();
return [
@ -44,7 +44,7 @@ final class ReadableTest extends RuleTestCase
*/
public function providerForInvalidInput(): array
{
$file = $this->getFixtureDirectory() . '/invalid-image.gif';
$file = self::fixture('invalid-image.gif');
$rule = new Readable();
return [

View file

@ -34,11 +34,15 @@ final class WritableTest extends RuleTestCase
public function providerForValidInput(): array
{
$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 [
'writable file' => [$sut, $filename],
'writable directory' => [$sut, $this->getFixtureDirectory()],
'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)],
@ -51,9 +55,9 @@ final class WritableTest extends RuleTestCase
public function providerForInvalidInput(): array
{
$rule = new Writable();
$filename = $this->getFixtureDirectory() . '/non-writable';
$filename = self::fixture('non-writable');
$this->changeFileModeToUnwritable($filename);
chmod($filename, 0555);
return [
'unwritable PSR-7 stream' => [$rule, $this->createPsr7Stream(false)],
@ -78,9 +82,4 @@ final class WritableTest extends RuleTestCase
return $stream;
}
private function changeFileModeToUnwritable(string $filename): void
{
chmod($filename, 0555);
}
}