1<?php
2/*
3 *  $Id$
4 *
5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 *
17 * This software consists of voluntary contributions made by many individuals
18 * and is licensed under the LGPL. For more information, see
19 * <http://www.doctrine-project.org>.
20 */
21
22/**
23 * Doctrine_Table_TestCase
24 *
25 * @package     Doctrine
26 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
27 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28 * @category    Object Relational Mapping
29 * @link        www.doctrine-project.org
30 * @since       1.0
31 * @version     $Revision$
32 */
33class Doctrine_Table_TestCase extends Doctrine_UnitTestCase
34{
35
36    public function prepareTables()
37    {
38        $this->tables[] = 'FieldNameTest';
39        parent::prepareTables();
40    }
41
42    public function testInitializingNewTableWorksWithoutConnection()
43    {
44        $table = new Doctrine_Table('Test', $this->conn);
45
46        $this->assertEqual($table->getComponentName(), 'Test');
47    }
48
49    public function testSerialize()
50    {
51        $table = new Doctrine_Table('FieldNameTest', $this->conn);
52        $serializedTable = serialize($table);
53
54        $unserializedTable = unserialize($serializedTable);
55        $unserializedTable->initializeFromCache($this->objTable->getConnection());
56
57        $this->assertEqual($table->getColumns(), $unserializedTable->getColumns());
58    }
59
60    public function testFieldConversion()
61    {
62        $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);
63
64        $t = new FieldNameTest();
65
66        $t->someColumn = 'abc';
67        $t->someEnum = 'php';
68        $t->someInt = 1;
69        $t->someArray = array();
70        $obj = new StdClass();
71        $t->someObject = $obj;
72
73        $this->assertEqual($t->someColumn, 'abc');
74        $this->assertEqual($t->someEnum, 'php');
75        $this->assertEqual($t->someInt, 1);
76        $this->assertEqual($t->someArray, array());
77        $this->assertEqual($t->someObject, $obj);
78
79        $t->save();
80
81        $this->assertEqual($t->someColumn, 'abc');
82        $this->assertEqual($t->someEnum, 'php');
83        $this->assertEqual($t->someInt, 1);
84        $this->assertEqual($t->someArray, array());
85        $this->assertEqual($t->someObject, $obj);
86
87        $t->refresh();
88
89        $this->assertEqual($t->someColumn, 'abc');
90        $this->assertEqual($t->someEnum, 'php');
91        $this->assertEqual($t->someInt, 1);
92        $this->assertEqual($t->someArray, array());
93        $this->assertEqual($t->someObject, $obj);
94
95        $this->connection->clear();
96
97        $t = $this->connection->getTable('FieldNameTest')->find(1);
98
99        $this->assertEqual($t->someColumn, 'abc');
100        $this->assertEqual($t->someEnum, 'php');
101        $this->assertEqual($t->someInt, 1);
102        $this->assertEqual($t->someArray, array());
103        $this->assertEqual($t->someObject, $obj);
104
105        $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
106    }
107
108    public function testGetForeignKey()
109    {
110        $fk = $this->objTable->getRelation("Group");
111        $this->assertTrue($fk instanceof Doctrine_Relation_Association);
112        $this->assertTrue($fk->getTable() instanceof Doctrine_Table);
113        $this->assertTrue($fk->getType() == Doctrine_Relation::MANY);
114        $this->assertTrue($fk->getLocal() == "user_id");
115        $this->assertTrue($fk->getForeign() == "group_id");
116
117        $fk = $this->objTable->getRelation("Email");
118        $this->assertTrue($fk instanceof Doctrine_Relation_LocalKey);
119        $this->assertTrue($fk->getTable() instanceof Doctrine_Table);
120        $this->assertTrue($fk->getType() == Doctrine_Relation::ONE);
121        $this->assertTrue($fk->getLocal() == "email_id");
122        $this->assertTrue($fk->getForeign() == $fk->getTable()->getIdentifier());
123
124
125        $fk = $this->objTable->getRelation('Phonenumber');
126        $this->assertTrue($fk instanceof Doctrine_Relation_ForeignKey);
127        $this->assertTrue($fk->getTable() instanceof Doctrine_Table);
128        $this->assertTrue($fk->getType() == Doctrine_Relation::MANY);
129        $this->assertTrue($fk->getLocal() == $this->objTable->getIdentifier());
130        $this->assertTrue($fk->getForeign() == 'entity_id');
131
132
133    }
134    public function testGetComponentName()
135    {
136        $this->assertTrue($this->objTable->getComponentName() == 'User');
137    }
138
139    public function testGetTableName()
140    {
141        $this->assertTrue($this->objTable->tableName == 'entity');
142    }
143
144    public function testGetConnection()
145    {
146        $this->assertTrue($this->objTable->getConnection() instanceof Doctrine_Connection);
147    }
148
149    public function testGetData()
150    {
151        $this->assertTrue($this->objTable->getData() == array());
152    }
153
154    public function testSetSequenceName()
155    {
156        $this->objTable->sequenceName = 'test-seq';
157        $this->assertEqual($this->objTable->sequenceName, 'test-seq');
158        $this->objTable->sequenceName = null;
159    }
160
161    public function testCreate()
162    {
163        $record = $this->objTable->create();
164        $this->assertTrue($record instanceof Doctrine_Record);
165        $this->assertTrue($record->state() == Doctrine_Record::STATE_TCLEAN);
166    }
167
168    public function testFind()
169    {
170        $record = $this->objTable->find(4);
171        $this->assertTrue($record instanceof Doctrine_Record);
172
173        try {
174            $record = $this->objTable->find('4');
175            $this->assertTrue($record instanceof Doctrine_Record);
176        } catch(Exception $e) {
177            $this->assertTrue(false);
178        }
179
180        try {
181            $record = $this->objTable->find('4', Doctrine_Core::HYDRATE_ARRAY);
182            $this->assertTrue(is_array($record));
183            $this->assertTrue( ! is_object($record));
184            $this->assertTrue(array_key_exists('id', $record));
185            $this->assertTrue(array_key_exists('name', $record));
186            $this->assertTrue( ! $record instanceof Doctrine_Record);
187        } catch(Exception $e) {
188            $this->assertTrue(false);
189        }
190
191        try {
192            $record = $this->objTable->find(123);
193            $this->assertTrue($record === false);
194        } catch(Exception $e) {
195            $this->assertTrue(false);
196        }
197
198        try {
199            $record = $this->objTable->find(null);
200            $this->assertTrue($record === false);
201        } catch(Exception $e) {
202            $this->assertTrue(false);
203        }
204
205        try {
206            $record = $this->objTable->find(false);
207            $this->assertTrue($record === false);
208        } catch(Exception $e) {
209            $this->assertTrue(false);
210        }
211    }
212
213    public function testFindAll()
214    {
215        $users = $this->objTable->findAll();
216        $this->assertEqual($users->count(), 8);
217        $this->assertTrue($users instanceof Doctrine_Collection);
218
219        $users = $this->objTable->findAll(Doctrine_Core::HYDRATE_ARRAY);
220        $this->assertTrue( ! $users instanceof Doctrine_Collection);
221        $this->assertTrue(is_array($users));
222        $this->assertTrue( ! is_object($users));
223        $this->assertEqual(count($users), 8);
224    }
225
226    public function testFindByDql()
227    {
228        $users = $this->objTable->findByDql("name LIKE '%Arnold%'");
229        $this->assertEqual($users->count(), 1);
230        $this->assertTrue($users instanceof Doctrine_Collection);
231    }
232
233    public function testFindByXXX()
234    {
235        $users = $this->objTable->findByName('zYne');
236        $this->assertEqual($users->count(), 1);
237        $this->assertTrue($users instanceof Doctrine_Collection);
238    }
239
240    public function testGetProxy()
241    {
242        $user = $this->objTable->getProxy(4);
243        $this->assertTrue($user instanceof Doctrine_Record);
244
245        try {
246            $record = $this->objTable->find(123);
247        } catch(Exception $e) {
248            $this->assertTrue($e instanceOf Doctrine_Find_Exception);
249        }
250    }
251
252    public function testGetColumns()
253    {
254        $columns = $this->objTable->getColumns();
255        $this->assertTrue(is_array($columns));
256
257    }
258
259    public function testApplyInheritance()
260    {
261        $this->assertEqual($this->objTable->applyInheritance("id = 3"), "id = 3 AND type = ?");
262    }
263}
264