Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Mar 25, 2015
1 parent 8ae6489 commit 371006c
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 2 deletions.
57 changes: 57 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Queue/TreeOperation.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ODM\PHPCR\Queue;

/**
* Represents a horizontal tree operation, encompassing
* MOVE, REMOVE and INSERT operations.
*
* @author Daniel Leech <[email protected]>
*/
class TreeOperation
{
const OP_MOVE = 'move';
Expand All @@ -13,33 +36,67 @@ class TreeOperation
private $args;
private $valid = true;

/**
* @param mixed $type
* @param mixed $oid
* @param mixed $args
*/
public function __construct($type, $oid, $args)
{
$this->oid = $oid;
$this->type = $type;
$this->args = $args;
}

/**
* Return the SPL object ID which maps
* to the document on which the operaton should be
* performed.
*
* @return string
*/
public function getOid()
{
return $this->oid;
}

/**
* Return the type of operation, one of
* the self::OP_* constants.
*
* @return string
*/
public function getType()
{
return $this->type;
}

/**
* Return the arguments for the operation
*
* @return mixed
*/
public function getArgs()
{
return $this->args;
}

/**
* Invalidate this item so that it will not be processed.
* Invalidation is quicker than removing the item from the queue.
*
* @param boolean
*/
public function invalidate()
{
$this->valid = false;
}

/**
* Return true if this item should be processed
*
* @return boolean
*/
public function isValid()
{
return $this->valid;
Expand Down
46 changes: 46 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Queue/TreeOperationBatch.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,73 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ODM\PHPCR\Queue;

/**
* Represents a batch of operations of the same type
*
* @author Daniel Leech <[email protected]>
*/
class TreeOperationBatch
{
private $type;
private $schedule = array();

/**
* @param mixed $type
*/
public function __construct($type)
{
$this->type = $type;
}

/**
* Return the operation schedule, e.g.
*
* array(
* $oid => $args
* )
*
* @return array
*/
public function getSchedule()
{
return $this->schedule;
}

/**
* Schedule an operation
*
* @param string $oid
* @param array $args
*/
public function schedule($oid, $args)
{
$this->schedule[$oid] = $args;
}

/**
* Return the operation type, one of the
* TreeOperation::OP_* constants.
*
* @return string
*/
public function getType()
{
return $this->type;
Expand Down
58 changes: 58 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Queue/TreeOperationQueue.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,50 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ODM\PHPCR\Queue;

use Doctrine\ODM\PHPCR\Queue\TreeOperation;
use Doctrine\ODM\PHPCR\Queue\TreeOperationBatch;

/**
* The tree operation queue
*
* @author Daniel Leech <[email protected]>
*/
class TreeOperationQueue
{
private $queue = array();

/**
* Push a new operation onto the queue
*
* @param TreeOperation $operation
*/
public function push(TreeOperation $operation)
{
$this->queue[] = $operation;
}

/**
* Partition into contiguous sets of operations
*
* @return TreeOperationBatch[]
*/
public function getBatches()
{
Expand All @@ -42,11 +71,21 @@ public function getBatches()
return $batches;
}

/**
* Clear the queue
*/
public function clear()
{
$this->queue = array();
}

/**
* Return the entire schedule for the given operation type
*
* @param string $type
*
* @return array
*/
public function getSchedule($type)
{
$schedule = array();
Expand All @@ -62,6 +101,14 @@ public function getSchedule($type)
return $schedule;
}

/**
* Return true if the spl object id is scheduled for the given type
*
* @param string $type
* @param string $oid
*
* @return boolean
*/
public function isQueued($type, $oid)
{
foreach ($this->queue as $operation) {
Expand All @@ -81,6 +128,12 @@ public function isQueued($type, $oid)
return false;
}

/**
* Remove all operations with the given spl object ID and type
*
* @param string $type
* @param string $oid
*/
public function unqueue($type, $oid)
{
foreach ($this->queue as $operation) {
Expand All @@ -96,6 +149,11 @@ public function unqueue($type, $oid)
}
}

/**
* Remove all references to the given spl object ID
*
* @param $oid string
*/
public function unregister($oid)
{
foreach ($this->queue as $operation) {
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/ODM/PHPCR/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -3035,6 +3035,7 @@ private function unregisterDocument($document)
}

$this->treeOpQueue->unregister($oid);

unset(
$this->scheduledUpdates[$oid],
$this->scheduledReorders[$oid],
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/Tests/ODM/PHPCR/Functional/BasicCrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ public function testInsertMoveDelete()
$subuser->username = "test";
$subuser->id = '/functional/user2/subuser';
$this->dm->persist($subuser);
$this->dm->move($subuser, '/functional/user1/subuser');
$this->dm->remove($user2);
$this->dm->move($subuser, '/functional/usere/subuser');
$this->dm->remove($user2);s
$this->dm->flush();

$subuser = $this->dm->find(null, '/functional/user1/subuser');
Expand Down
33 changes: 33 additions & 0 deletions tests/Doctrine/Tests/ODM/PHPCR/Queue/TreeOperationBatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Doctrine\Tests\ODM\PHPCR\Queue;

use Doctrine\ODM\PHPCR\Queue\TreeOperationBatch;
use Doctrine\ODM\PHPCR\Queue\TreeOperation;

class TreeOperationBatchTest extends \PHPUnit_Framework_Testcase
{
private $batch;

public function setUp()
{
$this->batch = new TreeOperationBatch(TreeOperation::OP_MOVE);
}

public function testGetters()
{
$this->assertEquals(TreeOperation::OP_MOVE, $this->batch->getType());
}

public function testSchedule()
{
$this->batch->schedule('1234', 'arg');
$this->batch->schedule('4321', 'arg');

$this->assertEquals(array(
'1234' => 'arg',
'4321' => 'arg',
), $this->batch->getSchedule());
}
}

Loading

0 comments on commit 371006c

Please sign in to comment.