IterationMethodsTest.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 Tests\AbstractTestCase;
  14. class IterationMethodsTest extends AbstractTestCase
  15. {
  16. public function testForEach()
  17. {
  18. $result = '';
  19. Carbon::create('2020-12-22')->daysUntil('2020-12-24')->forEach(function (Carbon $date) use (&$result) {
  20. $result .= $date->diffInDays('2020-12-25')." days before Christmas!\n";
  21. });
  22. $this->assertSame("3 days before Christmas!\n".
  23. "2 days before Christmas!\n".
  24. "1 days before Christmas!\n", $result);
  25. }
  26. public function testMap()
  27. {
  28. $result = iterator_to_array(Carbon::create('2020-12-22')->daysUntil('2020-12-24')->map(function (Carbon $date) {
  29. return $date->diffInDays('2020-12-25').' days before Christmas!';
  30. }));
  31. $this->assertSame([
  32. '3 days before Christmas!',
  33. '2 days before Christmas!',
  34. '1 days before Christmas!',
  35. ], $result);
  36. }
  37. }