PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/symfony/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/propel-generator/test/classes/propel/FieldnameRelatedTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 398 lines | 301 code | 30 blank | 67 comment | 0 complexity | 9c7ee35c61d0c4fb9a95d988a71a7d08 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, AGPL-3.0, BSD-3-Clause, AGPL-1.0, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * $Id: FieldnameRelatedTest.php 752 2007-11-03 17:19:19Z hans $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://propel.phpdb.org>.
  20. */
  21. require_once 'PHPUnit/Framework/TestCase.php';
  22. require_once 'bookstore/BookstoreTestBase.php';
  23. /**
  24. * Tests some of the methods of generated Object classes. These are:
  25. *
  26. * - Base[Object]Peer::getFieldNames()
  27. * - Base[Object]Peer::translateFieldName()
  28. * - BasePeer::getFieldNames()
  29. * - BasePeer::translateFieldName()
  30. * - Base[Object]::getByName()
  31. * - Base[Object]::setByName()
  32. * - Base[Object]::fromArray()
  33. * - Base[Object]::toArray()
  34. *
  35. * I've pulled these tests from the GeneratedObjectTest because the don't
  36. * need the BookstoreTestBase's setUp and tearDown (database de/population)
  37. * behaviour. The tests will run faster this way.
  38. *
  39. * @author Sven Fuchs <svenfuchs@artweb-design.de>
  40. */
  41. class FieldnameRelatedTest extends PHPUnit_Framework_TestCase {
  42. /**
  43. * Tests if fieldname type constants are defined
  44. */
  45. public function testFieldNameTypeConstants () {
  46. $result = defined('BasePeer::TYPE_PHPNAME');
  47. $this->assertTrue($result);
  48. }
  49. /**
  50. * Tests the Base[Object]Peer::getFieldNames() method
  51. */
  52. public function testGetFieldNames () {
  53. $types = array(
  54. BasePeer::TYPE_PHPNAME,
  55. BasePeer::TYPE_COLNAME,
  56. BasePeer::TYPE_FIELDNAME,
  57. BasePeer::TYPE_NUM
  58. );
  59. $expecteds = array (
  60. BasePeer::TYPE_PHPNAME => array(
  61. 0 => 'Id',
  62. 1 => 'Title',
  63. 2 => 'ISBN',
  64. 3 => 'Price',
  65. 4 => 'PublisherId',
  66. 5 => 'AuthorId'
  67. ),
  68. BasePeer::TYPE_STUDLYPHPNAME => array(
  69. 0 => 'id',
  70. 1 => 'title',
  71. 2 => 'iSBN',
  72. 3 => 'price',
  73. 4 => 'publisherId',
  74. 5 => 'authorId'
  75. ),
  76. BasePeer::TYPE_COLNAME => array(
  77. 0 => 'book.ID',
  78. 1 => 'book.TITLE',
  79. 2 => 'book.ISBN',
  80. 3 => 'book.PRICE',
  81. 4 => 'book.PUBLISHER_ID',
  82. 5 => 'book.AUTHOR_ID'
  83. ),
  84. BasePeer::TYPE_FIELDNAME => array(
  85. 0 => 'id',
  86. 1 => 'title',
  87. 2 => 'isbn',
  88. 3 => 'price',
  89. 4 => 'publisher_id',
  90. 5 => 'author_id'
  91. ),
  92. BasePeer::TYPE_NUM => array(
  93. 0 => 0,
  94. 1 => 1,
  95. 2 => 2,
  96. 3 => 3,
  97. 4 => 4,
  98. 5 => 5
  99. )
  100. );
  101. foreach ($types as $type) {
  102. $results[$type] = BookPeer::getFieldnames($type);
  103. $this->assertEquals(
  104. $expecteds[$type],
  105. $results[$type],
  106. 'expected was: ' . print_r($expecteds[$type], 1) .
  107. 'but getFieldnames() returned ' . print_r($results[$type], 1)
  108. );
  109. }
  110. }
  111. /**
  112. * Tests the Base[Object]Peer::translateFieldName() method
  113. */
  114. public function testTranslateFieldName () {
  115. $types = array(
  116. BasePeer::TYPE_PHPNAME,
  117. BasePeer::TYPE_STUDLYPHPNAME,
  118. BasePeer::TYPE_COLNAME,
  119. BasePeer::TYPE_FIELDNAME,
  120. BasePeer::TYPE_NUM
  121. );
  122. $expecteds = array (
  123. BasePeer::TYPE_PHPNAME => 'AuthorId',
  124. BasePeer::TYPE_STUDLYPHPNAME => 'authorId',
  125. BasePeer::TYPE_COLNAME => 'book.AUTHOR_ID',
  126. BasePeer::TYPE_FIELDNAME => 'author_id',
  127. BasePeer::TYPE_NUM => 5,
  128. );
  129. foreach ($types as $fromType) {
  130. foreach ($types as $toType) {
  131. $name = $expecteds[$fromType];
  132. $expected = $expecteds[$toType];
  133. $result = BookPeer::translateFieldName($name, $fromType, $toType);
  134. $this->assertEquals($expected, $result);
  135. }
  136. }
  137. }
  138. /**
  139. * Tests the BasePeer::getFieldNames() method
  140. */
  141. public function testGetFieldNamesStatic () {
  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. $types = array(
  206. BasePeer::TYPE_PHPNAME,
  207. BasePeer::TYPE_STUDLYPHPNAME,
  208. BasePeer::TYPE_COLNAME,
  209. BasePeer::TYPE_FIELDNAME,
  210. BasePeer::TYPE_NUM
  211. );
  212. $expecteds = array (
  213. BasePeer::TYPE_PHPNAME => 'AuthorId',
  214. BasePeer::TYPE_STUDLYPHPNAME => 'authorId',
  215. BasePeer::TYPE_COLNAME => 'book.AUTHOR_ID',
  216. BasePeer::TYPE_FIELDNAME => 'author_id',
  217. BasePeer::TYPE_NUM => 5,
  218. );
  219. foreach ($types as $fromType) {
  220. foreach ($types as $toType) {
  221. $name = $expecteds[$fromType];
  222. $expected = $expecteds[$toType];
  223. $result = BasePeer::translateFieldName('Book', $name, $fromType, $toType);
  224. $this->assertEquals($expected, $result);
  225. }
  226. }
  227. }
  228. /**
  229. * Tests the Base[Object]::getByName() method
  230. */
  231. public function testGetByName() {
  232. $types = array(
  233. BasePeer::TYPE_PHPNAME => 'Title',
  234. BasePeer::TYPE_STUDLYPHPNAME => 'title',
  235. BasePeer::TYPE_COLNAME => 'book.TITLE',
  236. BasePeer::TYPE_FIELDNAME => 'title',
  237. BasePeer::TYPE_NUM => 1
  238. );
  239. $book = new Book();
  240. $book->setTitle('Harry Potter and the Order of the Phoenix');
  241. $expected = 'Harry Potter and the Order of the Phoenix';
  242. foreach ($types as $type => $name) {
  243. $result = $book->getByName($name, $type);
  244. $this->assertEquals($expected, $result);
  245. }
  246. }
  247. /**
  248. * Tests the Base[Object]::setByName() method
  249. */
  250. public function testSetByName() {
  251. $book = new Book();
  252. $types = array(
  253. BasePeer::TYPE_PHPNAME => 'Title',
  254. BasePeer::TYPE_STUDLYPHPNAME => 'title',
  255. BasePeer::TYPE_COLNAME => 'book.TITLE',
  256. BasePeer::TYPE_FIELDNAME => 'title',
  257. BasePeer::TYPE_NUM => 1
  258. );
  259. $title = 'Harry Potter and the Order of the Phoenix';
  260. foreach ($types as $type => $name) {
  261. $book->setByName($name, $title, $type);
  262. $result = $book->getTitle();
  263. $this->assertEquals($title, $result);
  264. }
  265. }
  266. /**
  267. * Tests the Base[Object]::fromArray() method
  268. *
  269. * this also tests populateFromArray() because that's an alias
  270. */
  271. public function testFromArray(){
  272. $types = array(
  273. BasePeer::TYPE_PHPNAME,
  274. BasePeer::TYPE_STUDLYPHPNAME,
  275. BasePeer::TYPE_COLNAME,
  276. BasePeer::TYPE_FIELDNAME,
  277. BasePeer::TYPE_NUM
  278. );
  279. $expecteds = array (
  280. BasePeer::TYPE_PHPNAME => array (
  281. 'Title' => 'Harry Potter and the Order of the Phoenix',
  282. 'ISBN' => '043935806X'
  283. ),
  284. BasePeer::TYPE_STUDLYPHPNAME => array (
  285. 'title' => 'Harry Potter and the Order of the Phoenix',
  286. 'iSBN' => '043935806X'
  287. ),
  288. BasePeer::TYPE_COLNAME => array (
  289. 'book.TITLE' => 'Harry Potter and the Order of the Phoenix',
  290. 'book.ISBN' => '043935806X'
  291. ),
  292. BasePeer::TYPE_FIELDNAME => array (
  293. 'title' => 'Harry Potter and the Order of the Phoenix',
  294. 'isbn' => '043935806X'
  295. ),
  296. BasePeer::TYPE_NUM => array (
  297. '1' => 'Harry Potter and the Order of the Phoenix',
  298. '2' => '043935806X'
  299. )
  300. );
  301. $book = new Book();
  302. foreach ($types as $type) {
  303. $expected = $expecteds[$type];
  304. $book->fromArray($expected, $type);
  305. $result = array();
  306. foreach (array_keys($expected) as $key) {
  307. $result[$key] = $book->getByName($key, $type);
  308. }
  309. $this->assertEquals(
  310. $expected,
  311. $result,
  312. 'expected was: ' . print_r($expected, 1) .
  313. 'but fromArray() returned ' . print_r($result, 1)
  314. );
  315. }
  316. }
  317. /**
  318. * Tests the Base[Object]::toArray() method
  319. */
  320. public function testToArray(){
  321. $types = array(
  322. BasePeer::TYPE_PHPNAME,
  323. BasePeer::TYPE_STUDLYPHPNAME,
  324. BasePeer::TYPE_COLNAME,
  325. BasePeer::TYPE_FIELDNAME,
  326. BasePeer::TYPE_NUM
  327. );
  328. $book = new Book();
  329. $book->fromArray(array (
  330. 'Title' => 'Harry Potter and the Order of the Phoenix',
  331. 'ISBN' => '043935806X'
  332. ));
  333. $expecteds = array (
  334. BasePeer::TYPE_PHPNAME => array (
  335. 'Title' => 'Harry Potter and the Order of the Phoenix',
  336. 'ISBN' => '043935806X'
  337. ),
  338. BasePeer::TYPE_STUDLYPHPNAME => array (
  339. 'title' => 'Harry Potter and the Order of the Phoenix',
  340. 'iSBN' => '043935806X'
  341. ),
  342. BasePeer::TYPE_COLNAME => array (
  343. 'book.TITLE' => 'Harry Potter and the Order of the Phoenix',
  344. 'book.ISBN' => '043935806X'
  345. ),
  346. BasePeer::TYPE_FIELDNAME => array (
  347. 'title' => 'Harry Potter and the Order of the Phoenix',
  348. 'isbn' => '043935806X'
  349. ),
  350. BasePeer::TYPE_NUM => array (
  351. '1' => 'Harry Potter and the Order of the Phoenix',
  352. '2' => '043935806X'
  353. )
  354. );
  355. foreach ($types as $type) {
  356. $expected = $expecteds[$type];
  357. $result = $book->toArray($type);
  358. // remove ID since its autoincremented at each test iteration
  359. $result = array_slice($result, 1, 2, true);
  360. $this->assertEquals(
  361. $expected,
  362. $result,
  363. 'expected was: ' . print_r($expected, 1) .
  364. 'but toArray() returned ' . print_r($result, 1)
  365. );
  366. }
  367. }
  368. }