Making real tests for Exists rule

This commit is contained in:
Kennedy Tedesco 2015-10-31 12:32:25 -02:00 committed by Henrique Moody
parent 08a5ab0d81
commit 7d8b18baca
3 changed files with 55 additions and 27 deletions

View file

@ -0,0 +1,17 @@
--FILE--
<?php
require 'vendor/autoload.php';
use org\bovigo\vfs\content\LargeFileContent;
use Respect\Validation\Validator as v;
use org\bovigo\vfs\vfsStream;
$root = vfsStream::setup();
$file = vfsStream::newFile('2kb.txt')->withContent(LargeFileContent::withKilobytes(2))->at($root);
v::exists()->check($file->url());
$splFile = new SplFileInfo($file->url());
v::exists()->assert($splFile);
?>
--EXPECTF--

View file

@ -0,0 +1,16 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\ExistsException;
try {
v::exists()->check('/path/of/a/non-existent/file');
} catch (ExistsException $e) {
echo $e->getMainMessage();
}
?>
--EXPECTF--
"/path/of/a/non-existent/file" must exists

View file

@ -11,36 +11,26 @@
namespace Respect\Validation\Rules;
$GLOBALS['file_exists'] = null;
function file_exists($file)
{
$return = \file_exists($file); // Running the real function
if (null !== $GLOBALS['file_exists']) {
$return = $GLOBALS['file_exists'];
$GLOBALS['file_exists'] = null;
}
return $return;
}
use org\bovigo\vfs\content\LargeFileContent;
use PHPUnit_Framework_TestCase;
use org\bovigo\vfs\vfsStream;
use SplFileInfo;
/**
* @group rule
* @covers Respect\Validation\Rules\Exists
* @covers Respect\Validation\Exceptions\ExistsException
*/
class ExistsTest extends \PHPUnit_Framework_TestCase
class ExistsTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider fileProvider
* @covers Respect\Validation\Rules\Exists::validate
*/
public function testExistentFileShouldReturnTrue()
public function testExistentFileShouldReturnTrue($file)
{
$GLOBALS['file_exists'] = true;
$rule = new Exists();
$input = '/path/of/an/existent/file';
$this->assertTrue($rule->validate($input));
$this->assertTrue($rule->validate($file->url()));
}
/**
@ -48,23 +38,28 @@ class ExistsTest extends \PHPUnit_Framework_TestCase
*/
public function testNonExistentFileShouldReturnFalse()
{
$GLOBALS['file_exists'] = false;
$rule = new Exists();
$input = '/path/of/a/non-existent/file';
$this->assertFalse($rule->validate($input));
$this->assertFalse($rule->validate('/path/of/a/non-existent/file'));
}
/**
* @dataProvider fileProvider
* @covers Respect\Validation\Rules\Exists::validate
*/
public function testShouldValidateObjects()
public function testShouldValidateObjects($file)
{
$GLOBALS['file_exists'] = true;
$rule = new Exists();
$object = new \SplFileInfo('/path/of/an/existent/file');
$object = new SplFileInfo($file->url());
$this->assertTrue($rule->validate($object));
}
public function fileProvider()
{
$root = vfsStream::setup();
$file = vfsStream::newFile('2kb.txt')->withContent(LargeFileContent::withKilobytes(2))->at($root);
return [
[$file]
];
}
}