Skip to content

Commit

Permalink
fix bug tmarois#41 "results() returns document instead of array tmaro…
Browse files Browse the repository at this point in the history
  • Loading branch information
faryar committed Feb 24, 2019
1 parent 66b17e8 commit d7630c4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
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);
}
}

0 comments on commit d7630c4

Please sign in to comment.