PageRenderTime 84ms CodeModel.GetById 58ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/qeephp/tests/ut/orm/basic.php

http://enaj.googlecode.com/
PHP | 423 lines | 268 code | 59 blank | 96 comment | 6 complexity | 53d1253495f9e6c86c1a9bb8e6bd73a2 MD5 | raw file
  1<?php
  2// $Id: basic.php 2360 2009-04-01 15:35:38Z dualface $
  3
  4require_once dirname(__FILE__) . '/_setup.php';
  5
  6/**
  7 * ??  ORM ?????
  8 */
  9class Test_ORM_Basic extends QTest_Case_Abstract
 10{
 11    /**
 12     * @var QDB_Adapter_Abstract
 13     */
 14    protected $_conn;
 15
 16    function setUp()
 17    {
 18        $this->_conn = QDB::getConn();
 19        $this->_conn->startTrans();
 20    }
 21
 22    function tearDown()
 23    {
 24        $this->_conn->completeTrans(false);
 25    }
 26
 27    /**
 28     * ????
 29     */
 30    function testNew()
 31    {
 32        $content = new Content();
 33        $this->assertType('Content', $content);
 34    }
 35
 36    /**
 37     * ????????????????
 38     */
 39    function testCreate()
 40    {
 41        $count = $this->_queryAuthorsCount();
 42
 43        $time = time();
 44        $author = new Author();
 45        $author->name = 'name - ' . mt_rand();
 46        $author->save();
 47
 48        // ??????????????????
 49        $new_count = $this->_queryAuthorsCount();
 50        $this->assertEquals($new_count, $count + 1);
 51        $this->assertNotNull($author->id());
 52
 53        // ????????????????
 54        $row = $this->_queryAuthor($author->id());
 55        $this->assertTrue(!empty($row));
 56        $this->assertEquals($author->name, $row['name']);
 57        $this->assertGreaterThanOrEqual($time, $row['created']);
 58        $this->assertGreaterThanOrEqual($time, $row['updated']);
 59    }
 60
 61    /**
 62     * ??????????????
 63     */
 64    function testUpdate()
 65    {
 66        $name = 'name - ' . mt_rand();
 67        $author = new Author(array('name' => $name));
 68        $author->save();
 69        $id = $author->id();
 70        $row = $this->_queryAuthor($id);
 71        $count = $this->_queryAuthorsCount();
 72
 73        $new_name = 'name - new - ' . mt_rand();
 74        $author->name = $new_name;
 75        $author->save();
 76        $this->assertEquals($id, $author->id());
 77        $this->assertEquals($count, $this->_queryAuthorsCount());
 78
 79        $new_row = $this->_queryAuthor($id);
 80        $this->assertNotEquals($row['name'], $new_row['name']);
 81        $this->assertEquals($new_name, $new_row['name']);
 82    }
 83
 84    /**
 85     * ???????????
 86     */
 87    function testDestroyOne()
 88    {
 89        $author = new Author(array(
 90            'name' => 'congcong - ' . mt_rand(),
 91        ));
 92        $author->save();
 93        $id = $author->id();
 94
 95        $count = $this->_queryAuthorsCount();
 96        $author->destroy();
 97        $new_count = $this->_queryAuthorsCount();
 98
 99        $row = $this->_queryAuthor($id);
100        $this->assertTrue(empty($row));
101        $this->assertEquals($new_count, $count - 1);
102    }
103
104    /**
105     * ?????????
106     */
107    function testFindOne()
108    {
109        $id_list = $this->_createAuthors(5);
110
111        // ?????????? ????????
112        $id = $this->_getRandID($id_list);
113        $author = Author::find('author_id = ?', $id)->query();
114        $this->_checkAuthor($this->_queryAuthor($id), $author);
115
116        // ?????????:id ????????
117        $id = $this->_getRandID($id_list);
118        $author = Author::find('author_id = :id', array('id' => $id))->query();
119        $this->_checkAuthor($this->_queryAuthor($id), $author);
120
121        // ???????
122        $id = $this->_getRandID($id_list);
123        $author = Author::find(array('author_id' => $id))->query();
124        $this->_checkAuthor($this->_queryAuthor($id), $author);
125
126        // ? QDB_Expr ??????
127        $id = $this->_getRandID($id_list);
128        $author = Author::find(new QDB_Expr('author_id = ' . $id))->query();
129        $this->_checkAuthor($this->_queryAuthor($id), $author);
130
131        // ? QDB_Cond ???????? ????????
132        $id = $this->_getRandID($id_list);
133        $author = Author::find(new QDB_Cond('author_id = ?', $id))->query();
134        $this->_checkAuthor($this->_queryAuthor($id), $author);
135
136        // ? QDB_Cond ???????:id ????????
137        $id = $this->_getRandID($id_list);
138        $author = Author::find(new QDB_Cond('author_id = :id', array('id' => $id)))->query();
139        $this->_checkAuthor($this->_queryAuthor($id), $author);
140
141        // ? QDB_Cond ????????????
142        $id = $this->_getRandID($id_list);
143        $author = Author::find(new QDB_Cond(array('author_id' => $id)))->query();
144        $this->_checkAuthor($this->_queryAuthor($id), $author);
145    }
146
147    /**
148     * ??????
149     */
150    function testFindMore()
151    {
152        $id_list = $this->_createAuthors(15);
153
154        // ????
155        $authors = Author::find()->all()->query();
156        $this->assertGreaterThanOrEqual(15, count($authors));
157        $this->_checkAuthors($authors);
158        unset($authors);
159
160        // ??ID ????????
161        $authors = Author::find('author_id >= ?', $id_list[5])->all()->query();
162        $this->assertEquals(10, count($authors));
163        $this->_checkAuthors($authors);
164        unset($authors);
165    }
166
167    /**
168     * ????????
169     */
170    function testAssociationAccessWithNewObject()
171    {
172        $author = new Author();
173        $this->assertType('QDB_ActiveRecord_Association_Coll', $author->contents);
174        $this->assertEquals(0, count($author->contents));
175        $this->assertType('QDB_ActiveRecord_Association_Coll', $author->comments);
176        $this->assertEquals(0, count($author->comments));
177
178        $content = new Content();
179        $this->assertType('QDB_ActiveRecord_Association_Coll', $content->tags);
180        $this->assertEquals(0, count($content->tags));
181    }
182
183    /**
184     * ???????? has_many ?????
185     */
186    function testCreateWithHasMany()
187    {
188        $author = new Author(array('name' => 'name - ' . mt_rand()));
189        for ($i = 0; $i < 5; $i++) {
190	        $author->contents[] = new Content(array(
191	            'title' => 'title - ' . mt_rand(),
192	        ));
193        }
194        $author->save();
195
196        $this->assertNotNull($author->id());
197        $this->_checkAuthor($this->_queryAuthor($author->id()), $author);
198        $this->_checkContents($author->contents);
199    }
200
201    /**
202     * ???????? has_one ?????
203     */
204    function testCreateWithHasOne()
205    {
206        $author = new Author(array('name' => 'name - ' . mt_rand()));
207        $author->profile = new Profile(array('address' => 'ZiGong', 'postcode' => 643000));
208        $author->save();
209
210        $this->assertNotNull($author->id());
211        $this->assertNotNull($author->profile->id());
212        $this->assertEquals($author->id(), $author->profile->id());
213        $this->_checkAuthor($this->_queryAuthor($author->id()), $author);
214        $this->_checkProfile($this->_queryProfile($author->profile->id()), $author->profile);
215    }
216
217    /**
218     * ???????? many_to_many ??
219     */
220    function testCreateWithManyToMany()
221    {
222        $tags = array('PHP', 'C++', 'Java');
223
224        $content = new Content(array(
225            'title' => 'title - ' . mt_rand(),
226            'author_id' => 0,
227        ));
228        foreach ($tags as $tag_name) {
229            $content->tags[] = new Tag(array('name' => $tag_name));
230        }
231        $content->save();
232
233        $this->assertNotNull($content->id());
234        $row = $this->_queryContent($content->id());
235        $this->_checkContent($row, $content);
236
237        foreach ($tags as $offset => $tag_name) {
238            $row = $this->_queryTag($tag_name);
239            $this->assertType('array', $row);
240            $this->_checkTag($row, $content->tags[$offset]);
241        }
242
243        $mid_table_name = $content->getMeta()->associations['tags']->mid_table->qtable_name;
244        $sql = "SELECT * FROM {$mid_table_name} WHERE content_id = ?";
245        $rowset = $this->_conn->getAll($sql, array($content->id()));
246
247        $this->assertEquals(count($tags), count($rowset));
248
249        $tags = array_flip($tags);
250        foreach ($rowset as $row) {
251            $this->assertTrue(isset($tags[$row['tag_name']]));
252            unset($tags[$row['tag_name']]);
253        }
254    }
255
256    /**
257     * ???? Author ??
258     */
259    protected function _checkAuthors($authors)
260    {
261        if (!is_array($authors) && !($authors instanceof Iterator)) {
262            $this->fail('$authors must be Array or Iterator.');
263        }
264
265        foreach ($authors as $author) {
266            $this->assertType('Author', $author);
267            $this->assertNotNull($author->id());
268            $this->_checkAuthor($this->_queryAuthor($author->id()), $author);
269        }
270    }
271
272    /**
273     * ???? Content ??
274     */
275    protected function _checkContents($contents)
276    {
277        if (!is_array($contents) && !($contents instanceof Iterator)) {
278            $this->fail('$contents must be Array or Iterator.');
279        }
280
281        foreach ($contents as $content) {
282            $this->assertType('Content', $content);
283            $this->assertNotNull($content->id());
284            $this->_checkContent($this->_queryContent($content->id()), $content);
285        }
286    }
287
288    /**
289     * ?? Author ????????????
290     */
291    protected function _checkAuthor(array $row, $author)
292    {
293        $this->assertType('Author', $author);
294        $this->assertEquals($row['author_id'], $author->id());
295        $this->assertEquals($row['name'], $author->name);
296    }
297
298    /**
299     * ?? Tag ????????????
300     */
301    protected function _checkTag(array $row, $tag)
302    {
303        $this->assertType('Tag', $tag);
304        $this->assertEquals($row['name'], $tag->id());
305    }
306
307    /**
308     * ?? Profile ????????????
309     */
310    protected function _checkProfile(array $row, $profile)
311    {
312        $this->assertType('Profile', $profile);
313        $this->assertEquals($row['author_id'], $profile->id());
314        $this->assertEquals($row['address'], $profile->address);
315        $this->assertEquals($row['postcode'], $profile->postcode);
316    }
317
318    /**
319     * ?? Content ????????????
320     */
321    protected function _checkContent(array $row, $content)
322    {
323        $this->assertType('Content', $content);
324        $this->assertEquals($row['content_id'], $content->id());
325        $this->assertEquals($row['title'], $content->title);
326    }
327
328    /**
329     * ????????????????
330     */
331    protected function _getRandID(array $id_list)
332    {
333        $count = count($id_list);
334        $pos = mt_rand(0, $count - 1);
335        return $id_list[$pos];
336    }
337
338    /**
339     * ??????? Author ??????????? ID
340     */
341    protected function _createAuthors($count)
342    {
343        $return = array();
344        for ($i = 0; $i < $count; $i++) {
345            $author = new Author(array(
346                'name' => 'new author - ' . mt_rand(),
347            ));
348            $author->save();
349            $return[] = $author->id();
350        }
351        return $return;
352    }
353
354    /**
355     * ?? Author ??????
356     */
357    protected function _queryAuthorsCount()
358    {
359        return $this->_queryCount('authors');
360    }
361
362    /**
363     * ?? Content ??????
364     */
365    protected function _queryContentsCount()
366    {
367        return $this->_queryCount('contents');
368    }
369
370    /**
371     * ????????????
372     */
373    protected function _queryCount($table_name)
374    {
375        $prefix = $this->_conn->getTablePrefix();
376        $sql = "SELECT COUNT(*) FROM {$prefix}{$table_name}";
377        return intval($this->_conn->getOne($sql));
378    }
379
380    /**
381     * ???? id ? Tag ????
382     */
383    protected function _queryTag($id)
384    {
385        return $this->_queryRow('tags', 'name', $id);
386    }
387
388    /**
389     * ???? id ? Profile ????
390     */
391    protected function _queryProfile($id)
392    {
393        return $this->_queryRow('profiles', 'author_id', $id);
394    }
395
396    /**
397     * ???? id ? Author ????
398     */
399    protected function _queryAuthor($id)
400    {
401        return $this->_queryRow('authors', 'author_id', $id);
402    }
403
404    /**
405     * ???? id ? Content ????
406     */
407    protected function _queryContent($id)
408    {
409        return $this->_queryRow('contents', 'content_id', $id);
410    }
411
412    /**
413     * ???????????????
414     */
415    protected function _queryRow($table_name, $idname, $id)
416    {
417        $prefix = $this->_conn->getTablePrefix();
418        $sql = "SELECT * FROM {$prefix}{$table_name} WHERE {$idname} = ?";
419        return $this->_conn->getRow($sql, array($id));
420    }
421
422}
423