CustomResultSetOrderTestCase.php 6.31 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
/*
 *  $Id$
 *
 * 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 LGPL. For more information, see
19
 * <http://www.phpdoctrine.org>.
zYne's avatar
zYne committed
20 21 22 23 24 25 26 27 28
 */

/**
 * Doctrine_CustomResultSetOrder_TestCase
 *
 * @package     Doctrine
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
29
 * @link        www.phpdoctrine.org
zYne's avatar
zYne committed
30 31 32 33
 * @since       1.0
 * @version     $Revision$
 */
class Doctrine_CustomResultSetOrder_TestCase extends Doctrine_UnitTestCase {
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    
    /**
     * Prepares the data under test.
     * 
     * 1st category: 3 Boards
     * 2nd category: 1 Board
     * 3rd category: 0 boards
     * 
     */
    public function prepareData() {
        $cat1 = new CategoryWithPosition();
        $cat1->position = 0;
        $cat1->name = "First";
        
        $cat2 = new CategoryWithPosition();
        $cat2->position = 0; // same 'priority' as the first
        $cat2->name = "Second";
        
        $cat3 = new CategoryWithPosition();
        $cat3->position = 1;
        $cat3->name = "Third";
        
        $board1 = new BoardWithPosition();
        $board1->position = 0;
        
        $board2 = new BoardWithPosition();
        $board2->position = 1;
        
        $board3 = new BoardWithPosition();
        $board3->position = 2;
        
        // The first category gets 3 boards!
        $cat1->Boards[0] = $board1;
        $cat1->Boards[1] = $board2;
        $cat1->Boards[2] = $board3;
        
        $board4 = new BoardWithPosition();
        $board4->position = 0;
        
        // The second category gets 1 board!
        $cat2->Boards[0] = $board4;
        
76
        $this->connection->unitOfWork->saveAll();
77
    }
78

79 80 81 82 83
    /**
     * Prepares the tables.
     */
    public function prepareTables() {
        $this->tables[] = "CategoryWithPosition";
84
        $this->tables[] = "BoardWithPosition";
85 86
        parent::prepareTables();
    }
87

zYne's avatar
zYne committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101
    /**
     * Checks whether the boards are correctly assigned to the categories.
     *
     * The 'evil' result set that confuses the object population is displayed below.
     * 
     * catId | catPos | catName  | boardPos | board.category_id
     *  1    | 0      | First    | 0        | 1
     *  2    | 0      | Second   | 0        | 2   <-- The split that confuses the object population
     *  1    | 0      | First    | 1        | 1
     *  1    | 0      | First    | 2        | 1
     *  3    | 2      | Third    | NULL
     */
    public function testQueryWithOrdering2() {
        $q = new Doctrine_Query($this->connection);
zYne's avatar
zYne committed
102

zYne's avatar
zYne committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        $categories = $q->select('c.*, b.*')
                ->from('CategoryWithPosition c')
                ->leftJoin('c.Boards b')
                ->orderBy('c.position ASC, b.position ASC')
                ->execute(array(), Doctrine::FETCH_ARRAY);

        $this->assertEqual(3, count($categories), 'Some categories were doubled!');
                
        // Check each category
        foreach ($categories as $category) {
            switch ($category['name']) {
                case 'First':
                    // The first category should have 3 boards, right?
                    // It has only 1! The other two slipped to the 2nd category!
                    $this->assertEqual(3, count($category['Boards']));
                break;
                case 'Second':
                    // The second category should have 1 board, but it got 3 now
                    $this->assertEqual(1, count($category['Boards']));;
                break;
                case 'Third':
                    // The third has no boards as expected.
                    //print $category->Boards[0]->position;
                    $this->assertEqual(0, count($category['Boards']));
                break;
            }
            
        }
    }

133 134 135 136 137 138 139 140 141 142 143 144 145
    /**
     * Checks whether the boards are correctly assigned to the categories.
     *
     * The 'evil' result set that confuses the object population is displayed below.
     * 
     * catId | catPos | catName  | boardPos | board.category_id 
     *  1    | 0      | First    | 0        | 1
     *  2    | 0      | Second   | 0        | 2   <-- The split that confuses the object population
     *  1    | 0      | First    | 1        | 1
     *  1    | 0      | First    | 2        | 1
     *  3    | 2      | Third    | NULL
     */
    public function testQueryWithOrdering() {
146
        $q = new Doctrine_Query($this->connection);
zYne's avatar
zYne committed
147 148 149 150
        $categories = $q->select('c.*, b.*')
                ->from('CategoryWithPosition c')
                ->leftJoin('c.Boards b')
                ->orderBy('c.position ASC, b.position ASC')
151
                ->execute();
zYne's avatar
zYne committed
152 153

        $this->assertEqual(3, $categories->count(), 'Some categories were doubled!');
154
                
155 156 157 158
        // Check each category
        foreach ($categories as $category) {
            
            switch ($category->name) {
zYne's avatar
zYne committed
159
                case 'First':
160
                    // The first category should have 3 boards
zYne's avatar
zYne committed
161

162 163
                    $this->assertEqual(3, $category->Boards->count());
                break;
zYne's avatar
zYne committed
164
                case 'Second':
165
                    // The second category should have 1 board
zYne's avatar
zYne committed
166

167 168
                    $this->assertEqual(1, $category->Boards->count());
                break;
zYne's avatar
zYne committed
169
                case 'Third':
170
                    // The third has no boards as expected.
zYne's avatar
zYne committed
171
                    //print $category->Boards[0]->position;
172
                    $this->assertTrue(!isset($category->Boards));
173
                break;
174 175 176
            }
            
        }
177
    }
178
}