Skip to content

Commit 4609acf

Browse files
committed
Makes is_pipeline accept empty list
1 parent f1cc097 commit 4609acf

File tree

11 files changed

+24
-13
lines changed

11 files changed

+24
-13
lines changed

src/Operation/Aggregate.php

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

src/Operation/BulkWrite.php

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

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

src/Operation/FindOneAndUpdate.php

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

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

src/Operation/ReplaceOne.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 new InvalidArgumentException('First key in $replacement argument is an update operator');
9090
}
9191

92-
if (is_pipeline($replacement)) {
92+
if ($replacement !== [] && is_pipeline($replacement)) {
9393
throw new InvalidArgumentException('$replacement argument is a pipeline');
9494
}
9595

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
@@ -88,7 +88,7 @@ public function __construct(string $databaseName, string $collectionName, $filte
8888
throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
8989
}
9090

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

src/Operation/UpdateOne.php

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

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

src/functions.php

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

246246
if ($pipeline === []) {
247-
return false;
247+
return true;
248248
}
249249

250250
$expectedKey = 0;

tests/FunctionsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ public function providePipelines(): array
265265

266266
return [
267267
// Valid pipeline in various forms
268+
'valid: empty array' => [true, []],
268269
'valid: array' => [true, $valid],
269270
'valid: Serializable' => [true, new BSONArray($valid)],
270271
'valid: PackedArray' => [true, PackedArray::fromPHP($valid)],

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function testConstructorReplacementArgument($replacement): void
3737
public function provideReplacementDocuments()
3838
{
3939
return $this->wrapValuesForDataProvider([
40+
[],
4041
['y' => 1],
4142
(object) ['y' => 1],
4243
new BSONDocument(['y' => 1]),

0 commit comments

Comments
 (0)