PageRenderTime 90ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Propel/test/testsuite/misc/FieldnameRelatedTest.php

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