/tests/library/Respect/Relational/MapperTest.php

https://github.com/luizfonseca/Relational · PHP · 355 lines · 320 code · 35 blank · 0 comment · 0 complexity · 163dd4d08f5fd4b2d5753ecf714d538e MD5 · raw file

  1. <?php
  2. namespace Respect\Relational;
  3. use PDO;
  4. class MapperTest extends \PHPUnit_Framework_TestCase {
  5. protected $mapper, $posts, $authors, $comments, $categories, $postsCategories;
  6. public function setUp() {
  7. $conn = new PDO('sqlite::memory:');
  8. $db = new Db($conn);
  9. $conn->exec((string) Sql::createTable('post', array(
  10. 'id INTEGER PRIMARY KEY',
  11. 'title VARCHAR(255)',
  12. 'text TEXT',
  13. 'author_id INTEGER'
  14. )));
  15. $conn->exec((string) Sql::createTable('author', array(
  16. 'id INTEGER PRIMARY KEY',
  17. 'name VARCHAR(255)'
  18. )));
  19. $conn->exec((string) Sql::createTable('comment', array(
  20. 'id INTEGER PRIMARY KEY',
  21. 'post_id INTEGER',
  22. 'text TEXT',
  23. )));
  24. $conn->exec((string) Sql::createTable('category', array(
  25. 'id INTEGER PRIMARY KEY',
  26. 'name VARCHAR(255)',
  27. 'category_id INTEGER'
  28. )));
  29. $conn->exec((string) Sql::createTable('post_category', array(
  30. 'id INTEGER PRIMARY KEY',
  31. 'post_id INTEGER',
  32. 'category_id INTEGER'
  33. )));
  34. $this->posts = array(
  35. (object) array(
  36. 'id' => 5,
  37. 'title' => 'Post Title',
  38. 'text' => 'Post Text',
  39. 'author_id' => 1
  40. )
  41. );
  42. $this->authors = array(
  43. (object) array(
  44. 'id' => 1,
  45. 'name' => 'Author 1'
  46. )
  47. );
  48. $this->comments = array(
  49. (object) array(
  50. 'id' => 7,
  51. 'post_id' => 5,
  52. 'text' => 'Comment Text'
  53. ),
  54. (object) array(
  55. 'id' => 8,
  56. 'post_id' => 4,
  57. 'text' => 'Comment Text 2'
  58. )
  59. );
  60. $this->categories = array(
  61. (object) array(
  62. 'id' => 2,
  63. 'name' => 'Sample Category',
  64. 'category_id' => null
  65. ),
  66. (object) array(
  67. 'id' => 3,
  68. 'name' => 'NONON',
  69. 'category_id' => null
  70. )
  71. );
  72. $this->postsCategories = array(
  73. (object) array(
  74. 'id' => 66,
  75. 'post_id' => 5,
  76. 'category_id' => 2
  77. )
  78. );
  79. foreach ($this->authors as $author)
  80. $db->insertInto('author', (array) $author)->values((array) $author)->exec();
  81. foreach ($this->posts as $post)
  82. $db->insertInto('post', (array) $post)->values((array) $post)->exec();
  83. foreach ($this->comments as $comment)
  84. $db->insertInto('comment', (array) $comment)->values((array) $comment)->exec();
  85. foreach ($this->categories as $category)
  86. $db->insertInto('category', (array) $category)->values((array) $category)->exec();
  87. foreach ($this->postsCategories as $postCategory)
  88. $db->insertInto('post_category', (array) $postCategory)->values((array) $postCategory)->exec();
  89. $mapper = new Mapper($conn);
  90. $this->mapper = $mapper;
  91. $this->conn = $conn;
  92. }
  93. public function test_fetching_single_entity_from_collection_should_return_first_record_from_table()
  94. {
  95. $expectedFirstComment = reset($this->comments);
  96. $fetchedFirstComment = $this->mapper->comment->fetch();
  97. $this->assertEquals($expectedFirstComment, $fetchedFirstComment);
  98. }
  99. public function test_fetching_all_entites_from_collection_should_return_all_records()
  100. {
  101. $expectedCategories = $this->categories;
  102. $fetchedCategories = $this->mapper->category->fetchAll();
  103. $this->assertEquals($expectedCategories, $fetchedCategories);
  104. }
  105. public function test_extra_sql_on_single_fetch_should_be_applied_on_mapper_sql()
  106. {
  107. $expectedLast = end($this->comments);
  108. $fetchedLast = $this->mapper->comment->fetch(Sql::orderBy('id DESC'));
  109. $this->assertEquals($expectedLast, $fetchedLast);
  110. }
  111. public function test_extra_sql_on_fetchAll_should_be_applied_on_mapper_sql()
  112. {
  113. $expectedComments = array_reverse($this->comments);
  114. $fetchedComments = $this->mapper->comment->fetchAll(Sql::orderBy('id DESC'));
  115. $this->assertEquals($expectedComments, $fetchedComments);
  116. }
  117. public function test_nested_collections_should_hydrate_results() {
  118. $mapper = $this->mapper;
  119. $comment = $mapper->comment->post[5]->fetch();
  120. $this->assertEquals(7, $comment->id);
  121. $this->assertEquals('Comment Text', $comment->text);
  122. $this->assertEquals(3, count(get_object_vars($comment)));
  123. $this->assertEquals(5, $comment->post_id->id);
  124. $this->assertEquals('Post Title', $comment->post_id->title);
  125. $this->assertEquals('Post Text', $comment->post_id->text);
  126. $this->assertEquals(4, count(get_object_vars($comment->post_id)));
  127. }
  128. public function testOneToN() {
  129. $mapper = $this->mapper;
  130. $comments = $mapper->comment->post($mapper->author)->fetchAll();
  131. $comment = current($comments);
  132. $this->assertEquals(1, count($comments));
  133. $this->assertEquals(7, $comment->id);
  134. $this->assertEquals('Comment Text', $comment->text);
  135. $this->assertEquals(3, count(get_object_vars($comment)));
  136. $this->assertEquals(5, $comment->post_id->id);
  137. $this->assertEquals('Post Title', $comment->post_id->title);
  138. $this->assertEquals('Post Text', $comment->post_id->text);
  139. $this->assertEquals(4, count(get_object_vars($comment->post_id)));
  140. $this->assertEquals(1, $comment->post_id->author_id->id);
  141. $this->assertEquals('Author 1', $comment->post_id->author_id->name);
  142. $this->assertEquals(2, count(get_object_vars($comment->post_id->author_id)));
  143. }
  144. public function testNtoN() {
  145. $mapper = $this->mapper;
  146. $comments = $mapper->comment->post->post_category->category[2]->fetchAll();
  147. $comment = current($comments);
  148. $this->assertEquals(1, count($comments));
  149. $this->assertEquals(7, $comment->id);
  150. $this->assertEquals('Comment Text', $comment->text);
  151. $this->assertEquals(3, count(get_object_vars($comment)));
  152. $this->assertEquals(5, $comment->post_id->id);
  153. $this->assertEquals('Post Title', $comment->post_id->title);
  154. $this->assertEquals('Post Text', $comment->post_id->text);
  155. $this->assertEquals(4, count(get_object_vars($comment->post_id)));
  156. }
  157. public function testTracking() {
  158. $mapper = $this->mapper;
  159. $c7 = $mapper->comment[7]->fetch();
  160. $c8 = $mapper->comment[8]->fetch();
  161. $p5 = $mapper->post[5]->fetch();
  162. $c3 = $mapper->category[2]->fetch();
  163. $this->assertTrue($mapper->isTracked($c7));
  164. $this->assertTrue($mapper->isTracked($c8));
  165. $this->assertTrue($mapper->isTracked($p5));
  166. $this->assertTrue($mapper->isTracked($c3));
  167. $this->assertSame($c7, $mapper->getTracked('comment', 7));
  168. $this->assertSame($c8, $mapper->getTracked('comment', 8));
  169. $this->assertSame($p5, $mapper->getTracked('post', 5));
  170. $this->assertSame($c3, $mapper->getTracked('category', 2));
  171. $this->assertFalse($mapper->getTracked('none', 3));
  172. $this->assertFalse($mapper->getTracked('comment', 9889));
  173. }
  174. public function testSimplePersist() {
  175. $mapper = $this->mapper;
  176. $entity = (object) array('id' => 4, 'name' => 'inserted', 'category_id' => null);
  177. $mapper->persist(
  178. $entity, 'category'
  179. );
  180. $mapper->flush();
  181. $result = $this->conn->query('select * from category where id=4')->fetch(PDO::FETCH_OBJ);
  182. $this->assertEquals($entity, $result);
  183. }
  184. public function testSimplePersistCollection() {
  185. $mapper = $this->mapper;
  186. $entity = (object) array('id' => 4, 'name' => 'inserted', 'category_id' => null);
  187. $mapper->category->persist($entity);
  188. $mapper->flush();
  189. $result = $this->conn->query('select * from category where id=4')->fetch(PDO::FETCH_OBJ);
  190. $this->assertEquals($entity, $result);
  191. }
  192. public function testNestedPersistCollection() {
  193. $postWithAuthor = (object) array(
  194. 'id' => null,
  195. 'title' => 'hi',
  196. 'text' => 'hi text',
  197. 'author_id' => (object) array(
  198. 'id' => null,
  199. 'name' => 'New'
  200. )
  201. );
  202. $this->mapper->post->author->persist($postWithAuthor);
  203. $this->mapper->flush();
  204. $author = $this->conn->query('select * from author order by id desc limit 1')->fetch(PDO::FETCH_OBJ);
  205. $post = $this->conn->query('select * from post order by id desc limit 1')->fetch(PDO::FETCH_OBJ);
  206. $this->assertEquals('New', $author->name);
  207. $this->assertEquals('hi', $post->title);
  208. }
  209. public function testNestedPersistCollectionShortcut() {
  210. $postWithAuthor = (object) array(
  211. 'id' => null,
  212. 'title' => 'hi',
  213. 'text' => 'hi text',
  214. 'author_id' => (object) array(
  215. 'id' => null,
  216. 'name' => 'New'
  217. )
  218. );
  219. $this->mapper->postAuthor = $this->mapper->post->author;
  220. $this->mapper->postAuthor->persist($postWithAuthor);
  221. $this->mapper->flush();
  222. $author = $this->conn->query('select * from author order by id desc limit 1')->fetch(PDO::FETCH_OBJ);
  223. $post = $this->conn->query('select * from post order by id desc limit 1')->fetch(PDO::FETCH_OBJ);
  224. $this->assertEquals('New', $author->name);
  225. $this->assertEquals('hi', $post->title);
  226. }
  227. public function testNestedPersistCollectionWithChildrenShortcut() {
  228. $postWithAuthor = (object) array(
  229. 'id' => null,
  230. 'title' => 'hi',
  231. 'text' => 'hi text',
  232. 'author_id' => (object) array(
  233. 'id' => null,
  234. 'name' => 'New'
  235. )
  236. );
  237. $this->mapper->postAuthor = $this->mapper->post($this->mapper->author);
  238. $this->mapper->postAuthor->persist($postWithAuthor);
  239. $this->mapper->flush();
  240. $author = $this->conn->query('select * from author order by id desc limit 1')->fetch(PDO::FETCH_OBJ);
  241. $post = $this->conn->query('select * from post order by id desc limit 1')->fetch(PDO::FETCH_OBJ);
  242. $this->assertEquals('New', $author->name);
  243. $this->assertEquals('hi', $post->title);
  244. }
  245. public function testSubCategory() {
  246. $mapper = $this->mapper;
  247. $entity = (object) array('id' => 8, 'name' => 'inserted', 'category_id' => 2);
  248. $mapper->persist(
  249. $entity, 'category'
  250. );
  251. $mapper->flush();
  252. $result = $this->conn->query('select * from category where id=8')->fetch(PDO::FETCH_OBJ);
  253. $result2 = $mapper->category[8]->category->fetch();
  254. $this->assertEquals($result->id, $result2->id);
  255. $this->assertEquals($result->name, $result2->name);
  256. $this->assertEquals($entity, $result);
  257. }
  258. public function testSubCategoryCondition() {
  259. $mapper = $this->mapper;
  260. $entity = (object) array('id' => 8, 'name' => 'inserted', 'category_id' => 2);
  261. $mapper->persist(
  262. $entity, 'category'
  263. );
  264. $mapper->flush();
  265. $result = $this->conn->query('select * from category where id=8')->fetch(PDO::FETCH_OBJ);
  266. $result2 = $mapper->category(array("id"=>8))->category->fetch();
  267. $this->assertEquals($result->id, $result2->id);
  268. $this->assertEquals($result->name, $result2->name);
  269. $this->assertEquals($entity, $result);
  270. }
  271. public function testAutoIncrementPersist() {
  272. $mapper = $this->mapper;
  273. $entity = (object) array('id' => null, 'name' => 'inserted', 'category_id' => null);
  274. $mapper->persist(
  275. $entity, 'category'
  276. );
  277. $mapper->flush();
  278. $result = $this->conn->query('select * from category where name="inserted"')->fetch(PDO::FETCH_OBJ);
  279. $this->assertEquals($entity, $result);
  280. $this->assertEquals(4, $result->id);
  281. }
  282. public function testPassedIdentity() {
  283. $mapper = $this->mapper;
  284. $post = new \stdClass;
  285. $post->id = null;
  286. $post->title = 12345;
  287. $post->text = 'text abc';
  288. $comment = new \stdClass;
  289. $comment->id = null;
  290. $comment->post_id = $post;
  291. $comment->text = 'abc';
  292. $mapper->persist($post, 'post');
  293. $mapper->persist($comment, 'comment');
  294. $mapper->flush();
  295. $postId = $this->conn
  296. ->query('select id from post where title = 12345')
  297. ->fetchColumn(0);
  298. $comment = $this->conn->query('select * from comment where post_id = ' . $postId)
  299. ->fetchObject();
  300. $this->assertEquals('abc', $comment->text);
  301. }
  302. public function testJoinedPersist() {
  303. $mapper = $this->mapper;
  304. $entity = $mapper->comment[8]->fetch();
  305. $entity->text = 'HeyHey';
  306. $mapper->persist($entity, 'comment');
  307. $mapper->flush();
  308. $result = $this->conn->query('select text from comment where id=8')->fetchColumn(0);
  309. $this->assertEquals('HeyHey', $result);
  310. }
  311. public function testRemove() {
  312. $mapper = $this->mapper;
  313. $c8 = $mapper->comment[8]->fetch();
  314. $pre = $this->conn->query('select count(*) from comment')->fetchColumn(0);
  315. $mapper->remove($c8, "comment");
  316. $mapper->flush();
  317. $total = $this->conn->query('select count(*) from comment')->fetchColumn(0);
  318. $this->assertEquals($total, $pre - 1);
  319. }
  320. }