TypeTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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\TestSuite\TestCase;
  18. use PDO;
  19. use TestApp\Database\Type\BarType;
  20. use TestApp\Database\Type\FooType;
  21. /**
  22. * Tests Type class
  23. */
  24. class TypeTest extends TestCase
  25. {
  26. /**
  27. * Original type map
  28. *
  29. * @var array
  30. */
  31. protected $_originalMap = [];
  32. /**
  33. * Backup original Type class state
  34. *
  35. * @return void
  36. */
  37. public function setUp()
  38. {
  39. $this->_originalMap = Type::map();
  40. parent::setUp();
  41. }
  42. /**
  43. * Restores Type class state
  44. *
  45. * @return void
  46. */
  47. public function tearDown()
  48. {
  49. parent::tearDown();
  50. Type::map($this->_originalMap);
  51. }
  52. /**
  53. * Tests Type class is able to instantiate basic types
  54. *
  55. * @dataProvider basicTypesProvider
  56. * @return void
  57. */
  58. public function testBuildBasicTypes($name)
  59. {
  60. $type = Type::build($name);
  61. $this->assertInstanceOf('Cake\Database\Type', $type);
  62. $this->assertEquals($name, $type->getName());
  63. $this->assertEquals($name, $type->getBaseType());
  64. }
  65. /**
  66. * provides a basics type list to be used as data provided for a test
  67. *
  68. * @return void
  69. */
  70. public function basicTypesProvider()
  71. {
  72. return [
  73. ['string'],
  74. ['text'],
  75. ['smallinteger'],
  76. ['tinyinteger'],
  77. ['integer'],
  78. ['biginteger'],
  79. ];
  80. }
  81. /**
  82. * Tests trying to build an unknown type throws exception
  83. *
  84. * @return void
  85. */
  86. public function testBuildUnknownType()
  87. {
  88. $this->expectException(\InvalidArgumentException::class);
  89. Type::build('foo');
  90. }
  91. /**
  92. * Tests that once a type with a name is instantiated, the reference is kept
  93. * for future use
  94. *
  95. * @return void
  96. */
  97. public function testInstanceRecycling()
  98. {
  99. $type = Type::build('integer');
  100. $this->assertSame($type, Type::build('integer'));
  101. }
  102. /**
  103. * Tests new types can be registered and built
  104. *
  105. * @return void
  106. */
  107. public function testMapAndBuild()
  108. {
  109. $map = Type::map();
  110. $this->assertNotEmpty($map);
  111. $this->assertFalse(isset($map['foo']));
  112. $fooType = FooType::class;
  113. Type::map('foo', $fooType);
  114. $map = Type::map();
  115. $this->assertEquals($fooType, $map['foo']);
  116. $this->assertEquals($fooType, Type::map('foo'));
  117. $type = Type::build('foo');
  118. $this->assertInstanceOf($fooType, $type);
  119. $this->assertEquals('foo', $type->getName());
  120. $this->assertEquals('text', $type->getBaseType());
  121. Type::map('foo2', $fooType);
  122. $map = Type::map();
  123. $this->assertSame($fooType, $map['foo2']);
  124. $this->assertSame($fooType, Type::map('foo2'));
  125. $type = Type::build('foo2');
  126. $this->assertInstanceOf($fooType, $type);
  127. }
  128. /**
  129. * Tests overwriting type map works for building
  130. *
  131. * @return void
  132. */
  133. public function testReMapAndBuild()
  134. {
  135. $fooType = FooType::class;
  136. $map = Type::map('foo', $fooType);
  137. $type = Type::build('foo');
  138. $this->assertInstanceOf($fooType, $type);
  139. $barType = BarType::class;
  140. Type::map('foo', $barType);
  141. $type = Type::build('foo');
  142. $this->assertInstanceOf($barType, $type);
  143. }
  144. /**
  145. * Tests clear function in conjunction with map
  146. *
  147. * @return void
  148. */
  149. public function testClear()
  150. {
  151. $map = Type::map();
  152. $this->assertNotEmpty($map);
  153. $type = Type::build('float');
  154. Type::clear();
  155. $this->assertEmpty(Type::map());
  156. Type::map($map);
  157. $this->assertEquals($map, Type::map());
  158. $this->assertNotSame($type, Type::build('float'));
  159. }
  160. /**
  161. * Tests bigintegers from database are converted correctly to PHP
  162. *
  163. * @return void
  164. */
  165. public function testBigintegerToPHP()
  166. {
  167. $this->skipIf(
  168. PHP_INT_SIZE === 4,
  169. 'This test requires a php version compiled for 64 bits'
  170. );
  171. $type = Type::build('biginteger');
  172. $integer = time() * time();
  173. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  174. $this->assertSame($integer, $type->toPHP($integer, $driver));
  175. $this->assertSame($integer, $type->toPHP('' . $integer, $driver));
  176. $this->assertSame(3, $type->toPHP(3.57, $driver));
  177. }
  178. /**
  179. * Tests bigintegers from PHP are converted correctly to statement value
  180. *
  181. * @return void
  182. */
  183. public function testBigintegerToStatement()
  184. {
  185. $type = Type::build('biginteger');
  186. $integer = time() * time();
  187. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  188. $this->assertEquals(PDO::PARAM_INT, $type->toStatement($integer, $driver));
  189. }
  190. /**
  191. * Tests decimal from database are converted correctly to PHP
  192. *
  193. * @return void
  194. */
  195. public function testDecimalToPHP()
  196. {
  197. $type = Type::build('decimal');
  198. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  199. $this->assertSame(3.14159, $type->toPHP('3.14159', $driver));
  200. $this->assertSame(3.14159, $type->toPHP(3.14159, $driver));
  201. $this->assertSame(3.0, $type->toPHP(3, $driver));
  202. $this->assertSame(1.0, $type->toPHP(['3', '4'], $driver));
  203. }
  204. /**
  205. * Tests integers from PHP are converted correctly to statement value
  206. *
  207. * @return void
  208. */
  209. public function testDecimalToStatement()
  210. {
  211. $type = Type::build('decimal');
  212. $string = '12.55';
  213. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  214. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  215. }
  216. /**
  217. * Test setting instances into the factory/registry.
  218. *
  219. * @return void
  220. */
  221. public function testSet()
  222. {
  223. $instance = $this->getMockBuilder('Cake\Database\Type')->getMock();
  224. Type::set('random', $instance);
  225. $this->assertSame($instance, Type::build('random'));
  226. }
  227. }