TypeTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database;
  16. use Cake\Database\Type;
  17. use Cake\Database\Type\UuidType;
  18. use Cake\TestSuite\TestCase;
  19. use PDO;
  20. use TestApp\Database\Type\BarType;
  21. use TestApp\Database\Type\FooType;
  22. /**
  23. * Tests Type class
  24. */
  25. class TypeTest extends TestCase
  26. {
  27. /**
  28. * Original type map
  29. *
  30. * @var array
  31. */
  32. protected $_originalMap = [];
  33. /**
  34. * Backup original Type class state
  35. *
  36. * @return void
  37. */
  38. public function setUp()
  39. {
  40. $this->_originalMap = Type::getMap();
  41. parent::setUp();
  42. }
  43. /**
  44. * Restores Type class state
  45. *
  46. * @return void
  47. */
  48. public function tearDown()
  49. {
  50. parent::tearDown();
  51. Type::setMap($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. {
  61. $type = Type::build($name);
  62. $this->assertInstanceOf('Cake\Database\Type', $type);
  63. $this->assertEquals($name, $type->getName());
  64. $this->assertEquals($name, $type->getBaseType());
  65. }
  66. /**
  67. * provides a basics type list to be used as data provided for a test
  68. *
  69. * @return array
  70. */
  71. public function basicTypesProvider()
  72. {
  73. return [
  74. ['string'],
  75. ['text'],
  76. ['smallinteger'],
  77. ['tinyinteger'],
  78. ['integer'],
  79. ['biginteger'],
  80. ];
  81. }
  82. /**
  83. * Tests trying to build an unknown type throws exception
  84. *
  85. * @return void
  86. */
  87. public function testBuildUnknownType()
  88. {
  89. $this->expectException(\InvalidArgumentException::class);
  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. $this->deprecated(function () {
  111. $map = Type::map();
  112. $this->assertNotEmpty($map);
  113. $this->assertArrayNotHasKey('foo', $map);
  114. });
  115. $fooType = FooType::class;
  116. Type::map('foo', $fooType);
  117. $map = Type::getMap();
  118. $this->assertEquals($fooType, $map['foo']);
  119. $this->deprecated(function () use ($fooType) {
  120. $this->assertEquals($fooType, Type::map('foo'));
  121. });
  122. $type = Type::build('foo');
  123. $this->assertInstanceOf($fooType, $type);
  124. $this->assertEquals('foo', $type->getName());
  125. $this->assertEquals('text', $type->getBaseType());
  126. Type::map('foo2', $fooType);
  127. $map = Type::getMap();
  128. $this->assertSame($fooType, $map['foo2']);
  129. $this->assertSame($fooType, Type::getMap('foo2'));
  130. $type = Type::build('foo2');
  131. $this->assertInstanceOf($fooType, $type);
  132. }
  133. /**
  134. * Tests new types set with set() are returned by buildAll()
  135. *
  136. * @return void
  137. */
  138. public function testSetAndBuild()
  139. {
  140. $types = Type::buildAll();
  141. $this->assertFalse(isset($types['foo']));
  142. Type::set('foo', new FooType());
  143. $types = Type::buildAll();
  144. $this->assertTrue(isset($types['foo']));
  145. }
  146. /**
  147. * Tests overwriting type map works for building
  148. *
  149. * @return void
  150. */
  151. public function testReMapAndBuild()
  152. {
  153. $fooType = FooType::class;
  154. Type::map('foo', $fooType);
  155. $type = Type::build('foo');
  156. $this->assertInstanceOf($fooType, $type);
  157. $barType = BarType::class;
  158. Type::map('foo', $barType);
  159. $type = Type::build('foo');
  160. $this->assertInstanceOf($barType, $type);
  161. }
  162. /**
  163. * Tests new types can be registered and built as objects
  164. *
  165. * @return void
  166. */
  167. public function testMapAndBuildWithObjects()
  168. {
  169. $map = Type::getMap();
  170. Type::clear();
  171. $uuidType = new UuidType('uuid');
  172. $this->deprecated(function () use ($uuidType) {
  173. Type::map('uuid', $uuidType);
  174. });
  175. $this->assertSame($uuidType, Type::build('uuid'));
  176. Type::setMap($map);
  177. }
  178. /**
  179. * testGetMapAndSetMap
  180. *
  181. * @return void
  182. */
  183. public function testGetMapAndSetMap()
  184. {
  185. $map = Type::getMap();
  186. $this->assertNotEmpty($map);
  187. $this->assertArrayNotHasKey('foo', $map);
  188. $expected = [
  189. 'foo' => 'bar',
  190. 'ping' => 'pong',
  191. ];
  192. Type::setMap($expected);
  193. $this->assertEquals($expected, Type::getMap());
  194. $this->assertEquals('bar', Type::getMap('foo'));
  195. }
  196. /**
  197. * Tests clear function in conjunction with map
  198. *
  199. * @return void
  200. */
  201. public function testClear()
  202. {
  203. $map = Type::getMap();
  204. $this->assertNotEmpty($map);
  205. $type = Type::build('float');
  206. Type::clear();
  207. $this->assertEmpty(Type::getMap());
  208. Type::setMap($map);
  209. $newMap = Type::getMap();
  210. $this->assertEquals(array_keys($map), array_keys($newMap));
  211. $this->assertEquals($map['integer'], $newMap['integer']);
  212. $this->assertEquals($type, Type::build('float'));
  213. }
  214. /**
  215. * Tests bigintegers from database are converted correctly to PHP
  216. *
  217. * @return void
  218. */
  219. public function testBigintegerToPHP()
  220. {
  221. $this->skipIf(
  222. PHP_INT_SIZE === 4,
  223. 'This test requires a php version compiled for 64 bits'
  224. );
  225. $type = Type::build('biginteger');
  226. $integer = time() * time();
  227. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  228. $this->assertSame($integer, $type->toPHP($integer, $driver));
  229. $this->assertSame($integer, $type->toPHP('' . $integer, $driver));
  230. $this->assertSame(3, $type->toPHP(3.57, $driver));
  231. }
  232. /**
  233. * Tests bigintegers from PHP are converted correctly to statement value
  234. *
  235. * @return void
  236. */
  237. public function testBigintegerToStatement()
  238. {
  239. $type = Type::build('biginteger');
  240. $integer = time() * time();
  241. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  242. $this->assertEquals(PDO::PARAM_INT, $type->toStatement($integer, $driver));
  243. }
  244. /**
  245. * Tests decimal from database are converted correctly to PHP
  246. *
  247. * @return void
  248. */
  249. public function testDecimalToPHP()
  250. {
  251. $type = Type::build('decimal');
  252. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  253. $this->assertSame(3.14159, $type->toPHP('3.14159', $driver));
  254. $this->assertSame(3.14159, $type->toPHP(3.14159, $driver));
  255. $this->assertSame(3.0, $type->toPHP(3, $driver));
  256. }
  257. /**
  258. * Tests integers from PHP are converted correctly to statement value
  259. *
  260. * @return void
  261. */
  262. public function testDecimalToStatement()
  263. {
  264. $type = Type::build('decimal');
  265. $string = '12.55';
  266. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  267. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  268. }
  269. /**
  270. * Test setting instances into the factory/registry.
  271. *
  272. * @return void
  273. */
  274. public function testSet()
  275. {
  276. $instance = $this->getMockBuilder('Cake\Database\Type')->getMock();
  277. Type::set('random', $instance);
  278. $this->assertSame($instance, Type::build('random'));
  279. }
  280. /**
  281. * @return void
  282. */
  283. public function testDebugInfo()
  284. {
  285. $type = new Type('foo');
  286. $result = $type->__debugInfo();
  287. $expected = [
  288. 'name' => 'foo',
  289. ];
  290. $this->assertEquals($expected, $result);
  291. }
  292. }