Exemplo n.º 1
0
 public function testTest()
 {
     $criteria1 = new \Reach\Mongo\Criteria();
     $criteria1->addOr(['title' => 'Title1']);
     $expected = ['$query' => ['$or' => [['title' => 'Title1']]]];
     $this->assertEquals($expected, $criteria1->asArray());
     $criteria1->addOr(['title' => 'Title2']);
     $criteria1->addOr(['title' => 'Title3']);
     $criteria2 = new \Reach\Mongo\Criteria();
     $criteria2->add('created', '<', new \MongoDate());
     $criteria2->add(['created' => ['<' => new \MongoDate()]]);
     $criteria2->add($criteria1);
     $criteria2->where('this.created < (new Date())');
     $criteria2->orderBy(['title' => -1]);
     $query = TestSchema::query($criteria2);
     $this->assertInstanceOf('\\Model\\Mongo\\TestSchemaQuery', $query);
     $resultSet = $query->all();
     $this->assertInstanceOf('\\Reach\\Mongo\\ResultSet', $resultSet);
     $result = $resultSet->limit(2)->asArray();
     $this->assertEquals(2, count($result));
     $this->assertEquals('Title3', $result[0]->title);
     $this->assertEquals('Title2', $result[1]->title);
     $doc = $query->title1()->one();
     $this->assertInstanceOf('\\Model\\Mongo\\TestSchema', $doc);
     $this->assertEquals('Title1', $doc->title);
 }
Exemplo n.º 2
0
 public function testPaginator()
 {
     $resultSet = TestSchema::find();
     $paginator = new Paginator($resultSet, 10, 1);
     $this->assertInstanceOf('\\Reach\\Mongo\\Paginator', $paginator);
     $this->assertEquals(10, $paginator->getLimit());
     $this->assertEquals(1, $paginator->getCurrentPage());
     $this->assertInstanceOf('\\Reach\\Mongo\\ResultSet', $paginator->getResultSet());
     $paginator->setCurrentPage(2);
     $this->assertEquals(2, $paginator->getCurrentPage());
     $page = $paginator->getPaginate();
     $this->assertEquals(1, $page->first);
     $this->assertEquals(2, $page->current);
     $this->assertEquals(3, $page->next);
     $this->assertEquals(1, $page->prev);
     $this->assertEquals(10, $page->last);
     $this->assertEquals(10, $page->pages);
     $this->assertEquals(100, $page->total_items);
     $this->assertInstanceOf('\\Reach\\Mongo\\ResultSet', $page->items);
     $this->assertEquals('Title10', $page->items->first()->title);
 }
Exemplo n.º 3
0
 public function testBatchInsert()
 {
     $resultSet = new \Reach\ResultSet();
     $model = new \stdClass();
     $model->title = 'TitleStdClass';
     $resultSet->append($model);
     $model = new TestSchema(['title' => 'TestSchema1']);
     $resultSet->append($model);
     $model = new TestSchema(['title' => 'TestSchema2']);
     $resultSet->append($model);
     $result = TestSchema::batchInsert($resultSet);
     $this->assertInstanceOf('\\Reach\\ResultSet', $result);
     $this->assertEquals(2, $result->count());
     $this->assertFalse($result->first()->getIsNew());
     $resultSet = new \Reach\ResultSet();
     $model = new \stdClass();
     $model->title = 'TitleStdClass';
     $resultSet->append($model);
     $result = TestSchema::batchInsert($resultSet);
     $this->assertInstanceOf('\\Reach\\ResultSet', $result);
     $this->assertEquals(0, $result->count());
 }
Exemplo n.º 4
0
 public function testIdentityMapOverflow()
 {
     $model = new TestSchema(['title' => 'TitleFirst']);
     $model->save();
     $first_id = $model->_id;
     for ($i = 0; $i < 1000; $i++) {
         $model = new TestSchema(['title' => 'Title' . $i]);
         $model->save();
     }
     $this->assertFalse(TestSchema::getCollection()->getIdentityMap($first_id));
     TestSchema::deleteAll();
 }
Exemplo n.º 5
0
 public function testAsArray()
 {
     $resultSet = TestSchema::find();
     $array = $resultSet->asArray();
     $this->assertEquals(0, key($array));
     $resultSet = TestSchema::find()->sort(['title' => 1]);
     $array = $resultSet->export(true);
     $this->assertEquals((string) $this->_ids[0], key($array));
 }
Exemplo n.º 6
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.º 7
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__);
     }
 }