FactoryTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\Factory;
  12. use Carbon\Carbon;
  13. use Carbon\CarbonImmutable;
  14. use Carbon\Factory;
  15. use Carbon\FactoryImmutable;
  16. use DateTimeImmutable;
  17. use Psr\Clock\ClockInterface;
  18. use Tests\AbstractTestCase;
  19. use Tests\Carbon\Fixtures\MyCarbon;
  20. class FactoryTest extends AbstractTestCase
  21. {
  22. public function testFactory()
  23. {
  24. $factory = new Factory();
  25. $this->assertInstanceOf(Carbon::class, $factory->parse('2018-01-01'));
  26. $this->assertSame('01/01/2018', $factory->parse('2018-01-01')->format('d/m/Y'));
  27. $factory = new Factory([
  28. 'locale' => 'fr',
  29. ]);
  30. $this->assertSame('fr', $factory->parse('2018-01-01')->locale);
  31. $factory = new Factory([
  32. 'locale' => 'fr',
  33. ], MyCarbon::class);
  34. $this->assertInstanceOf(MyCarbon::class, $factory->parse('2018-01-01'));
  35. $this->assertSame('01/01/2018', $factory->parse('2018-01-01')->format('d/m/Y'));
  36. $factory = new FactoryImmutable([
  37. 'locale' => 'fr',
  38. ]);
  39. $this->assertInstanceOf(CarbonImmutable::class, $factory->parse('2018-01-01'));
  40. $this->assertSame('01/01/2018', $factory->parse('2018-01-01')->format('d/m/Y'));
  41. }
  42. public function testFactoryModification()
  43. {
  44. $factory = new Factory();
  45. $this->assertSame(Carbon::class, $factory->className());
  46. $this->assertSame($factory, $factory->className(MyCarbon::class));
  47. $this->assertSame(MyCarbon::class, $factory->className());
  48. $this->assertSame([], $factory->settings());
  49. $this->assertSame($factory, $factory->settings([
  50. 'locale' => 'fr',
  51. ]));
  52. $this->assertSame([
  53. 'locale' => 'fr',
  54. ], $factory->settings());
  55. $this->assertSame($factory, $factory->mergeSettings([
  56. 'timezone' => 'Europe/Paris',
  57. ]));
  58. $this->assertSame([
  59. 'locale' => 'fr',
  60. 'timezone' => 'Europe/Paris',
  61. ], $factory->settings());
  62. $this->assertSame($factory, $factory->settings([
  63. 'timezone' => 'Europe/Paris',
  64. ]));
  65. $this->assertSame([
  66. 'timezone' => 'Europe/Paris',
  67. ], $factory->settings());
  68. }
  69. public function testFactoryTimezone()
  70. {
  71. Carbon::setTestNowAndTimezone(Carbon::parse('2020-09-04 03:39:04.123456', 'UTC'));
  72. $factory = new Factory();
  73. $date = $factory->now();
  74. $this->assertInstanceOf(Carbon::class, $date);
  75. $this->assertSame('2020-09-04 03:39:04.123456 UTC', $date->format('Y-m-d H:i:s.u e'));
  76. $factory = new Factory([
  77. 'timezone' => 'Europe/Paris',
  78. ]);
  79. $this->assertSame('2020-09-04 05:39:04.123456 Europe/Paris', $factory->now()->format('Y-m-d H:i:s.u e'));
  80. $this->assertSame('2020-09-04 00:00:00.000000 Europe/Paris', $factory->today()->format('Y-m-d H:i:s.u e'));
  81. $this->assertSame('2020-09-05 00:00:00.000000 Europe/Paris', $factory->tomorrow()->format('Y-m-d H:i:s.u e'));
  82. $this->assertSame('2020-09-04 09:39:04.123456 Europe/Paris', $factory->parse('2020-09-04 09:39:04.123456')->format('Y-m-d H:i:s.u e'));
  83. $factory = new Factory([
  84. 'timezone' => 'America/Toronto',
  85. ]);
  86. $this->assertSame('2020-09-03 23:39:04.123456 America/Toronto', $factory->now()->format('Y-m-d H:i:s.u e'));
  87. $this->assertSame('2020-09-03 00:00:00.000000 America/Toronto', $factory->today()->format('Y-m-d H:i:s.u e'));
  88. $this->assertSame('2020-09-04 00:00:00.000000 America/Toronto', $factory->tomorrow()->format('Y-m-d H:i:s.u e'));
  89. $this->assertSame('2020-09-04 09:39:04.123456 America/Toronto', $factory->parse('2020-09-04 09:39:04.123456')->format('Y-m-d H:i:s.u e'));
  90. $factory = new Factory([
  91. 'timezone' => 'Asia/Shanghai',
  92. ]);
  93. $baseDate = Carbon::parse('2021-08-01 08:00:00', 'UTC');
  94. $date = $factory->createFromTimestamp($baseDate->getTimestamp());
  95. $this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));
  96. $date = $factory->make('2021-08-01 08:00:00');
  97. $this->assertSame('2021-08-01T08:00:00+08:00', $date->format('c'));
  98. $date = $factory->make($baseDate);
  99. $this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));
  100. $date = $factory->create($baseDate);
  101. $this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));
  102. $date = $factory->parse($baseDate);
  103. $this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));
  104. $date = $factory->instance($baseDate);
  105. $this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));
  106. $date = $factory->make('2021-08-01 08:00:00+00:20');
  107. $this->assertSame('2021-08-01T08:00:00+00:20', $date->format('c'));
  108. $date = $factory->parse('2021-08-01T08:00:00Z');
  109. $this->assertSame('2021-08-01T08:00:00+00:00', $date->format('c'));
  110. $date = $factory->create('2021-08-01 08:00:00 UTC');
  111. $this->assertSame('2021-08-01T08:00:00+00:00', $date->format('c'));
  112. $date = $factory->make('2021-08-01 08:00:00 Europe/Paris');
  113. $this->assertSame('2021-08-01T08:00:00+02:00', $date->format('c'));
  114. }
  115. public function testPsrClock()
  116. {
  117. $factory = new FactoryImmutable();
  118. $this->assertInstanceOf(ClockInterface::class, $factory);
  119. $this->assertInstanceOf(DateTimeImmutable::class, $factory->now());
  120. $this->assertInstanceOf(CarbonImmutable::class, $factory->now());
  121. }
  122. }