Exemplo n.º 1
0
 public function testRelationDocumentSaveAndFetch()
 {
     $document = new TestSchema();
     $document->title = 'TestSchemaTitle';
     $document->object = new \stdClass();
     $document->save();
     $stdClassEmbedOne = new \stdClass();
     $stdClassEmbedOne->user_id = new \MongoId();
     $stdClassEmbedOne->title = 'stdClassTitle';
     $testSchema = TestSchema::findOne();
     $model = new TestRelDocument();
     $model->title = 'TestRelDocumentTitle';
     $model->testSchemaRefPk = $testSchema;
     $model->testSchemaRefOne = $testSchema;
     $model->stdClassEmbedOne = $stdClassEmbedOne;
     $model->testSchemaEmbedOne = $testSchema;
     $model->testSchemaRefMany = [$testSchema, $testSchema];
     $model->testSchemaEmbedMany = [$testSchema, $stdClassEmbedOne];
     $model->save();
     // Save
     $document = TestRelDocument::getMongoCollection()->findOne();
     $this->assertEquals('TestRelDocumentTitle', $document['title']);
     $this->assertEquals($testSchema->_id, $document['test_schema_id']);
     $this->assertEquals(['$ref' => 'test_model_mongo_document', '$id' => $testSchema->_id], $document['ref_one']);
     $this->assertTrue(is_array($document['ref_many']));
     $this->assertEquals([['$ref' => 'test_model_mongo_document', '$id' => $testSchema->_id], ['$ref' => 'test_model_mongo_document', '$id' => $testSchema->_id]], $document['ref_many']);
     $this->assertEquals(['user_id' => $stdClassEmbedOne->user_id, 'title' => 'stdClassTitle'], $document['std_embed_one']);
     $this->assertEquals($testSchema->title, $document['embed_one']['title']);
     $this->assertEquals($testSchema->_id, $document['embed_one']['_id']);
     $this->assertTrue(is_array($document['embed_many']));
     $this->assertEquals([$testSchema->title, $stdClassEmbedOne->title], [$document['embed_many'][0]['title'], $document['embed_many'][1]['title']]);
     // Fetch All
     //$testRelDocument = TestRelDocument::find();
     // Fetch One
     $testRelDocument = TestRelDocument::findOne();
     $model = $testRelDocument->testSchemaRefPk;
     $this->assertInstanceOf('\\Model\\Mongo\\TestSchema', $model);
     $this->assertEquals('TestSchemaTitle', $model->title);
     $testRelDocument->testSchemaRefOne;
     $model = $testRelDocument->testSchemaRefOne;
     $this->assertInstanceOf('\\Model\\Mongo\\TestSchema', $model);
     $this->assertEquals('TestSchemaTitle', $model->title);
     $models = $testRelDocument->testSchemaRefMany;
     $this->assertTrue(is_array($models));
     $this->assertInstanceOf('\\Model\\Mongo\\TestSchema', $models[0]);
     $this->assertEquals('TestSchemaTitle', $models[0]->title);
     $this->assertCount(2, $models);
     $model = $testRelDocument->stdClassEmbedOne;
     $this->assertInstanceOf('\\stdClass', $model);
     $this->assertEquals('stdClassTitle', $model->title);
     $this->assertEquals($stdClassEmbedOne->user_id, $model->user_id);
     $model = $testRelDocument->testSchemaEmbedOne;
     $this->assertInstanceOf('\\Model\\Mongo\\TestSchema', $model);
     $this->assertEquals('TestSchemaTitle', $model->title);
     $models = $testRelDocument->testSchemaEmbedMany;
     $this->assertTrue(is_array($models));
     $this->assertInstanceOf('\\Model\\Mongo\\TestSchema', $models[0]);
     $this->assertEquals('TestSchemaTitle', $models[0]->title);
     $this->assertCount(2, $models);
     // Delete cascade
     //$testRelDocument->delete();
 }
Exemplo n.º 2
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);
 }
Exemplo n.º 3
0
 /**
  * @iterations 500
  */
 public function deleteOneByPrimaryKey()
 {
     $idx = rand(0, count($this->_ids) - 1);
     $_id = $this->_ids[$idx];
     array_splice($this->_ids, $idx, 1);
     $model = TestSchema::findOne($_id);
     if (!$model instanceof TestSchema) {
         throw new Exception(__METHOD__);
     }
     if (!$model->delete()) {
         throw new Exception(__METHOD__);
     }
 }