root/trunk/generator/test/classes/propel/GeneratedNestedSetTest.php

Revision 1082, 4.2 kB (checked in by tony, 2 months ago)

Refs #683: Added support for PHP 5.3 namespaces

Line 
1 <?php
2
3 use bookstore::Peer as PropelPeer;
4 use bookstore::Model as PropelModel;
5
6 /*
7  *  $Id: GeneratedNestedSetTest.php 989 2008-03-11 14:29:30Z heltem $
8  *
9  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
12  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
13  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
14  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
15  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
16  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
17  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
18  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
19  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20  *
21  * This software consists of voluntary contributions made by many individuals
22  * and is licensed under the LGPL. For more information please see
23  * <http://propel.phpdb.org>.
24  */
25
26 require_once 'cms/CmsTestBase.php';
27
28 /**
29  * Tests the generated nested-set Object classes.
30  *
31  * This test uses generated Bookstore-Cms classes to test the behavior of various
32  * object operations.  The _idea_ here is to test every possible generated method
33  * from Object.tpl; if necessary, bookstore will be expanded to accommodate this.
34  *
35  * The database is relaoded before every test and flushed after every test.  This
36  * means that you can always rely on the contents of the databases being the same
37  * for each test method in this class.  See the CmsDataPopulator::populate()
38  * method for the exact contents of the database.
39  *
40  * @see        CmsDataPopulator
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) { /* @var        $item Page */
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         //$db->beginTransaction();
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         //$db->commit();
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         // I'm not sure if the specific ID matters, but this should match original
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
Note: See TracBrowser for help on using the browser.