TableTestCase.php 8.48 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  $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_Table_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
 * @since       1.0
 * @version     $Revision$
 */
zYne's avatar
zYne committed
33
class Doctrine_Table_TestCase extends Doctrine_UnitTestCase
zYne's avatar
zYne committed
34 35 36 37 38
{
    
    public function prepareTables()
    {
        $this->tables[] = 'FieldNameTest';
zYne's avatar
zYne committed
39
        parent::prepareTables();
zYne's avatar
zYne committed
40
    }
41

zYne's avatar
zYne committed
42 43 44 45 46 47 48 49
    public function testInitializingNewTableWorksWithoutConnection()
    {
        $table = new Doctrine_Table('Test', $this->conn);
        
        $this->assertEqual($table->getComponentName(), 'Test');
    }

    public function testFieldConversion()
zYne's avatar
zYne committed
50
    {
zYne's avatar
zYne committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);

        $t = new FieldNameTest();
        
        $t->someColumn = 'abc';
        $t->someEnum = 'php';
        $t->someInt = 1;
        $t->someArray = array();
        $obj = new StdClass();
        $t->someObject = $obj;

        $this->assertEqual($t->someColumn, 'abc');
        $this->assertEqual($t->someEnum, 'php');
        $this->assertEqual($t->someInt, 1);
        $this->assertEqual($t->someArray, array());
        $this->assertEqual($t->someObject, $obj);

        $t->save();

        $this->assertEqual($t->someColumn, 'abc');
        $this->assertEqual($t->someEnum, 'php');
        $this->assertEqual($t->someInt, 1);
        $this->assertEqual($t->someArray, array());
        $this->assertEqual($t->someObject, $obj);
75

zYne's avatar
zYne committed
76 77 78 79 80 81 82 83 84 85
        $t->refresh();
        
        $this->assertEqual($t->someColumn, 'abc');
        $this->assertEqual($t->someEnum, 'php');
        $this->assertEqual($t->someInt, 1);
        $this->assertEqual($t->someArray, array());
        $this->assertEqual($t->someObject, $obj);
        
        $this->connection->clear();
        
86
        $t = $this->connection->getRepository('FieldNameTest')->find(1);
zYne's avatar
zYne committed
87

zYne's avatar
zYne committed
88 89 90 91 92
        $this->assertEqual($t->someColumn, 'abc');
        $this->assertEqual($t->someEnum, 'php');
        $this->assertEqual($t->someInt, 1);
        $this->assertEqual($t->someArray, array());
        $this->assertEqual($t->someObject, $obj);
93 94
        
        $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
zYne's avatar
zYne committed
95
    }
96

zYne's avatar
zYne committed
97 98
    public function testGetForeignKey() 
    {
99
        $fk = $this->objTable->getTable()->getRelation("Group");
100
        $this->assertTrue($fk instanceof Doctrine_Relation_Association);
101
        $this->assertTrue($fk->getTable() instanceof Doctrine_ClassMetadata);
102
        $this->assertTrue($fk->getType() == Doctrine_Relation::MANY_AGGREGATE);
doctrine's avatar
doctrine committed
103 104 105
        $this->assertTrue($fk->getLocal() == "user_id");
        $this->assertTrue($fk->getForeign() == "group_id");

106
        $fk = $this->objTable->getTable()->getRelation("Email");
107
        $this->assertTrue($fk instanceof Doctrine_Relation_LocalKey);
108 109

        $this->assertTrue($fk->getTable() instanceof Doctrine_ClassMetadata);
jwage's avatar
jwage committed
110
        $this->assertTrue($fk->getType() == Doctrine_Relation::ONE_AGGREGATE);
111

doctrine's avatar
doctrine committed
112
        $this->assertTrue($fk->getLocal() == "email_id");
113 114
        $fkIdFieldNames = (array)$fk->getTable()->getIdentifier();
        $this->assertTrue($fk->getForeign() == $fkIdFieldNames[0]);
doctrine's avatar
doctrine committed
115 116


117
        $fk = $this->objTable->getTable()->getRelation('Phonenumber');
118
        $this->assertTrue($fk instanceof Doctrine_Relation_ForeignKey);
119
        $this->assertTrue($fk->getTable() instanceof Doctrine_ClassMetadata);
zYne's avatar
zYne committed
120
        $this->assertTrue($fk->getType() == Doctrine_Relation::MANY);
121 122
        $objTableIdFieldNames = (array)$this->objTable->getTable()->getIdentifier();
        $this->assertTrue($fk->getLocal() == $objTableIdFieldNames[0]);
zYne's avatar
zYne committed
123
        $this->assertTrue($fk->getForeign() == 'entity_id');
zYne's avatar
zYne committed
124

doctrine's avatar
doctrine committed
125

doctrine's avatar
doctrine committed
126
    }
zYne's avatar
zYne committed
127 128
    public function testGetComponentName() 
    {
zYne's avatar
zYne committed
129
        $this->assertTrue($this->objTable->getComponentName() == 'User');
doctrine's avatar
doctrine committed
130
    } 
zYne's avatar
zYne committed
131 132 133

    public function testGetTableName() 
    {
134
        $this->assertTrue($this->objTable->getTable()->getTableName() == 'entity');
doctrine's avatar
doctrine committed
135
    } 
zYne's avatar
zYne committed
136 137 138

    public function testGetConnection() 
    {
zYne's avatar
zYne committed
139
        $this->assertTrue($this->objTable->getConnection() instanceof Doctrine_Connection);
doctrine's avatar
doctrine committed
140
    }
zYne's avatar
zYne committed
141 142 143

    public function testSetSequenceName()
    {
zYne's avatar
zYne committed
144 145
        $this->objTable->sequenceName = 'test-seq';
        $this->assertEqual($this->objTable->sequenceName, 'test-seq');
zYne's avatar
zYne committed
146
        $this->objTable->sequenceName = null;
doctrine's avatar
doctrine committed
147
    }
zYne's avatar
zYne committed
148 149 150

    public function testCreate() 
    {
doctrine's avatar
doctrine committed
151
        $record = $this->objTable->create();
152 153
        $this->assertTrue($record instanceof Doctrine_Entity);
        $this->assertTrue($record->state() == Doctrine_Entity::STATE_TCLEAN);
doctrine's avatar
doctrine committed
154
    }
zYne's avatar
zYne committed
155 156 157

    public function testFind() 
    {
doctrine's avatar
doctrine committed
158
        $record = $this->objTable->find(4);
159
        $this->assertTrue($record instanceof Doctrine_Entity);
doctrine's avatar
doctrine committed
160
        
161 162
        try {
            $record = $this->objTable->find('4');
163
            $this->assertTrue($record instanceof Doctrine_Entity);
164
        } catch(Exception $e) {
pookey's avatar
pookey committed
165
            $this->assertTrue(false);
166 167
        }

168 169 170
        try {
            $record = $this->objTable->find('4', Doctrine::FETCH_ARRAY);
            $this->assertTrue(is_array($record));
171
            $this->assertTrue( ! is_object($record));
172 173
            $this->assertTrue(array_key_exists('id', $record));
            $this->assertTrue(array_key_exists('name', $record));
174
            $this->assertTrue( ! $record instanceof Doctrine_Entity);
175 176 177 178
        } catch(Exception $e) {
            $this->assertTrue(false);
        }

doctrine's avatar
doctrine committed
179 180
        try {
            $record = $this->objTable->find(123);
pookey's avatar
pookey committed
181
            $this->assertTrue($record === false);
182
        } catch(Exception $e) {
pookey's avatar
pookey committed
183
            $this->assertTrue(false);
184 185 186 187
        }

        try {
            $record = $this->objTable->find(null);
pookey's avatar
pookey committed
188
            $this->assertTrue($record === false);
189
        } catch(Exception $e) {
pookey's avatar
pookey committed
190
            $this->assertTrue(false);
191 192 193 194
        }

        try {
            $record = $this->objTable->find(false);
pookey's avatar
pookey committed
195
            $this->assertTrue($record === false);
doctrine's avatar
doctrine committed
196
        } catch(Exception $e) {
pookey's avatar
pookey committed
197
            $this->assertTrue(false);
doctrine's avatar
doctrine committed
198 199
        }
    }
zYne's avatar
zYne committed
200 201 202

    public function testFindAll() 
    {
doctrine's avatar
doctrine committed
203 204
        $users = $this->objTable->findAll();
        $this->assertEqual($users->count(), 8);
205
        $this->assertTrue($users instanceof Doctrine_Collection);
206 207

        $users = $this->objTable->findAll(Doctrine::FETCH_ARRAY);
208
        $this->assertTrue( ! $users instanceof Doctrine_Collection);
209
        $this->assertTrue(is_array($users));
210
        $this->assertTrue( ! is_object($users));
211
        $this->assertEqual(count($users), 8);
doctrine's avatar
doctrine committed
212
    }
zYne's avatar
zYne committed
213 214 215

    public function testFindByDql() 
    {
zYne's avatar
zYne committed
216
        $users = $this->objTable->findByDql("name LIKE '%Arnold%'");
doctrine's avatar
doctrine committed
217
        $this->assertEqual($users->count(), 1);
218
        $this->assertTrue($users instanceof Doctrine_Collection);
doctrine's avatar
doctrine committed
219
    }
zYne's avatar
zYne committed
220 221 222

    public function testGetProxy() 
    {
doctrine's avatar
doctrine committed
223
        $user = $this->objTable->getProxy(4);
224
        $this->assertTrue($user instanceof Doctrine_Entity);
doctrine's avatar
doctrine committed
225 226 227 228 229 230 231

        try {
            $record = $this->objTable->find(123);
        } catch(Exception $e) {
            $this->assertTrue($e instanceOf Doctrine_Find_Exception);
        }
    }
zYne's avatar
zYne committed
232 233 234

    public function testGetColumns() 
    {
235
        $columns = $this->objTable->getTable()->getColumns();
doctrine's avatar
doctrine committed
236 237 238
        $this->assertTrue(is_array($columns));

    }
zYne's avatar
zYne committed
239 240 241

    public function testApplyInheritance() 
    {
242
        $this->assertEqual($this->objTable->applyInheritance("id = 3"), "id = 3 AND type = ?");
doctrine's avatar
doctrine committed
243
    }
244

doctrine's avatar
doctrine committed
245
}