TypeTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Test\TestCase\Database;
  18. use Cake\Database\Type;
  19. use PDO;
  20. /**
  21. * Mock class for testing type registering
  22. *
  23. */
  24. class FooType extends \Cake\Database\Type {
  25. }
  26. /**
  27. * Tests Type class
  28. */
  29. class TypeTest extends \Cake\TestSuite\TestCase {
  30. /**
  31. * Original type map
  32. *
  33. * @var array
  34. */
  35. protected $_originalMap = array();
  36. /**
  37. * Backup original Type class state
  38. *
  39. * @return void
  40. */
  41. public function setUp() {
  42. $this->_originalMap = Type::map();
  43. parent::setUp();
  44. }
  45. /**
  46. * Restores Type class state
  47. *
  48. * @return void
  49. */
  50. public function tearDown() {
  51. Type::map($this->_originalMap);
  52. }
  53. /**
  54. * Tests Type class is able to instantiate basic types
  55. *
  56. * @dataProvider basicTypesProvider
  57. * @return void
  58. */
  59. public function testBuildBasicTypes($name) {
  60. $type = Type::build($name);
  61. $this->assertInstanceOf('\Cake\Database\Type', $type);
  62. $this->assertEquals($name, $type->getName());
  63. }
  64. /**
  65. * provides a basics type list to be used as data provided for a test
  66. *
  67. * @return void
  68. */
  69. public function basicTypesProvider() {
  70. return array(
  71. array('float'),
  72. array('integer'),
  73. array('string'),
  74. array('text'),
  75. array('boolean')
  76. );
  77. }
  78. /**
  79. * Tests trying to build an unknown type throws exception
  80. *
  81. * @expectedException InvalidArgumentException
  82. * @return void
  83. */
  84. public function testBuildUnknownType() {
  85. Type::build('foo');
  86. }
  87. /**
  88. * Tests that once a type with a name is instantiated, the reference is kept
  89. * for future use
  90. *
  91. * @return void
  92. */
  93. public function testInstanceRecycling() {
  94. $type = Type::build('integer');
  95. $this->assertSame($type, Type::build('integer'));
  96. }
  97. /**
  98. * Tests new types can be registered and built
  99. *
  100. * @return void
  101. */
  102. public function testMapAndBuild() {
  103. $map = Type::map();
  104. $this->assertNotEmpty($map);
  105. $this->assertFalse(isset($map['foo']));
  106. $fooType = __NAMESPACE__ . '\FooType';
  107. Type::map('foo', $fooType);
  108. $map = Type::map();
  109. $this->assertEquals($fooType, $map['foo']);
  110. $this->assertEquals($fooType, Type::map('foo'));
  111. $type = Type::build('foo');
  112. $this->assertInstanceOf($fooType, $type);
  113. $this->assertEquals('foo', $type->getName());
  114. }
  115. /**
  116. * Tests clear function in conjunction with map
  117. *
  118. * @return void
  119. */
  120. public function testClear() {
  121. $map = Type::map();
  122. $this->assertNotEmpty($map);
  123. $type = Type::build('float');
  124. Type::clear();
  125. $this->assertEmpty(Type::map());
  126. Type::map($map);
  127. $this->assertEquals($map, Type::map());
  128. $this->assertNotSame($type, Type::build('float'));
  129. }
  130. /**
  131. * Tests floats from database are converted correctly to PHP
  132. *
  133. * @return void
  134. */
  135. public function testFloatToPHP() {
  136. $type = Type::build('float');
  137. $float = '3.14159';
  138. $driver = $this->getMock('\Cake\Database\Driver');
  139. $this->assertEquals(3.14159, $type->toPHP($float, $driver));
  140. $this->assertEquals(3.14159, $type->toPHP(3.14159, $driver));
  141. $this->assertEquals(3.00, $type->toPHP(3, $driver));
  142. }
  143. /**
  144. * Tests floats from PHP are converted correctly to statement value
  145. *
  146. * @return void
  147. */
  148. public function testFloatToStatement() {
  149. $type = Type::build('float');
  150. $float = '3.14159';
  151. $driver = $this->getMock('\Cake\Database\Driver');
  152. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($float, $driver));
  153. }
  154. /**
  155. * Tests integers from database are converted correctly to PHP
  156. *
  157. * @return void
  158. */
  159. public function testIntegerToPHP() {
  160. $type = Type::build('integer');
  161. $integer = '3';
  162. $driver = $this->getMock('\Cake\Database\Driver');
  163. $this->assertEquals(3, $type->toPHP($integer, $driver));
  164. $this->assertEquals(3, $type->toPHP(3, $driver));
  165. $this->assertEquals(3, $type->toPHP(3.57, $driver));
  166. }
  167. /**
  168. * Tests integers from PHP are converted correctly to statement value
  169. *
  170. * @return void
  171. */
  172. public function testIntegerToStatement() {
  173. $type = Type::build('integer');
  174. $integer = '3';
  175. $driver = $this->getMock('\Cake\Database\Driver');
  176. $this->assertEquals(PDO::PARAM_INT, $type->toStatement($integer, $driver));
  177. }
  178. /**
  179. * Tests bigintegers from database are converted correctly to PHP
  180. *
  181. * @return void
  182. */
  183. public function testBigintegerToPHP() {
  184. $type = Type::build('biginteger');
  185. $integer = time() * time();
  186. $driver = $this->getMock('\Cake\Database\Driver');
  187. $this->assertSame($integer, $type->toPHP($integer, $driver));
  188. $this->assertSame($integer, $type->toPHP('' . $integer, $driver));
  189. $this->assertSame(3, $type->toPHP(3.57, $driver));
  190. }
  191. /**
  192. * Tests bigintegers from PHP are converted correctly to statement value
  193. *
  194. * @return void
  195. */
  196. public function testBigintegerToStatement() {
  197. $type = Type::build('biginteger');
  198. $integer = time() * time();
  199. $driver = $this->getMock('\Cake\Database\Driver');
  200. $this->assertEquals(PDO::PARAM_INT, $type->toStatement($integer, $driver));
  201. }
  202. /**
  203. * Tests string from database are converted correctly to PHP
  204. *
  205. * @return void
  206. */
  207. public function testStringToPHP() {
  208. $type = Type::build('string');
  209. $string = 'foo';
  210. $driver = $this->getMock('\Cake\Database\Driver');
  211. $this->assertEquals('foo', $type->toPHP($string, $driver));
  212. $this->assertEquals('3', $type->toPHP(3, $driver));
  213. $this->assertEquals('3.14159', $type->toPHP(3.14159, $driver));
  214. }
  215. /**
  216. * Tests integers from PHP are converted correctly to statement value
  217. *
  218. * @return void
  219. */
  220. public function testStringToStatement() {
  221. $type = Type::build('string');
  222. $string = '3';
  223. $driver = $this->getMock('\Cake\Database\Driver');
  224. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  225. }
  226. /**
  227. * Tests integers from database are converted correctly to PHP
  228. *
  229. * @return void
  230. */
  231. public function testTextToPHP() {
  232. $type = Type::build('string');
  233. $string = 'foo';
  234. $driver = $this->getMock('\Cake\Database\Driver');
  235. $this->assertEquals('foo', $type->toPHP($string, $driver));
  236. $this->assertEquals('3', $type->toPHP(3, $driver));
  237. $this->assertEquals('3.14159', $type->toPHP(3.14159, $driver));
  238. }
  239. /**
  240. * Tests integers from PHP are converted correctly to statement value
  241. *
  242. * @return void
  243. */
  244. public function testTextToStatement() {
  245. $type = Type::build('string');
  246. $string = '3';
  247. $driver = $this->getMock('\Cake\Database\Driver');
  248. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  249. }
  250. /**
  251. * Test convertring booleans to database types.
  252. *
  253. * @return void
  254. */
  255. public function testBooleanToDatabase() {
  256. $type = Type::build('boolean');
  257. $driver = $this->getMock('\Cake\Database\Driver');
  258. $this->assertTrue($type->toDatabase(true, $driver));
  259. $driver = $this->getMock('\Cake\Database\Driver');
  260. $this->assertFalse($type->toDatabase(false, $driver));
  261. $driver = $this->getMock('\Cake\Database\Driver');
  262. $this->assertTrue($type->toDatabase(1, $driver));
  263. $driver = $this->getMock('\Cake\Database\Driver');
  264. $this->assertFalse($type->toDatabase(0, $driver));
  265. $driver = $this->getMock('\Cake\Database\Driver');
  266. $this->assertTrue($type->toDatabase('1', $driver));
  267. $driver = $this->getMock('\Cake\Database\Driver');
  268. $this->assertFalse($type->toDatabase('0', $driver));
  269. }
  270. /**
  271. * Test convertring booleans to PDO types.
  272. *
  273. * @return void
  274. */
  275. public function testBooleanToStatement() {
  276. $type = Type::build('boolean');
  277. $driver = $this->getMock('\Cake\Database\Driver');
  278. $this->assertEquals(PDO::PARAM_BOOL, $type->toStatement(true, $driver));
  279. $driver = $this->getMock('\Cake\Database\Driver');
  280. $this->assertEquals(PDO::PARAM_BOOL, $type->toStatement(false, $driver));
  281. }
  282. /**
  283. * Test convertring string booleans to PHP values.
  284. *
  285. * @return void
  286. */
  287. public function testBooleanToPHP() {
  288. $type = Type::build('boolean');
  289. $driver = $this->getMock('\Cake\Database\Driver');
  290. $this->assertTrue($type->toPHP(true, $driver));
  291. $this->assertTrue($type->toPHP(1, $driver));
  292. $this->assertTrue($type->toPHP('1', $driver));
  293. $this->assertTrue($type->toPHP('TRUE', $driver));
  294. $this->assertTrue($type->toPHP('true', $driver));
  295. $this->assertFalse($type->toPHP(false, $driver));
  296. $this->assertFalse($type->toPHP(0, $driver));
  297. $this->assertFalse($type->toPHP('0', $driver));
  298. $this->assertFalse($type->toPHP('FALSE', $driver));
  299. $this->assertFalse($type->toPHP('false', $driver));
  300. }
  301. /**
  302. * Tests uuid from database are converted correctly to PHP
  303. *
  304. * @return void
  305. */
  306. public function testUuidToPHP() {
  307. $type = Type::build('uuid');
  308. $string = 'abc123-de456-fg789';
  309. $driver = $this->getMock('\Cake\Database\Driver');
  310. $this->assertEquals($string, $type->toPHP($string, $driver));
  311. $this->assertEquals('3', $type->toPHP(3, $driver));
  312. $this->assertEquals('3.14159', $type->toPHP(3.14159, $driver));
  313. }
  314. /**
  315. * Tests integers from PHP are converted correctly to statement value
  316. *
  317. * @return void
  318. */
  319. public function testUuidToStatement() {
  320. $type = Type::build('uuid');
  321. $string = 'abc123-def456-ghi789';
  322. $driver = $this->getMock('\Cake\Database\Driver');
  323. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  324. }
  325. /**
  326. * Tests decimal from database are converted correctly to PHP
  327. *
  328. * @return void
  329. */
  330. public function testDecimalToPHP() {
  331. $type = Type::build('decimal');
  332. $driver = $this->getMock('\Cake\Database\Driver');
  333. $this->assertSame(3.14159, $type->toPHP('3.14159', $driver));
  334. $this->assertSame(3.14159, $type->toPHP(3.14159, $driver));
  335. $this->assertSame(3.0, $type->toPHP(3, $driver));
  336. }
  337. /**
  338. * Tests integers from PHP are converted correctly to statement value
  339. *
  340. * @return void
  341. */
  342. public function testDecimalToStatement() {
  343. $type = Type::build('decimal');
  344. $string = '12.55';
  345. $driver = $this->getMock('\Cake\Database\Driver');
  346. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  347. }
  348. }