PageRenderTime 50ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/propelorm/test/testsuite/misc/FieldnameRelatedTest.php

https://bitbucket.org/franhb/propel
PHP | 394 lines | 317 code | 21 blank | 56 comment | 0 complexity | 7961e9021b2d160a71e1b589e44b7251 MD5 | raw file
Possible License(s): LGPL-3.0
  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. {
  43. $result = defined('BasePeer::TYPE_PHPNAME');
  44. $this->assertTrue($result);
  45. }
  46. /**
  47. * Tests the Base[Object]Peer::getFieldNames() method
  48. */
  49. public function testGetFieldNames ()
  50. {
  51. $types = array(
  52. BasePeer::TYPE_PHPNAME,
  53. BasePeer::TYPE_COLNAME,
  54. BasePeer::TYPE_FIELDNAME,
  55. BasePeer::TYPE_NUM
  56. );
  57. $expecteds = array (
  58. BasePeer::TYPE_PHPNAME => array(
  59. 0 => 'Id',
  60. 1 => 'Title',
  61. 2 => 'ISBN',
  62. 3 => 'Price',
  63. 4 => 'PublisherId',
  64. 5 => 'AuthorId'
  65. ),
  66. BasePeer::TYPE_STUDLYPHPNAME => array(
  67. 0 => 'id',
  68. 1 => 'title',
  69. 2 => 'iSBN',
  70. 3 => 'price',
  71. 4 => 'publisherId',
  72. 5 => 'authorId'
  73. ),
  74. BasePeer::TYPE_COLNAME => array(
  75. 0 => 'book.ID',
  76. 1 => 'book.TITLE',
  77. 2 => 'book.ISBN',
  78. 3 => 'book.PRICE',
  79. 4 => 'book.PUBLISHER_ID',
  80. 5 => 'book.AUTHOR_ID'
  81. ),
  82. BasePeer::TYPE_FIELDNAME => array(
  83. 0 => 'id',
  84. 1 => 'title',
  85. 2 => 'isbn',
  86. 3 => 'price',
  87. 4 => 'publisher_id',
  88. 5 => 'author_id'
  89. ),
  90. BasePeer::TYPE_NUM => array(
  91. 0 => 0,
  92. 1 => 1,
  93. 2 => 2,
  94. 3 => 3,
  95. 4 => 4,
  96. 5 => 5
  97. )
  98. );
  99. foreach ($types as $type) {
  100. $results[$type] = BookPeer::getFieldnames($type);
  101. $this->assertEquals(
  102. $expecteds[$type],
  103. $results[$type],
  104. 'expected was: ' . print_r($expecteds[$type], 1) .
  105. 'but getFieldnames() returned ' . print_r($results[$type], 1)
  106. );
  107. }
  108. }
  109. /**
  110. * Tests the Base[Object]Peer::translateFieldName() method
  111. */
  112. public function testTranslateFieldName ()
  113. {
  114. $types = array(
  115. BasePeer::TYPE_PHPNAME,
  116. BasePeer::TYPE_STUDLYPHPNAME,
  117. BasePeer::TYPE_COLNAME,
  118. BasePeer::TYPE_FIELDNAME,
  119. BasePeer::TYPE_NUM
  120. );
  121. $expecteds = array (
  122. BasePeer::TYPE_PHPNAME => 'AuthorId',
  123. BasePeer::TYPE_STUDLYPHPNAME => 'authorId',
  124. BasePeer::TYPE_COLNAME => 'book.AUTHOR_ID',
  125. BasePeer::TYPE_FIELDNAME => 'author_id',
  126. BasePeer::TYPE_NUM => 5,
  127. );
  128. foreach ($types as $fromType) {
  129. foreach ($types as $toType) {
  130. $name = $expecteds[$fromType];
  131. $expected = $expecteds[$toType];
  132. $result = BookPeer::translateFieldName($name, $fromType, $toType);
  133. $this->assertEquals($expected, $result);
  134. }
  135. }
  136. }
  137. /**
  138. * Tests the BasePeer::getFieldNames() method
  139. */
  140. public function testGetFieldNamesStatic ()
  141. {
  142. $types = array(
  143. BasePeer::TYPE_PHPNAME,
  144. BasePeer::TYPE_STUDLYPHPNAME,
  145. BasePeer::TYPE_COLNAME,
  146. BasePeer::TYPE_FIELDNAME,
  147. BasePeer::TYPE_NUM
  148. );
  149. $expecteds = array (
  150. BasePeer::TYPE_PHPNAME => array(
  151. 0 => 'Id',
  152. 1 => 'Title',
  153. 2 => 'ISBN',
  154. 3 => 'Price',
  155. 4 => 'PublisherId',
  156. 5 => 'AuthorId'
  157. ),
  158. BasePeer::TYPE_STUDLYPHPNAME => array(
  159. 0 => 'id',
  160. 1 => 'title',
  161. 2 => 'iSBN',
  162. 3 => 'price',
  163. 4 => 'publisherId',
  164. 5 => 'authorId'
  165. ),
  166. BasePeer::TYPE_COLNAME => array(
  167. 0 => 'book.ID',
  168. 1 => 'book.TITLE',
  169. 2 => 'book.ISBN',
  170. 3 => 'book.PRICE',
  171. 4 => 'book.PUBLISHER_ID',
  172. 5 => 'book.AUTHOR_ID'
  173. ),
  174. BasePeer::TYPE_FIELDNAME => array(
  175. 0 => 'id',
  176. 1 => 'title',
  177. 2 => 'isbn',
  178. 3 => 'price',
  179. 4 => 'publisher_id',
  180. 5 => 'author_id'
  181. ),
  182. BasePeer::TYPE_NUM => array(
  183. 0 => 0,
  184. 1 => 1,
  185. 2 => 2,
  186. 3 => 3,
  187. 4 => 4,
  188. 5 => 5
  189. )
  190. );
  191. foreach ($types as $type) {
  192. $results[$type] = BasePeer::getFieldnames('Book', $type);
  193. $this->assertEquals(
  194. $expecteds[$type],
  195. $results[$type],
  196. 'expected was: ' . print_r($expecteds[$type], 1) .
  197. 'but getFieldnames() returned ' . print_r($results[$type], 1)
  198. );
  199. }
  200. }
  201. /**
  202. * Tests the BasePeer::translateFieldName() method
  203. */
  204. public function testTranslateFieldNameStatic ()
  205. {
  206. $types = array(
  207. BasePeer::TYPE_PHPNAME,
  208. BasePeer::TYPE_STUDLYPHPNAME,
  209. BasePeer::TYPE_COLNAME,
  210. BasePeer::TYPE_FIELDNAME,
  211. BasePeer::TYPE_NUM
  212. );
  213. $expecteds = array (
  214. BasePeer::TYPE_PHPNAME => 'AuthorId',
  215. BasePeer::TYPE_STUDLYPHPNAME => 'authorId',
  216. BasePeer::TYPE_COLNAME => 'book.AUTHOR_ID',
  217. BasePeer::TYPE_FIELDNAME => 'author_id',
  218. BasePeer::TYPE_NUM => 5,
  219. );
  220. foreach ($types as $fromType) {
  221. foreach ($types as $toType) {
  222. $name = $expecteds[$fromType];
  223. $expected = $expecteds[$toType];
  224. $result = BasePeer::translateFieldName('Book', $name, $fromType, $toType);
  225. $this->assertEquals($expected, $result);
  226. }
  227. }
  228. }
  229. /**
  230. * Tests the Base[Object]::getByName() method
  231. */
  232. public function testGetByName()
  233. {
  234. $types = array(
  235. BasePeer::TYPE_PHPNAME => 'Title',
  236. BasePeer::TYPE_STUDLYPHPNAME => 'title',
  237. BasePeer::TYPE_COLNAME => 'book.TITLE',
  238. BasePeer::TYPE_FIELDNAME => 'title',
  239. BasePeer::TYPE_NUM => 1
  240. );
  241. $book = new Book();
  242. $book->setTitle('Harry Potter and the Order of the Phoenix');
  243. $expected = 'Harry Potter and the Order of the Phoenix';
  244. foreach ($types as $type => $name) {
  245. $result = $book->getByName($name, $type);
  246. $this->assertEquals($expected, $result);
  247. }
  248. }
  249. /**
  250. * Tests the Base[Object]::setByName() method
  251. */
  252. public function testSetByName()
  253. {
  254. $book = new Book();
  255. $types = array(
  256. BasePeer::TYPE_PHPNAME => 'Title',
  257. BasePeer::TYPE_STUDLYPHPNAME => 'title',
  258. BasePeer::TYPE_COLNAME => 'book.TITLE',
  259. BasePeer::TYPE_FIELDNAME => 'title',
  260. BasePeer::TYPE_NUM => 1
  261. );
  262. $title = 'Harry Potter and the Order of the Phoenix';
  263. foreach ($types as $type => $name) {
  264. $book->setByName($name, $title, $type);
  265. $result = $book->getTitle();
  266. $this->assertEquals($title, $result);
  267. }
  268. }
  269. /**
  270. * Tests the Base[Object]::fromArray() method
  271. *
  272. * this also tests populateFromArray() because that's an alias
  273. */
  274. public function testFromArray()
  275. {
  276. $types = array(
  277. BasePeer::TYPE_PHPNAME,
  278. BasePeer::TYPE_STUDLYPHPNAME,
  279. BasePeer::TYPE_COLNAME,
  280. BasePeer::TYPE_FIELDNAME,
  281. BasePeer::TYPE_NUM
  282. );
  283. $expecteds = array (
  284. BasePeer::TYPE_PHPNAME => array (
  285. 'Title' => 'Harry Potter and the Order of the Phoenix',
  286. 'ISBN' => '043935806X'
  287. ),
  288. BasePeer::TYPE_STUDLYPHPNAME => array (
  289. 'title' => 'Harry Potter and the Order of the Phoenix',
  290. 'iSBN' => '043935806X'
  291. ),
  292. BasePeer::TYPE_COLNAME => array (
  293. 'book.TITLE' => 'Harry Potter and the Order of the Phoenix',
  294. 'book.ISBN' => '043935806X'
  295. ),
  296. BasePeer::TYPE_FIELDNAME => array (
  297. 'title' => 'Harry Potter and the Order of the Phoenix',
  298. 'isbn' => '043935806X'
  299. ),
  300. BasePeer::TYPE_NUM => array (
  301. '1' => 'Harry Potter and the Order of the Phoenix',
  302. '2' => '043935806X'
  303. )
  304. );
  305. $book = new Book();
  306. foreach ($types as $type) {
  307. $expected = $expecteds[$type];
  308. $book->fromArray($expected, $type);
  309. $result = array();
  310. foreach (array_keys($expected) as $key) {
  311. $result[$key] = $book->getByName($key, $type);
  312. }
  313. $this->assertEquals(
  314. $expected,
  315. $result,
  316. 'expected was: ' . print_r($expected, 1) .
  317. 'but fromArray() returned ' . print_r($result, 1)
  318. );
  319. }
  320. }
  321. /**
  322. * Tests the Base[Object]::toArray() method
  323. */
  324. public function testToArray()
  325. {
  326. $types = array(
  327. BasePeer::TYPE_PHPNAME,
  328. BasePeer::TYPE_STUDLYPHPNAME,
  329. BasePeer::TYPE_COLNAME,
  330. BasePeer::TYPE_FIELDNAME,
  331. BasePeer::TYPE_NUM
  332. );
  333. $book = new Book();
  334. $book->fromArray(array (
  335. 'Title' => 'Harry Potter and the Order of the Phoenix',
  336. 'ISBN' => '043935806X'
  337. ));
  338. $expecteds = array (
  339. BasePeer::TYPE_PHPNAME => array (
  340. 'Title' => 'Harry Potter and the Order of the Phoenix',
  341. 'ISBN' => '043935806X'
  342. ),
  343. BasePeer::TYPE_STUDLYPHPNAME => array (
  344. 'title' => 'Harry Potter and the Order of the Phoenix',
  345. 'iSBN' => '043935806X'
  346. ),
  347. BasePeer::TYPE_COLNAME => array (
  348. 'book.TITLE' => 'Harry Potter and the Order of the Phoenix',
  349. 'book.ISBN' => '043935806X'
  350. ),
  351. BasePeer::TYPE_FIELDNAME => array (
  352. 'title' => 'Harry Potter and the Order of the Phoenix',
  353. 'isbn' => '043935806X'
  354. ),
  355. BasePeer::TYPE_NUM => array (
  356. '1' => 'Harry Potter and the Order of the Phoenix',
  357. '2' => '043935806X'
  358. )
  359. );
  360. foreach ($types as $type) {
  361. $expected = $expecteds[$type];
  362. $result = $book->toArray($type);
  363. // remove ID since its autoincremented at each test iteration
  364. $result = array_slice($result, 1, 2, true);
  365. $this->assertEquals(
  366. $expected,
  367. $result,
  368. 'expected was: ' . print_r($expected, 1) .
  369. 'but toArray() returned ' . print_r($result, 1)
  370. );
  371. }
  372. }
  373. }