ToDatePeriodTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\Translator;
  16. use DatePeriod;
  17. use DateTime;
  18. use DateTimeImmutable;
  19. use ReflectionMethod;
  20. use Tests\AbstractTestCase;
  21. class ToDatePeriodTest extends AbstractTestCase
  22. {
  23. public function testToArrayIsNotEmptyArray()
  24. {
  25. $periodClass = $this->periodClass;
  26. $period = $periodClass::create('2021-01-05', '2021-02-15');
  27. $result = $period->toDatePeriod();
  28. $this->assertFalse($period->isEndExcluded());
  29. $this->assertSame(DatePeriod::class, \get_class($result));
  30. $this->assertSame('2021-01-05', $result->getStartDate()->format('Y-m-d'));
  31. $this->assertSame('2021-02-15', $result->getEndDate()->format('Y-m-d'));
  32. // CarbonPeriod includes end date by default while DatePeriod will always exclude it
  33. $dates = iterator_to_array($result);
  34. $this->assertSame('2021-02-14', end($dates)->format('Y-m-d'));
  35. $this->assertTrue($period->equalTo($periodClass::instance($result)));
  36. $period = $periodClass::create('2021-01-05', '2021-02-15', $periodClass::EXCLUDE_END_DATE);
  37. $result = $period->toDatePeriod();
  38. $newInstance = $periodClass::instance($result);
  39. $this->assertTrue($period->isEndExcluded());
  40. $this->assertSame(DatePeriod::class, \get_class($result));
  41. $this->assertSame('2021-01-05', $result->getStartDate()->format('Y-m-d'));
  42. $this->assertSame('2021-02-14', $result->getEndDate()->format('Y-m-d'));
  43. $dates = iterator_to_array($result);
  44. $this->assertSame('2021-02-13', end($dates)->format('Y-m-d'));
  45. $this->assertSame('2021-01-05', $newInstance->getStartDate()->format('Y-m-d'));
  46. $this->assertSame('2021-02-14', $newInstance->getEndDate()->format('Y-m-d'));
  47. $period = $periodClass::create('2021-01-05', 3);
  48. $result = $period->toDatePeriod();
  49. $newInstance = $periodClass::instance($result);
  50. $this->assertSame(DatePeriod::class, \get_class($result));
  51. $this->assertSame('2021-01-05', $result->getStartDate()->format('Y-m-d'));
  52. $this->assertNull($result->getEndDate());
  53. $dates = iterator_to_array($result);
  54. $this->assertSame('2021-01-08', end($dates)->format('Y-m-d'));
  55. $this->assertSame('2021-01-05', $newInstance->getStartDate()->format('Y-m-d'));
  56. $this->assertSame(3, $newInstance->getRecurrences());
  57. }
  58. public function testWithIntervalLocalized()
  59. {
  60. CarbonInterval::setLocale('fr');
  61. $periodClass = $this->periodClass;
  62. $period = $periodClass::create('2021-01-05', 3);
  63. $result = $period->floor()->toDatePeriod();
  64. $this->assertSame(DatePeriod::class, \get_class($result));
  65. $this->assertSame('2021-01-05', $result->getStartDate()->format('Y-m-d'));
  66. $this->assertNull($result->getEndDate());
  67. if (method_exists($result, 'getRecurrences')) {
  68. $this->assertSame(3, $result->getRecurrences());
  69. }
  70. CarbonInterval::setLocale('en');
  71. }
  72. public function testWithModifiedEnglish()
  73. {
  74. $periodClass = $this->periodClass;
  75. $translator = Translator::get('en');
  76. $translator->setTranslations([
  77. 'day' => ':count boring day|:count boring days',
  78. ]);
  79. $period = $periodClass::create('2021-01-05', 3);
  80. $result = $period->floor()->toDatePeriod();
  81. $this->assertSame(DatePeriod::class, \get_class($result));
  82. $this->assertSame('2021-01-05', $result->getStartDate()->format('Y-m-d'));
  83. $this->assertNull($result->getEndDate());
  84. if (method_exists($result, 'getRecurrences')) {
  85. $this->assertSame(3, $result->getRecurrences());
  86. }
  87. $translator->resetMessages();
  88. }
  89. public function testRawDate()
  90. {
  91. $periodClass = $this->periodClass;
  92. $period = new $periodClass();
  93. $method = new ReflectionMethod($periodClass, 'rawDate');
  94. $method->setAccessible(true);
  95. $this->assertNull($method->invoke($period, false));
  96. $this->assertNull($method->invoke($period, null));
  97. $date = new DateTime();
  98. $this->assertSame($date, $method->invoke($period, $date));
  99. $date = new DateTimeImmutable();
  100. $this->assertSame($date, $method->invoke($period, $date));
  101. $date = new Carbon();
  102. $raw = $method->invoke($period, $date);
  103. $this->assertInstanceOf(DateTime::class, $raw);
  104. $this->assertEquals($date, $raw);
  105. $date = new CarbonImmutable();
  106. $raw = $method->invoke($period, $date);
  107. $this->assertInstanceOf(DateTimeImmutable::class, $raw);
  108. $this->assertEquals($date, $raw);
  109. $date = new class() extends DateTime {
  110. // void
  111. };
  112. $raw = $method->invoke($period, $date);
  113. $this->assertInstanceOf(DateTime::class, $raw);
  114. $this->assertEquals($date, $raw);
  115. $date = new class() extends DateTimeImmutable {
  116. // void
  117. };
  118. $raw = $method->invoke($period, $date);
  119. $this->assertInstanceOf(DateTimeImmutable::class, $raw);
  120. $this->assertEquals($date, $raw);
  121. }
  122. }