TypeTest.php 6.2 KB

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