Skip to content

Commit

Permalink
Final test done
Browse files Browse the repository at this point in the history
  • Loading branch information
Sammaye committed Mar 21, 2016
1 parent 886ef79 commit 293dd6c
Showing 1 changed file with 39 additions and 85 deletions.
124 changes: 39 additions & 85 deletions tests/MongoDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,7 @@ public function setUpRelationalModel()
array('name' => 'cars')
);

$docsLinkedByDBRef=array(
array('name' => 'python'),
array('name' => 'java'),
array('name' => 'php'),
array('name' => 'lisp'),
array('name' => 'C'),
array('name' => 'Objective C'),
array('name' => 'C++'),
array('name' => 'ruby'),
);

foreach(array(array('class' => 'Interest','data' => $childDocs), array('class' => 'Skill','data' => $docsLinkedByDBRef)) as $subgroup){
foreach(array(array('class' => 'Interest','data' => $childDocs)) as $subgroup){
foreach($subgroup['data'] as $doc){
$i = new $subgroup['class']();
foreach($doc as $k => $v){
Expand All @@ -50,8 +39,6 @@ public function setUpRelationalModel()
}

// Lets make sure those child docs actually went in
$skills = iterator_to_array(Skill::model()->find());
$this->assertTrue(count($skills) > 0);
$c = Interest::model()->find();
$this->assertTrue($c->count() > 0);

Expand All @@ -70,20 +57,6 @@ public function setUpRelationalModel()
}
$u->interests = $interest_ids;

//Let`s take random skill as primary for this user
$primarySkill = $skills[array_rand($skills)]->_id;
$u->mainSkill = MongoDBRef::create(Skill::model()->collectionName(), $primarySkill);

//Now, lets pick array of secondary skills
$allSecondarySkills = array();
foreach(array_rand($skills, rand(3, 5)) as $secondarySkill){
if ($secondarySkill == $primarySkill){
continue;
}
array_push($allSecondarySkills, MongoDBRef::create(Skill::model()->collectionName(), $skills[$secondarySkill]->_id));
}

$u->otherSkills = $allSecondarySkills;
$this->assertTrue($u->save());
$user_ids[] = $u->_id;
}
Expand Down Expand Up @@ -140,11 +113,11 @@ public function tearDown()
public function testModel()
{
$c = User::model();
$this->assertInstanceOf('EMongoDocument', $c);
$this->assertInstanceOf('sammaye\mongoyii\Document', $c);
}

/**
* @covers EMongoDocument::save
* @covers sammaye\mongoyii\Document::save
*/
public function testSaving()
{
Expand All @@ -153,19 +126,19 @@ public function testSaving()
$this->assertTrue($c->save());

$r = User::model()->find();
$this->assertTrue($r->count()>0);
$this->assertTrue(count(iterator_to_array($r))>0);

foreach($r as $doc){
$doc->username = "dan";
$this->assertTrue($doc->save());
}
// Dan, is it you?
$r = User::model()->findOne(array('username' => 'dan') , array('username' => 1));
$r = User::model()->findOne(['username' => 'dan'] , ['projection' => ['username' => 1));
$this->assertEquals('dan', $r->username);
}

/**
* @covers EMongoDocument::delete
* @covers sammaye\mongoyii\Document::delete
*/
public function testDeleting()
{
Expand All @@ -175,26 +148,25 @@ public function testDeleting()
$r=$c->delete();
$this->assertTrue($r['n'] == 1 && $r['err'] === null);

$r = User::model()->find();
$this->assertFalse($r->count() > 0);
$r = User::model()->count();
$this->assertFalse($r > 0);
}

/**
* @covers EMongoDocument::findOne
* @covers sammaye\mongoyii\Document::findOne
*/
public function testFindOne()
{
$c = new User;
$c->username = 'sally';
$this->assertTrue($c->save());

$r = User::model()->findOne(array('username' => 'sally') , array('username' => 1));
$this->assertTrue($r->count() > 0);
$r = User::model()->findOne(['username' => 'sally'] , ['projection' => ['username' => 1]]);
$this->assertEquals('sally', $r->username);
}

/**
* @covers EMongoDocument::findBy_id
* @covers sammaye\mongoyii\Document::findBy_id
*/
public function testFindById()
{
Expand All @@ -212,7 +184,7 @@ public function testFindById()
}

/**
* @covers EMongoDocument::findAllByPk
* @covers sammaye\mongoyii\Document::findAllByPk
*/
public function testFindAllByPk()
{
Expand All @@ -229,14 +201,14 @@ public function testFindAllByPk()
$r = User::model()->findAllByPk(array((string)$c->_id));
$this->assertTrue(!is_null($r));

$this->assertInstanceOf('EMongoCursor', $r);
$this->assertInstanceOf('sammaye\mongoyii\Cursor', $r);

$r = User::model()->findOne(array('_id' => $c->_id) , array('username' => 1));
$r = User::model()->findOne(['_id' => $c->_id] , ['projection' => ['username' => 1]]);
$this->assertEquals('harry', $r->username);
}

/**
* @covers EMongoDocument::updateByPk
* @covers sammaye\mongoyii\Document::updateByPk
*/
public function testUpdateByPk()
{
Expand All @@ -247,12 +219,12 @@ public function testUpdateByPk()
$c->updateByPk($c->_id, array('$set' => array('username' => 'gfgfgf')));

$r = User::model()->findOne(array('username' => 'gfgfgf'));
$this->assertInstanceOf('EMongoDocument', $r);
$this->assertInstanceOf('sammaye\mongoyii\Document', $r);
$this->assertEquals('gfgfgf', $r->username);
}

/**
* @covers EMongoDocument::deleteByPk
* @covers sammaye\mongoyii\Document::deleteByPk
*/
public function testDeleteByPk()
{
Expand All @@ -267,7 +239,7 @@ public function testDeleteByPk()
}

/**
* @covers EMongoDocument::updateAll
* @covers sammaye\mongoyii\Document::updateAll
*/
public function testUpdateAll()
{
Expand All @@ -280,14 +252,14 @@ public function testUpdateAll()
$c->updateAll(array('username' => 'frodo'), array('$set' => array('username' => 'gdgdgd')));

$r = User::model()->findOne(array('username' => 'gdgdgd'));
$this->assertInstanceOf('EMongoDocument', $r);
$this->assertInstanceOf('sammaye\mongoyii\Document', $r);

$r = User::model()->find(array('username' => 'gdgdgd'));
$this->assertEquals(4, $r->count());
$r = User::model()->count(array('username' => 'gdgdgd'));
$this->assertEquals(4, $r);
}

/**
* @covers EMongoDocument::deleteAll
* @covers sammaye\mongoyii\Document::deleteAll
*/
public function testDeleteAll()
{
Expand All @@ -312,16 +284,16 @@ public function testDeleteAll()
$c->mainSkill = 'LoTR';
$this->assertTrue($c->save());

$r = User::model()->find(array('mainSkill' => 'LoTR'));
$this->assertEquals(10, $r->count());
$r = User::model()->count(array('mainSkill' => 'LoTR'));
$this->assertEquals(10, $r);

$c->deleteAll(array('username' => 'ringwraith'));
$r = User::model()->find(array('mainSkill' => 'LoTR'));
$this->assertEquals(1, $r->count());
$r = User::model()->count(array('mainSkill' => 'LoTR'));
$this->assertEquals(1, $r);
}

/**
* @covers EMongoDocument::saveAttributes
* @covers sammaye\mongoyii\Document::saveAttributes
*/
public function testSaveAttributes()
{
Expand All @@ -340,7 +312,7 @@ public function testSaveAttributes()
$c = new User;
$c->username = 'radagast';
$c->job_title = 'wizard';
$this->setExpectedException('EMongoException');
$this->setExpectedException('sammaye\mongoyii\Exception');
$c->saveAttributes(array('job_title'));
}

Expand All @@ -350,14 +322,14 @@ public function testPartialDocuments()
$u->username = 'sammaye';
$this->assertTrue($u->save());

$r = User::model()->findOne(array(), array('username' => 1));
$r = User::model()->findOne([], ['projection' => ['username' => 1]]);
$this->assertTrue($r->getIsPartial());

$p = $r->getProjectedFields();
$this->assertTrue(isset($p['username'], $p['_id']));
$this->assertFalse(isset($p['addresses']));

$r2 = User::model()->find(array(), array('username' => 1));
$r2 = User::model()->find([], ['projection' => ['username' => 1]]);
foreach($r2 as $row){
$this->assertTrue($row->getIsPartial());
}
Expand All @@ -371,7 +343,7 @@ public function testOneRelation()
{
$this->setUpRelationalModel();
$r = User::model()->findOne();
$this->assertInstanceOf('EMongoDocument', $r->one_interest);
$this->assertInstanceOf('sammaye\mongoyii\Document', $r->one_interest);
}

public function testManyRelation()
Expand All @@ -383,24 +355,6 @@ public function testManyRelation()
$this->assertTrue(count($r->many_interests) > 0);
}

public function testOneDBRefRelation()
{
$this->setUpRelationalModel();
$r = User::model()->findOne();
$this->assertInstanceOf('Skill', $r->primarySkill);
}

public function testManyDBRefRelation()
{
$this->setUpRelationalModel();
$r = User::model()->findOne();
$secondarySkills = $r->secondarySkills;
$this->assertTrue(count($secondarySkills) > 0);
foreach($secondarySkills as $skill){
$this->assertInstanceOf('Skill', $skill);
}
}

public function testEmbeddedRelation()
{
$this->setUpRelationalModel();
Expand All @@ -414,7 +368,7 @@ public function testWhereRelation()
{
$this->setUpRelationalModel();
$r = User::model()->findOne();
$this->assertInstanceOf('EMongoCursor', $r->where_interest);
$this->assertInstanceOf('sammaye\mongoyii\Cursor', $r->where_interest);
}

public function testFunctionalRelation()
Expand Down Expand Up @@ -545,7 +499,7 @@ public function testProperSubdocumentErrorsIndexation($post)
}

/**
* @covers EMongoDocument::exists
* @covers sammaye\mongoyii\Document::exists
*/
public function testExists()
{
Expand All @@ -556,7 +510,7 @@ public function testExists()
}

/**
* @covers EMongoDocument::equals
* @covers sammaye\mongoyii\Document::equals
*/
public function testEquals()
{
Expand Down Expand Up @@ -586,8 +540,8 @@ public function testScopes()
$this->assertTrue($c->save());
}

$u = User::model()->programmers()->find();
$this->assertTrue($u->count(true) == 2);
$u = User::model()->programmers()->count();
$this->assertTrue($u == 2);
User::model()->resetScope();
}

Expand All @@ -602,15 +556,15 @@ public function testCleanRefresh()
$this->assertNull($c->username);

$r = User::model()->findOne();
$this->assertInstanceOf('EMongoDocument', $r);
$this->assertInstanceOf('sammaye\mongoyii\Document', $r);

$r->username = 'fgfgfg';
$r->refresh();
$this->assertEquals('sammaye', $r->username);
}

/**
* @covers EMongoDocument::getAttributeLabel
* @covers sammaye\mongoyii\Document::getAttributeLabel
*/
public function testGetAttributeLabel()
{
Expand Down Expand Up @@ -641,7 +595,7 @@ public function testSaveCounters()

$f = new User;
$f->username = 'merry';
$this->setExpectedException('EMongoException');
$this->setExpectedException('sammaye\mongoyii\Exception');
$f->saveCounters(array('i' => 1));
}

Expand Down

0 comments on commit 293dd6c

Please sign in to comment.