Exemplo n.º 1
0
 public function testFindOne()
 {
     $model = new TestSchema();
     $model->title = '123';
     $model->object = ['item' => 1];
     $model->save();
     $mongoId = $model->_id;
     $this->assertNull(TestSchema::findOne(123));
     $this->assertNull(TestSchema::findOne('123'));
     $this->assertNull(TestSchema::findOne(new MongoId()));
     $this->assertInstanceOf('\\Reach\\Mongo\\Document\\Schema', TestSchema::findOne(null));
     unset($model);
     $model = TestSchema::findOne($mongoId);
     $this->assertInstanceOf('\\Reach\\Mongo\\Document\\Schema', $model);
     $this->assertEquals($mongoId, $model->_id);
     $model = TestSchema::getIdentityMap($mongoId);
     $this->assertEquals($mongoId, $model->_id);
     TestSchema::getCollection()->clearIdentityMap();
     $this->assertFalse(TestSchema::getIdentityMap($mongoId));
     $model = TestSchema::findOne((string) $mongoId);
     $this->assertEquals($mongoId, $model->_id);
     TestSchema::getCollection()->clearIdentityMap();
     $model = TestSchema::findOne(['_id' => $mongoId]);
     $this->assertEquals($mongoId, $model->_id);
     TestSchema::getCollection()->clearIdentityMap();
     $model = TestSchema::findOne(['_id' => (string) $mongoId]);
     $this->assertEquals($mongoId, $model->_id);
     TestSchema::getCollection()->clearIdentityMap();
     $model = TestSchema::findOne($mongoId, ['title']);
     $this->assertEquals($mongoId, $model->_id);
     $this->assertEquals('123', $model->title);
     $this->assertEmpty($model->object);
     TestSchema::getCollection()->clearIdentityMap();
     $document = TestSchema::findOne($mongoId, ['title'], true);
     $this->assertEquals(['_id' => $mongoId, 'title' => '123'], $document);
 }