Skip to content

Commit

Permalink
Merge 27de2de into 07590ef
Browse files Browse the repository at this point in the history
  • Loading branch information
pl-github committed Mar 31, 2020
2 parents 07590ef + 27de2de commit 453e6d5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Storage/Adapter.php
Expand Up @@ -67,11 +67,13 @@ public function setFromStorage($json)
{
list($cache, $complete, $expire) = json_decode($json, true);

if (! $expire || $expire > $this->getTime()) {
$this->cache = $cache;
$this->complete = $complete;
} else {
$this->adapter->delete($this->file);
if (json_last_error() === JSON_ERROR_NONE && is_array($cache) && is_array($complete)) {
if (! $expire || $expire > $this->getTime()) {
$this->cache = $cache;
$this->complete = $complete;
} else {
$this->adapter->delete($this->file);
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions tests/AdapterCacheTests.php
Expand Up @@ -14,6 +14,39 @@ public function testLoadFail()
$this->assertFalse($cache->isComplete('', false));
}

public function testLoadInvalidJson()
{
$response = ['contents' => 'invalid json', 'path' => 'file.json'];
$adapter = Mockery::mock('League\Flysystem\AdapterInterface');
$adapter->shouldReceive('has')->once()->with('file.json')->andReturn(true);
$adapter->shouldReceive('read')->once()->with('file.json')->andReturn($response);
$cache = new Adapter($adapter, 'file.json', 10);
$cache->load();
$this->assertFalse($cache->isComplete('', false));
}

public function testLoadInvalidDataCacheIsNotAnArray()
{
$response = ['contents' => json_encode([null, ['' => true], 9876543210]), 'path' => 'file.json'];
$adapter = Mockery::mock('League\Flysystem\AdapterInterface');
$adapter->shouldReceive('has')->once()->with('file.json')->andReturn(true);
$adapter->shouldReceive('read')->once()->with('file.json')->andReturn($response);
$cache = new Adapter($adapter, 'file.json', 10);
$cache->load();
$this->assertFalse($cache->isComplete('', false));
}

public function testLoadInvalidDataCompleteIsNotAnArray()
{
$response = ['contents' => json_encode([[], null, 9876543210]), 'path' => 'file.json'];
$adapter = Mockery::mock('League\Flysystem\AdapterInterface');
$adapter->shouldReceive('has')->once()->with('file.json')->andReturn(true);
$adapter->shouldReceive('read')->once()->with('file.json')->andReturn($response);
$cache = new Adapter($adapter, 'file.json', 10);
$cache->load();
$this->assertFalse($cache->isComplete('', false));
}

public function testLoadExpired()
{
$response = ['contents' => json_encode([[], ['' => true], 1234567890]), 'path' => 'file.json'];
Expand Down

0 comments on commit 453e6d5

Please sign in to comment.