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