Skip to content

Commit

Permalink
Assert filepath by regex (#2279)
Browse files Browse the repository at this point in the history
* Implement file path regex matching

* Implement tests

* Fix tests missing params

* Fix style?

* Fix tests once again, bad file name

* Trying to fix style once more

* Style once more ...
  • Loading branch information
fkupper authored and patrickbrouwers committed Jul 16, 2019
1 parent c9f3f3a commit 7d47c7a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Facades/Excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* @method static array toArray(object $import, string $filePath, string $disk = null, string $readerType = null)
* @method static Collection toCollection(object $import, string $filePath, string $disk = null, string $readerType = null)
* @method static PendingDispatch queueImport(object $import, string $filePath, string $disk = null, string $readerType = null)
* @method static void matchByRegex()
* @method static void doNotMatchByRegex()
* @method static void assertDownloaded(string $fileName, callable $callback = null)
* @method static void assertStored(string $filePath, string $disk = null, callable $callback = null)
* @method static void assertQueued(string $filePath, string $disk = null, callable $callback = null)
Expand Down
64 changes: 60 additions & 4 deletions src/Fakes/ExcelFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class ExcelFake implements Exporter, Importer
*/
protected $imported = [];

/**
* @var bool
*/
protected $matchByRegex = false;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -173,13 +178,35 @@ public function handle()
});
}

/**
* When asserting downloaded, stored, queued or imported, use regular expression
* to look for a matching file path.
*
* @return void
*/
public function matchByRegex()
{
$this->matchByRegex = true;
}

/**
* When asserting downloaded, stored, queued or imported, use regular string
* comparison for matching file path.
*
* @return void
*/
public function doNotMatchByRegex()
{
$this->matchByRegex = false;
}

/**
* @param string $fileName
* @param callable|null $callback
*/
public function assertDownloaded(string $fileName, $callback = null)
{
Assert::assertArrayHasKey($fileName, $this->downloads, sprintf('%s is not downloaded', $fileName));
$fileName = $this->assertArrayHasKey($fileName, $this->downloads, sprintf('%s is not downloaded', $fileName));

$callback = $callback ?: function () {
return true;
Expand All @@ -206,7 +233,7 @@ public function assertStored(string $filePath, $disk = null, $callback = null)
$disk = $disk ?? 'default';
$storedOnDisk = $this->stored[$disk] ?? [];

Assert::assertArrayHasKey(
$filePath = $this->assertArrayHasKey(
$filePath,
$storedOnDisk,
sprintf('%s is not stored on disk %s', $filePath, $disk)
Expand Down Expand Up @@ -237,7 +264,7 @@ public function assertQueued(string $filePath, $disk = null, $callback = null)
$disk = $disk ?? 'default';
$queuedForDisk = $this->queued[$disk] ?? [];

Assert::assertArrayHasKey(
$filePath = $this->assertArrayHasKey(
$filePath,
$queuedForDisk,
sprintf('%s is not queued for export on disk %s', $filePath, $disk)
Expand Down Expand Up @@ -268,7 +295,7 @@ public function assertImported(string $filePath, $disk = null, $callback = null)
$disk = $disk ?? 'default';
$importedOnDisk = $this->imported[$disk] ?? [];

Assert::assertArrayHasKey(
$filePath = $this->assertArrayHasKey(
$filePath,
$importedOnDisk,
sprintf('%s is not stored on disk %s', $filePath, $disk)
Expand All @@ -283,4 +310,33 @@ public function assertImported(string $filePath, $disk = null, $callback = null)
"The file [{$filePath}] was not imported with the expected data."
);
}

/**
* Asserts that an array has a specified key and returns the key if successful.
* @see matchByRegex for more information about file path matching
*
* @param string $key
* @param array $array
* @param string $message
*
* @return string
*
* @throws ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
* @throws Exception
*/
protected function assertArrayHasKey(string $key, array $disk, string $message = ''): string
{
if ($this->matchByRegex) {
$files = array_keys($disk);
$results = preg_grep($key, $files);
Assert::assertGreaterThan(0, count($results), $message);
Assert::assertEquals(1, count($results), "More than one result matches the file name expression '$key'.");

return $results[0];
}
Assert::assertArrayHasKey($key, $disk, $message);

return $key;
}
}
20 changes: 20 additions & 0 deletions tests/ExcelFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function can_assert_against_a_fake_downloaded_export()
ExcelFacade::assertDownloaded('downloaded-filename.csv', function (FromCollection $export) {
return $export->collection()->contains('foo');
});
ExcelFacade::matchByRegex();
ExcelFacade::assertDownloaded('/\w{10}-\w{8}\.csv/');
}

/**
Expand All @@ -58,6 +60,8 @@ public function can_assert_against_a_fake_stored_export()
ExcelFacade::assertStored('stored-filename.csv', 's3', function (FromCollection $export) {
return $export->collection()->contains('foo');
});
ExcelFacade::matchByRegex();
ExcelFacade::assertStored('/\w{6}-\w{8}\.csv/', 's3');
}

/**
Expand All @@ -75,6 +79,8 @@ public function a_callback_can_be_passed_as_the_second_argument_when_asserting_a
ExcelFacade::assertStored('stored-filename.csv', function (FromCollection $export) {
return $export->collection()->contains('foo');
});
ExcelFacade::matchByRegex();
ExcelFacade::assertStored('/\w{6}-\w{8}\.csv/');
}

/**
Expand All @@ -92,6 +98,8 @@ public function can_assert_against_a_fake_queued_export()
ExcelFacade::assertQueued('queued-filename.csv', 's3', function (FromCollection $export) {
return $export->collection()->contains('foo');
});
ExcelFacade::matchByRegex();
ExcelFacade::assertQueued('/\w{6}-\w{8}\.csv/', 's3');
}

/**
Expand All @@ -110,6 +118,8 @@ public function can_assert_against_a_fake_implicitly_queued_export()
ExcelFacade::assertQueued('queued-filename.csv', 's3', function (FromCollection $export) {
return $export->collection()->contains('foo');
});
ExcelFacade::matchByRegex();
ExcelFacade::assertQueued('/\w{6}-\w{8}\.csv/', 's3');
}

/**
Expand All @@ -125,6 +135,8 @@ public function can_assert_against_a_fake_import()
ExcelFacade::assertImported('stored-filename.csv', 's3', function (ToModel $import) {
return $import->model([]) instanceof User;
});
ExcelFacade::matchByRegex();
ExcelFacade::assertImported('/\w{6}-\w{8}\.csv/', 's3');
}

/**
Expand All @@ -140,6 +152,8 @@ public function can_assert_against_a_fake_import_with_uploaded_file()
ExcelFacade::assertImported('import.xlsx', function (ToModel $import) {
return $import->model([]) instanceof User;
});
ExcelFacade::matchByRegex();
ExcelFacade::assertImported('/\w{6}\.xlsx/');
}

/**
Expand All @@ -158,6 +172,8 @@ public function can_assert_against_a_fake_queued_import()
ExcelFacade::assertQueued('queued-filename.csv', 's3', function (ToModel $import) {
return $import->model([]) instanceof User;
});
ExcelFacade::matchByRegex();
ExcelFacade::assertQueued('/\w{6}-\w{8}\.csv/', 's3');
}

/**
Expand All @@ -176,6 +192,8 @@ public function can_assert_against_a_fake_implicitly_queued_import()
ExcelFacade::assertQueued('queued-filename.csv', 's3', function (ToModel $import) {
return $import->model([]) instanceof User;
});
ExcelFacade::matchByRegex();
ExcelFacade::assertQueued('/\w{6}-\w{8}\.csv/', 's3');
}

/**
Expand All @@ -193,6 +211,8 @@ public function a_callback_can_be_passed_as_the_second_argument_when_asserting_a
ExcelFacade::assertQueued('queued-filename.csv', function (FromCollection $export) {
return $export->collection()->contains('foo');
});
ExcelFacade::matchByRegex();
ExcelFacade::assertQueued('/\w{6}-\w{8}\.csv/');
}

/**
Expand Down

0 comments on commit 7d47c7a

Please sign in to comment.