Skip to content

Commit

Permalink
Simplify expected pipeline in tests by converting assoc array to objects
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Sep 28, 2023
1 parent c1e2cb4 commit 19b4755
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions tests/Builder/BuilderCodecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
use MongoDB\Builder\Stage;
use MongoDB\Tests\TestCase;

use function array_is_list;
use function array_merge;
use function array_walk;
use function is_array;

class BuilderCodecTest extends TestCase
{
Expand All @@ -24,9 +27,9 @@ public function testPipeline(): void
);

$expected = [
(object) ['$match' => (object) ['$eq' => ['$foo', 1]]],
(object) ['$match' => (object) ['$ne' => ['$foo', 2]]],
(object) ['$limit' => 1],
['$match' => ['$eq' => ['$foo', 1]]],
['$match' => ['$ne' => ['$foo', 2]]],
['$limit' => 1],
];

$this->assertSamePipeline($expected, $pipeline);
Expand All @@ -44,18 +47,18 @@ public function testPerformCount(): void
);

$expected = [
(object) [
'$match' => (object) [
[
'$match' => [
'$or' => [
(object) ['score' => (object) ['$gt' => 70, '$lt' => 90]],
(object) ['views' => (object) ['$gte' => 1000]],
['score' => ['$gt' => 70, '$lt' => 90]],
['views' => ['$gte' => 1000]],
],
],
],
(object) [
'$group' => (object) [
[
'$group' => [
'_id' => null,
'count' => (object) ['$sum' => 1],
'count' => ['$sum' => 1],
],
],
];
Expand All @@ -81,13 +84,13 @@ public function testAggregationFilter($limit, $expectedLimit): void
);

$expected = [
(object) [
'$project' => (object) [
'items' => (object) [
'$filter' => (object) array_merge([
[
'$project' => [
'items' => [
'$filter' => array_merge([
'input' => '$items',
'as' => 'item',
'cond' => (object) ['$gte' => ['$$item.price', 100]],
'cond' => ['$gte' => ['$$item.price', 100]],
], $expectedLimit),
],
],
Expand Down Expand Up @@ -115,7 +118,24 @@ private static function assertSamePipeline(array $expected, Pipeline $pipeline):
$codec = new BuilderEncoder();
$actual = $codec->encode($pipeline);

// @todo walk in array to cast associative array to an object
self::objectify($expected);

self::assertEquals($expected, $actual);
}

/**
* Recursively convert associative arrays to objects.
*/
private static function objectify(array &$array): void
{
array_walk($array, function (&$value): void {
if (is_array($value)) {
self::objectify($value);

if (! array_is_list($value)) {
$value = (object) $value;
}
}
});
}
}

0 comments on commit 19b4755

Please sign in to comment.