TypeTest.php 10 KB

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