-
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 all 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 |
---|---|---|
|
@@ -85,6 +85,8 @@ Available Matchers | |
------------------ | ||
* [Array](../master/README.md#array) | ||
* [Collection](../master/README.md#collection) | ||
* [Core](../master/README.md#core) | ||
* [File](../master/README.md#file) | ||
* [Object](../master/README.md#object) | ||
* [Numbers](../master/README.md#numbers) | ||
* [Type checking](../master/README.md#type-checking) | ||
|
@@ -227,6 +229,99 @@ assertThat([2, 4, 6], hasItem(equalTo(2))); | |
assertThat([1, 3, 5], hasItems(equalTo(1), equalTo(3))); | ||
``` | ||
|
||
### File | ||
|
||
All file matchers accepts `\SplFileInfo` objects or `string` paths only. | ||
|
||
* `anExistingDirectory` - evaluates to true if the file exists and is a directory | ||
```php | ||
$directory = new \SplFileInfo('/var/log'); | ||
assertThat($directory, anExistingDirectory()); | ||
|
||
assertThat('/var/log', anExistingDirectory()); | ||
``` | ||
|
||
* `anExistingFile` - evaluates to true if the file exists and is a regular file | ||
```php | ||
$file = new \SplFileInfo('/var/log/php-fpm.log'); | ||
assertThat($file, anExistingFile()); | ||
|
||
assertThat('/var/log/php-fpm.log', anExistingFile()); | ||
``` | ||
|
||
* `anExistingFileOrDirectory` - evaluates to true if the file exists and is a regular file or a directory | ||
```php | ||
$directory = new \SplFileInfo('/var/log'); | ||
$file = new \SplFileInfo('/var/log/php-fpm.log'); | ||
assertThat($directory, anExistingFileOrDirectory()); | ||
assertThat($file, anExistingFileOrDirectory()); | ||
|
||
assertThat('/var/log', anExistingFileOrDirectory()); | ||
assertThat('/var/log/php-fpm.log', anExistingFileOrDirectory()); | ||
``` | ||
|
||
* `aReadableFile` - evaluates to true if the file is readable | ||
```php | ||
$file = new \SplFileInfo('/var/log/php-fpm.log'); | ||
assertThat($file, aReadableFile()); | ||
|
||
assertThat('/var/log/php-fpm.log', aReadableFile()); | ||
``` | ||
|
||
* `aWritableFile` - evaluates to true if the file is writable | ||
```php | ||
$file = new \SplFileInfo('/var/log/php-fpm.log'); | ||
assertThat($file, aWritableFile()); | ||
|
||
assertThat('/var/log/php-fpm.log', aWritableFile()); | ||
``` | ||
|
||
* `aFileWithSize` - evaluates to true if the file size is equal to given value or the provided matcher matches the file size | ||
```php | ||
$file = new \SplFileInfo('/var/log/php-fpm.log'); | ||
assertThat($file, aFileWithSize(42)); | ||
assertThat($file, aFileWithSize(greaterThan(42))); | ||
|
||
assertThat('/var/log/php-fpm.log', aFileWithSize(42)); | ||
assertThat('/var/log/php-fpm.log', aFileWithSize(greaterThan(42))); | ||
``` | ||
|
||
* `anEmptyFile` - evaluates to true if the file is empty | ||
```php | ||
$file = new \SplFileInfo('/var/log/php-fpm.log'); | ||
assertThat($file, anEmptyFile()); | ||
|
||
assertThat('/var/log/php-fpm.log', anEmptyFile()); | ||
``` | ||
|
||
* `aNonEmptyFile` - evaluates to true if the file is not empty | ||
```php | ||
$file = new \SplFileInfo('/var/log/php-fpm.log'); | ||
assertThat($file, aNonEmptyFile()); | ||
|
||
assertThat('/var/log/php-fpm.log', aNonEmptyFile()); | ||
``` | ||
|
||
* `aFileNamed` - evaluates to true if the file name is equal to given value or the provided matcher matches the file name | ||
```php | ||
$file = new \SplFileInfo('/var/log/php-fpm.log'); | ||
assertThat($file, aFileNamed('php-fpm.log')); | ||
assertThat($file, aFileNamed(startsWith('php'))); | ||
|
||
assertThat('/var/log/php-fpm.log', aFileNamed('php-fpm.log')); | ||
assertThat('/var/log/php-fpm.log', aFileNamed(startsWith('php'))); | ||
``` | ||
|
||
* `aFileWithCanonicalPath` - evaluates to true if the file canonical path is equal to given value or the provided matcher matches the canonical path of the file | ||
```php | ||
$file = new \SplFileInfo('/var/log/./php-fpm.log'); | ||
assertThat($file, aFileWithCanonicalPath('/var/log/php-fpm.log')); | ||
assertThat($file, aFileWithCanonicalPath(endsWith('log/php-fpm.log'))); | ||
|
||
assertThat('/var/log/./php-fpm.log', aFileWithCanonicalPath('/var/log/php-fpm.log')); | ||
assertThat('/var/log/./php-fpm.log', aFileWithCanonicalPath(endsWith('log/php-fpm.log'))); | ||
``` | ||
|
||
Comment on lines
+305
to
+324
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. In the Java version of the library, these matchers only accept another matcher as an argument but don't accept file reference (string or SPL). |
||
### Object | ||
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. The |
||
|
||
* `hasToString` - check `__toString` or `toString` method | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,11 @@ public function describeTo(Description $description) | |
} | ||
} | ||
|
||
public function describeMismatch($item, Description $description) | ||
{ | ||
$this->_matcher->describeMismatch($item, $description); | ||
} | ||
|
||
Comment on lines
+54
to
+58
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 this is needed? All existing matches seem to work without this. |
||
/** | ||
* Wraps an existing matcher and overrides the description when it fails. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
/** | ||
* @author Vasek Brychta <[email protected]> | ||
*/ | ||
|
||
namespace Hamcrest\File; | ||
|
||
use Hamcrest\BaseMatcher; | ||
use Hamcrest\Description; | ||
|
||
abstract class FileMatcher extends BaseMatcher | ||
{ | ||
/** | ||
* Description of the matcher that will be returned from the {@link describeTo()} method. | ||
* | ||
* @var string | ||
*/ | ||
private $ownDescription; | ||
|
||
/** | ||
* Description of a mismatch for the matcher, it will be prepended by the actual value that does not match. | ||
* | ||
* @var string | ||
*/ | ||
private $failureDescription; | ||
|
||
/** | ||
* @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); | ||
} | ||
} | ||
|
||
/** | ||
* Implement this method to provide the actual functionality of the matcher. | ||
* It is guaranteed that the {@link $file} parameter is always {@code \SplFileInfo}. | ||
* | ||
* @param \SplFileInfo $file | ||
* @return bool | ||
*/ | ||
abstract protected function matchesFile(\SplFileInfo $file); | ||
|
||
protected function describeFileMismatch(\SplFileInfo $file, Description $mismatchDescription) | ||
{ | ||
$mismatchDescription->appendValue($file)->appendText(' ')->appendText($this->failureDescription); | ||
} | ||
|
||
/** | ||
* @param $value | ||
* @return bool | ||
*/ | ||
private function isSafeType($value) | ||
{ | ||
return $value instanceof \SplFileInfo || is_string($value); | ||
} | ||
|
||
/** | ||
* @param string|\SplFileInfo $item | ||
* @return \SplFileInfo | ||
*/ | ||
private function createSplFileInfoObjectFromPath($item) | ||
{ | ||
if (is_string($item)) | ||
return new \SplFileInfo($item); | ||
|
||
return $item; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/** | ||
* @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) | ||
{ | ||
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(); | ||
} | ||
} |
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.
Are you sure, that such matches exist in the Java version of the library?