Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

fix bug #41 #50

Merged
merged 2 commits into from
Feb 24, 2019
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
40 changes: 24 additions & 16 deletions src/QueryLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ public function __construct(Database $database)
}
}

private function loadDocuments()
{
$predicates = $this->predicate->get();

if ($this->cache===false)
{
$this->documents = $this->database->findAll(true,false);
return $this;
}

$this->cache->setKey(json_encode($predicates));

if ($cached_documents = $this->cache->get())
{
$this->documents = $cached_documents;

$this->sort();
$this->offsetLimit();
return $this;
}
$this->documents = $this->database->findAll(true,false);
return $this;
}
/**
* run
*
Expand All @@ -57,22 +80,7 @@ public function run()
$predicates = 'findAll';
}

if ($this->cache !== false)
{
$this->cache->setKey(json_encode($predicates));

if ($cached_documents = $this->cache->get())
{
$this->documents = $cached_documents;

$this->sort();
$this->offsetLimit();

return $this;
}
}

$this->documents = $this->database->findAll(true,false);
$this->loadDocuments();

if ($predicates !== 'findAll')
{
Expand Down
30 changes: 30 additions & 0 deletions tests/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,34 @@ public function test_must_return_exception_on_non_exist_method()
$this->expectException(\BadMethodCallException::class);
$results = $db->none('name','=','John')->andWhere('email','==','[email protected]')->resultDocuments();
}
/**
* based on issue #41
* results() returns document instead of array #41
*/
public function test_must_return_array_on_select_an_culomn_from_cache()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases/saved',
'cache' => true
]);

$db->flush(true);

for ($x = 1; $x <= 10; $x++)
{
$user = $db->get(uniqid());
$user->name = 'John';
$user->email = '[email protected]';
$user->save();
}

$db->where('name','=','John')->andWhere('email','==','[email protected]')->select('email')->results();
$result_from_cache = $db->where('name','=','John')->andWhere('email','==','[email protected]')->select('email')->results();

$this->assertCount(10,$result_from_cache);
$this->assertEquals(['email'=>'[email protected]'],$result_from_cache[0]);
$this->assertInternalType('array', $result_from_cache[0]);
$this->assertInternalType('string', $result_from_cache[0]['email']);
$db->flush(true);
}
}