Skip to content

Commit

Permalink
Wildcard file name matching (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
orkhanahmadov authored Dec 21, 2023
1 parent 6b36e45 commit 98dc8ae
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Custom Laravel validation rule for checking ZIP file content.

## Requirements

- Laravel **6** or higher
- PHP **7.2** or higher with `zip` extension enabled
- Laravel **9** or higher
- PHP **8.0** or higher with `zip` extension enabled

## Installation

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
"require": {
"php": "^8.0",
"ext-zip": "*",
"illuminate/contracts": "^8.0|^9.0|^10.0",
"illuminate/support": "^8.0|^9.0|^10.0"
"illuminate/contracts": "^9.0|^10.0",
"illuminate/support": "^9.0|^10.0"
},
"require-dev": {
"orchestra/testbench": "^6.0|^7.0|^8.0",
"phpunit/phpunit": "^8.0|^9.0|^10.0"
"orchestra/testbench": "^7.0|^8.0",
"phpunit/phpunit": "^9.0|^10.0"
},
"autoload": {
"psr-4": {
Expand Down
10 changes: 6 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/coverage"/>
Expand All @@ -21,4 +18,9 @@
<php>
<server name="XDEBUG_MODE" value="coverage"/>
</php>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
7 changes: 6 additions & 1 deletion src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ public function contains(Collection $names, string $search): ?string
$options = explode('|', $search);

return $names->first(function ($name) use ($options) {
return in_array($name, $options);
foreach ($options as $option) {
if (fnmatch($option, $name)) {
return true;
}
}
return false;
});
}

Expand Down
41 changes: 32 additions & 9 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ValidatorTest extends TestCase
{
public function test_validates_with_string_and_array_list_of_files()
public function testValidatesWithStringAndArrayListOfFiles()
{
$this->assertCount(
0,
Expand All @@ -23,7 +23,7 @@ public function test_validates_with_string_and_array_list_of_files()
$this->assertSame('folder_1/text_file_2.txt', $failedFiles->first());
}

public function test_file_size_check()
public function testFileSizeCheck()
{
$this->assertCount(
0,
Expand All @@ -34,7 +34,30 @@ public function test_file_size_check()
);
}

public function test_returns_true_if_one_of_the_options_exist()
public function testCheckWithWildcard()
{
$this->assertCount(
0,
Validator::rules(['*.pdf'])->validate($this->filePath) // dummy.pdf exists
);

$this->assertCount(
0,
Validator::rules(['*.doc|folder_1/*.txt'])->validate($this->filePath) // doc does not exist but txt does
);

$this->assertCount(
1,
Validator::rules(['*.doc|*.xls'])->validate($this->filePath) // both does not exist
);

$this->assertCount(
1,
Validator::rules(['*.pdf|*.xls' => 13000])->validate($this->filePath) // both does not exist
);
}

public function testReturnsTrueIfOneOfTheOptionsExist()
{
$this->assertCount(
0,
Expand All @@ -45,31 +68,31 @@ public function test_returns_true_if_one_of_the_options_exist()
);
}

public function test_returns_false_if_file_size_does_not_meet_the_requirements()
public function testReturnsFalseIfFileSizeDoesNotMeetTheRequirements()
{
$failedFiles = Validator::rules(['dummy.pdf' => 14000, 'folder_1/text_file.txt' => 10])->validate($this->filePath);

$this->assertCount(1, $failedFiles);
$this->assertSame('folder_1/text_file.txt', $failedFiles->first());
}

public function test_returns_false_when_valid_file_size_but_wrong_file_name_passed()
public function testReturnsFalseWhenValidFileSizeButWrongFileNamePassed()
{
$failedFiles = Validator::rules(['dummy.pdf' => 14000, 'folder_1/text_file_2.txt' => 16])->validate($this->filePath);

$this->assertCount(1, $failedFiles);
$this->assertSame('folder_1/text_file_2.txt', $failedFiles->first());
}

public function test_true_when_valid_files_passed_as_mixed_simple_array_and_size_check()
public function testTrueWhenValidFilesPassedAsMixedSimpleArrayAndSizeCheck()
{
$this->assertCount(
0,
Validator::rules(['dummy.pdf', 'folder_1/text_file.txt' => 17])->validate($this->filePath)
);
}

public function test_false_when_invalid_file_passed_with_valid_file_and_size()
public function testFalseWhenInvalidFilePassedWithValidFileAndSize()
{
$failedFiles = Validator::rules(['dummy.pdf', 'folder_1/text_file.txt' => 10])->validate($this->filePath);

Expand All @@ -80,7 +103,7 @@ public function test_false_when_invalid_file_passed_with_valid_file_and_size()
$this->assertSame('folder_1/text_file.txt', $failedFiles->first());
}

public function test_validate_method()
public function testValidateMethod()
{
$zipContent = collect([
['name' => 'one', 'size' => 10],
Expand All @@ -99,7 +122,7 @@ public function test_validate_method()
$this->assertFalse(Validator::rules([], false)->checkFile($zipContent, 10, 'ten'));
}

public function test_contains_method()
public function testContainsMethod()
{
$names = collect(['one', 'two']);
$zip = new Validator([]);
Expand Down
6 changes: 3 additions & 3 deletions tests/ZipContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class ZipContentTest extends TestCase
{
public function test_returns_true_when_required_string_list_of_files_exist()
public function testReturnsTrueWhenRequiredStringListOfFilesExist()
{
$this->assertTrue(
(new ZipContent('dummy.pdf', false))
Expand All @@ -28,7 +28,7 @@ public function test_returns_true_when_required_string_list_of_files_exist()
);
}

public function test_default_error_from_translations()
public function testDefaultErrorFromTranslations()
{
$rule = new ZipContent([
'dummy.pdf',
Expand All @@ -43,7 +43,7 @@ public function test_default_error_from_translations()
);
}

public function test_returns_false_when_required_list_of_files_do_not_exist()
public function testReturnsFalseWhenRequiredListOfFilesDoNotExist()
{
Lang::addLines([
'messages.failed' => ':files',
Expand Down

0 comments on commit 98dc8ae

Please sign in to comment.