Alcaeus\MongoDbAdapter\Tests\Mongo\MongoUpdateBatchTest::testUpsert PHP Method

testUpsert() public method

public testUpsert ( )
    public function testUpsert()
    {
        $document = ['foo' => 'foo'];
        $this->getCollection()->insert($document);
        $batch = new \MongoUpdateBatch($this->getCollection());
        $this->assertTrue($batch->add(['q' => ['foo' => 'foo'], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
        $this->assertTrue($batch->add(['q' => ['bar' => 'foo'], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true]));
        $expected = ['upserted' => [['index' => 1]], 'nMatched' => 1, 'nModified' => 1, 'nUpserted' => 1, 'ok' => true];
        $result = $batch->execute();
        $this->assertArraySubset($expected, $result);
        $this->assertInstanceOf('MongoId', $result['upserted'][0]['_id']);
        $newCollection = $this->getCheckDatabase()->selectCollection('test');
        $this->assertSame(0, $newCollection->count(['foo' => 'foo']));
        $this->assertSame(2, $newCollection->count());
        $record = $newCollection->findOne();
        $this->assertNotNull($record);
        $this->assertObjectHasAttribute('foo', $record);
        $this->assertAttributeSame('bar', 'foo', $record);
    }