SnapshotTestCase.php 4.67 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 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
<?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
 * <http://www.phpdoctrine.com>.
 */

/**
 * Doctrine_Collection_Snapshot_TestCase
 *
 * This test case is used for testing the snapshot functionality
 * of the Doctrine_Collection
 *
 * Snapshots are used for counting the diff between original and changed 
 * state of the collection.
 *
 * @package     Doctrine
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
class Doctrine_Collection_Snapshot_TestCase extends Doctrine_UnitTestCase
{
zYne's avatar
zYne committed
41 42
    public function prepareTables()
    {
43
        $this->tables = array('Entity', 'User', 'Group', 'Groupuser', 'Account', 'Album', 'Phonenumber', 'Email', 'Book');
44 45
        
        parent::prepareTables();
zYne's avatar
zYne committed
46
    }
zYne's avatar
zYne committed
47

zYne's avatar
zYne committed
48 49
    public function testDiffForSimpleCollection()
    {
zYne's avatar
zYne committed
50 51 52
        $q = Doctrine_Query::create()->from('User u')->orderby('u.id');

        $coll = $q->execute();
zYne's avatar
zYne committed
53 54 55 56 57 58 59 60
        $this->assertEqual($coll->count(), 8);

        unset($coll[0]);
        unset($coll[1]);

        $coll[]->name = 'new user';

        $this->assertEqual($coll->count(), 7);
zYne's avatar
zYne committed
61 62 63
        $this->assertEqual(count($coll->getSnapshot()), 8);

        $count = $this->conn->count();
zYne's avatar
zYne committed
64 65 66 67 68 69 70

        $coll->save();

        $this->connection->clear();
        $coll = Doctrine_Query::create()->from('User u')->execute();
        $this->assertEqual($coll->count(), 7);
    }
zYne's avatar
zYne committed
71

zYne's avatar
zYne committed
72 73
    public function testDiffForOneToManyRelatedCollection()
    {
zYne's avatar
zYne committed
74 75 76
        $q = new Doctrine_Query();
        $q->from('User u LEFT JOIN u.Phonenumber p')
             ->where('u.id = 8');
zYne's avatar
zYne committed
77 78 79 80 81 82

        $coll = $q->execute();

        $this->assertEqual($coll->count(), 1);

        $this->assertEqual($coll[0]->Phonenumber->count(), 3);
zYne's avatar
zYne committed
83
        $this->assertTrue($coll[0]->Phonenumber instanceof Doctrine_Collection);
zYne's avatar
zYne committed
84 85 86 87

        unset($coll[0]->Phonenumber[0]);
        $coll[0]->Phonenumber->remove(2);

zYne's avatar
zYne committed
88
        $this->assertEqual(count($coll[0]->Phonenumber->getSnapshot()), 3);
zYne's avatar
zYne committed
89 90 91 92 93 94
        $coll[0]->save();

        $this->assertEqual($coll[0]->Phonenumber->count(), 1);

        $this->connection->clear();

zYne's avatar
zYne committed
95
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
96 97 98 99 100
        $q = Doctrine_Query::create()->from('User u LEFT JOIN u.Phonenumber p')->where('u.id = 8');

        $coll = $q->execute();

        $this->assertEqual($coll[0]->Phonenumber->count(), 1);
zYne's avatar
zYne committed
101

zYne's avatar
zYne committed
102
    }
zYne's avatar
zYne committed
103

zYne's avatar
zYne committed
104 105 106 107 108 109 110
    public function testDiffForManyToManyRelatedCollection()
    {
        $user = new User();
        $user->name = 'zYne';
        $user->Group[0]->name = 'PHP';
        $user->Group[1]->name = 'Web';
        $user->save();
zYne's avatar
zYne committed
111

zYne's avatar
zYne committed
112 113 114 115 116 117 118
        $this->connection->clear();

        $users = Doctrine_Query::create()->from('User u LEFT JOIN u.Group g')
                 ->where('u.id = ' . $user->id)->execute();

        $this->assertEqual($users[0]->Group[0]->name, 'PHP');
        $this->assertEqual($users[0]->Group[1]->name, 'Web');
zYne's avatar
zYne committed
119
        $this->assertEqual(count($user->Group->getSnapshot()), 2);
zYne's avatar
zYne committed
120 121 122
        unset($user->Group[0]);

        $user->save();
zYne's avatar
zYne committed
123
        $this->assertEqual(count($user->Group), 1);
zYne's avatar
zYne committed
124 125 126 127

        $this->assertEqual(count($user->Group->getSnapshot()), 1);
        unset($user->Group[1]);
        $this->assertEqual(count($user->Group->getSnapshot()), 1);
zYne's avatar
zYne committed
128 129
        
        $count = count($this->conn);
zYne's avatar
zYne committed
130 131 132
        $user->save();

        $this->assertEqual(count($user->Group->getSnapshot()), 0);
zYne's avatar
zYne committed
133 134 135 136 137 138 139
        
        $this->conn->clear();

        $users = Doctrine_Query::create()->from('User u LEFT JOIN u.Group g')
                 ->where('u.id = ' . $user->id)->execute();
        
        $this->assertEqual(count($user->Group), 0);
zYne's avatar
zYne committed
140

zYne's avatar
zYne committed
141
    }
zYne's avatar
zYne committed
142

zYne's avatar
zYne committed
143
}