Skip to content

Commit 3da7d2e

Browse files
committed
Makes is_pipeline accept empty list
1 parent da3f71f commit 3da7d2e

File tree

11 files changed

+37
-22
lines changed

11 files changed

+37
-22
lines changed

src/Operation/Aggregate.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ class Aggregate implements Executable, Explainable
138138
*/
139139
public function __construct(string $databaseName, ?string $collectionName, array $pipeline, array $options = [])
140140
{
141-
// Empty pipelines are allowed
142-
if ($pipeline !== [] && ! is_pipeline($pipeline)) {
141+
if (! is_pipeline($pipeline)) {
143142
throw new InvalidArgumentException('$pipeline is not a valid aggregation pipeline');
144143
}
145144

src/Operation/BulkWrite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function __construct(string $databaseName, string $collectionName, array
229229
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array or object');
230230
}
231231

232-
if (! is_first_key_operator($args[1]) && ! is_pipeline($args[1])) {
232+
if ($args[1] === [] || ! is_first_key_operator($args[1]) && ! is_pipeline($args[1])) {
233233
throw new InvalidArgumentException(sprintf('First key in $operations[%d]["%s"][1] is neither an update operator nor a pipeline', $i, $type));
234234
}
235235

src/Operation/FindOneAndUpdate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function __construct(string $databaseName, string $collectionName, $filte
114114
throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
115115
}
116116

117-
if (! is_first_key_operator($update) && ! is_pipeline($update)) {
117+
if ($update === [] || ! is_first_key_operator($update) && ! is_pipeline($update)) {
118118
throw new InvalidArgumentException('Expected an update document with operator as first key or a pipeline');
119119
}
120120

src/Operation/ReplaceOne.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ public function __construct(string $databaseName, string $collectionName, $filte
8686
throw InvalidArgumentException::invalidType('$replacement', $replacement, 'array or object');
8787
}
8888

89+
if ($replacement === []) {
90+
throw new InvalidArgumentException('$replacement argument is empty');
91+
}
92+
8993
if (is_first_key_operator($replacement)) {
9094
throw new InvalidArgumentException('First key in $replacement argument is an update operator');
9195
}

src/Operation/Update.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function __construct(string $databaseName, string $collectionName, $filte
147147
throw InvalidArgumentException::invalidType('"multi" option', $options['multi'], 'boolean');
148148
}
149149

150-
if ($options['multi'] && ! is_first_key_operator($update) && ! is_pipeline($update)) {
150+
if ($options['multi'] && ($update === [] || ! is_first_key_operator($update) && ! is_pipeline($update))) {
151151
throw new InvalidArgumentException('"multi" option cannot be true if $update is a replacement document');
152152
}
153153

src/Operation/UpdateMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function __construct(string $databaseName, string $collectionName, $filte
8989
throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
9090
}
9191

92-
if (! is_first_key_operator($update) && ! is_pipeline($update)) {
92+
if ($update === [] || ! is_first_key_operator($update) && ! is_pipeline($update)) {
9393
throw new InvalidArgumentException('Expected an update document with operator as first key or a pipeline');
9494
}
9595

src/Operation/UpdateOne.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function __construct(string $databaseName, string $collectionName, $filte
8989
throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
9090
}
9191

92-
if (! is_first_key_operator($update) && ! is_pipeline($update)) {
92+
if ($update === [] || ! is_first_key_operator($update) && ! is_pipeline($update)) {
9393
throw new InvalidArgumentException('Expected an update document with operator as first key or a pipeline');
9494
}
9595

src/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ function is_pipeline($pipeline): bool
213213
}
214214

215215
if ($pipeline === []) {
216-
return false;
216+
return true;
217217
}
218218

219219
$expectedKey = 0;

tests/FunctionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public function providePipelines()
225225
{
226226
return [
227227
'Not an array' => [false, (object) []],
228-
'Empty array' => [false, []],
228+
'Empty array' => [true, []],
229229
'Non-sequential indexes in array' => [false, [1 => ['$group' => []]]],
230230
'Update document instead of pipeline' => [false, ['$set' => ['foo' => 'bar']]],
231231
'Invalid pipeline stage' => [false, [['group' => []]]],

tests/Operation/BulkWriteTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,13 @@ public function testUpdateManyUpdateArgumentTypeCheck($update): void
236236
]);
237237
}
238238

239-
public function testUpdateManyUpdateArgumentRequiresOperatorsOrPipeline(): void
239+
/** @dataProvider provideInvalidOperatorsOrPipeline */
240+
public function testUpdateManyUpdateArgumentRequiresOperatorsOrPipeline($arg1): void
240241
{
241242
$this->expectException(InvalidArgumentException::class);
242243
$this->expectExceptionMessage('First key in $operations[0]["updateMany"][1] is neither an update operator nor a pipeline');
243244
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
244-
[BulkWrite::UPDATE_MANY => [['_id' => ['$gt' => 1]], ['x' => 1]]],
245+
[BulkWrite::UPDATE_MANY => [['_id' => ['$gt' => 1]], $arg1]],
245246
]);
246247
}
247248

@@ -313,12 +314,13 @@ public function testUpdateOneUpdateArgumentTypeCheck($update): void
313314
]);
314315
}
315316

316-
public function testUpdateOneUpdateArgumentRequiresOperatorsOrPipeline(): void
317+
/** @dataProvider provideInvalidOperatorsOrPipeline */
318+
public function testUpdateOneUpdateArgumentRequiresOperatorsOrPipeline($arg1): void
317319
{
318320
$this->expectException(InvalidArgumentException::class);
319321
$this->expectExceptionMessage('First key in $operations[0]["updateOne"][1] is neither an update operator nor a pipeline');
320322
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
321-
[BulkWrite::UPDATE_ONE => [['_id' => 1], ['x' => 1]]],
323+
[BulkWrite::UPDATE_ONE => [['_id' => 1], $arg1]],
322324
]);
323325
}
324326

@@ -386,4 +388,12 @@ public function provideInvalidConstructorOptions()
386388

387389
return $options;
388390
}
391+
392+
public function provideInvalidOperatorsOrPipeline()
393+
{
394+
return $this->wrapValuesForDataProvider([
395+
[],
396+
['x' => 1],
397+
]);
398+
}
389399
}

tests/Operation/ReplaceOneTest.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public function testConstructorReplacementArgument($replacement): void
3131
new ReplaceOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $replacement);
3232
}
3333

34-
/** @dataProvider provideUpdateDocuments */
35-
public function testConstructorReplacementArgumentRequiresNoOperators($replacement): void
34+
/** @dataProvider provideInvalidReplacementDocuments */
35+
public function testConstructorReplacementArgumentRequiresDocument($replacement, string $exceptionMessage): void
3636
{
3737
$this->expectException(InvalidArgumentException::class);
38-
$this->expectExceptionMessage('First key in $replacement argument is an update operator');
38+
$this->expectExceptionMessage($exceptionMessage);
3939
new ReplaceOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $replacement);
4040
}
4141

@@ -48,12 +48,14 @@ public function provideReplacementDocuments()
4848
]);
4949
}
5050

51-
public function provideUpdateDocuments()
51+
public function provideInvalidReplacementDocuments()
5252
{
53-
return $this->wrapValuesForDataProvider([
54-
['$set' => ['y' => 1]],
55-
(object) ['$set' => ['y' => 1]],
56-
new BSONDocument(['$set' => ['y' => 1]]),
57-
]);
53+
return [
54+
[[], '$replacement argument is empty'],
55+
[['$set' => ['y' => 1]], 'First key in $replacement argument is an update operator'],
56+
[(object) ['$set' => ['y' => 1]], 'First key in $replacement argument is an update operator'],
57+
[new BSONDocument(['$set' => ['y' => 1]]), 'First key in $replacement argument is an update operator'],
58+
[[['$group' => ['_id' => 1]]], '$replacement argument is a pipeline'],
59+
];
5860
}
5961
}

0 commit comments

Comments
 (0)