From c985df90111123c73f9bab91898568c21086dd1a Mon Sep 17 00:00:00 2001 From: Timothy Marois Date: Fri, 4 Aug 2017 15:11:54 -0400 Subject: [PATCH] Fixed greater than or equals issue. Comparison operator was incorrect. Added testing for those. --- README.md | 22 ++++++------ src/Predicate.php | 12 ++++++- src/QueryLogic.php | 2 +- tests/QueryTest.php | 88 +++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 105 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index bb8f037..9ea932c 100644 --- a/README.md +++ b/README.md @@ -29,13 +29,13 @@ Run `composer require tmarois/filebase` or add to your main `composer.json` file ```php // setting the access and configration to your database -$my_database = new \Filebase\Database([ +$database = new \Filebase\Database([ 'dir' => 'path/to/database/dir' ]); // in this example, you would replace user_name with the actual user name. // It would technically be stored as user_name.json -$item = $my_database->get('user_name'); +$item = $database->get('user_name'); // display property values echo $item->first_name; @@ -101,10 +101,10 @@ After you've loaded up your database config, then you can use the `get()` method ```php // my user id -$user_id = '92832711'; +$userId = '92832711'; // get the user information by id -$item = $db->get($user_id); +$item = $db->get($userId); ``` `get()` returns `\Filebase\Document` object and has its own methods which you can call. @@ -125,17 +125,17 @@ Example: ```php // get the timestamp when the user was created -echo $db->get($user_id)->createdAt(); +echo $db->get($userId)->createdAt(); // grabbing a specific field "tags" within the user // in this case, tags might come back as an array ["php","html","javascript"] -$user_tags = $db->get($user_id)->field('tags'); +$user_tags = $db->get($userId)->field('tags'); // or if "tags" is nested in the user data, such as aboutme->tags -$user_tags = $db->get($user_id)->field('aboutme.tags'); +$user_tags = $db->get($userId)->field('aboutme.tags'); // and of course you can do this as well for getting "tags" -$user = $db->get($user_id); +$user = $db->get($userId); $user_tags = $user->tags; $user_tags = $user->aboutme->tags; ``` @@ -293,20 +293,20 @@ If caching is enabled, queries will use `findAll()` and then cache results for t ```php // Simple (equal to) Query // return all the users that are blocked. -$users = $userdb->query() +$users = $db->query() ->where(['status' => 'blocked']) ->results(); // Stackable WHERE clauses // return all the users who are blocked, // AND have "php" within the tag array -$users = $userdb->query() +$users = $db->query() ->where('status','=','blocked') ->where('tag','IN','php') ->results(); // You can also use `.` dot delimiter to use on nested keys -$users = $userdb->query()->where('status.language.english','=','blocked')->results(); +$users = $db->query()->where('status.language.english','=','blocked')->results(); ``` To run the query use `results()` diff --git a/src/Predicate.php b/src/Predicate.php index 4ac233a..af75e17 100644 --- a/src/Predicate.php +++ b/src/Predicate.php @@ -10,7 +10,17 @@ class Predicate * Allowed operators within the query */ protected $allowed_operators = [ - '=','==','===','!=','!==','>','<','=>','<=','IN','NOT' + '=', + '==', + '===', + '!=', + '!==', + '>', + '<', + '>=', + '<=', + 'IN', + 'NOT' ]; diff --git a/src/QueryLogic.php b/src/QueryLogic.php index a6850e8..797c0a3 100644 --- a/src/QueryLogic.php +++ b/src/QueryLogic.php @@ -195,7 +195,7 @@ public function match($document, $field, $operator, $value) return true; case ($operator === '<' && $d_value < $value): return true; - case ($operator === '>=' && $d_value >= $value): + case ($operator === '<=' && $d_value <= $value): return true; case (strtoupper($operator) === 'IN' && in_array($d_value, (array) $value)): return true; diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 076221f..53f6b09 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -7,14 +7,16 @@ class QueryTest extends \PHPUnit\Framework\TestCase /** * testWhereCountAllEqualCompare() * - * TEST: + * TEST CASE: + * - Creates 10 items in database with ["name" = "John"] + * - Counts the total items in the database * - * 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 + * FIRST TEST (standard matches): + * - Compares the number of items in db to the number items the query found + * - Should match "10" * - * Results: Should be the EXACT SAME (query should find "all" items in db) + * SECOND TEST (nested arrays) + * - Tests the inner array level field findings ["about" => ["name" => "Roy"] ]) * * Comparisons used "=", "==", "===" * @@ -26,26 +28,33 @@ public function testWhereCountAllEqualCompare() 'cache' => false ]); + // FIRST TEST $db->flush(true); for ($x = 1; $x <= 10; $x++) { $user = $db->get(uniqid()); $user->name = 'John'; + $user->contact['email'] = 'john@john.com'; $user->save(); } $count = $db->count(); + // standard matches $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(); + // testing nested level + $query5 = $db->query()->where('contact.email','=','john@john.com')->results(); + $this->assertEquals($count, count($query1)); $this->assertEquals($count, count($query2)); $this->assertEquals($count, count($query3)); $this->assertEquals($count, count($query4)); + $this->assertEquals($count, count($query5)); $db->flush(true); } @@ -113,6 +122,73 @@ public function testWhereCountAllNotEqualCompare() //-------------------------------------------------------------------- + + /** + * testWhereCountAllGreaterLessCompare() + * + * TEST CASE: + * - Creates 10 items in database with ["pages" = 5] + * - Counts the total items in the database + * + * FIRST TEST: Greater Than + * - Should match "10" + * + * SECOND TEST: Less Than + * - Should match "10" + * + * THIRD TEST: Less/Greater than "no match" + * - Should match "0" + * + * Comparisons used ">=", ">", "<=", "<" + * + */ + public function testWhereCountAllGreaterLessCompare() + { + $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->pages = 5; + $user->save(); + } + + $count = $db->count(); + + // FIRST TEST + $query1 = $db->query()->where('pages','>','4')->results(); + $query2 = $db->query()->where('pages','>=','5')->results(); + + // SECOND TEST + $query3 = $db->query()->where('pages','<','6')->results(); + $query4 = $db->query()->where('pages','<=','5')->results(); + + // THIRD TEST + $query5 = $db->query()->where('pages','>','5')->results(); + $query6 = $db->query()->where('pages','<','5')->results(); + + $this->assertEquals($count, count($query1)); + $this->assertEquals($count, count($query2)); + $this->assertEquals($count, count($query3)); + $this->assertEquals($count, count($query4)); + $this->assertEquals(0, count($query5)); + $this->assertEquals(0, count($query6)); + + $db->flush(true); + } + + + //-------------------------------------------------------------------- + + + + + public function testWhereQueryWhereCount() { $db = new \Filebase\Database([