forked from thephpleague/monga
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Query\Update tests, fixed update bugs
- Loading branch information
1 parent
8e65eb6
commit c6058d8
Showing
3 changed files
with
221 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
|
||
class QueryUpdateTests extends PHPUnit_Framework_TestCase | ||
{ | ||
protected $update; | ||
|
||
public function setUp() | ||
{ | ||
$this->update = new Monga\Query\Update(); | ||
} | ||
|
||
public function getProperty($property) | ||
{ | ||
$reflection = new ReflectionObject($this->update); | ||
$property = $property = $reflection->getProperty($property); | ||
$property->setAccessible(true); | ||
return $property->getValue($this->update); | ||
} | ||
|
||
public function testSingle() | ||
{ | ||
$this->assertTrue($this->getProperty('multiple')); | ||
$this->update->single(); | ||
$this->assertFalse($this->getProperty('multiple')); | ||
$this->update->single(false); | ||
$this->assertTrue($this->getProperty('multiple')); | ||
} | ||
|
||
public function testMultiple() | ||
{ | ||
$this->assertTrue($this->getProperty('multiple')); | ||
$this->update->multiple(false); | ||
$this->assertFalse($this->getProperty('multiple')); | ||
$this->update->multiple(); | ||
$this->assertTrue($this->getProperty('multiple')); | ||
} | ||
|
||
public function testUpsert() | ||
{ | ||
$this->update->upsert(false); | ||
$this->assertFalse($this->getProperty('upsert')); | ||
$this->update->upsert(); | ||
$this->assertTrue($this->getProperty('upsert')); | ||
} | ||
|
||
public function testSet() | ||
{ | ||
$this->update->set('field', 'value'); | ||
|
||
$this->assertEquals(array( | ||
'field' => array('$set', 'value'), | ||
), | ||
$this->getProperty('update')); | ||
} | ||
|
||
public function testRemove() | ||
{ | ||
$this->update->remove('one', 'two'); | ||
|
||
$this->assertEquals(array( | ||
'one' => array('$unset', 1), | ||
'two' => array('$unset', 1), | ||
), | ||
$this->getProperty('update')); | ||
} | ||
|
||
public function testPush() | ||
{ | ||
$this->update->push('field', 'value'); | ||
$this->update->push('field2', 'value', true); | ||
|
||
$this->assertEquals(array( | ||
'field' => array('$push', 'value'), | ||
'field2' => array('$pushAll', 'value'), | ||
), | ||
$this->getProperty('update')); | ||
} | ||
|
||
public function testPushAll() | ||
{ | ||
$this->update->pushAll('field', array('value', 'other')); | ||
|
||
$this->assertEquals(array( | ||
'field' => array('$pushAll', array('value', 'other')), | ||
), | ||
$this->getProperty('update')); | ||
} | ||
|
||
public function testPull() | ||
{ | ||
$this->update->pull('field', 'value'); | ||
$this->update->pull('field2', 'value', true); | ||
|
||
$this->assertEquals(array( | ||
'field' => array('$pull', 'value'), | ||
'field2' => array('$pullAll', 'value'), | ||
), | ||
$this->getProperty('update')); | ||
} | ||
|
||
public function testPullAll() | ||
{ | ||
$this->update->pullAll('field', array('value', 'other')); | ||
|
||
$this->assertEquals(array( | ||
'field' => array('$pullAll', array('value', 'other')), | ||
), | ||
$this->getProperty('update')); | ||
} | ||
|
||
public function testAddToSet() | ||
{ | ||
$this->update->addToSet('field', array('value', 'other')); | ||
|
||
$this->assertEquals(array( | ||
'field' => array('$addToSet', array('value', 'other')), | ||
), | ||
$this->getProperty('update')); | ||
} | ||
|
||
public function testPop() | ||
{ | ||
$this->update->pop('field'); | ||
|
||
$this->assertEquals(array( | ||
'field' => array('$pop', 1), | ||
), | ||
$this->getProperty('update')); | ||
} | ||
|
||
public function testUnshift() | ||
{ | ||
$this->update->unshift('field'); | ||
|
||
$this->assertEquals(array( | ||
'field' => array('$pop', -1), | ||
), | ||
$this->getProperty('update')); | ||
} | ||
|
||
public function testIncrement() | ||
{ | ||
$this->update->increment('field', 2); | ||
|
||
$this->assertEquals(array( | ||
'field' => array('$inc', 2), | ||
), | ||
$this->getProperty('update')); | ||
} | ||
|
||
public function testGetUpdate() | ||
{ | ||
$expected = array( | ||
'$set' => array('field' => 'value'), | ||
'$inc' => array('downloads' => 1), | ||
); | ||
|
||
$result = $this->update->increment('downloads', 1)->set('field', 'value')->getUpdate(); | ||
|
||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
public function testGetOptions() | ||
{ | ||
$this->update->single(); | ||
$this->assertEquals(array( | ||
'safe' => false, | ||
'fsync' => false, | ||
'timeout' => MongoCursor::$timeout, | ||
'upsert' => false, | ||
'multiple' => false, | ||
), $this->update->getOptions()); | ||
} | ||
} |