TypeTest.php 11 KB

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