/test/testsuite/misc/FieldnameRelatedTest.php

https://github.com/1989gaurav/Propel · PHP · 394 lines · 309 code · 29 blank · 56 comment · 0 complexity · cffeae03acb1344e6e22e963ea887257 MD5 · raw file

  1. <?php
  2. /**
  3. * 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. * @license MIT License
  8. */
  9. /**
  10. * Tests some of the methods of generated Object classes. These are:
  11. *
  12. * - Base[Object]Peer::getFieldNames()
  13. * - Base[Object]Peer::translateFieldName()
  14. * - BasePeer::getFieldNames()
  15. * - BasePeer::translateFieldName()
  16. * - Base[Object]::getByName()
  17. * - Base[Object]::setByName()
  18. * - Base[Object]::fromArray()
  19. * - Base[Object]::toArray()
  20. *
  21. * I've pulled these tests from the GeneratedObjectTest because the don't
  22. * need the BookstoreTestBase's setUp and tearDown (database de/population)
  23. * behaviour. The tests will run faster this way.
  24. *
  25. * @author Sven Fuchs <svenfuchs@artweb-design.de>
  26. * @package misc
  27. */
  28. class FieldnameRelatedTest extends PHPUnit_Framework_TestCase
  29. {
  30. protected function setUp()
  31. {
  32. parent::setUp();
  33. set_include_path(get_include_path() . PATH_SEPARATOR . "fixtures/bookstore/build/classes");
  34. require_once 'bookstore/map/BookTableMap.php';
  35. require_once 'bookstore/BookPeer.php';
  36. require_once 'bookstore/Book.php';
  37. }
  38. /**
  39. * Tests if fieldname type constants are defined
  40. */
  41. public function testFieldNameTypeConstants () {
  42. $result = defined('BasePeer::TYPE_PHPNAME');
  43. $this->assertTrue($result);
  44. }
  45. /**
  46. * Tests the Base[Object]Peer::getFieldNames() method
  47. */
  48. public function testGetFieldNames ()
  49. {
  50. $types = array(
  51. BasePeer::TYPE_PHPNAME,
  52. BasePeer::TYPE_COLNAME,
  53. BasePeer::TYPE_FIELDNAME,
  54. BasePeer::TYPE_NUM
  55. );
  56. $expecteds = array (
  57. BasePeer::TYPE_PHPNAME => array(
  58. 0 => 'Id',
  59. 1 => 'Title',
  60. 2 => 'ISBN',
  61. 3 => 'Price',
  62. 4 => 'PublisherId',
  63. 5 => 'AuthorId'
  64. ),
  65. BasePeer::TYPE_STUDLYPHPNAME => array(
  66. 0 => 'id',
  67. 1 => 'title',
  68. 2 => 'iSBN',
  69. 3 => 'price',
  70. 4 => 'publisherId',
  71. 5 => 'authorId'
  72. ),
  73. BasePeer::TYPE_COLNAME => array(
  74. 0 => 'book.ID',
  75. 1 => 'book.TITLE',
  76. 2 => 'book.ISBN',
  77. 3 => 'book.PRICE',
  78. 4 => 'book.PUBLISHER_ID',
  79. 5 => 'book.AUTHOR_ID'
  80. ),
  81. BasePeer::TYPE_FIELDNAME => array(
  82. 0 => 'id',
  83. 1 => 'title',
  84. 2 => 'isbn',
  85. 3 => 'price',
  86. 4 => 'publisher_id',
  87. 5 => 'author_id'
  88. ),
  89. BasePeer::TYPE_NUM => array(
  90. 0 => 0,
  91. 1 => 1,
  92. 2 => 2,
  93. 3 => 3,
  94. 4 => 4,
  95. 5 => 5
  96. )
  97. );
  98. foreach ($types as $type) {
  99. $results[$type] = BookPeer::getFieldnames($type);
  100. $this->assertEquals(
  101. $expecteds[$type],
  102. $results[$type],
  103. 'expected was: ' . print_r($expecteds[$type], 1) .
  104. 'but getFieldnames() returned ' . print_r($results[$type], 1)
  105. );
  106. }
  107. }
  108. /**
  109. * Tests the Base[Object]Peer::translateFieldName() method
  110. */
  111. public function testTranslateFieldName () {
  112. $types = array(
  113. BasePeer::TYPE_PHPNAME,
  114. BasePeer::TYPE_STUDLYPHPNAME,
  115. BasePeer::TYPE_COLNAME,
  116. BasePeer::TYPE_FIELDNAME,
  117. BasePeer::TYPE_NUM
  118. );
  119. $expecteds = array (
  120. BasePeer::TYPE_PHPNAME => 'AuthorId',
  121. BasePeer::TYPE_STUDLYPHPNAME => 'authorId',
  122. BasePeer::TYPE_COLNAME => 'book.AUTHOR_ID',
  123. BasePeer::TYPE_FIELDNAME => 'author_id',
  124. BasePeer::TYPE_NUM => 5,
  125. );
  126. foreach ($types as $fromType) {
  127. foreach ($types as $toType) {
  128. $name = $expecteds[$fromType];
  129. $expected = $expecteds[$toType];
  130. $result = BookPeer::translateFieldName($name, $fromType, $toType);
  131. $this->assertEquals($expected, $result);
  132. }
  133. }
  134. }
  135. /**
  136. * Tests the BasePeer::getFieldNames() method
  137. */
  138. public function testGetFieldNamesStatic () {
  139. $types = array(
  140. BasePeer::TYPE_PHPNAME,
  141. BasePeer::TYPE_STUDLYPHPNAME,
  142. BasePeer::TYPE_COLNAME,
  143. BasePeer::TYPE_FIELDNAME,
  144. BasePeer::TYPE_NUM
  145. );
  146. $expecteds = array (
  147. BasePeer::TYPE_PHPNAME => array(
  148. 0 => 'Id',
  149. 1 => 'Title',
  150. 2 => 'ISBN',
  151. 3 => 'Price',
  152. 4 => 'PublisherId',
  153. 5 => 'AuthorId'
  154. ),
  155. BasePeer::TYPE_STUDLYPHPNAME => array(
  156. 0 => 'id',
  157. 1 => 'title',
  158. 2 => 'iSBN',
  159. 3 => 'price',
  160. 4 => 'publisherId',
  161. 5 => 'authorId'
  162. ),
  163. BasePeer::TYPE_COLNAME => array(
  164. 0 => 'book.ID',
  165. 1 => 'book.TITLE',
  166. 2 => 'book.ISBN',
  167. 3 => 'book.PRICE',
  168. 4 => 'book.PUBLISHER_ID',
  169. 5 => 'book.AUTHOR_ID'
  170. ),
  171. BasePeer::TYPE_FIELDNAME => array(
  172. 0 => 'id',
  173. 1 => 'title',
  174. 2 => 'isbn',
  175. 3 => 'price',
  176. 4 => 'publisher_id',
  177. 5 => 'author_id'
  178. ),
  179. BasePeer::TYPE_NUM => array(
  180. 0 => 0,
  181. 1 => 1,
  182. 2 => 2,
  183. 3 => 3,
  184. 4 => 4,
  185. 5 => 5
  186. )
  187. );
  188. foreach ($types as $type) {
  189. $results[$type] = BasePeer::getFieldnames('Book', $type);
  190. $this->assertEquals(
  191. $expecteds[$type],
  192. $results[$type],
  193. 'expected was: ' . print_r($expecteds[$type], 1) .
  194. 'but getFieldnames() returned ' . print_r($results[$type], 1)
  195. );
  196. }
  197. }
  198. /**
  199. * Tests the BasePeer::translateFieldName() method
  200. */
  201. public function testTranslateFieldNameStatic () {
  202. $types = array(
  203. BasePeer::TYPE_PHPNAME,
  204. BasePeer::TYPE_STUDLYPHPNAME,
  205. BasePeer::TYPE_COLNAME,
  206. BasePeer::TYPE_FIELDNAME,
  207. BasePeer::TYPE_NUM
  208. );
  209. $expecteds = array (
  210. BasePeer::TYPE_PHPNAME => 'AuthorId',
  211. BasePeer::TYPE_STUDLYPHPNAME => 'authorId',
  212. BasePeer::TYPE_COLNAME => 'book.AUTHOR_ID',
  213. BasePeer::TYPE_FIELDNAME => 'author_id',
  214. BasePeer::TYPE_NUM => 5,
  215. );
  216. foreach ($types as $fromType) {
  217. foreach ($types as $toType) {
  218. $name = $expecteds[$fromType];
  219. $expected = $expecteds[$toType];
  220. $result = BasePeer::translateFieldName('Book', $name, $fromType, $toType);
  221. $this->assertEquals($expected, $result);
  222. }
  223. }
  224. }
  225. /**
  226. * Tests the Base[Object]::getByName() method
  227. */
  228. public function testGetByName() {
  229. $types = array(
  230. BasePeer::TYPE_PHPNAME => 'Title',
  231. BasePeer::TYPE_STUDLYPHPNAME => 'title',
  232. BasePeer::TYPE_COLNAME => 'book.TITLE',
  233. BasePeer::TYPE_FIELDNAME => 'title',
  234. BasePeer::TYPE_NUM => 1
  235. );
  236. $book = new Book();
  237. $book->setTitle('Harry Potter and the Order of the Phoenix');
  238. $expected = 'Harry Potter and the Order of the Phoenix';
  239. foreach ($types as $type => $name) {
  240. $result = $book->getByName($name, $type);
  241. $this->assertEquals($expected, $result);
  242. }
  243. }
  244. /**
  245. * Tests the Base[Object]::setByName() method
  246. */
  247. public function testSetByName() {
  248. $book = new Book();
  249. $types = array(
  250. BasePeer::TYPE_PHPNAME => 'Title',
  251. BasePeer::TYPE_STUDLYPHPNAME => 'title',
  252. BasePeer::TYPE_COLNAME => 'book.TITLE',
  253. BasePeer::TYPE_FIELDNAME => 'title',
  254. BasePeer::TYPE_NUM => 1
  255. );
  256. $title = 'Harry Potter and the Order of the Phoenix';
  257. foreach ($types as $type => $name) {
  258. $book->setByName($name, $title, $type);
  259. $result = $book->getTitle();
  260. $this->assertEquals($title, $result);
  261. }
  262. }
  263. /**
  264. * Tests the Base[Object]::fromArray() method
  265. *
  266. * this also tests populateFromArray() because that's an alias
  267. */
  268. public function testFromArray(){
  269. $types = array(
  270. BasePeer::TYPE_PHPNAME,
  271. BasePeer::TYPE_STUDLYPHPNAME,
  272. BasePeer::TYPE_COLNAME,
  273. BasePeer::TYPE_FIELDNAME,
  274. BasePeer::TYPE_NUM
  275. );
  276. $expecteds = array (
  277. BasePeer::TYPE_PHPNAME => array (
  278. 'Title' => 'Harry Potter and the Order of the Phoenix',
  279. 'ISBN' => '043935806X'
  280. ),
  281. BasePeer::TYPE_STUDLYPHPNAME => array (
  282. 'title' => 'Harry Potter and the Order of the Phoenix',
  283. 'iSBN' => '043935806X'
  284. ),
  285. BasePeer::TYPE_COLNAME => array (
  286. 'book.TITLE' => 'Harry Potter and the Order of the Phoenix',
  287. 'book.ISBN' => '043935806X'
  288. ),
  289. BasePeer::TYPE_FIELDNAME => array (
  290. 'title' => 'Harry Potter and the Order of the Phoenix',
  291. 'isbn' => '043935806X'
  292. ),
  293. BasePeer::TYPE_NUM => array (
  294. '1' => 'Harry Potter and the Order of the Phoenix',
  295. '2' => '043935806X'
  296. )
  297. );
  298. $book = new Book();
  299. foreach ($types as $type) {
  300. $expected = $expecteds[$type];
  301. $book->fromArray($expected, $type);
  302. $result = array();
  303. foreach (array_keys($expected) as $key) {
  304. $result[$key] = $book->getByName($key, $type);
  305. }
  306. $this->assertEquals(
  307. $expected,
  308. $result,
  309. 'expected was: ' . print_r($expected, 1) .
  310. 'but fromArray() returned ' . print_r($result, 1)
  311. );
  312. }
  313. }
  314. /**
  315. * Tests the Base[Object]::toArray() method
  316. */
  317. public function testToArray(){
  318. $types = array(
  319. BasePeer::TYPE_PHPNAME,
  320. BasePeer::TYPE_STUDLYPHPNAME,
  321. BasePeer::TYPE_COLNAME,
  322. BasePeer::TYPE_FIELDNAME,
  323. BasePeer::TYPE_NUM
  324. );
  325. $book = new Book();
  326. $book->fromArray(array (
  327. 'Title' => 'Harry Potter and the Order of the Phoenix',
  328. 'ISBN' => '043935806X'
  329. ));
  330. $expecteds = array (
  331. BasePeer::TYPE_PHPNAME => array (
  332. 'Title' => 'Harry Potter and the Order of the Phoenix',
  333. 'ISBN' => '043935806X'
  334. ),
  335. BasePeer::TYPE_STUDLYPHPNAME => array (
  336. 'title' => 'Harry Potter and the Order of the Phoenix',
  337. 'iSBN' => '043935806X'
  338. ),
  339. BasePeer::TYPE_COLNAME => array (
  340. 'book.TITLE' => 'Harry Potter and the Order of the Phoenix',
  341. 'book.ISBN' => '043935806X'
  342. ),
  343. BasePeer::TYPE_FIELDNAME => array (
  344. 'title' => 'Harry Potter and the Order of the Phoenix',
  345. 'isbn' => '043935806X'
  346. ),
  347. BasePeer::TYPE_NUM => array (
  348. '1' => 'Harry Potter and the Order of the Phoenix',
  349. '2' => '043935806X'
  350. )
  351. );
  352. foreach ($types as $type) {
  353. $expected = $expecteds[$type];
  354. $result = $book->toArray($type);
  355. // remove ID since its autoincremented at each test iteration
  356. $result = array_slice($result, 1, 2, true);
  357. $this->assertEquals(
  358. $expected,
  359. $result,
  360. 'expected was: ' . print_r($expected, 1) .
  361. 'but toArray() returned ' . print_r($result, 1)
  362. );
  363. }
  364. }
  365. }