Skip to content

Commit 5d78fd7

Browse files
committed
Added tests
1 parent 8ae6489 commit 5d78fd7

File tree

7 files changed

+356
-0
lines changed

7 files changed

+356
-0
lines changed

lib/Doctrine/ODM/PHPCR/Queue/TreeOperation.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
11
<?php
2+
/*
3+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
*
15+
* This software consists of voluntary contributions made by many individuals
16+
* and is licensed under the MIT license. For more information, see
17+
* <http://www.doctrine-project.org>.
18+
*/
219

320
namespace Doctrine\ODM\PHPCR\Queue;
421

22+
/**
23+
* Represents a horizontal tree operation, encompassing
24+
* MOVE, REMOVE and INSERT operations.
25+
*
26+
* @author Daniel Leech <[email protected]>
27+
*/
528
class TreeOperation
629
{
730
const OP_MOVE = 'move';
@@ -13,33 +36,67 @@ class TreeOperation
1336
private $args;
1437
private $valid = true;
1538

39+
/**
40+
* @param mixed $type
41+
* @param mixed $oid
42+
* @param mixed $args
43+
*/
1644
public function __construct($type, $oid, $args)
1745
{
1846
$this->oid = $oid;
1947
$this->type = $type;
2048
$this->args = $args;
2149
}
2250

51+
/**
52+
* Return the SPL object ID which maps
53+
* to the document on which the operaton should be
54+
* performed.
55+
*
56+
* @return string
57+
*/
2358
public function getOid()
2459
{
2560
return $this->oid;
2661
}
2762

63+
/**
64+
* Return the type of operation, one of
65+
* the self::OP_* constants.
66+
*
67+
* @return string
68+
*/
2869
public function getType()
2970
{
3071
return $this->type;
3172
}
3273

74+
/**
75+
* Return the arguments for the operation
76+
*
77+
* @return mixed
78+
*/
3379
public function getArgs()
3480
{
3581
return $this->args;
3682
}
3783

84+
/**
85+
* Invalidate this item so that it will not be processed.
86+
* Invalidation is quicker than removing the item from the queue.
87+
*
88+
* @param boolean
89+
*/
3890
public function invalidate()
3991
{
4092
$this->valid = false;
4193
}
4294

95+
/**
96+
* Return true if this item should be processed
97+
*
98+
* @return boolean
99+
*/
43100
public function isValid()
44101
{
45102
return $this->valid;

lib/Doctrine/ODM/PHPCR/Queue/TreeOperationBatch.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,73 @@
11
<?php
2+
/*
3+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
*
15+
* This software consists of voluntary contributions made by many individuals
16+
* and is licensed under the MIT license. For more information, see
17+
* <http://www.doctrine-project.org>.
18+
*/
219

320
namespace Doctrine\ODM\PHPCR\Queue;
421

22+
/**
23+
* Represents a batch of operations of the same type
24+
*
25+
* @author Daniel Leech <[email protected]>
26+
*/
527
class TreeOperationBatch
628
{
729
private $type;
830
private $schedule = array();
931

32+
/**
33+
* @param mixed $type
34+
*/
1035
public function __construct($type)
1136
{
1237
$this->type = $type;
1338
}
1439

40+
/**
41+
* Return the operation schedule, e.g.
42+
*
43+
* array(
44+
* $oid => $args
45+
* )
46+
*
47+
* @return array
48+
*/
1549
public function getSchedule()
1650
{
1751
return $this->schedule;
1852
}
1953

54+
/**
55+
* Schedule an operation
56+
*
57+
* @param string $oid
58+
* @param array $args
59+
*/
2060
public function schedule($oid, $args)
2161
{
2262
$this->schedule[$oid] = $args;
2363
}
2464

65+
/**
66+
* Return the operation type, one of the
67+
* TreeOperation::OP_* constants.
68+
*
69+
* @return string
70+
*/
2571
public function getType()
2672
{
2773
return $this->type;

lib/Doctrine/ODM/PHPCR/Queue/TreeOperationQueue.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,50 @@
11
<?php
2+
/*
3+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
*
15+
* This software consists of voluntary contributions made by many individuals
16+
* and is licensed under the MIT license. For more information, see
17+
* <http://www.doctrine-project.org>.
18+
*/
219

320
namespace Doctrine\ODM\PHPCR\Queue;
421

522
use Doctrine\ODM\PHPCR\Queue\TreeOperation;
623
use Doctrine\ODM\PHPCR\Queue\TreeOperationBatch;
724

25+
/**
26+
* The tree operation queue
27+
*
28+
* @author Daniel Leech <[email protected]>
29+
*/
830
class TreeOperationQueue
931
{
1032
private $queue = array();
1133

34+
/**
35+
* Push a new operation onto the queue
36+
*
37+
* @param TreeOperation $operation
38+
*/
1239
public function push(TreeOperation $operation)
1340
{
1441
$this->queue[] = $operation;
1542
}
1643

1744
/**
1845
* Partition into contiguous sets of operations
46+
*
47+
* @return TreeOperationBatch[]
1948
*/
2049
public function getBatches()
2150
{
@@ -42,11 +71,21 @@ public function getBatches()
4271
return $batches;
4372
}
4473

74+
/**
75+
* Clear the queue
76+
*/
4577
public function clear()
4678
{
4779
$this->queue = array();
4880
}
4981

82+
/**
83+
* Return the entire schedule for the given operation type
84+
*
85+
* @param string $type
86+
*
87+
* @return array
88+
*/
5089
public function getSchedule($type)
5190
{
5291
$schedule = array();
@@ -62,6 +101,14 @@ public function getSchedule($type)
62101
return $schedule;
63102
}
64103

104+
/**
105+
* Return true if the spl object id is scheduled for the given type
106+
*
107+
* @param string $type
108+
* @param string $oid
109+
*
110+
* @return boolean
111+
*/
65112
public function isQueued($type, $oid)
66113
{
67114
foreach ($this->queue as $operation) {
@@ -81,6 +128,12 @@ public function isQueued($type, $oid)
81128
return false;
82129
}
83130

131+
/**
132+
* Remove all operations with the given spl object ID and type
133+
*
134+
* @param string $type
135+
* @param string $oid
136+
*/
84137
public function unqueue($type, $oid)
85138
{
86139
foreach ($this->queue as $operation) {
@@ -96,6 +149,11 @@ public function unqueue($type, $oid)
96149
}
97150
}
98151

152+
/**
153+
* Remove all references to the given spl object ID
154+
*
155+
* @param $oid string
156+
*/
99157
public function unregister($oid)
100158
{
101159
foreach ($this->queue as $operation) {

lib/Doctrine/ODM/PHPCR/UnitOfWork.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,6 +3035,7 @@ private function unregisterDocument($document)
30353035
}
30363036

30373037
$this->treeOpQueue->unregister($oid);
3038+
30383039
unset(
30393040
$this->scheduledUpdates[$oid],
30403041
$this->scheduledReorders[$oid],
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Doctrine\Tests\ODM\PHPCR\Queue;
4+
5+
use Doctrine\ODM\PHPCR\Queue\TreeOperationBatch;
6+
use Doctrine\ODM\PHPCR\Queue\TreeOperation;
7+
8+
class TreeOperationBatchTest extends \PHPUnit_Framework_Testcase
9+
{
10+
private $batch;
11+
12+
public function setUp()
13+
{
14+
$this->batch = new TreeOperationBatch(TreeOperation::OP_MOVE);
15+
}
16+
17+
public function testGetters()
18+
{
19+
$this->assertEquals(TreeOperation::OP_MOVE, $this->batch->getType());
20+
}
21+
22+
public function testSchedule()
23+
{
24+
$this->batch->schedule('1234', 'arg');
25+
$this->batch->schedule('4321', 'arg');
26+
27+
$this->assertEquals(array(
28+
'1234' => 'arg',
29+
'4321' => 'arg',
30+
), $this->batch->getSchedule());
31+
}
32+
}
33+

0 commit comments

Comments
 (0)