TypeTest.php 10 KB

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