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

Commit

Permalink
Fixed greater than or equals issue. Comparison operator was incorrect…
Browse files Browse the repository at this point in the history
…. Added testing for those.
  • Loading branch information
timothymarois committed Aug 4, 2017
1 parent 3f099cb commit c985df9
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 19 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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;
```
Expand Down Expand Up @@ -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()`
Expand Down
12 changes: 11 additions & 1 deletion src/Predicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ class Predicate
* Allowed operators within the query
*/
protected $allowed_operators = [
'=','==','===','!=','!==','>','<','=>','<=','IN','NOT'
'=',
'==',
'===',
'!=',
'!==',
'>',
'<',
'>=',
'<=',
'IN',
'NOT'
];


Expand Down
2 changes: 1 addition & 1 deletion src/QueryLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
88 changes: 82 additions & 6 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 "=", "==", "==="
*
Expand All @@ -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);
}
Expand Down Expand Up @@ -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([
Expand Down

0 comments on commit c985df9

Please sign in to comment.