This repository was archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
PHPORM-45 Add Query\Builder::toMql() to simplify comprehensive query tests #6
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
46028c7
PHPORM-45 Add Query\Builder::toMql() to simplify comprehensive query …
GromNaN fd63196
Move Query/Builder unit tests to a dedicated test class
GromNaN a57478c
Fix count
GromNaN fe9b338
name dataprovider
GromNaN cb17468
Remove argument from toMql
GromNaN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jenssegers\Mongodb\Tests\Query; | ||
|
||
use DateTimeImmutable; | ||
use Jenssegers\Mongodb\Connection; | ||
use Jenssegers\Mongodb\Query\Builder; | ||
use Jenssegers\Mongodb\Query\Processor; | ||
use Mockery as m; | ||
use MongoDB\BSON\UTCDateTime; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class BuilderTest extends TestCase | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new test class will be reserved to unit-tests on generated queries. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In PHPLIB we have a few of those instances, and we usually name the database-dependent test |
||
{ | ||
/** | ||
* @dataProvider provideQueryBuilderToMql | ||
*/ | ||
public function testMql(array $expected, \Closure $build): void | ||
{ | ||
$builder = $build(self::getBuilder()); | ||
$this->assertInstanceOf(Builder::class, $builder); | ||
$mql = $builder->toMql(); | ||
|
||
// Operations that return a Cursor expect a "typeMap" option. | ||
if (isset($expected['find'][1])) { | ||
$expected['find'][1]['typeMap'] = ['root' => 'array', 'document' => 'array']; | ||
} | ||
if (isset($expected['aggregate'][1])) { | ||
$expected['aggregate'][1]['typeMap'] = ['root' => 'array', 'document' => 'array']; | ||
} | ||
|
||
// Compare with assertEquals because the query can contain BSON objects. | ||
$this->assertEquals($expected, $mql, var_export($mql, true)); | ||
} | ||
|
||
public static function provideQueryBuilderToMql(): iterable | ||
{ | ||
/** | ||
* Builder::aggregate() and Builder::count() cannot be tested because they return the result, | ||
* without modifying the builder. | ||
*/ | ||
$date = new DateTimeImmutable('2016-07-12 15:30:00'); | ||
|
||
yield 'find' => [ | ||
['find' => [['foo' => 'bar'], []]], | ||
fn (Builder $builder) => $builder->where('foo', 'bar'), | ||
]; | ||
|
||
yield 'find > date' => [ | ||
['find' => [['foo' => ['$gt' => new UTCDateTime($date)]], []]], | ||
fn (Builder $builder) => $builder->where('foo', '>', $date), | ||
]; | ||
|
||
yield 'find in array' => [ | ||
['find' => [['foo' => ['$in' => ['bar', 'baz']]], []]], | ||
fn (Builder $builder) => $builder->whereIn('foo', ['bar', 'baz']), | ||
]; | ||
|
||
yield 'find limit offset select' => [ | ||
['find' => [[], ['limit' => 10, 'skip' => 5, 'projection' => ['foo' => 1, 'bar' => 1]]]], | ||
fn (Builder $builder) => $builder->limit(10)->offset(5)->select('foo', 'bar'), | ||
]; | ||
|
||
yield 'distinct' => [ | ||
['distinct' => ['foo', [], []]], | ||
fn (Builder $builder) => $builder->distinct('foo'), | ||
]; | ||
|
||
yield 'groupBy' => [ | ||
['aggregate' => [[['$group' => ['_id' => ['foo' => '$foo'], 'foo' => ['$last' => '$foo']]]], []]], | ||
fn (Builder $builder) => $builder->groupBy('foo'), | ||
]; | ||
} | ||
|
||
private static function getBuilder(): Builder | ||
{ | ||
$connection = m::mock(Connection::class); | ||
$processor = m::mock(Processor::class); | ||
$connection->shouldReceive('getSession')->andReturn(null); | ||
|
||
return new Builder($connection, $processor); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to #4 (comment)
I came back to "count" for now. Otherwise there is a test failure.