Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
composer.lock
tests/cache
vendor/
composer.phar
.DS_Store
Expand Down
7 changes: 6 additions & 1 deletion src/CodeManipulation.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ function getCachedPath($file)
if (State::$cacheIndexFile === null) {
$indexPath = Config\getCachePath() . '/index.csv';
if (file_exists($indexPath)) {
$table = array_map('str_getcsv', file($indexPath));
$table = array_map(
static function($line) {
return str_getcsv($line, ',', '"', '\\');
},
file($indexPath)
);
foreach ($table as $row) {
list($key, $value) = $row;
State::$cacheIndex[$key] = $value;
Expand Down
48 changes: 48 additions & 0 deletions tests/get-cached-path.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
Test getCachedPath function (https://github.com/antecedent/patchwork/pull/170)

--FILE--
<?php

require __DIR__ . "/../Patchwork.php";

use Patchwork\CodeManipulation\Stream;

$cachePath = str_replace('\\', '/', __DIR__ . '/cache');
Patchwork\CodeManipulation\State::$cacheIndexFile = null;
Patchwork\Config\State::$cachePath = $cachePath;

if ( ! is_dir($cachePath)) {
mkdir($cachePath);
} else {
echo "The cache directory should not exist in advance.\n";
}
Comment thread
antecedent marked this conversation as resolved.
Resolved

// The expected path depends on the current implementation of getCachedPath().
$file = 'test-file.php';
$hash = md5($file);

$expectedPath = $cachePath . '/' . $hash . '.php';

// First pass - index.csv file does not exist.
$actualPath = \Patchwork\CodeManipulation\getCachedPath($file);

echo $actualPath === $expectedPath ? 'PASS' : 'FAIL';
echo "\n";

// Second pass - index.csv file exists.
$actualPath = \Patchwork\CodeManipulation\getCachedPath($file);

echo $actualPath === $expectedPath ? 'PASS' : 'FAIL';
?>

--CLEAN--
<?php

unlink(__DIR__ . '/cache/index.csv');
rmdir(__DIR__ . '/cache');

?>
--EXPECT--
Comment thread
antecedent marked this conversation as resolved.
Resolved
PASS
PASS