TypeTest.php 6.0 KB

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