Skip to content
This repository has been archived by the owner on Nov 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #307 from alcaeus/fix-empty-pipeline-match-stages
Browse files Browse the repository at this point in the history
Convert empty match stages to objects
  • Loading branch information
alcaeus authored Oct 9, 2017
2 parents 4421aee + fe80498 commit 2872db0
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG-1.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ CHANGELOG for 1.6.x
This changelog references the relevant changes (bug and security fixes) done
in 1.6.x patch versions.

1.6.1 (2017-10-09)
------------------

* [#306](https://github.com/doctrine/mongodb/pull/306): Fix wrong stage name for `$bucketAuto` pipeline stage
* [#307](https://github.com/doctrine/mongodb/pull/307): Convert empty match stages to object

1.6.0 (2017-08-09)
------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(Builder $builder, GraphLookup $graphLookup)
*/
public function getExpression()
{
return $this->query->getQuery();
return $this->query->getQuery() ?: (object) [];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/MongoDB/Aggregation/Stage/Match.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(Builder $builder)
public function getExpression()
{
return [
'$match' => $this->query->getQuery()
'$match' => $this->query->getQuery() ?: (object) [],
];
}

Expand Down
67 changes: 67 additions & 0 deletions tests/Doctrine/MongoDB/Tests/Aggregation/FunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Doctrine\MongoDB\Tests\Aggregation;

use Doctrine\MongoDB\Tests\DatabaseTestCase;

class FunctionalTest extends DatabaseTestCase
{
public function testEmptyMatchStageDoesNotCauseError()
{
$result = $this->createAggregationBuilder()
->match()
->execute();

$this->assertCount(0, $result);
}

public function testGeoNearWithEmptyQueryDoesNotCauseError()
{
if (version_compare($this->getServerVersion(), '3.4.0', '>=')) {
$this->conn->selectDatabase('admin')->command(['setFeatureCompatibilityVersion' => '3.4']);
}

$this->getCollection()->ensureIndex(['location' => '2dsphere']);
$result = $this->createAggregationBuilder()
->geoNear(0, 0)
->distanceField('distance')
->spherical()
->execute();

$this->assertCount(0, $result);
}

public function testGraphLookupWithoutMatch()
{
if (version_compare($this->getServerVersion(), '3.4.0', '<')) {
$this->markTestSkipped('$graphLookup is only available on server version 3.4 and later.');
}

$result = $this->createAggregationBuilder()
->graphLookup('employees')
->startWith('$reportsTo')
->connectFromField('reportsTo')
->connectToField('name')
->alias('reportingHierarchy')
->execute();

$this->assertCount(0, $result);
}

/**
* @return \Doctrine\MongoDB\Aggregation\Builder
*/
protected function createAggregationBuilder()
{
return $this->getCollection()->createAggregationBuilder();
}

/**
* @return \Doctrine\MongoDB\Collection
*/
protected function getCollection()
{
$db = $this->conn->selectDatabase(self::$dbName);
return $db->selectCollection('test');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public function testGraphLookupStage()
->connectToField('name')
->alias('reportingHierarchy');

$this->assertSame(
$this->assertEquals(
['$graphLookup' => [
'from' => 'employees',
'startWith' => '$reportsTo',
'connectFromField' => 'reportsTo',
'connectToField' => 'name',
'as' => 'reportingHierarchy',
'restrictSearchWithMatch' => [],
'restrictSearchWithMatch' => (object) [],
]],
$graphLookupStage->getExpression()
);
Expand All @@ -41,14 +41,14 @@ public function testGraphLookupFromBuilder()
->connectToField('name')
->alias('reportingHierarchy');

$this->assertSame(
$this->assertEquals(
[['$graphLookup' => [
'from' => 'employees',
'startWith' => '$reportsTo',
'connectFromField' => 'reportsTo',
'connectToField' => 'name',
'as' => 'reportingHierarchy',
'restrictSearchWithMatch' => [],
'restrictSearchWithMatch' => (object) [],
]]],
$builder->getPipeline()
);
Expand Down
3 changes: 3 additions & 0 deletions tests/Doctrine/MongoDB/Tests/DatabaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ abstract class DatabaseTestCase extends TestCase
{
protected static $dbName = 'doctrine_mongodb';

/**
* @var Connection
*/
protected $conn;

public function setUp()
Expand Down

0 comments on commit 2872db0

Please sign in to comment.