TypeTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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. ];
  84. }
  85. /**
  86. * Tests trying to build an unknown type throws exception
  87. *
  88. * @expectedException \InvalidArgumentException
  89. * @return void
  90. */
  91. public function testBuildUnknownType()
  92. {
  93. Type::build('foo');
  94. }
  95. /**
  96. * Tests that once a type with a name is instantiated, the reference is kept
  97. * for future use
  98. *
  99. * @return void
  100. */
  101. public function testInstanceRecycling()
  102. {
  103. $type = Type::build('integer');
  104. $this->assertSame($type, Type::build('integer'));
  105. }
  106. /**
  107. * Tests new types can be registered and built
  108. *
  109. * @return void
  110. */
  111. public function testMapAndBuild()
  112. {
  113. $map = Type::map();
  114. $this->assertNotEmpty($map);
  115. $this->assertFalse(isset($map['foo']));
  116. $fooType = __NAMESPACE__ . '\FooType';
  117. Type::map('foo', $fooType);
  118. $map = Type::map();
  119. $this->assertEquals($fooType, $map['foo']);
  120. $this->assertEquals($fooType, Type::map('foo'));
  121. $type = Type::build('foo');
  122. $this->assertInstanceOf($fooType, $type);
  123. $this->assertEquals('foo', $type->getName());
  124. $this->assertEquals('text', $type->getBaseType());
  125. $fooType = new FooType();
  126. Type::map('foo2', $fooType);
  127. $map = Type::map();
  128. $this->assertSame($fooType, $map['foo2']);
  129. $this->assertSame($fooType, Type::map('foo2'));
  130. }
  131. /**
  132. * Tests clear function in conjunction with map
  133. *
  134. * @return void
  135. */
  136. public function testClear()
  137. {
  138. $map = Type::map();
  139. $this->assertNotEmpty($map);
  140. $type = Type::build('float');
  141. Type::clear();
  142. $this->assertEmpty(Type::map());
  143. Type::map($map);
  144. $this->assertEquals($map, Type::map());
  145. $this->assertNotSame($type, Type::build('float'));
  146. }
  147. /**
  148. * Tests bigintegers from database are converted correctly to PHP
  149. *
  150. * @return void
  151. */
  152. public function testBigintegerToPHP()
  153. {
  154. $this->skipIf(
  155. PHP_INT_SIZE === 4,
  156. 'This test requires a php version compiled for 64 bits'
  157. );
  158. $type = Type::build('biginteger');
  159. $integer = time() * time();
  160. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  161. $this->assertSame($integer, $type->toPHP($integer, $driver));
  162. $this->assertSame($integer, $type->toPHP('' . $integer, $driver));
  163. $this->assertSame(3, $type->toPHP(3.57, $driver));
  164. }
  165. /**
  166. * Tests bigintegers from PHP are converted correctly to statement value
  167. *
  168. * @return void
  169. */
  170. public function testBigintegerToStatement()
  171. {
  172. $type = Type::build('biginteger');
  173. $integer = time() * time();
  174. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  175. $this->assertEquals(PDO::PARAM_INT, $type->toStatement($integer, $driver));
  176. }
  177. /**
  178. * Tests decimal from database are converted correctly to PHP
  179. *
  180. * @return void
  181. */
  182. public function testDecimalToPHP()
  183. {
  184. $type = Type::build('decimal');
  185. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  186. $this->assertSame(3.14159, $type->toPHP('3.14159', $driver));
  187. $this->assertSame(3.14159, $type->toPHP(3.14159, $driver));
  188. $this->assertSame(3.0, $type->toPHP(3, $driver));
  189. $this->assertSame(1.0, $type->toPHP(['3', '4'], $driver));
  190. }
  191. /**
  192. * Tests integers from PHP are converted correctly to statement value
  193. *
  194. * @return void
  195. */
  196. public function testDecimalToStatement()
  197. {
  198. $type = Type::build('decimal');
  199. $string = '12.55';
  200. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  201. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  202. }
  203. /**
  204. * Test setting instances into the factory/registry.
  205. *
  206. * @return void
  207. */
  208. public function testSet()
  209. {
  210. $instance = $this->getMockBuilder('Cake\Database\Type')->getMock();
  211. Type::set('random', $instance);
  212. $this->assertSame($instance, Type::build('random'));
  213. }
  214. }