-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding file matchers #73
base: master
Are you sure you want to change the base?
Changes from 3 commits
23a1edc
de2beb5
0576f73
7d90e18
c992e71
900240f
f857e2e
38958e9
4234a1e
8cb405b
5b57f05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this and don't use non-PHP 5.3+ (from I haven't seen this in any of the other files. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I saw that the tests are running for PHP 7.0 and above, so I assumed that the first supported version is PHP 7.0. Will change the code to be compatible with PHP 5.3 (btw PHP 5.3 reached end of life on 14 Aug 2014) |
||
|
||
/** | ||
* @author Vasek Brychta <[email protected]> | ||
*/ | ||
|
||
namespace Hamcrest\File; | ||
|
||
use Hamcrest\BaseMatcher; | ||
use Hamcrest\Description; | ||
|
||
abstract class FileMatcher extends BaseMatcher | ||
{ | ||
/** @var string */ | ||
private $ownDescription; | ||
/** @var string */ | ||
private $failureDescription; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please follow the code format (also in comments) of the main codebase. P.S. DocBlock style doesn't match the rest of the matchers. They either don't have DocBlocks or format them in a multi-line way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry, I was too focused on the functionality itself that I forgot to maintain the style. Is there any cookbook/style definition or should I just try to find a similar case in the codebase? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no idea. The https://github.com/hamcrest/hamcrest-php/blob/master/CONTRIBUTING.md file mentions coding standard but doesn't tell what it is. I'm assuming it's PSR-2. Anyway, it's usually best to copy/paste new files from parts of the existing file to ensure that the code style is the same. |
||
|
||
/** | ||
* @param string $ownDescription | ||
* @param string $failureDescription | ||
*/ | ||
public function __construct($ownDescription, $failureDescription) | ||
{ | ||
$this->ownDescription = $ownDescription; | ||
$this->failureDescription = $failureDescription; | ||
} | ||
|
||
public function describeTo(Description $description) | ||
{ | ||
$description->appendText($this->ownDescription); | ||
} | ||
|
||
final public function matches($item) | ||
{ | ||
return $this->isSafeType($item) && $this->matchesFile($this->createSplFileInfoObjectFromPath($item)); | ||
} | ||
|
||
final public function describeMismatch($item, Description $mismatchDescription) | ||
{ | ||
if ($this->isSafeType($item)) | ||
{ | ||
$this->describeFileMismatch($this->createSplFileInfoObjectFromPath($item), $mismatchDescription); | ||
} | ||
else | ||
{ | ||
parent::describeMismatch($item, $mismatchDescription); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code formatting. Generally, it's the best idea to configure IDE as per the coding standards of the project and mass-reformat any new code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where can I get the coding standards for this project? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no idea. The https://github.com/hamcrest/hamcrest-php/blob/master/CONTRIBUTING.md file mentions coding standard but doesn't tell what it is. I'm assuming it's PSR-2. Anyway, it's usually best to copy/paste new files from parts of the existing file to ensure that the code style is the same. |
||
} | ||
|
||
abstract protected function matchesFile(\SplFileInfo $file): bool; | ||
|
||
protected function describeFileMismatch(\SplFileInfo $file, Description $mismatchDescription) | ||
{ | ||
$mismatchDescription->appendValue($file)->appendText(' ')->appendText($this->failureDescription); | ||
} | ||
|
||
private function isSafeType($value): bool | ||
{ | ||
return $value instanceof \SplFileInfo || is_string($value); | ||
} | ||
|
||
/** | ||
* @param string|\SplFileInfo $item | ||
* @return \SplFileInfo | ||
*/ | ||
private function createSplFileInfoObjectFromPath($item): \SplFileInfo | ||
{ | ||
if (is_string($item)) | ||
return new \SplFileInfo($item); | ||
|
||
return $item; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Vasek Brychta <[email protected]> | ||
*/ | ||
|
||
namespace Hamcrest\File; | ||
|
||
class IsExistingDirectory extends FileMatcher | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('an existing directory', 'is not a directory'); | ||
} | ||
|
||
protected function matchesFile(\SplFileInfo $file): bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non a PHP 5.3+ compatible code. Likely you have more places, like this. P.S. I guess that issue needs to be fixed in a separate PR if you're up for it. |
||
{ | ||
return $file->isDir(); | ||
} | ||
|
||
/** | ||
* Evaluates to true if the file exists and is a directory. | ||
* Accepts only <code>\SplFileInfo</code> objects or <code>string</code> paths. | ||
* | ||
* @return \Hamcrest\File\IsExistingDirectory | ||
* @factory | ||
*/ | ||
public static function anExistingDirectory() | ||
{ | ||
return new self(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Vasek Brychta <[email protected]> | ||
*/ | ||
|
||
namespace Hamcrest\File; | ||
|
||
use Hamcrest\BaseMatcher; | ||
use Hamcrest\Description; | ||
|
||
class IsExistingFile extends FileMatcher | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('an existing file', 'is not a file'); | ||
} | ||
|
||
protected function matchesFile(\SplFileInfo $file): bool | ||
{ | ||
return $file->isFile(); | ||
} | ||
|
||
/** | ||
* Evaluates to true if the file exists and is a regular file. | ||
* Accepts only <code>\SplFileInfo</code> objects or <code>string</code> paths. | ||
* | ||
* @return \Hamcrest\File\IsExistingFile | ||
* @factory | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is likely to fail when used by a generator mentioned above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you think it might fail? I used the generator and it worked correctly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because I saw |
||
*/ | ||
public static function anExistingFile() | ||
{ | ||
return new self(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Vasek Brychta <[email protected]> | ||
*/ | ||
|
||
namespace Hamcrest\File; | ||
|
||
use Hamcrest\AbstractMatcherTest; | ||
|
||
class IsExistingDirectoryTest extends AbstractMatcherTest | ||
{ | ||
private static $EXISTING_DIRECTORY_PATH; | ||
private static $NON_EXISTING_DIRECTORY_PATH; | ||
private static $EXISTING_FILE_PATH; | ||
|
||
private static $EXISTING_DIRECTORY; | ||
private static $NON_EXISTING_DIRECTORY; | ||
private static $EXISTING_FILE; | ||
|
||
/** | ||
* @beforeClass | ||
*/ | ||
public static function initializeFiles() | ||
{ | ||
self::$EXISTING_DIRECTORY_PATH = __DIR__; | ||
self::$NON_EXISTING_DIRECTORY_PATH = __DIR__ . '/does-not-exist'; | ||
self::$EXISTING_FILE_PATH = __FILE__; | ||
|
||
self::$EXISTING_DIRECTORY = new \SplFileInfo(self::$EXISTING_DIRECTORY_PATH); | ||
self::$NON_EXISTING_DIRECTORY = new \SplFileInfo(self::$NON_EXISTING_DIRECTORY_PATH); | ||
self::$EXISTING_FILE = new \SplFileInfo(self::$EXISTING_FILE_PATH); | ||
} | ||
|
||
protected function createMatcher() | ||
{ | ||
return IsExistingDirectory::anExistingDirectory(); | ||
} | ||
|
||
public function testMatchesAnExistingDirectory() | ||
{ | ||
$this->assertMatches( | ||
$this->createMatcher(), | ||
self::$EXISTING_DIRECTORY, | ||
"should match a directory that actualy exists" | ||
); | ||
} | ||
|
||
public function testMatchesAnExistingDirectoryPath() | ||
{ | ||
$this->assertMatches( | ||
$this->createMatcher(), | ||
self::$EXISTING_DIRECTORY_PATH, | ||
"should match a path to directory that actualy exists" | ||
); | ||
} | ||
|
||
public function testDoesNotMatchADirectoryThatDoesNotExist() | ||
{ | ||
$this->assertDoesNotMatch( | ||
$this->createMatcher(), | ||
self::$NON_EXISTING_DIRECTORY, | ||
"should not match a directory that does not exist" | ||
); | ||
} | ||
|
||
public function testDoesNotMatchADirectoryPathThatDoesNotExist() | ||
{ | ||
$this->assertDoesNotMatch( | ||
$this->createMatcher(), | ||
self::$NON_EXISTING_DIRECTORY_PATH, | ||
"should not match a path to directory that does not exist" | ||
); | ||
} | ||
|
||
public function testDoesNotMatchAnExistingFileThatIsNotADirectory() | ||
{ | ||
$this->assertDoesNotMatch( | ||
$this->createMatcher(), | ||
self::$EXISTING_FILE, | ||
"should not match an existing file that is not a directory" | ||
); | ||
} | ||
|
||
public function testDoesNotMatchAnExistingFilePathThatIsNotADirectory() | ||
{ | ||
$this->assertDoesNotMatch( | ||
$this->createMatcher(), | ||
self::$EXISTING_FILE_PATH, | ||
"should not match a path to existing file that is not a directory" | ||
); | ||
} | ||
|
||
public function testDoesNotMatchNull() | ||
{ | ||
$this->assertDoesNotMatch( | ||
$this->createMatcher(), | ||
null, | ||
'should not match null' | ||
); | ||
} | ||
|
||
public function testHasAReadableDescription() | ||
{ | ||
$this->assertDescription('an existing directory', $this->createMatcher()); | ||
} | ||
|
||
public function testHasAReadableMissmatchDescription() | ||
{ | ||
$this->assertMismatchDescription( | ||
'<' . self::$NON_EXISTING_DIRECTORY . '> is not a directory', | ||
$this->createMatcher(), | ||
self::$NON_EXISTING_DIRECTORY | ||
); | ||
} | ||
|
||
public function testHasAReadableMissmatchDescriptionForPath() | ||
{ | ||
$this->assertMismatchDescription( | ||
'<' . self::$NON_EXISTING_DIRECTORY_PATH . '> is not a directory', | ||
$this->createMatcher(), | ||
self::$NON_EXISTING_DIRECTORY_PATH | ||
); | ||
} | ||
|
||
public function testHasAReadableTypeMissmatchDescription() | ||
{ | ||
$this->assertMismatchDescription( | ||
'was <stdClass>', | ||
$this->createMatcher(), | ||
new \stdClass() | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
aFileWithAbsolutePath
matcher wasn't implemented.