TypeTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. /**
  26. * Tests Type class
  27. */
  28. class TypeTest extends TestCase {
  29. /**
  30. * Original type map
  31. *
  32. * @var array
  33. */
  34. protected $_originalMap = array();
  35. /**
  36. * Backup original Type class state
  37. *
  38. * @return void
  39. */
  40. public function setUp() {
  41. $this->_originalMap = Type::map();
  42. parent::setUp();
  43. }
  44. /**
  45. * Restores Type class state
  46. *
  47. * @return void
  48. */
  49. public function tearDown() {
  50. parent::tearDown();
  51. Type::map($this->_originalMap);
  52. }
  53. /**
  54. * Tests Type class is able to instantiate basic types
  55. *
  56. * @dataProvider basicTypesProvider
  57. * @return void
  58. */
  59. public function testBuildBasicTypes($name) {
  60. $type = Type::build($name);
  61. $this->assertInstanceOf('\Cake\Database\Type', $type);
  62. $this->assertEquals($name, $type->getName());
  63. }
  64. /**
  65. * provides a basics type list to be used as data provided for a test
  66. *
  67. * @return void
  68. */
  69. public function basicTypesProvider() {
  70. return array(
  71. array('float'),
  72. array('integer'),
  73. array('string'),
  74. array('text'),
  75. array('boolean')
  76. );
  77. }
  78. /**
  79. * Tests trying to build an unknown type throws exception
  80. *
  81. * @expectedException InvalidArgumentException
  82. * @return void
  83. */
  84. public function testBuildUnknownType() {
  85. Type::build('foo');
  86. }
  87. /**
  88. * Tests that once a type with a name is instantiated, the reference is kept
  89. * for future use
  90. *
  91. * @return void
  92. */
  93. public function testInstanceRecycling() {
  94. $type = Type::build('integer');
  95. $this->assertSame($type, Type::build('integer'));
  96. }
  97. /**
  98. * Tests new types can be registered and built
  99. *
  100. * @return void
  101. */
  102. public function testMapAndBuild() {
  103. $map = Type::map();
  104. $this->assertNotEmpty($map);
  105. $this->assertFalse(isset($map['foo']));
  106. $fooType = __NAMESPACE__ . '\FooType';
  107. Type::map('foo', $fooType);
  108. $map = Type::map();
  109. $this->assertEquals($fooType, $map['foo']);
  110. $this->assertEquals($fooType, Type::map('foo'));
  111. $type = Type::build('foo');
  112. $this->assertInstanceOf($fooType, $type);
  113. $this->assertEquals('foo', $type->getName());
  114. }
  115. /**
  116. * Tests clear function in conjunction with map
  117. *
  118. * @return void
  119. */
  120. public function testClear() {
  121. $map = Type::map();
  122. $this->assertNotEmpty($map);
  123. $type = Type::build('float');
  124. Type::clear();
  125. $this->assertEmpty(Type::map());
  126. Type::map($map);
  127. $this->assertEquals($map, Type::map());
  128. $this->assertNotSame($type, Type::build('float'));
  129. }
  130. /**
  131. * Tests floats from database are converted correctly to PHP
  132. *
  133. * @return void
  134. */
  135. public function testFloatToPHP() {
  136. $type = Type::build('float');
  137. $float = '3.14159';
  138. $driver = $this->getMock('\Cake\Database\Driver');
  139. $this->assertEquals(3.14159, $type->toPHP($float, $driver));
  140. $this->assertEquals(3.14159, $type->toPHP(3.14159, $driver));
  141. $this->assertEquals(3.00, $type->toPHP(3, $driver));
  142. }
  143. /**
  144. * Tests floats from PHP are converted correctly to statement value
  145. *
  146. * @return void
  147. */
  148. public function testFloatToStatement() {
  149. $type = Type::build('float');
  150. $float = '3.14159';
  151. $driver = $this->getMock('\Cake\Database\Driver');
  152. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($float, $driver));
  153. }
  154. /**
  155. * Tests integers from database are converted correctly to PHP
  156. *
  157. * @return void
  158. */
  159. public function testIntegerToPHP() {
  160. $type = Type::build('integer');
  161. $integer = '3';
  162. $driver = $this->getMock('\Cake\Database\Driver');
  163. $this->assertEquals(3, $type->toPHP($integer, $driver));
  164. $this->assertEquals(3, $type->toPHP(3, $driver));
  165. $this->assertEquals(3, $type->toPHP(3.57, $driver));
  166. }
  167. /**
  168. * Tests integers from PHP are converted correctly to statement value
  169. *
  170. * @return void
  171. */
  172. public function testIntegerToStatement() {
  173. $type = Type::build('integer');
  174. $integer = '3';
  175. $driver = $this->getMock('\Cake\Database\Driver');
  176. $this->assertEquals(PDO::PARAM_INT, $type->toStatement($integer, $driver));
  177. }
  178. /**
  179. * Tests bigintegers from database are converted correctly to PHP
  180. *
  181. * @return void
  182. */
  183. public function testBigintegerToPHP() {
  184. $this->skipIf(
  185. PHP_INT_SIZE === 4,
  186. 'This test requires a php version compiled for 64 bits'
  187. );
  188. $type = Type::build('biginteger');
  189. $integer = time() * time();
  190. $driver = $this->getMock('\Cake\Database\Driver');
  191. $this->assertSame($integer, $type->toPHP($integer, $driver));
  192. $this->assertSame($integer, $type->toPHP('' . $integer, $driver));
  193. $this->assertSame(3, $type->toPHP(3.57, $driver));
  194. }
  195. /**
  196. * Tests bigintegers from PHP are converted correctly to statement value
  197. *
  198. * @return void
  199. */
  200. public function testBigintegerToStatement() {
  201. $type = Type::build('biginteger');
  202. $integer = time() * time();
  203. $driver = $this->getMock('\Cake\Database\Driver');
  204. $this->assertEquals(PDO::PARAM_INT, $type->toStatement($integer, $driver));
  205. }
  206. /**
  207. * Tests string from database are converted correctly to PHP
  208. *
  209. * @return void
  210. */
  211. public function testStringToPHP() {
  212. $type = Type::build('string');
  213. $string = 'foo';
  214. $driver = $this->getMock('\Cake\Database\Driver');
  215. $this->assertEquals('foo', $type->toPHP($string, $driver));
  216. $this->assertEquals('3', $type->toPHP(3, $driver));
  217. $this->assertEquals('3.14159', $type->toPHP(3.14159, $driver));
  218. }
  219. /**
  220. * Tests integers from PHP are converted correctly to statement value
  221. *
  222. * @return void
  223. */
  224. public function testStringToStatement() {
  225. $type = Type::build('string');
  226. $string = '3';
  227. $driver = $this->getMock('\Cake\Database\Driver');
  228. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  229. }
  230. /**
  231. * Tests integers from database are converted correctly to PHP
  232. *
  233. * @return void
  234. */
  235. public function testTextToPHP() {
  236. $type = Type::build('string');
  237. $string = 'foo';
  238. $driver = $this->getMock('\Cake\Database\Driver');
  239. $this->assertEquals('foo', $type->toPHP($string, $driver));
  240. $this->assertEquals('3', $type->toPHP(3, $driver));
  241. $this->assertEquals('3.14159', $type->toPHP(3.14159, $driver));
  242. }
  243. /**
  244. * Tests integers from PHP are converted correctly to statement value
  245. *
  246. * @return void
  247. */
  248. public function testTextToStatement() {
  249. $type = Type::build('string');
  250. $string = '3';
  251. $driver = $this->getMock('\Cake\Database\Driver');
  252. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  253. }
  254. /**
  255. * Test convertring booleans to database types.
  256. *
  257. * @return void
  258. */
  259. public function testBooleanToDatabase() {
  260. $type = Type::build('boolean');
  261. $driver = $this->getMock('\Cake\Database\Driver');
  262. $this->assertTrue($type->toDatabase(true, $driver));
  263. $driver = $this->getMock('\Cake\Database\Driver');
  264. $this->assertFalse($type->toDatabase(false, $driver));
  265. $driver = $this->getMock('\Cake\Database\Driver');
  266. $this->assertTrue($type->toDatabase(1, $driver));
  267. $driver = $this->getMock('\Cake\Database\Driver');
  268. $this->assertFalse($type->toDatabase(0, $driver));
  269. $driver = $this->getMock('\Cake\Database\Driver');
  270. $this->assertTrue($type->toDatabase('1', $driver));
  271. $driver = $this->getMock('\Cake\Database\Driver');
  272. $this->assertFalse($type->toDatabase('0', $driver));
  273. }
  274. /**
  275. * Test convertring booleans to PDO types.
  276. *
  277. * @return void
  278. */
  279. public function testBooleanToStatement() {
  280. $type = Type::build('boolean');
  281. $driver = $this->getMock('\Cake\Database\Driver');
  282. $this->assertEquals(PDO::PARAM_BOOL, $type->toStatement(true, $driver));
  283. $driver = $this->getMock('\Cake\Database\Driver');
  284. $this->assertEquals(PDO::PARAM_BOOL, $type->toStatement(false, $driver));
  285. }
  286. /**
  287. * Test convertring string booleans to PHP values.
  288. *
  289. * @return void
  290. */
  291. public function testBooleanToPHP() {
  292. $type = Type::build('boolean');
  293. $driver = $this->getMock('\Cake\Database\Driver');
  294. $this->assertTrue($type->toPHP(true, $driver));
  295. $this->assertTrue($type->toPHP(1, $driver));
  296. $this->assertTrue($type->toPHP('1', $driver));
  297. $this->assertTrue($type->toPHP('TRUE', $driver));
  298. $this->assertTrue($type->toPHP('true', $driver));
  299. $this->assertFalse($type->toPHP(false, $driver));
  300. $this->assertFalse($type->toPHP(0, $driver));
  301. $this->assertFalse($type->toPHP('0', $driver));
  302. $this->assertFalse($type->toPHP('FALSE', $driver));
  303. $this->assertFalse($type->toPHP('false', $driver));
  304. }
  305. /**
  306. * Tests uuid from database are converted correctly to PHP
  307. *
  308. * @return void
  309. */
  310. public function testUuidToPHP() {
  311. $type = Type::build('uuid');
  312. $string = 'abc123-de456-fg789';
  313. $driver = $this->getMock('\Cake\Database\Driver');
  314. $this->assertEquals($string, $type->toPHP($string, $driver));
  315. $this->assertEquals('3', $type->toPHP(3, $driver));
  316. $this->assertEquals('3.14159', $type->toPHP(3.14159, $driver));
  317. }
  318. /**
  319. * Tests integers from PHP are converted correctly to statement value
  320. *
  321. * @return void
  322. */
  323. public function testUuidToStatement() {
  324. $type = Type::build('uuid');
  325. $string = 'abc123-def456-ghi789';
  326. $driver = $this->getMock('\Cake\Database\Driver');
  327. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  328. }
  329. /**
  330. * Tests decimal from database are converted correctly to PHP
  331. *
  332. * @return void
  333. */
  334. public function testDecimalToPHP() {
  335. $type = Type::build('decimal');
  336. $driver = $this->getMock('\Cake\Database\Driver');
  337. $this->assertSame(3.14159, $type->toPHP('3.14159', $driver));
  338. $this->assertSame(3.14159, $type->toPHP(3.14159, $driver));
  339. $this->assertSame(3.0, $type->toPHP(3, $driver));
  340. }
  341. /**
  342. * Tests integers from PHP are converted correctly to statement value
  343. *
  344. * @return void
  345. */
  346. public function testDecimalToStatement() {
  347. $type = Type::build('decimal');
  348. $string = '12.55';
  349. $driver = $this->getMock('\Cake\Database\Driver');
  350. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  351. }
  352. }