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

Commit

Permalink
Added limit() method on query. Includes limiting results and offset r…
Browse files Browse the repository at this point in the history
…esults functionality.
  • Loading branch information
timothymarois committed Aug 5, 2017
1 parent 7567826 commit 35bb646
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 34 deletions.
44 changes: 44 additions & 0 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
class Query extends QueryLogic
{

protected $limit = 0;
protected $offset = 0;
protected $sortBy = 'ASC';
protected $orderBy = '';


/**
* $documents
*
Expand Down Expand Up @@ -59,6 +65,44 @@ public function orWhere(...$arg)
//--------------------------------------------------------------------


/**
* ->limit()
*
*/
public function limit($limit, $offset = 0)
{
$this->limit = (int) $limit;

if ($this->limit === 0)
{
$this->limit = 9999999;
}

$this->offset = (int) $offset;

return $this;
}


//--------------------------------------------------------------------


/**
* ->orderBy()
*
*/
public function orderBy(string $field, string $sort)
{
$this->orderBy = $field;
$this->sortBy = $sort;

return $this;
}


//--------------------------------------------------------------------


/**
* addPredicate
*
Expand Down
20 changes: 20 additions & 0 deletions src/QueryLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public function run()
{
$this->documents = $cached_documents;

$this->offsetLimit();

return $this;
}
}
Expand All @@ -92,6 +94,8 @@ public function run()
}
}

$this->offsetLimit();

return $this;
}

Expand Down Expand Up @@ -144,6 +148,22 @@ protected function filter($documents, $predicates)
//--------------------------------------------------------------------


/**
* offsetLimit
*
*/
protected function offsetLimit()
{
if ($this->limit != 0 || $this->offset != 0)
{
$this->documents = array_slice($this->documents, $this->offset, $this->limit);
}
}


//--------------------------------------------------------------------


/**
* matchDocuments
*
Expand Down
80 changes: 46 additions & 34 deletions tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,84 @@

class DocumentTest extends \PHPUnit\Framework\TestCase
{

/**
* testSave()
*
* TEST CASE:
* - Save document with data
* - Get the document
* - Check that the data is there and the document exist
*
*/
public function testSave()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases'
'dir' => __DIR__.'/databases',
'cache' => false
]);

$db->flush(true);

$test = $db->get('test')->save(['key'=>'value']);
$val = $db->get('test')->toArray();

$this->assertEquals(['key'=>'value'], $val);

$db->flush(true);
}
// save data
$doc = $db->get('test_save')->save(['key'=>'value']);

// get saved data (put into array)
$val = $db->get('test_save');

public function testArraySetValue()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases'
]);
// should equal...
$this->assertEquals('value', $val->key);

$db->flush(true);
}

$test = $db->get('test')->set(['key'=>'value']);

$this->assertEquals('value', $test->key);

$db->flush(true);
}
//--------------------------------------------------------------------


public function testPropertySetValue()
/**
* testSetValue()
*
* TEST CASE:
* - Using the set method, set the value in object ( DO NOT SAVE )
* - Check that the properties are in the object (matching)
*
*/
public function testSetValue()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases'
'dir' => __DIR__.'/databases',
'cache' => false
]);

$db->flush(true);

$test = $db->get('test');
$test->key = 'value';
// FIRST TEST
// use the set() method
$test1 = $db->get('test1')->set(['key'=>'value']);

$this->assertEquals('value', $test->key);
$this->assertEquals('value', $test1->key);

$db->flush(true);
}

// SECOND TEST:
// use the property setter
$test2 = $db->get('test2');
$test2->key = 'value';

public function testPropertySetValueNull()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases'
]);
$this->assertEquals('value', $test2->key);

$db->flush(true);

$test = $db->get('test');
// THIRD TEST (null test)
$test3 = $db->get('test3');

$this->assertEquals(null, $test->key);
$this->assertEquals(null, $test3->key);

$db->flush(true);
}


//--------------------------------------------------------------------


public function testArraySetValueSave()
{
$db = new \Filebase\Database([
Expand Down
50 changes: 50 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,56 @@ public function testWhereLikeRegex()
//--------------------------------------------------------------------


/**
* testLimitOffset()
*
* TEST CASE:
* - Creates 6 company profiles
* - Queries them and limits the results
*
*
*/
public function testLimitOffset()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases/users_orderby',
'cache' => false
]);

$db->flush(true);

$companies = ['Google'=>150, 'Apple'=>150, 'Microsoft'=>150, 'Amex'=>150, 'Hooli'=>20, 'Amazon'=>10];

foreach($companies as $company=>$rank)
{
$user = $db->get(uniqid());
$user->name = $company;
$user->rank = $rank;
$user->save();
}

$count = $db->count();

// test that it limits the results to "2" (total query pulls "5")
$test1 = $db->query()->where('rank','=',150)->limit(2)->results();

// test the offset, no limit, should be 3 (total query pulls "5")
$test2 = $db->query()->where('rank','=',150)->limit(0,1)->results();

// test that the offset takes off the first array (should return "apple", not "google")
$test3 = $db->query()->where('rank','=',150)->limit(1,1)->results();

$this->assertEquals(2, (count($test1)));
$this->assertEquals(3, (count($test2)));
$this->assertEquals('Apple', $test3[0]['name']);

$db->flush(true);
}


//--------------------------------------------------------------------





Expand Down

0 comments on commit 35bb646

Please sign in to comment.