ToArrayTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of the Carbon package.
  5. *
  6. * (c) Brian Nesbitt <brian@nesbot.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Tests\CarbonPeriod;
  12. use Carbon\Carbon;
  13. use Carbon\CarbonImmutable;
  14. use Carbon\CarbonInterval;
  15. use Carbon\CarbonPeriod;
  16. use Carbon\Exceptions\EndLessPeriodException;
  17. use DateTimeInterface;
  18. use Tests\AbstractTestCase;
  19. use Tests\CarbonPeriod\Fixtures\CarbonPeriodFactory;
  20. class ToArrayTest extends AbstractTestCase
  21. {
  22. public function testToArrayIsNotEmptyArray()
  23. {
  24. $result = CarbonPeriodFactory::withEvenDaysFilter($this->periodClass)->toArray();
  25. $this->assertIsArray($result);
  26. $this->assertNotEmpty($result);
  27. }
  28. public function testToArrayHasCorrectCount()
  29. {
  30. $period = CarbonPeriodFactory::withEvenDaysFilter($this->periodClass);
  31. $this->assertCount(3, $period->toArray());
  32. }
  33. public function testToArrayValuesAreCarbonInstances()
  34. {
  35. $result = CarbonPeriodFactory::withEvenDaysFilter($this->periodClass)->toArray();
  36. foreach ($result as $key => $current) {
  37. $this->assertInstanceOfCarbon($current);
  38. }
  39. }
  40. public function testToArrayKeysAreSequential()
  41. {
  42. $result = CarbonPeriodFactory::withEvenDaysFilter($this->periodClass)->toArray();
  43. $this->assertSame([0, 1, 2], array_keys($result));
  44. }
  45. public function testToArrayHasCorrectValues()
  46. {
  47. $result = CarbonPeriodFactory::withEvenDaysFilter($this->periodClass)->toArray();
  48. $this->assertSame(
  49. $this->standardizeDates(['2012-07-04', '2012-07-10', '2012-07-16']),
  50. $this->standardizeDates($result)
  51. );
  52. }
  53. public function testJsonSerialize()
  54. {
  55. $result = json_encode(CarbonPeriodFactory::withEvenDaysFilter($this->periodClass));
  56. $this->assertSame('["2012-07-04T04:00:00.000000Z","2012-07-10T04:00:00.000000Z","2012-07-16T04:00:00.000000Z"]', $result);
  57. }
  58. public function testCountEmptyPeriod()
  59. {
  60. $periodClass = $this->periodClass;
  61. $assertThrow = function (CarbonPeriod $period) {
  62. $message = null;
  63. try {
  64. $period->count();
  65. } catch (EndLessPeriodException $exception) {
  66. $message = $exception->getMessage();
  67. }
  68. $this->assertSame("Endless period can't be converted to array nor counted.", $message);
  69. };
  70. $period = new $periodClass();
  71. $this->assertTrue($period->isUnfilteredAndEndLess());
  72. $assertThrow($period);
  73. $this->assertSame(Carbon::now()->format('Y-m-d H:i:s'), $period->first()->format('Y-m-d H:i:s'));
  74. foreach ($period as $date) {
  75. break;
  76. }
  77. $this->assertInstanceOfCarbon($date ?? null);
  78. $this->assertSame(Carbon::now()->format('Y-m-d H:i:s'), $date->format('Y-m-d H:i:s'));
  79. $period = new $periodClass(INF);
  80. $this->assertTrue($period->isUnfilteredAndEndLess());
  81. $assertThrow($period);
  82. $period = new $periodClass('2022-05-18', '1 day');
  83. $this->assertTrue($period->isUnfilteredAndEndLess());
  84. $assertThrow($period);
  85. $period = new $periodClass('2022-05-18', '1 day', CarbonImmutable::endOfTime());
  86. $this->assertTrue($period->isUnfilteredAndEndLess());
  87. $assertThrow($period);
  88. $period = new $periodClass('2022-05-18', '1 day');
  89. $period->setDateClass(CarbonImmutable::class);
  90. $period->setEndDate(CarbonImmutable::endOfTime());
  91. $this->assertTrue($period->isUnfilteredAndEndLess());
  92. $assertThrow($period);
  93. $period = new $periodClass(3);
  94. $this->assertFalse($period->isUnfilteredAndEndLess());
  95. $this->assertSame(3, $period->count());
  96. $period = new $periodClass('2022-05-18', '2022-05-23');
  97. $this->assertFalse($period->isUnfilteredAndEndLess());
  98. $this->assertSame(6, $period->count());
  99. $period = new $periodClass('2022-05-18', INF);
  100. $period = $period->addFilter(static function (DateTimeInterface $date) use ($periodClass) {
  101. if ($date->format('Y-m-d') > '2022-05-20') {
  102. return $periodClass::END_ITERATION;
  103. }
  104. return true;
  105. });
  106. $this->assertFalse($period->isUnfilteredAndEndLess());
  107. $this->assertSame(3, $period->count());
  108. }
  109. public function testCountByMethod()
  110. {
  111. $period = CarbonPeriodFactory::withEvenDaysFilter($this->periodClass);
  112. $this->assertSame(3, $period->count());
  113. }
  114. public function testCountByFunction()
  115. {
  116. $period = CarbonPeriodFactory::withEvenDaysFilter($this->periodClass);
  117. $this->assertCount(3, $period);
  118. }
  119. public function testFirst()
  120. {
  121. $period = CarbonPeriodFactory::withEvenDaysFilter($this->periodClass);
  122. $this->assertEquals(new Carbon('2012-07-04'), $period->first());
  123. }
  124. public function testLast()
  125. {
  126. $period = CarbonPeriodFactory::withEvenDaysFilter($this->periodClass);
  127. $this->assertEquals(new Carbon('2012-07-16'), $period->last());
  128. }
  129. public function testToArrayOfEmptyPeriod()
  130. {
  131. $periodClass = $this->periodClass;
  132. $result = $periodClass::create(0)->toArray();
  133. $this->assertIsArray($result);
  134. $this->assertEmpty($result);
  135. }
  136. public function testCountOfEmptyPeriod()
  137. {
  138. $periodClass = $this->periodClass;
  139. $period = $periodClass::create(0);
  140. $this->assertSame(0, $period->count());
  141. }
  142. public function testFirstOfEmptyPeriod()
  143. {
  144. $periodClass = $this->periodClass;
  145. $period = $periodClass::create(0);
  146. $this->assertNull($period->first());
  147. $period = new class(0) extends CarbonPeriod {
  148. public function isUnfilteredAndEndLess(): bool
  149. {
  150. return true;
  151. }
  152. };
  153. $this->assertNull($period->first());
  154. }
  155. public function testLastOfEmptyPeriod()
  156. {
  157. $periodClass = $this->periodClass;
  158. $period = $periodClass::create(0);
  159. $this->assertNull($period->last());
  160. }
  161. public function testRestoreIterationStateAfterCallingToArray()
  162. {
  163. $period = CarbonPeriodFactory::withEvenDaysFilter($this->periodClass);
  164. $key = $period->key();
  165. $current = $period->current();
  166. $this->assertSame(0, $key);
  167. $this->assertEquals(new Carbon('2012-07-04'), $current);
  168. $period->next();
  169. $this->assertSame(
  170. $this->standardizeDates(['2012-07-04', '2012-07-10', '2012-07-16']),
  171. $this->standardizeDates($period->toArray())
  172. );
  173. $this->assertSame(1, $period->key());
  174. $this->assertEquals(new Carbon('2012-07-10'), $period->current());
  175. $period->next();
  176. $this->assertSame(2, $period->key());
  177. $this->assertEquals(new Carbon('2012-07-16'), $period->current());
  178. }
  179. public function testToArrayResultsAreInTheExpectedTimezone()
  180. {
  181. $periodClass = $this->periodClass;
  182. $period = $periodClass::create('2018-05-13 12:00 Asia/Kabul', 'PT1H', 3);
  183. $expected = [
  184. '2018-05-13 12:00:00 +04:30',
  185. '2018-05-13 13:00:00 +04:30',
  186. '2018-05-13 14:00:00 +04:30',
  187. ];
  188. $this->assertSame($expected, $this->standardizeDates($period->toArray()));
  189. }
  190. public function testDebugInfo()
  191. {
  192. $periodClass = $this->periodClass;
  193. $period = $periodClass::create('2018-05-13 12:00 Asia/Kabul', 'PT1H', 3);
  194. $expected = [
  195. 'dateClass' => Carbon::class,
  196. 'dateInterval' => CarbonInterval::hour(),
  197. 'filters' => [
  198. [
  199. $periodClass::RECURRENCES_FILTER,
  200. null,
  201. ],
  202. ],
  203. 'startDate' => Carbon::parse('2018-05-13 12:00 Asia/Kabul'),
  204. 'recurrences' => 3,
  205. ];
  206. $this->assertEquals($expected, $period->__debugInfo());
  207. }
  208. }