Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit 214baf1

Browse files
authored
PHPLIB-1355 Add tests to $meta expression (#59)
1 parent ca173b2 commit 214baf1

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

generator/config/expression/meta.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,29 @@ arguments:
1111
name: keyword
1212
type:
1313
- string
14+
tests:
15+
-
16+
name: 'textScore'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta/#-meta---textscore-'
18+
pipeline:
19+
-
20+
$match:
21+
$text:
22+
$search: 'cake'
23+
-
24+
$group:
25+
_id:
26+
$meta: 'textScore'
27+
count:
28+
$sum: 1
29+
-
30+
name: 'indexKey'
31+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta/#-meta---indexkey-'
32+
pipeline:
33+
-
34+
$match:
35+
type: 'apparel'
36+
-
37+
$addFields:
38+
idxKey:
39+
$meta: 'indexKey'
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Expression;
6+
7+
use MongoDB\Builder\Accumulator;
8+
use MongoDB\Builder\Expression;
9+
use MongoDB\Builder\Pipeline;
10+
use MongoDB\Builder\Query;
11+
use MongoDB\Builder\Stage;
12+
use MongoDB\Tests\Builder\PipelineTestCase;
13+
14+
/**
15+
* Test $meta expression
16+
*/
17+
class MetaOperatorTest extends PipelineTestCase
18+
{
19+
public function testIndexKey(): void
20+
{
21+
$pipeline = new Pipeline(
22+
Stage::match(
23+
type: 'apparel',
24+
),
25+
Stage::addFields(
26+
idxKey: Expression::meta('indexKey'),
27+
),
28+
);
29+
30+
$this->assertSamePipeline(Pipelines::MetaIndexKey, $pipeline);
31+
}
32+
33+
public function testTextScore(): void
34+
{
35+
$pipeline = new Pipeline(
36+
Stage::match(
37+
Query::text(
38+
search: 'cake',
39+
),
40+
),
41+
Stage::group(
42+
_id: Expression::meta('textScore'),
43+
count: Accumulator::sum(1),
44+
),
45+
);
46+
47+
$this->assertSamePipeline(Pipelines::MetaTextScore, $pipeline);
48+
}
49+
}

tests/Builder/Expression/Pipelines.php

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)