TypeTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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\BoolType;
  18. use Cake\Database\Type\IntegerType;
  19. use Cake\Database\Type\UuidType;
  20. use Cake\TestSuite\TestCase;
  21. use PDO;
  22. use TestApp\Database\Type\BarType;
  23. use TestApp\Database\Type\FooType;
  24. /**
  25. * Tests Type class
  26. */
  27. class TypeTest extends TestCase
  28. {
  29. /**
  30. * Original type map
  31. *
  32. * @var array
  33. */
  34. protected $_originalMap = [];
  35. /**
  36. * Backup original Type class state
  37. *
  38. * @return void
  39. */
  40. public function setUp()
  41. {
  42. $this->_originalMap = Type::map();
  43. parent::setUp();
  44. }
  45. /**
  46. * Restores Type class state
  47. *
  48. * @return void
  49. */
  50. public function tearDown()
  51. {
  52. parent::tearDown();
  53. Type::map($this->_originalMap);
  54. }
  55. /**
  56. * Tests Type class is able to instantiate basic types
  57. *
  58. * @dataProvider basicTypesProvider
  59. * @return void
  60. */
  61. public function testBuildBasicTypes($name)
  62. {
  63. $type = Type::build($name);
  64. $this->assertInstanceOf('Cake\Database\Type', $type);
  65. $this->assertEquals($name, $type->getName());
  66. $this->assertEquals($name, $type->getBaseType());
  67. }
  68. /**
  69. * provides a basics type list to be used as data provided for a test
  70. *
  71. * @return void
  72. */
  73. public function basicTypesProvider()
  74. {
  75. return [
  76. ['string'],
  77. ['text'],
  78. ['smallinteger'],
  79. ['tinyinteger'],
  80. ['integer'],
  81. ['biginteger'],
  82. ];
  83. }
  84. /**
  85. * Tests trying to build an unknown type throws exception
  86. *
  87. * @return void
  88. */
  89. public function testBuildUnknownType()
  90. {
  91. $this->expectException(\InvalidArgumentException::class);
  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 = FooType::class;
  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. $this->assertEquals('text', $type->getBaseType());
  124. Type::map('foo2', $fooType);
  125. $map = Type::map();
  126. $this->assertSame($fooType, $map['foo2']);
  127. $this->assertSame($fooType, Type::map('foo2'));
  128. $type = Type::build('foo2');
  129. $this->assertInstanceOf($fooType, $type);
  130. }
  131. /**
  132. * Tests overwriting type map works for building
  133. *
  134. * @return void
  135. */
  136. public function testReMapAndBuild()
  137. {
  138. $fooType = FooType::class;
  139. Type::map('foo', $fooType);
  140. $type = Type::build('foo');
  141. $this->assertInstanceOf($fooType, $type);
  142. $barType = BarType::class;
  143. Type::map('foo', $barType);
  144. $type = Type::build('foo');
  145. $this->assertInstanceOf($barType, $type);
  146. }
  147. /**
  148. * Tests new types can be registered and built as objects
  149. *
  150. * @return void
  151. */
  152. public function testMapAndBuildWithObjects()
  153. {
  154. $map = Type::map();
  155. Type::clear();
  156. $uuidType = new UuidType('uuid');
  157. Type::map('uuid', $uuidType);
  158. $this->assertSame($uuidType, Type::build('uuid'));
  159. Type::map($map);
  160. }
  161. /**
  162. * Tests clear function in conjunction with map
  163. *
  164. * @return void
  165. */
  166. public function testClear()
  167. {
  168. $map = Type::map();
  169. $this->assertNotEmpty($map);
  170. $type = Type::build('float');
  171. Type::clear();
  172. $this->assertEmpty(Type::map());
  173. Type::map($map);
  174. $newMap = Type::map();
  175. $this->assertEquals(array_keys($map), array_keys($newMap));
  176. $this->assertEquals($map['integer'], $newMap['integer']);
  177. $this->assertEquals($type, Type::build('float'));
  178. }
  179. /**
  180. * Tests bigintegers from database are converted correctly to PHP
  181. *
  182. * @return void
  183. */
  184. public function testBigintegerToPHP()
  185. {
  186. $this->skipIf(
  187. PHP_INT_SIZE === 4,
  188. 'This test requires a php version compiled for 64 bits'
  189. );
  190. $type = Type::build('biginteger');
  191. $integer = time() * time();
  192. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  193. $this->assertSame($integer, $type->toPHP($integer, $driver));
  194. $this->assertSame($integer, $type->toPHP('' . $integer, $driver));
  195. $this->assertSame(3, $type->toPHP(3.57, $driver));
  196. }
  197. /**
  198. * Tests bigintegers from PHP are converted correctly to statement value
  199. *
  200. * @return void
  201. */
  202. public function testBigintegerToStatement()
  203. {
  204. $type = Type::build('biginteger');
  205. $integer = time() * time();
  206. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  207. $this->assertEquals(PDO::PARAM_INT, $type->toStatement($integer, $driver));
  208. }
  209. /**
  210. * Tests decimal from database are converted correctly to PHP
  211. *
  212. * @return void
  213. */
  214. public function testDecimalToPHP()
  215. {
  216. $type = Type::build('decimal');
  217. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  218. $this->assertSame(3.14159, $type->toPHP('3.14159', $driver));
  219. $this->assertSame(3.14159, $type->toPHP(3.14159, $driver));
  220. $this->assertSame(3.0, $type->toPHP(3, $driver));
  221. $this->assertSame(1.0, $type->toPHP(['3', '4'], $driver));
  222. }
  223. /**
  224. * Tests integers from PHP are converted correctly to statement value
  225. *
  226. * @return void
  227. */
  228. public function testDecimalToStatement()
  229. {
  230. $type = Type::build('decimal');
  231. $string = '12.55';
  232. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  233. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  234. }
  235. /**
  236. * Test setting instances into the factory/registry.
  237. *
  238. * @return void
  239. */
  240. public function testSet()
  241. {
  242. $instance = $this->getMockBuilder('Cake\Database\Type')->getMock();
  243. Type::set('random', $instance);
  244. $this->assertSame($instance, Type::build('random'));
  245. }
  246. /**
  247. * @return void
  248. */
  249. public function testDebugInfo()
  250. {
  251. $type = new Type('foo');
  252. $result = $type->__debugInfo();
  253. $expected = [
  254. 'name' => 'foo',
  255. ];
  256. $this->assertEquals($expected, $result);
  257. }
  258. }