UpdateManyTest.php 1.97 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\Operation;

5
use MongoDB\Exception\InvalidArgumentException;
6
use MongoDB\Model\BSONDocument;
7 8 9 10 11
use MongoDB\Operation\UpdateMany;

class UpdateManyTest extends TestCase
{
    /**
12
     * @dataProvider provideInvalidDocumentValues
13
     */
14
    public function testConstructorFilterArgumentTypeCheck($filter)
15
    {
16
        $this->expectException(InvalidArgumentException::class);
Jeremy Mikola's avatar
Jeremy Mikola committed
17
        new UpdateMany($this->getDatabaseName(), $this->getCollectionName(), $filter, ['$set' => ['x' => 1]]);
18 19 20
    }

    /**
21
     * @dataProvider provideInvalidDocumentValues
22
     */
23
    public function testConstructorUpdateArgumentTypeCheck($update)
24
    {
25
        $this->expectException(InvalidArgumentException::class);
Jeremy Mikola's avatar
Jeremy Mikola committed
26
        new UpdateMany($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $update);
27 28
    }

29 30
    /**
     * @dataProvider provideUpdateDocuments
31
     * @doesNotPerformAssertions
32 33 34 35 36 37
     */
    public function testConstructorUpdateArgument($update)
    {
        new UpdateMany($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $update);
    }

38
    /**
39
     * @dataProvider provideReplacementDocuments
40
     */
41 42
    public function testConstructorUpdateArgumentRequiresOperators($replacement)
    {
43
        $this->expectException(InvalidArgumentException::class);
44
        $this->expectExceptionMessage('Expected an update document with operator as first key or a pipeline');
45 46 47 48 49 50 51 52 53 54 55 56 57
        new UpdateMany($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1], $replacement);
    }

    public function provideReplacementDocuments()
    {
        return $this->wrapValuesForDataProvider([
            ['y' => 1],
            (object) ['y' => 1],
            new BSONDocument(['y' => 1]),
        ]);
    }

    public function provideUpdateDocuments()
58
    {
59 60 61 62 63
        return $this->wrapValuesForDataProvider([
            ['$set' => ['y' => 1]],
            (object) ['$set' => ['y' => 1]],
            new BSONDocument(['$set' => ['y' => 1]]),
        ]);
64 65
    }
}