ToPeriodTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\CarbonInterval;
  12. use Carbon\Carbon;
  13. use Carbon\CarbonInterval;
  14. use Carbon\CarbonPeriod;
  15. use Generator;
  16. use Tests\AbstractTestCase;
  17. class ToPeriodTest extends AbstractTestCase
  18. {
  19. /**
  20. * @dataProvider \Tests\CarbonInterval\ToPeriodTest::dataForToPeriodParameters
  21. */
  22. public function testConvertToDatePeriod($interval, $arguments, $expected)
  23. {
  24. $period = ([$interval, 'toPeriod'])(...$arguments);
  25. $this->assertInstanceOf(CarbonPeriod::class, $period);
  26. $this->assertSame($expected, $period->spec());
  27. }
  28. public static function dataForToPeriodParameters(): Generator
  29. {
  30. yield [
  31. CarbonInterval::days(3),
  32. ['2017-10-15', 4],
  33. 'R4/2017-10-15T00:00:00-04:00/P3D',
  34. ];
  35. yield [
  36. CarbonInterval::hours(7),
  37. ['2017-10-15', '2017-10-17'],
  38. '2017-10-15T00:00:00-04:00/PT7H/2017-10-17T00:00:00-04:00',
  39. ];
  40. yield [
  41. CarbonInterval::months(3)->days(2)->hours(10)->minutes(20),
  42. ['2017-10-15'],
  43. '2017-10-15T00:00:00-04:00/P3M2DT10H20M',
  44. ];
  45. yield [
  46. CarbonInterval::minutes(30),
  47. [new Carbon('2018-05-14 17:30 UTC'), new Carbon('2018-05-14 18:00 Europe/Oslo')],
  48. '2018-05-14T17:30:00+00:00/PT30M/2018-05-14T18:00:00+02:00',
  49. ];
  50. }
  51. public function testToDatePeriodWithTimezone(): void
  52. {
  53. $period = CarbonInterval::minutes(30)
  54. ->setTimezone('Asia/Tokyo')
  55. ->toPeriod('2021-08-14 00:00', '2021-08-14 02:00');
  56. $this->assertSame('2021-08-14 00:00 Asia/Tokyo', $period->start->format('Y-m-d H:i e'));
  57. $this->assertSame('2021-08-14 02:00 Asia/Tokyo', $period->end->format('Y-m-d H:i e'));
  58. }
  59. }