Skip to content

Commit

Permalink
Added Query\Update tests, fixed update bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Nov 13, 2012
1 parent 8e65eb6 commit c6058d8
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 13 deletions.
34 changes: 22 additions & 12 deletions src/Monga/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,28 +268,38 @@ public function insert(array $data, $options = array())
/**
* Updates a collection
*
* @param mixed $query update array or callback
* @param array $options update query options
* @param mixed $values update array or callback
* @param mixed $query update filter
* @param array $options update options
* @return boolean query success
*/
public function update($query = array(), $options = array())
public function update($values = array(), $query = null, $options = array())
{
if ($query instanceof CLosure)
if ($values instanceof CLosure)
{
$update = new Query\Update();
$update->setOptions($options);
$query($update);
$query = new Query\Update();
$query->setOptions($options);
$values($query);

$query = $update->getUpdate();
$options = $update->getOptions();
$options = $query->getOptions();
$values = $query->getUpdate();
$query = $query->getWhere();
}

if ( ! is_array($query) or ! is_array($options))
if ( ! is_array($values) or ! is_array($options))
{
throw new \InvalidArgumentException('Update params $query and $options must be arrays.');
throw new \InvalidArgumentException('Update params $update and $options must be arrays.');
}

$result = $this->collection->update($query, $options);
isset($query) or $query = array();

var_dump($query);
var_dump($values);
var_dump($options);

$result = $this->collection->update($query, $values, $options);

var_dump($result);

return $result === true or !! $result['ok'];
}
Expand Down
26 changes: 25 additions & 1 deletion src/Monga/Query/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,36 @@

class Update extends Where
{
/**
* @var bool $upsert wether to allow upserts
*/
protected $upsert = false;

/**
* @var bool $multiple wether to update multiple or only one
*/
protected $multiple = true;

/**
* @var array @update update query
*/
protected $update = array();

/**
* Returns the query options
*
* @return array query options
*/
public function getOptions()
{
$options = parent::getOptions();

$options['upsert'] = $this->upsert;
$options['multiple'] = $this->multiple;

return $options;
}

/**
* Set the multiple option negatively.
*
Expand Down Expand Up @@ -239,6 +263,6 @@ public function getUpdate()
$update[$type][$field] = $value;
}

return $update;;
return $update;
}
}
174 changes: 174 additions & 0 deletions tests/QueryUpdateTests.php
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());
}
}

0 comments on commit c6058d8

Please sign in to comment.