TypeTest.php 12 KB

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