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

Commit

Permalink
Added new comparison operator "NOT" equals to "!=", added new method …
Browse files Browse the repository at this point in the history
…for returning only objects resultDocuments(), results() is now just an array. Added some more extensive testing on query comparisons.
  • Loading branch information
timothymarois committed Aug 4, 2017
1 parent a4c78b7 commit 3f099cb
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 29 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ To run the query use `results()`
- `where()` param `array` for simple "equal to" OR `where($field, $operator, $value)`
- `andWhere()` *optional* see `where()`, uses the logical `AND`
- `orWhere()` *optional* see `where()`, this uses the logical `OR`
- `results()` This will return all the document objects.
- `results()` This will return all the document data as an array.
- `resultDocuments()` This will return all the document objects

### Comparison Operators:

Expand All @@ -325,6 +326,7 @@ To run the query use `results()`
|`=` or `==` |Equality|
|`===` |Strict Equality|
|`!=` |Not Equals|
|`NOT` |Not Equals (same as `!=`)|
|`!==` |Strict Not Equals|
|`>` |Greater than|
|`>=` |Greater than or equal|
Expand Down
2 changes: 1 addition & 1 deletion src/Format/FormatInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
interface FormatInterface
{
public static function getFileExtension();
public static function encode($data,$pretty);
public static function encode($data, $pretty);
public static function decode($data);
}
4 changes: 2 additions & 2 deletions src/Format/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static function getFileExtension()
* encode
*
*/
public static function encode($data = [],$pretty = true)
public static function encode($data = [], $pretty = true)
{
$p = 1;
if ($pretty==true) $p = JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES;
Expand All @@ -39,7 +39,7 @@ public static function encode($data = [],$pretty = true)
*/
public static function decode($data)
{
return json_decode($data,1);
return json_decode($data, 1);
}


Expand Down
2 changes: 1 addition & 1 deletion src/Predicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Predicate
* Allowed operators within the query
*/
protected $allowed_operators = [
'=','==','===','!=','!==','>','<','=>','<=','IN'
'=','==','===','!=','!==','>','<','=>','<=','IN','NOT'
];


Expand Down
36 changes: 31 additions & 5 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ public function orWhere(...$arg)


/**
* add
* addPredicate
*
*/
protected function addPredicate($logic,$arg)
{
if (count($arg) == 3)
{
$this->predicate->add($logic,$arg);
$this->predicate->add($logic, $arg);
}

if (count($arg) == 1)
Expand All @@ -92,7 +92,7 @@ protected function addPredicate($logic,$arg)
* formatWhere
*
*/
protected function formatWhere($key,$value)
protected function formatWhere($key, $value)
{
return [$key,'==',$value];
}
Expand All @@ -101,13 +101,39 @@ protected function formatWhere($key,$value)
//--------------------------------------------------------------------


/**
* ->getDocuments()
*
*/
public function getDocuments()
{
return $this->documents;
}


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


/**
* ->results()
*
*/
public function results($returnArray = true)
public function results()
{
return parent::run()->toArray();
}


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


/**
* ->resultDocuments()
*
*/
public function resultDocuments()
{
return parent::run($returnArray);
return parent::run()->getDocuments();
}


Expand Down
19 changes: 9 additions & 10 deletions src/QueryLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(Database $database)
* run
*
*/
public function run($returnArray)
public function run()
{
$predicates = $this->predicate->get();
$this->documents = [];
Expand All @@ -68,7 +68,9 @@ public function run($returnArray)

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

return $this;
}
}

Expand All @@ -90,12 +92,7 @@ public function run($returnArray)
}
}

if ($returnArray === true)
{
return $this->toArray();
}

return $this->documents;
return $this;
}


Expand Down Expand Up @@ -190,6 +187,8 @@ public function match($document, $field, $operator, $value)
return true;
case ($operator === '!==' && $d_value !== $value):
return true;
case (strtoupper($operator) === 'NOT' && $d_value != $value):
return true;
case ($operator === '>' && $d_value > $value):
return true;
case ($operator === '>=' && $d_value >= $value):
Expand All @@ -198,9 +197,9 @@ public function match($document, $field, $operator, $value)
return true;
case ($operator === '>=' && $d_value >= $value):
return true;
case ($operator === 'IN' && in_array($d_value, (array) $value)):
case (strtoupper($operator) === 'IN' && in_array($d_value, (array) $value)):
return true;
case ($operator === 'IN' && in_array($value, (array) $d_value)):
case (strtoupper($operator) === 'IN' && in_array($value, (array) $d_value)):
return true;
default:
return false;
Expand Down
98 changes: 89 additions & 9 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,22 @@
class QueryTest extends \PHPUnit\Framework\TestCase
{

public function testWhereQueryCount()
/**
* testWhereCountAllEqualCompare()
*
* TEST:
*
* 1. Creates 10 items in database with ["name" = "John"]
* 2. Runs query to find items that have ["name" = "John"]
* 3. Counts the total items in the database
* 4. Compares the number of items in db to the number items the query found
*
* Results: Should be the EXACT SAME (query should find "all" items in db)
*
* Comparisons used "=", "==", "==="
*
*/
public function testWhereCountAllEqualCompare()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases/users_1',
Expand All @@ -17,21 +32,87 @@ public function testWhereQueryCount()
{
$user = $db->get(uniqid());
$user->name = 'John';
$user->save();
}

$count = $db->count();

$query1 = $db->query()->where('name','=','John')->results();
$query2 = $db->query()->where('name','==','John')->results();
$query3 = $db->query()->where('name','===','John')->results();
$query4 = $db->query()->where(['name' => 'John'])->results();

$this->assertEquals($count, count($query1));
$this->assertEquals($count, count($query2));
$this->assertEquals($count, count($query3));
$this->assertEquals($count, count($query4));

$db->flush(true);
}


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


/**
* testWhereCountAllNotEqualCompare()
*
* TEST CASE:
* - Creates 10 items in database with ["name" = "John"]
* - Counts the total items in the database
*
* FIRST TEST:
* - Compares the number of items in db to the number items the query found
* - Should match "10"
*
* SECOND TEST:
* - Secondary Tests to find items that DO NOT match "John"
* - Should match "0"
*
* Comparisons used "!=", "!==", "NOT"
*
*/
public function testWhereCountAllNotEqualCompare()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases/users_1',
'cache' => false
]);

$db->flush(true);

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

$results = $db->query()
->where('name','=','John')
->results();
$count = $db->count();

$this->assertEquals($db->count(), count($results));
$query1 = $db->query()->where('name','!=','Max')->results();
$query2 = $db->query()->where('name','!==','Smith')->results();
$query3 = $db->query()->where('name','NOT','Jason')->results();

$query4 = $db->query()->where('name','!=','John')->results();
$query5 = $db->query()->where('name','!==','John')->results();
$query6 = $db->query()->where('name','NOT','John')->results();

$this->assertEquals($count, count($query1));
$this->assertEquals($count, count($query2));
$this->assertEquals($count, count($query3));

$this->assertEquals(0, count($query4));
$this->assertEquals(0, count($query5));
$this->assertEquals(0, count($query6));

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


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


public function testWhereQueryWhereCount()
{
$db = new \Filebase\Database([
Expand Down Expand Up @@ -152,19 +233,18 @@ public function testWhereQueryFromCache()
$user = $db->get(uniqid());
$user->name = 'John';
$user->email = '[email protected]';

$user->save();
}

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

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

$this->assertEquals(10, count($results));
$this->assertEquals(true, ($result_from_cache[0]->isCache()));
Expand Down

0 comments on commit 3f099cb

Please sign in to comment.