| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
use bookstore::Peer as PropelPeer; |
|---|
| 4 |
use bookstore::Model as PropelModel; |
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
require_once 'cms/CmsTestBase.php'; |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
class GeneratedNestedSetTest extends CmsTestBase { |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
* A convenience method to dump the page rows. |
|---|
| 46 |
*/ |
|---|
| 47 |
private function showPageItems() |
|---|
| 48 |
{ |
|---|
| 49 |
$tree = PropelPeer::PagePeer::retrieveTree(); |
|---|
| 50 |
$iterator = new RecursiveIteratorIterator($tree, RecursiveIteratorIterator::SELF_FIRST); |
|---|
| 51 |
|
|---|
| 52 |
foreach ($iterator as $item) { |
|---|
| 53 |
echo str_repeat('- ', $iterator->getDepth()) |
|---|
| 54 |
, $item->getId() , ': ' |
|---|
| 55 |
, $item->getTitle() |
|---|
| 56 |
, ' [', $item->getLeftValue(), ':', $item->getRightValue() , ']' |
|---|
| 57 |
. "\n"; |
|---|
| 58 |
} |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
* Adds a new Page row with specified parent Id. |
|---|
| 63 |
* |
|---|
| 64 |
* @param int $parentId |
|---|
| 65 |
*/ |
|---|
| 66 |
protected function addNewChildPage($parentId) |
|---|
| 67 |
{ |
|---|
| 68 |
$db = ::Propel::getConnection(PropelPeer::PagePeer::DATABASE_NAME); |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
$parent = PropelPeer::PagePeer::retrieveByPK($parentId); |
|---|
| 73 |
$page = new PropelModel::Page(); |
|---|
| 74 |
$page->setTitle('new page '.time()); |
|---|
| 75 |
$page->insertAsLastChildOf($parent); |
|---|
| 76 |
$page->save(); |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
* Asserts that the Page table tree integrity is intact. |
|---|
| 83 |
*/ |
|---|
| 84 |
protected function assertPageTreeIntegrity() |
|---|
| 85 |
{ |
|---|
| 86 |
$db = ::Propel::getConnection(PropelPeer::PagePeer::DATABASE_NAME); |
|---|
| 87 |
|
|---|
| 88 |
$values = array(); |
|---|
| 89 |
$log = ''; |
|---|
| 90 |
|
|---|
| 91 |
foreach ($db->query('SELECT Id, LeftChild, RightChild, Title FROM Page', PDO::FETCH_NUM) as $row) { |
|---|
| 92 |
|
|---|
| 93 |
list($id, $leftChild, $rightChild, $title) = $row; |
|---|
| 94 |
|
|---|
| 95 |
if (!in_array($leftChild, $values)) { |
|---|
| 96 |
$values[] = (int) $leftChild; |
|---|
| 97 |
} else { |
|---|
| 98 |
$this->fail('Duplicate LeftChild value '.$leftChild); |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
if (!in_array($rightChild, $values)) { |
|---|
| 102 |
$values[] = (int) $rightChild; |
|---|
| 103 |
} else { |
|---|
| 104 |
$this->fail('Duplicate RightChild value '.$rightChild); |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
$log .= "[$id($leftChild:$rightChild)]"; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
sort($values); |
|---|
| 111 |
|
|---|
| 112 |
if ($values[count($values)-1] != count($values)) { |
|---|
| 113 |
$message = sprintf("Tree integrity NOT ok (%s)\n", $log); |
|---|
| 114 |
$message .= sprintf('Integrity error: value count: %d, high value: %d', count($values), $values[count($values)-1]); |
|---|
| 115 |
$this->fail($message); |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
* Tests adding a node to the Page tree. |
|---|
| 122 |
*/ |
|---|
| 123 |
public function testAdd() |
|---|
| 124 |
{ |
|---|
| 125 |
$db = ::Propel::getConnection(PropelPeer::PagePeer::DATABASE_NAME); |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
// code. The ID will change with subsequent runs (e.g. the first time it will be 11) |
|---|
| 129 |
$startId = $db->query('SELECT MIN(Id) FROM Page')->fetchColumn(); |
|---|
| 130 |
$this->addNewChildPage($startId + 10); |
|---|
| 131 |
$this->assertPageTreeIntegrity(); |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
} |
|---|
| 135 |
|
|---|