-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add enum for $meta operator and add tests
- Loading branch information
Showing
4 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Builder; | ||
|
||
use MongoDB\Builder\Type\Encode; | ||
use MongoDB\Builder\Type\ExpressionInterface; | ||
use MongoDB\Builder\Type\OperatorInterface; | ||
|
||
/** | ||
* Returns the metadata associated with a document, e.g. "textScore" when performing text search. | ||
* | ||
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta/ | ||
*/ | ||
enum Meta: string implements OperatorInterface, ExpressionInterface | ||
{ | ||
public const ENCODE = Encode::Single; | ||
|
||
/** | ||
* Returns the score associated with the corresponding $text query for each | ||
* matching document. The text score signifies how well the document matched | ||
* the search term or terms. | ||
*/ | ||
case TextScore = 'textScore'; | ||
|
||
/** | ||
* Returns an index key for the document if a non-text index is used. The | ||
* { $meta: "indexKey" } expression is for debugging purposes only, and | ||
* not for application logic, and is preferred over cursor.returnKey(). | ||
*/ | ||
case IndexKey = 'indexKey'; | ||
|
||
public function getOperator(): string | ||
{ | ||
return '$meta'; | ||
} | ||
} |
This file contains 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,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Tests\Builder\Expression; | ||
|
||
use MongoDB\Builder\Accumulator; | ||
use MongoDB\Builder\Expression; | ||
use MongoDB\Builder\Meta; | ||
use MongoDB\Builder\Pipeline; | ||
use MongoDB\Builder\Query; | ||
use MongoDB\Builder\Stage; | ||
use MongoDB\Tests\Builder\PipelineTestCase; | ||
|
||
/** | ||
* Test $meta expression | ||
*/ | ||
class MetaOperatorTest extends PipelineTestCase | ||
{ | ||
public function testIndexKey(): void | ||
{ | ||
$pipeline = new Pipeline( | ||
Stage::match( | ||
type: 'apparel', | ||
), | ||
Stage::addFields( | ||
idxKey: Expression::meta('indexKey'), | ||
), | ||
); | ||
|
||
$this->assertSamePipeline(Pipelines::MetaIndexKey, $pipeline); | ||
} | ||
|
||
public function testIndexKeyWithEnum(): void | ||
{ | ||
$pipeline = new Pipeline( | ||
Stage::match( | ||
type: 'apparel', | ||
), | ||
Stage::addFields( | ||
idxKey: Meta::IndexKey, | ||
), | ||
); | ||
|
||
$this->assertSamePipeline(Pipelines::MetaIndexKey, $pipeline); | ||
} | ||
|
||
public function testTextScore(): void | ||
{ | ||
$pipeline = new Pipeline( | ||
Stage::match( | ||
Query::text('cake'), | ||
), | ||
Stage::group( | ||
_id: Expression::meta('textScore'), | ||
count: Accumulator::sum(1), | ||
), | ||
); | ||
|
||
$this->assertSamePipeline(Pipelines::MetaTextScore, $pipeline); | ||
} | ||
|
||
public function testTextScoreWithEnum(): void | ||
{ | ||
$pipeline = new Pipeline( | ||
Stage::match( | ||
Query::text('cake'), | ||
), | ||
Stage::group( | ||
_id: Meta::TextScore, | ||
count: Accumulator::sum(1), | ||
), | ||
); | ||
|
||
$this->assertSamePipeline(Pipelines::MetaTextScore, $pipeline); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.