Skip to content

Commit

Permalink
tmarois#40 Add ability to query on document ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Jansen Price committed Feb 8, 2019
1 parent 433190a commit f4badc8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,10 @@ public function field($field)
return $context;
}

if ($field == '__id') {
return $this->__id;
}

foreach($parts as $part)
{
if (trim($part) == '')
Expand Down
16 changes: 16 additions & 0 deletions tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,22 @@ public function testFieldMethod()
$db->flush(true);
}

public function testFieldId()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases'
]);

$db->flush(true);

$db->get('weather')->set(['cityname'=>'condition1'])->save();

$actual = $db->get('weather')->field('__id');
$this->assertEquals('weather', $actual);

$db->flush(true);
}


public function testNestedFieldMethod()
{
Expand Down
31 changes: 31 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1045,4 +1045,35 @@ public function testQueryFromCacheAfterSave()
$db->flush(true);
}

/**
* testWhereInUsingDocId
*
* TEST CASE:
* - Testing the where "IN" operator when applied to the document ids to fetch
*/
public function testWhereInUsingDocId()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases/users_1',
'cache' => false
]);

$db->flush(true);

$user1 = $db->get('obj1')->save(['name' => 'Bob']);
$user2 = $db->get('obj2')->save(['name' => 'Jenny']);
$user3 = $db->get('obj3')->save(['name' => 'Cyrus']);

// Make sure it works with just one
$test1 = $db->query()->where('__id', '=', 'obj1')->first();
$expected = ['name' => 'Bob'];
$this->assertEquals($expected, $test1);

// Make sure it works with a list
$test2 = $db->query()->where('__id', 'IN', ['obj2', 'obj3'])->results();
$expected = [['name' => 'Jenny'], ['name' => 'Cyrus']];
$this->assertEquals($expected, $test2);

$db->flush(true);
}
}

0 comments on commit f4badc8

Please sign in to comment.