TypeTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. ['string'],
  78. ['text'],
  79. ['boolean']
  80. ];
  81. }
  82. /**
  83. * Tests trying to build an unknown type throws exception
  84. *
  85. * @expectedException \InvalidArgumentException
  86. * @return void
  87. */
  88. public function testBuildUnknownType()
  89. {
  90. Type::build('foo');
  91. }
  92. /**
  93. * Tests that once a type with a name is instantiated, the reference is kept
  94. * for future use
  95. *
  96. * @return void
  97. */
  98. public function testInstanceRecycling()
  99. {
  100. $type = Type::build('integer');
  101. $this->assertSame($type, Type::build('integer'));
  102. }
  103. /**
  104. * Tests new types can be registered and built
  105. *
  106. * @return void
  107. */
  108. public function testMapAndBuild()
  109. {
  110. $map = Type::map();
  111. $this->assertNotEmpty($map);
  112. $this->assertFalse(isset($map['foo']));
  113. $fooType = __NAMESPACE__ . '\FooType';
  114. Type::map('foo', $fooType);
  115. $map = Type::map();
  116. $this->assertEquals($fooType, $map['foo']);
  117. $this->assertEquals($fooType, Type::map('foo'));
  118. $type = Type::build('foo');
  119. $this->assertInstanceOf($fooType, $type);
  120. $this->assertEquals('foo', $type->getName());
  121. }
  122. /**
  123. * Tests clear function in conjunction with map
  124. *
  125. * @return void
  126. */
  127. public function testClear()
  128. {
  129. $map = Type::map();
  130. $this->assertNotEmpty($map);
  131. $type = Type::build('float');
  132. Type::clear();
  133. $this->assertEmpty(Type::map());
  134. Type::map($map);
  135. $this->assertEquals($map, Type::map());
  136. $this->assertNotSame($type, Type::build('float'));
  137. }
  138. /**
  139. * Tests bigintegers from database are converted correctly to PHP
  140. *
  141. * @return void
  142. */
  143. public function testBigintegerToPHP()
  144. {
  145. $this->skipIf(
  146. PHP_INT_SIZE === 4,
  147. 'This test requires a php version compiled for 64 bits'
  148. );
  149. $type = Type::build('biginteger');
  150. $integer = time() * time();
  151. $driver = $this->getMock('\Cake\Database\Driver');
  152. $this->assertSame($integer, $type->toPHP($integer, $driver));
  153. $this->assertSame($integer, $type->toPHP('' . $integer, $driver));
  154. $this->assertSame(3, $type->toPHP(3.57, $driver));
  155. }
  156. /**
  157. * Tests bigintegers from PHP are converted correctly to statement value
  158. *
  159. * @return void
  160. */
  161. public function testBigintegerToStatement()
  162. {
  163. $type = Type::build('biginteger');
  164. $integer = time() * time();
  165. $driver = $this->getMock('\Cake\Database\Driver');
  166. $this->assertEquals(PDO::PARAM_INT, $type->toStatement($integer, $driver));
  167. }
  168. /**
  169. * Tests string from database are converted correctly to PHP
  170. *
  171. * @return void
  172. */
  173. public function testStringToPHP()
  174. {
  175. $type = Type::build('string');
  176. $string = 'foo';
  177. $driver = $this->getMock('\Cake\Database\Driver');
  178. $this->assertEquals('foo', $type->toPHP($string, $driver));
  179. $this->assertEquals('3', $type->toPHP(3, $driver));
  180. $this->assertEquals('3.14159', $type->toPHP(3.14159, $driver));
  181. $this->assertEquals('', $type->toPHP([3, 'elf'], $driver));
  182. }
  183. /**
  184. * Tests integers from PHP are converted correctly to statement value
  185. *
  186. * @return void
  187. */
  188. public function testStringToStatement()
  189. {
  190. $type = Type::build('string');
  191. $string = '3';
  192. $driver = $this->getMock('\Cake\Database\Driver');
  193. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  194. }
  195. /**
  196. * Tests integers from database are converted correctly to PHP
  197. *
  198. * @return void
  199. */
  200. public function testTextToPHP()
  201. {
  202. $type = Type::build('string');
  203. $string = 'foo';
  204. $driver = $this->getMock('\Cake\Database\Driver');
  205. $this->assertEquals('foo', $type->toPHP($string, $driver));
  206. $this->assertEquals('3', $type->toPHP(3, $driver));
  207. $this->assertEquals('3.14159', $type->toPHP(3.14159, $driver));
  208. $this->assertEquals('', $type->toPHP([2, 3], $driver));
  209. }
  210. /**
  211. * Tests integers from PHP are converted correctly to statement value
  212. *
  213. * @return void
  214. */
  215. public function testTextToStatement()
  216. {
  217. $type = Type::build('string');
  218. $string = '3';
  219. $driver = $this->getMock('\Cake\Database\Driver');
  220. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  221. }
  222. /**
  223. * Test convertring booleans to database types.
  224. *
  225. * @return void
  226. */
  227. public function testBooleanToDatabase()
  228. {
  229. $type = Type::build('boolean');
  230. $driver = $this->getMock('\Cake\Database\Driver');
  231. $this->assertTrue($type->toDatabase(true, $driver));
  232. $this->assertFalse($type->toDatabase(false, $driver));
  233. $this->assertTrue($type->toDatabase(1, $driver));
  234. $this->assertFalse($type->toDatabase(0, $driver));
  235. $this->assertTrue($type->toDatabase('1', $driver));
  236. $this->assertFalse($type->toDatabase('0', $driver));
  237. $this->assertTrue($type->toDatabase([1, 2], $driver));
  238. }
  239. /**
  240. * Test convertring booleans to PDO types.
  241. *
  242. * @return void
  243. */
  244. public function testBooleanToStatement()
  245. {
  246. $type = Type::build('boolean');
  247. $driver = $this->getMock('\Cake\Database\Driver');
  248. $this->assertEquals(PDO::PARAM_BOOL, $type->toStatement(true, $driver));
  249. $this->assertEquals(PDO::PARAM_BOOL, $type->toStatement(false, $driver));
  250. }
  251. /**
  252. * Test convertring string booleans to PHP values.
  253. *
  254. * @return void
  255. */
  256. public function testBooleanToPHP()
  257. {
  258. $type = Type::build('boolean');
  259. $driver = $this->getMock('\Cake\Database\Driver');
  260. $this->assertTrue($type->toPHP(true, $driver));
  261. $this->assertTrue($type->toPHP(1, $driver));
  262. $this->assertTrue($type->toPHP('1', $driver));
  263. $this->assertTrue($type->toPHP('TRUE', $driver));
  264. $this->assertTrue($type->toPHP('true', $driver));
  265. $this->assertFalse($type->toPHP(false, $driver));
  266. $this->assertFalse($type->toPHP(0, $driver));
  267. $this->assertFalse($type->toPHP('0', $driver));
  268. $this->assertFalse($type->toPHP('FALSE', $driver));
  269. $this->assertFalse($type->toPHP('false', $driver));
  270. $this->assertTrue($type->toPHP(['2', '3'], $driver));
  271. }
  272. /**
  273. * Test marshalling booleans
  274. *
  275. * @return void
  276. */
  277. public function testBooleanMarshal()
  278. {
  279. $type = Type::build('boolean');
  280. $this->assertTrue($type->marshal(true));
  281. $this->assertTrue($type->marshal(1));
  282. $this->assertTrue($type->marshal('1'));
  283. $this->assertTrue($type->marshal('true'));
  284. $this->assertFalse($type->marshal('false'));
  285. $this->assertFalse($type->marshal('0'));
  286. $this->assertFalse($type->marshal(0));
  287. $this->assertFalse($type->marshal(''));
  288. $this->assertFalse($type->marshal('invalid'));
  289. $this->assertTrue($type->marshal(['2', '3']));
  290. }
  291. /**
  292. * Tests uuid from database are converted correctly to PHP
  293. *
  294. * @return void
  295. */
  296. public function testUuidToPHP()
  297. {
  298. $type = Type::build('uuid');
  299. $string = 'abc123-de456-fg789';
  300. $driver = $this->getMock('\Cake\Database\Driver');
  301. $this->assertEquals($string, $type->toPHP($string, $driver));
  302. $this->assertEquals('3', $type->toPHP(3, $driver));
  303. $this->assertEquals('3.14159', $type->toPHP(3.14159, $driver));
  304. }
  305. /**
  306. * Tests integers from PHP are converted correctly to statement value
  307. *
  308. * @return void
  309. */
  310. public function testUuidToStatement()
  311. {
  312. $type = Type::build('uuid');
  313. $string = 'abc123-def456-ghi789';
  314. $driver = $this->getMock('\Cake\Database\Driver');
  315. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  316. }
  317. /**
  318. * Tests decimal from database are converted correctly to PHP
  319. *
  320. * @return void
  321. */
  322. public function testDecimalToPHP()
  323. {
  324. $type = Type::build('decimal');
  325. $driver = $this->getMock('\Cake\Database\Driver');
  326. $this->assertSame(3.14159, $type->toPHP('3.14159', $driver));
  327. $this->assertSame(3.14159, $type->toPHP(3.14159, $driver));
  328. $this->assertSame(3.0, $type->toPHP(3, $driver));
  329. $this->assertSame(1, $type->toPHP(['3', '4'], $driver));
  330. }
  331. /**
  332. * Tests integers from PHP are converted correctly to statement value
  333. *
  334. * @return void
  335. */
  336. public function testDecimalToStatement()
  337. {
  338. $type = Type::build('decimal');
  339. $string = '12.55';
  340. $driver = $this->getMock('\Cake\Database\Driver');
  341. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  342. }
  343. }