/tests/TestCase/Database/Type/DateTypeTest.php

https://github.com/LubosRemplik/cakephp · PHP · 244 lines · 151 code · 21 blank · 72 comment · 2 complexity · b6847abc210f2465c86271b325d71b32 MD5 · raw file

  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Type;
  16. use Cake\Chronos\Date;
  17. use Cake\Database\Type\DateType;
  18. use Cake\I18n\Time;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Test for the Date type.
  22. */
  23. class DateTypeTest extends TestCase
  24. {
  25. /**
  26. * @var \Cake\Database\Type\DateType
  27. */
  28. public $type;
  29. /**
  30. * @var \Cake\Database\Driver
  31. */
  32. public $driver;
  33. /**
  34. * Setup
  35. *
  36. * @return void
  37. */
  38. public function setUp()
  39. {
  40. parent::setUp();
  41. $this->type = new DateType();
  42. $this->driver = $this->getMockBuilder('Cake\Database\Driver')->getMock();
  43. }
  44. /**
  45. * Test toPHP
  46. *
  47. * @return void
  48. */
  49. public function testToPHP()
  50. {
  51. $this->assertNull($this->type->toPHP(null, $this->driver));
  52. $this->assertNull($this->type->toPHP('0000-00-00', $this->driver));
  53. $result = $this->type->toPHP('2001-01-04', $this->driver);
  54. $this->assertInstanceOf('DateTime', $result);
  55. $this->assertEquals('2001', $result->format('Y'));
  56. $this->assertEquals('01', $result->format('m'));
  57. $this->assertEquals('04', $result->format('d'));
  58. }
  59. /**
  60. * Test converting string dates to PHP values.
  61. *
  62. * @return void
  63. */
  64. public function testManyToPHP()
  65. {
  66. $values = [
  67. 'a' => null,
  68. 'b' => '2001-01-04',
  69. 'c' => '2001-01-04 12:13:14.12345',
  70. ];
  71. $expected = [
  72. 'a' => null,
  73. 'b' => new Date('2001-01-04'),
  74. 'c' => new Date('2001-01-04'),
  75. ];
  76. $this->assertEquals(
  77. $expected,
  78. $this->type->manyToPHP($values, array_keys($values), $this->driver)
  79. );
  80. }
  81. /**
  82. * Test converting to database format
  83. *
  84. * @return void
  85. */
  86. public function testToDatabase()
  87. {
  88. $value = '2001-01-04';
  89. $result = $this->type->toDatabase($value, $this->driver);
  90. $this->assertEquals($value, $result);
  91. $date = new Time('2013-08-12');
  92. $result = $this->type->toDatabase($date, $this->driver);
  93. $this->assertEquals('2013-08-12', $result);
  94. $date = new Time('2013-08-12 15:16:18');
  95. $result = $this->type->toDatabase($date, $this->driver);
  96. $this->assertEquals('2013-08-12', $result);
  97. }
  98. /**
  99. * Data provider for marshal()
  100. *
  101. * @return array
  102. */
  103. public function marshalProvider()
  104. {
  105. $date = new Date('@1392387900');
  106. return [
  107. // invalid types.
  108. [null, null],
  109. [false, null],
  110. [true, null],
  111. ['', null],
  112. ['derpy', 'derpy'],
  113. ['2013-nope!', '2013-nope!'],
  114. ['14-02-14', '14-02-14'],
  115. ['2014-02-14 13:14:15', '2014-02-14 13:14:15'],
  116. // valid string types
  117. ['1392387900', $date],
  118. [1392387900, $date],
  119. ['2014-02-14', new Date('2014-02-14')],
  120. // valid array types
  121. [
  122. ['year' => '', 'month' => '', 'day' => ''],
  123. null,
  124. ],
  125. [
  126. ['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 13, 'minute' => 14, 'second' => 15],
  127. new Date('2014-02-14')
  128. ],
  129. [
  130. [
  131. 'year' => 2014, 'month' => 2, 'day' => 14,
  132. 'hour' => 1, 'minute' => 14, 'second' => 15,
  133. 'meridian' => 'am'
  134. ],
  135. new Date('2014-02-14')
  136. ],
  137. [
  138. [
  139. 'year' => 2014, 'month' => 2, 'day' => 14,
  140. 'hour' => 1, 'minute' => 14, 'second' => 15,
  141. 'meridian' => 'pm'
  142. ],
  143. new Date('2014-02-14')
  144. ],
  145. [
  146. [
  147. 'year' => 2014, 'month' => 2, 'day' => 14,
  148. ],
  149. new Date('2014-02-14')
  150. ],
  151. // Invalid array types
  152. [
  153. ['year' => 'farts', 'month' => 'derp'],
  154. new Date(date('Y-m-d'))
  155. ],
  156. [
  157. ['year' => 'farts', 'month' => 'derp', 'day' => 'farts'],
  158. new Date(date('Y-m-d'))
  159. ],
  160. [
  161. [
  162. 'year' => '2014', 'month' => '02', 'day' => '14',
  163. 'hour' => 'farts', 'minute' => 'farts'
  164. ],
  165. new Date('2014-02-14')
  166. ],
  167. ];
  168. }
  169. /**
  170. * test marshaling data.
  171. *
  172. * @dataProvider marshalProvider
  173. * @return void
  174. */
  175. public function testMarshal($value, $expected)
  176. {
  177. $result = $this->type->marshal($value);
  178. if (is_object($expected)) {
  179. $this->assertEquals($expected, $result);
  180. } else {
  181. $this->assertSame($expected, $result);
  182. }
  183. }
  184. /**
  185. * Tests marshalling dates using the locale aware parser
  186. *
  187. * @return void
  188. */
  189. public function testMarshalWithLocaleParsing()
  190. {
  191. $this->type->useLocaleParser();
  192. $expected = new Date('13-10-2013');
  193. $result = $this->type->marshal('10/13/2013');
  194. $this->assertEquals($expected->format('Y-m-d'), $result->format('Y-m-d'));
  195. $this->assertNull($this->type->marshal('11/derp/2013'));
  196. }
  197. /**
  198. * Tests marshalling dates using the locale aware parser and custom format
  199. *
  200. * @return void
  201. */
  202. public function testMarshalWithLocaleParsingWithFormat()
  203. {
  204. $this->type->useLocaleParser()->setLocaleFormat('dd MMM, y');
  205. $expected = new Date('13-10-2013');
  206. $result = $this->type->marshal('13 Oct, 2013');
  207. $this->assertEquals($expected->format('Y-m-d'), $result->format('Y-m-d'));
  208. }
  209. /**
  210. * Test that toImmutable changes all the methods to create frozen time instances.
  211. *
  212. * @return void
  213. */
  214. public function testToImmutableAndToMutable()
  215. {
  216. $this->type->useImmutable();
  217. $this->assertInstanceOf('DateTimeImmutable', $this->type->marshal('2015-11-01'));
  218. $this->assertInstanceOf('DateTimeImmutable', $this->type->toPHP('2015-11-01', $this->driver));
  219. $this->type->useMutable();
  220. $this->assertInstanceOf('DateTime', $this->type->marshal('2015-11-01'));
  221. $this->assertInstanceOf('DateTime', $this->type->toPHP('2015-11-01', $this->driver));
  222. }
  223. }