TypeTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. $type = Type::build('biginteger');
  183. $integer = time() * time();
  184. $driver = $this->getMock('\Cake\Database\Driver');
  185. $this->assertSame($integer, $type->toPHP($integer, $driver));
  186. $this->assertSame($integer, $type->toPHP('' . $integer, $driver));
  187. $this->assertSame(3, $type->toPHP(3.57, $driver));
  188. }
  189. /**
  190. * Tests bigintegers from PHP are converted correctly to statement value
  191. *
  192. * @return void
  193. */
  194. public function testBigintegerToStatement() {
  195. $type = Type::build('biginteger');
  196. $integer = time() * time();
  197. $driver = $this->getMock('\Cake\Database\Driver');
  198. $this->assertEquals(PDO::PARAM_INT, $type->toStatement($integer, $driver));
  199. }
  200. /**
  201. * Tests string from database are converted correctly to PHP
  202. *
  203. * @return void
  204. */
  205. public function testStringToPHP() {
  206. $type = Type::build('string');
  207. $string = 'foo';
  208. $driver = $this->getMock('\Cake\Database\Driver');
  209. $this->assertEquals('foo', $type->toPHP($string, $driver));
  210. $this->assertEquals('3', $type->toPHP(3, $driver));
  211. $this->assertEquals('3.14159', $type->toPHP(3.14159, $driver));
  212. }
  213. /**
  214. * Tests integers from PHP are converted correctly to statement value
  215. *
  216. * @return void
  217. */
  218. public function testStringToStatement() {
  219. $type = Type::build('string');
  220. $string = '3';
  221. $driver = $this->getMock('\Cake\Database\Driver');
  222. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  223. }
  224. /**
  225. * Tests integers from database are converted correctly to PHP
  226. *
  227. * @return void
  228. */
  229. public function testTextToPHP() {
  230. $type = Type::build('string');
  231. $string = 'foo';
  232. $driver = $this->getMock('\Cake\Database\Driver');
  233. $this->assertEquals('foo', $type->toPHP($string, $driver));
  234. $this->assertEquals('3', $type->toPHP(3, $driver));
  235. $this->assertEquals('3.14159', $type->toPHP(3.14159, $driver));
  236. }
  237. /**
  238. * Tests integers from PHP are converted correctly to statement value
  239. *
  240. * @return void
  241. */
  242. public function testTextToStatement() {
  243. $type = Type::build('string');
  244. $string = '3';
  245. $driver = $this->getMock('\Cake\Database\Driver');
  246. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  247. }
  248. /**
  249. * Test convertring booleans to database types.
  250. *
  251. * @return void
  252. */
  253. public function testBooleanToDatabase() {
  254. $type = Type::build('boolean');
  255. $driver = $this->getMock('\Cake\Database\Driver');
  256. $this->assertTrue($type->toDatabase(true, $driver));
  257. $driver = $this->getMock('\Cake\Database\Driver');
  258. $this->assertFalse($type->toDatabase(false, $driver));
  259. $driver = $this->getMock('\Cake\Database\Driver');
  260. $this->assertTrue($type->toDatabase(1, $driver));
  261. $driver = $this->getMock('\Cake\Database\Driver');
  262. $this->assertFalse($type->toDatabase(0, $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. }
  268. /**
  269. * Test convertring booleans to PDO types.
  270. *
  271. * @return void
  272. */
  273. public function testBooleanToStatement() {
  274. $type = Type::build('boolean');
  275. $driver = $this->getMock('\Cake\Database\Driver');
  276. $this->assertEquals(PDO::PARAM_BOOL, $type->toStatement(true, $driver));
  277. $driver = $this->getMock('\Cake\Database\Driver');
  278. $this->assertEquals(PDO::PARAM_BOOL, $type->toStatement(false, $driver));
  279. }
  280. /**
  281. * Test convertring string booleans to PHP values.
  282. *
  283. * @return void
  284. */
  285. public function testBooleanToPHP() {
  286. $type = Type::build('boolean');
  287. $driver = $this->getMock('\Cake\Database\Driver');
  288. $this->assertTrue($type->toPHP(true, $driver));
  289. $this->assertTrue($type->toPHP(1, $driver));
  290. $this->assertTrue($type->toPHP('1', $driver));
  291. $this->assertTrue($type->toPHP('TRUE', $driver));
  292. $this->assertTrue($type->toPHP('true', $driver));
  293. $this->assertFalse($type->toPHP(false, $driver));
  294. $this->assertFalse($type->toPHP(0, $driver));
  295. $this->assertFalse($type->toPHP('0', $driver));
  296. $this->assertFalse($type->toPHP('FALSE', $driver));
  297. $this->assertFalse($type->toPHP('false', $driver));
  298. }
  299. /**
  300. * Tests uuid from database are converted correctly to PHP
  301. *
  302. * @return void
  303. */
  304. public function testUuidToPHP() {
  305. $type = Type::build('uuid');
  306. $string = 'abc123-de456-fg789';
  307. $driver = $this->getMock('\Cake\Database\Driver');
  308. $this->assertEquals($string, $type->toPHP($string, $driver));
  309. $this->assertEquals('3', $type->toPHP(3, $driver));
  310. $this->assertEquals('3.14159', $type->toPHP(3.14159, $driver));
  311. }
  312. /**
  313. * Tests integers from PHP are converted correctly to statement value
  314. *
  315. * @return void
  316. */
  317. public function testUuidToStatement() {
  318. $type = Type::build('uuid');
  319. $string = 'abc123-def456-ghi789';
  320. $driver = $this->getMock('\Cake\Database\Driver');
  321. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  322. }
  323. /**
  324. * Tests decimal from database are converted correctly to PHP
  325. *
  326. * @return void
  327. */
  328. public function testDecimalToPHP() {
  329. $type = Type::build('decimal');
  330. $driver = $this->getMock('\Cake\Database\Driver');
  331. $this->assertSame(3.14159, $type->toPHP('3.14159', $driver));
  332. $this->assertSame(3.14159, $type->toPHP(3.14159, $driver));
  333. $this->assertSame(3.0, $type->toPHP(3, $driver));
  334. }
  335. /**
  336. * Tests integers from PHP are converted correctly to statement value
  337. *
  338. * @return void
  339. */
  340. public function testDecimalToStatement() {
  341. $type = Type::build('decimal');
  342. $string = '12.55';
  343. $driver = $this->getMock('\Cake\Database\Driver');
  344. $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
  345. }
  346. }