Skip to content

Commit

Permalink
Fix path-directory calculations. (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
twistor authored and frankdejonge committed Nov 20, 2016
1 parent 4eac1a3 commit ebb03c2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/Storage/AbstractCache.php
Expand Up @@ -65,16 +65,16 @@ public function storeContents($directory, array $contents, $recursive = false)
{
$directories = [$directory];

foreach ($contents as $index => $object) {
foreach ($contents as $object) {
$this->updateObject($object['path'], $object);
$object = $this->cache[$object['path']];

if ($recursive && (empty($directory) || strpos($object['dirname'], $directory) !== false)) {
if ($recursive && $this->pathIsInDirectory($directory, $object['path'])) {
$directories[] = $object['dirname'];
}
}

foreach ($directories as $directory) {
foreach (array_unique($directories) as $directory) {
$this->setComplete($directory, $recursive);
}

Expand Down Expand Up @@ -127,14 +127,10 @@ public function listContents($dirname = '', $recursive = false)
$result = [];

foreach ($this->cache as $object) {
if ($object['dirname'] !== $dirname) {
continue;
}

$result[] = $object;

if ($recursive && $object['type'] === 'dir') {
$result = array_merge($result, $this->listContents($object['path'], true));
if ($object['dirname'] === $dirname) {
$result[] = $object;
} elseif ($recursive && $this->pathIsInDirectory($dirname, $object['path'])) {
$result[] = $object;
}
}

Expand Down Expand Up @@ -216,14 +212,12 @@ public function delete($path)
public function deleteDir($dirname)
{
foreach ($this->cache as $path => $object) {
if (strpos($path, $dirname) === 0) {
if ($this->pathIsInDirectory($dirname, $path) || $path === $dirname) {
unset($this->cache[$path]);
}
}

if (isset($this->complete[$dirname])) {
unset($this->complete[$dirname]);
}
unset($this->complete[$dirname]);

$this->autosave();
}
Expand Down Expand Up @@ -404,4 +398,17 @@ public function ensureParentDirectories($path)
$this->cache[$object['path']] = $object;
}
}

/**
* Determines if the path is inside the directory.
*
* @param string $directory
* @param string $path
*
* @return bool
*/
protected function pathIsInDirectory($directory, $path)
{
return $directory === '' || strpos($path, $directory . '/') === 0;
}
}
45 changes: 45 additions & 0 deletions tests/AdapterCacheTests.php
Expand Up @@ -55,4 +55,49 @@ public function testSaveNew()
$cache = new Adapter($adapter, 'file.json', null);
$cache->save();
}

public function testStoreContentsRecursive()
{
$adapter = Mockery::mock('League\Flysystem\AdapterInterface');
$adapter->shouldReceive('has')->once()->with('file.json')->andReturn(false);
$adapter->shouldReceive('write')->once()->with('file.json', Mockery::any(), Mockery::any());

$cache = new Adapter($adapter, 'file.json', null);

$contents = [
['path' => 'foo/bar', 'dirname' => 'foo'],
['path' => 'afoo/bang', 'dirname' => 'afoo'],
];

$cache->storeContents('foo', $contents, true);

$this->assertTrue($cache->isComplete('foo', true));
$this->assertFalse($cache->isComplete('afoo', true));
}

public function testDeleteDir()
{
$cache_data = [
'foo' => ['path' => 'foo', 'type' => 'dir', 'dirname' => ''],
'foo/bar' => ['path' => 'foo/bar', 'type' => 'file', 'dirname' => 'foo'],
'foobaz' => ['path' => 'foobaz', 'type' => 'file', 'dirname' => ''],
];

$response = [
'contents' => json_encode([$cache_data, [], null]),
'path' => 'file.json',
];

$adapter = Mockery::mock('League\Flysystem\AdapterInterface');
$adapter->shouldReceive('has')->zeroOrMoreTimes()->with('file.json')->andReturn(true);
$adapter->shouldReceive('read')->once()->with('file.json')->andReturn($response);
$adapter->shouldReceive('update')->once()->with('file.json', Mockery::any(), Mockery::any())->andReturn(true);

$cache = new Adapter($adapter, 'file.json', null);
$cache->load();

$cache->deleteDir('foo', true);

$this->assertSame(1, count($cache->listContents('', true)));
}
}

0 comments on commit ebb03c2

Please sign in to comment.