TypeTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 overwriting type map works for building
  135. *
  136. * @return void
  137. */
  138. public function testReMapAndBuild()
  139. {
  140. $fooType = FooType::class;
  141. Type::map('foo', $fooType);
  142. $type = Type::build('foo');
  143. $this->assertInstanceOf($fooType, $type);
  144. $barType = BarType::class;
  145. Type::map('foo', $barType);
  146. $type = Type::build('foo');
  147. $this->assertInstanceOf($barType, $type);
  148. }
  149. /**
  150. * Tests new types can be registered and built as objects
  151. *
  152. * @return void
  153. */
  154. public function testMapAndBuildWithObjects()
  155. {
  156. $map = Type::getMap();
  157. Type::clear();
  158. $uuidType = new UuidType('uuid');
  159. $this->deprecated(function () use ($uuidType) {
  160. Type::map('uuid', $uuidType);
  161. });
  162. $this->assertSame($uuidType, Type::build('uuid'));
  163. Type::setMap($map);
  164. }
  165. /**
  166. * testGetMapAndSetMap
  167. *
  168. * @return void
  169. */
  170. public function testGetMapAndSetMap()
  171. {
  172. $map = Type::getMap();
  173. $this->assertNotEmpty($map);
  174. $this->assertArrayNotHasKey('foo', $map);
  175. $expected = [
  176. 'foo' => 'bar',
  177. 'ping' => 'pong',
  178. ];
  179. Type::setMap($expected);
  180. $this->assertEquals($expected, Type::getMap());
  181. $this->assertEquals('bar', Type::getMap('foo'));
  182. }
  183. /**
  184. * Tests clear function in conjunction with map
  185. *
  186. * @return void
  187. */
  188. public function testClear()
  189. {
  190. $map = Type::getMap();
  191. $this->assertNotEmpty($map);
  192. $type = Type::build('float');
  193. Type::clear();
  194. $this->assertEmpty(Type::getMap());
  195. Type::setMap($map);
  196. $newMap = Type::getMap();
  197. $this->assertEquals(array_keys($map), array_keys($newMap));
  198. $this->assertEquals($map['integer'], $newMap['integer']);
  199. $this->assertEquals($type, Type::build('float'));
  200. }
  201. /**
  202. * Tests bigintegers from database are converted correctly to PHP
  203. *
  204. * @return void
  205. */
  206. public function testBigintegerToPHP()
  207. {
  208. $this->skipIf(
  209. PHP_INT_SIZE === 4,
  210. 'This test requires a php version compiled for 64 bits'
  211. );
  212. $type = Type::build('biginteger');
  213. $integer = time() * time();
  214. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  215. $this->assertSame($integer, $type->toPHP($integer, $driver));
  216. $this->assertSame($integer, $type->toPHP('' . $integer, $driver));
  217. $this->assertSame(3, $type->toPHP(3.57, $driver));
  218. }
  219. /**
  220. * Tests bigintegers from PHP are converted correctly to statement value
  221. *
  222. * @return void
  223. */
  224. public function testBigintegerToStatement()
  225. {
  226. $type = Type::build('biginteger');
  227. $integer = time() * time();
  228. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  229. $this->assertEquals(PDO::PARAM_INT, $type->toStatement($integer, $driver));
  230. }
  231. /**
  232. * Tests decimal from database are converted correctly to PHP
  233. *
  234. * @return void
  235. */
  236. public function testDecimalToPHP()
  237. {
  238. $type = Type::build('decimal');
  239. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  240. $this->assertSame(3.14159, $type->toPHP('3.14159', $driver));
  241. $this->assertSame(3.14159, $type->toPHP(3.14159, $driver));
  242. $this->assertSame(3.0, $type->toPHP(3, $driver));
  243. }
  244. /**
  245. * Tests integers from PHP are converted correctly to statement value
  246. *
  247. * @return void
  248. */
  249. public function testDecimalToStatement()
  250. {
  251. $type = Type::build('decimal');
  252. $string = '12.55';
  253. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  254. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  255. }
  256. /**
  257. * Test setting instances into the factory/registry.
  258. *
  259. * @return void
  260. */
  261. public function testSet()
  262. {
  263. $instance = $this->getMockBuilder('Cake\Database\Type')->getMock();
  264. Type::set('random', $instance);
  265. $this->assertSame($instance, Type::build('random'));
  266. }
  267. /**
  268. * @return void
  269. */
  270. public function testDebugInfo()
  271. {
  272. $type = new Type('foo');
  273. $result = $type->__debugInfo();
  274. $expected = [
  275. 'name' => 'foo',
  276. ];
  277. $this->assertEquals($expected, $result);
  278. }
  279. }