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

PHPLIB-1355 Add tests to $meta expression #59

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions generator/config/expression/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,29 @@ arguments:
name: keyword
type:
- string
tests:
-
name: 'textScore'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta/#-meta---textscore-'
pipeline:
-
$match:
$text:
$search: 'cake'
-
$group:
_id:
$meta: 'textScore'
count:
$sum: 1
-
name: 'indexKey'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta/#-meta---indexkey-'
pipeline:
-
$match:
type: 'apparel'
-
$addFields:
idxKey:
$meta: 'indexKey'
49 changes: 49 additions & 0 deletions tests/Builder/Expression/MetaOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace MongoDB\Tests\Builder\Expression;

use MongoDB\Builder\Accumulator;
use MongoDB\Builder\Expression;
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 testTextScore(): void
{
$pipeline = new Pipeline(
Stage::match(
Query::text(
search: 'cake',
),
),
Stage::group(
_id: Expression::meta('textScore'),
count: Accumulator::sum(1),
),
);

$this->assertSamePipeline(Pipelines::MetaTextScore, $pipeline);
}
}
51 changes: 51 additions & 0 deletions tests/Builder/Expression/Pipelines.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.