/tests/Propel/Tests/Runtime/ActiveQuery/ModelWithTest.php

http://github.com/propelorm/Propel2 · PHP · 265 lines · 202 code · 26 blank · 37 comment · 0 complexity · 25098b5a531b29d35d472568df556eeb MD5 · raw file

  1. <?php
  2. /**
  3. * MIT License. This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. */
  7. namespace Propel\Tests\Runtime\ActiveQuery;
  8. use Propel\Runtime\ActiveQuery\ModelWith;
  9. use Propel\Tests\Bookstore\AuthorQuery;
  10. use Propel\Tests\Bookstore\BookQuery;
  11. use Propel\Tests\Bookstore\BookReaderQuery;
  12. use Propel\Tests\Bookstore\BookstoreEmployeeQuery;
  13. use Propel\Tests\Bookstore\BookSummaryQuery;
  14. use Propel\Tests\Bookstore\ReviewQuery;
  15. use Propel\Tests\TestCaseFixtures;
  16. /**
  17. * Test class for ModelWith.
  18. *
  19. * @author François Zaninotto
  20. */
  21. class ModelWithTest extends TestCaseFixtures
  22. {
  23. /**
  24. * @return void
  25. */
  26. public function testModelNameManyToOne()
  27. {
  28. $q = BookQuery::create()
  29. ->joinAuthor();
  30. $joins = $q->getJoins();
  31. $join = $joins['Author'];
  32. $with = new ModelWith($join);
  33. $this->assertEquals('Propel\Tests\Bookstore\Author', $with->getModelName(), 'A ModelWith computes the model name from the join');
  34. }
  35. /**
  36. * @return void
  37. */
  38. public function testModelNameOneToMany()
  39. {
  40. $q = AuthorQuery::create()
  41. ->joinBook();
  42. $joins = $q->getJoins();
  43. $join = $joins['Book'];
  44. $with = new ModelWith($join);
  45. $this->assertEquals('Propel\Tests\Bookstore\Book', $with->getModelName(), 'A ModelWith computes the model name from the join');
  46. }
  47. /**
  48. * @return void
  49. */
  50. public function testModelNameAlias()
  51. {
  52. $q = BookQuery::create()
  53. ->joinAuthor('a');
  54. $joins = $q->getJoins();
  55. $join = $joins['a'];
  56. $with = new ModelWith($join);
  57. $this->assertEquals('Propel\Tests\Bookstore\Author', $with->getModelName(), 'A ModelWith computes the model name from the join');
  58. }
  59. /**
  60. * @return void
  61. */
  62. public function testRelationManyToOne()
  63. {
  64. $q = BookQuery::create()
  65. ->joinAuthor();
  66. $joins = $q->getJoins();
  67. $join = $joins['Author'];
  68. $with = new ModelWith($join);
  69. $this->assertEquals($with->getRelationMethod(), 'setAuthor', 'A ModelWith computes the relation method from the join');
  70. $this->assertEquals($with->getRelationName(), 'Author', 'A ModelWith computes the relation name from the join');
  71. $this->assertFalse($with->isAdd(), 'A ModelWith computes the relation cardinality from the join');
  72. }
  73. /**
  74. * @return void
  75. */
  76. public function testRelationOneToMany()
  77. {
  78. $q = AuthorQuery::create()
  79. ->joinBook();
  80. $joins = $q->getJoins();
  81. $join = $joins['Book'];
  82. $with = new ModelWith($join);
  83. $this->assertEquals($with->getRelationMethod(), 'addBook', 'A ModelWith computes the relation method from the join');
  84. $this->assertEquals($with->getRelationName(), 'Books', 'A ModelWith computes the relation name from the join');
  85. $this->assertTrue($with->isAdd(), 'A ModelWith computes the relation cardinality from the join');
  86. }
  87. /**
  88. * @return void
  89. */
  90. public function testRelationOneToOne()
  91. {
  92. $q = BookstoreEmployeeQuery::create()
  93. ->joinBookstoreEmployeeAccount();
  94. $joins = $q->getJoins();
  95. $join = $joins['BookstoreEmployeeAccount'];
  96. $with = new ModelWith($join);
  97. $this->assertEquals($with->getRelationMethod(), 'setBookstoreEmployeeAccount', 'A ModelWith computes the relation method from the join');
  98. $this->assertEquals($with->getRelationName(), 'BookstoreEmployeeAccount', 'A ModelWith computes the relation name from the join');
  99. $this->assertFalse($with->isAdd(), 'A ModelWith computes the relation cardinality from the join');
  100. }
  101. /**
  102. * @return void
  103. */
  104. public function testIsPrimary()
  105. {
  106. $q = AuthorQuery::create()
  107. ->joinBook();
  108. $joins = $q->getJoins();
  109. $join = $joins['Book'];
  110. $with = new ModelWith($join);
  111. $this->assertTrue($with->isPrimary(), 'A ModelWith initialized from a primary join is primary');
  112. $q = BookQuery::create()
  113. ->joinAuthor()
  114. ->joinReview();
  115. $joins = $q->getJoins();
  116. $join = $joins['Review'];
  117. $with = new ModelWith($join);
  118. $this->assertTrue($with->isPrimary(), 'A ModelWith initialized from a primary join is primary');
  119. $q = AuthorQuery::create()
  120. ->join('Propel\Tests\Bookstore\Author.Book')
  121. ->join('Book.Publisher');
  122. $joins = $q->getJoins();
  123. $join = $joins['Publisher'];
  124. $with = new ModelWith($join);
  125. $this->assertFalse($with->isPrimary(), 'A ModelWith initialized from a non-primary join is not primary');
  126. }
  127. /**
  128. * @return void
  129. */
  130. public function testGetLeftPhpName()
  131. {
  132. $q = AuthorQuery::create()
  133. ->joinBook();
  134. $joins = $q->getJoins();
  135. $join = $joins['Book'];
  136. $with = new ModelWith($join);
  137. $this->assertNull($with->getLeftPhpName(), 'A ModelWith initialized from a primary join has a null left phpName');
  138. $q = AuthorQuery::create('a')
  139. ->joinBook();
  140. $joins = $q->getJoins();
  141. $join = $joins['Book'];
  142. $with = new ModelWith($join);
  143. $this->assertNull($with->getLeftPhpName(), 'A ModelWith initialized from a primary join with alias has a null left phpName');
  144. $q = AuthorQuery::create()
  145. ->joinBook('b');
  146. $joins = $q->getJoins();
  147. $join = $joins['b'];
  148. $with = new ModelWith($join);
  149. $this->assertNull($with->getLeftPhpName(), 'A ModelWith initialized from a primary join with alias has a null left phpName');
  150. $q = AuthorQuery::create()
  151. ->join('Propel\Tests\Bookstore\Author.Book')
  152. ->join('Book.Publisher');
  153. $joins = $q->getJoins();
  154. $join = $joins['Publisher'];
  155. $with = new ModelWith($join);
  156. $this->assertEquals('Book', $with->getLeftPhpName(), 'A ModelWith uses the previous join relation name as left phpName');
  157. $q = ReviewQuery::create()
  158. ->join('Propel\Tests\Bookstore\Review.Book')
  159. ->join('Book.Author')
  160. ->join('Book.Publisher');
  161. $joins = $q->getJoins();
  162. $join = $joins['Publisher'];
  163. $with = new ModelWith($join);
  164. $this->assertEquals('Book', $with->getLeftPhpName(), 'A ModelWith uses the previous join relation name as left phpName');
  165. $q = ReviewQuery::create()
  166. ->join('Propel\Tests\Bookstore\Review.Book')
  167. ->join('Book.BookOpinion')
  168. ->join('BookOpinion.BookReader');
  169. $joins = $q->getJoins();
  170. $join = $joins['BookOpinion'];
  171. $with = new ModelWith($join);
  172. $this->assertEquals('Book', $with->getLeftPhpName(), 'A ModelWith uses the previous join relation name as left phpName');
  173. $join = $joins['BookReader'];
  174. $with = new ModelWith($join);
  175. $this->assertEquals('BookOpinion', $with->getLeftPhpName(), 'A ModelWith uses the previous join relation name as left phpName');
  176. $q = BookReaderQuery::create()
  177. ->join('Propel\Tests\Bookstore\BookReader.BookOpinion')
  178. ->join('BookOpinion.Book')
  179. ->join('Book.Author');
  180. $joins = $q->getJoins();
  181. $join = $joins['Book'];
  182. $with = new ModelWith($join);
  183. $this->assertEquals('BookOpinion', $with->getLeftPhpName(), 'A ModelWith uses the previous join relation name as related class');
  184. $join = $joins['Author'];
  185. $with = new ModelWith($join);
  186. $this->assertEquals('Book', $with->getLeftPhpName(), 'A ModelWith uses the previous join relation name as left phpName');
  187. $q = BookSummaryQuery::create()
  188. ->join('Propel\Tests\Bookstore\BookSummary.SummarizedBook')
  189. ->join('SummarizedBook.Author');
  190. $joins = $q->getJoins();
  191. $join = $joins['Author'];
  192. $with = new ModelWith($join);
  193. $this->assertEquals('SummarizedBook', $with->getLeftPhpName(), 'A ModelWith uses the previous join relation name as left phpName');
  194. }
  195. /**
  196. * @return void
  197. */
  198. public function testGetRightPhpName()
  199. {
  200. $q = AuthorQuery::create()
  201. ->joinBook();
  202. $joins = $q->getJoins();
  203. $join = $joins['Book'];
  204. $with = new ModelWith($join);
  205. $this->assertEquals('Book', $with->getRightPhpName(), 'A ModelWith initialized from a primary join has a right phpName');
  206. $q = AuthorQuery::create('a')
  207. ->joinBook();
  208. $joins = $q->getJoins();
  209. $join = $joins['Book'];
  210. $with = new ModelWith($join);
  211. $this->assertEquals('Book', $with->getRightPhpName(), 'A ModelWith initialized from a primary join with alias has a right phpName');
  212. $q = AuthorQuery::create()
  213. ->joinBook('b');
  214. $joins = $q->getJoins();
  215. $join = $joins['b'];
  216. $with = new ModelWith($join);
  217. $this->assertEquals('b', $with->getRightPhpName(), 'A ModelWith initialized from a primary join with alias uses the alias as right phpName');
  218. $q = AuthorQuery::create()
  219. ->join('Propel\Tests\Bookstore\Author.Book')
  220. ->join('Book.Publisher');
  221. $joins = $q->getJoins();
  222. $join = $joins['Publisher'];
  223. $with = new ModelWith($join);
  224. $this->assertEquals('Publisher', $with->getRightPhpName(), 'A ModelWith has a right phpName even when there are previous joins');
  225. $q = BookSummaryQuery::create()
  226. ->join('Propel\Tests\Bookstore\BookSummary.SummarizedBook');
  227. $joins = $q->getJoins();
  228. $join = $joins['SummarizedBook'];
  229. $with = new ModelWith($join);
  230. $this->assertEquals('SummarizedBook', $with->getRightPhpName(), 'A ModelWith uses the relation name rather than the class phpName when it exists');
  231. $q = BookSummaryQuery::create()
  232. ->join('Propel\Tests\Bookstore\BookSummary.SummarizedBook')
  233. ->join('SummarizedBook.Author');
  234. $joins = $q->getJoins();
  235. $join = $joins['Author'];
  236. $with = new ModelWith($join);
  237. $this->assertEquals('Author', $with->getRightPhpName(), 'A ModelWith has a right phpName even when there are previous joins with custom relation names');
  238. }
  239. }