ServiceProviderTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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\Laravel;
  12. use Carbon\Carbon;
  13. use Carbon\CarbonImmutable;
  14. use Carbon\CarbonInterval;
  15. use Carbon\CarbonPeriod;
  16. use Carbon\Laravel\ServiceProvider;
  17. use Generator;
  18. use Illuminate\Events\Dispatcher;
  19. use Illuminate\Events\EventDispatcher;
  20. use Illuminate\Support\Carbon as SupportCarbon;
  21. use Illuminate\Support\Facades\Date;
  22. use PHPUnit\Framework\TestCase;
  23. use stdClass;
  24. class ServiceProviderTest extends TestCase
  25. {
  26. public static function dataForDispatchers(): Generator
  27. {
  28. if (!class_exists(Dispatcher::class)) {
  29. include_once __DIR__.'/Dispatcher.php';
  30. }
  31. if (!class_exists(EventDispatcher::class)) {
  32. include_once __DIR__.'/EventDispatcher.php';
  33. }
  34. yield [new Dispatcher()];
  35. yield [new EventDispatcher()];
  36. }
  37. /**
  38. * @dataProvider \Tests\Laravel\ServiceProviderTest::dataForDispatchers
  39. */
  40. public function testBoot($dispatcher)
  41. {
  42. // Reset language
  43. Carbon::setLocale('en');
  44. CarbonImmutable::setLocale('en');
  45. CarbonPeriod::setLocale('en');
  46. CarbonInterval::setLocale('en');
  47. $service = new ServiceProvider($dispatcher);
  48. $this->assertSame('en', Carbon::getLocale());
  49. $this->assertSame('en', CarbonImmutable::getLocale());
  50. $this->assertSame('en', CarbonPeriod::getLocale());
  51. $this->assertSame('en', CarbonInterval::getLocale());
  52. $service->boot();
  53. $this->assertSame('en', Carbon::getLocale());
  54. $this->assertSame('en', CarbonImmutable::getLocale());
  55. $this->assertSame('en', CarbonPeriod::getLocale());
  56. $this->assertSame('en', CarbonInterval::getLocale());
  57. $service->app->register();
  58. $service->boot();
  59. $this->assertSame('de', Carbon::getLocale());
  60. $this->assertSame('de', CarbonImmutable::getLocale());
  61. $this->assertSame('de', CarbonPeriod::getLocale());
  62. $this->assertSame('de', CarbonInterval::getLocale());
  63. $service->app->setLocale('fr');
  64. $this->assertSame('fr', Carbon::getLocale());
  65. $this->assertSame('fr', CarbonImmutable::getLocale());
  66. $this->assertSame('fr', CarbonPeriod::getLocale());
  67. $this->assertSame('fr', CarbonInterval::getLocale());
  68. $this->assertNull($service->register());
  69. // Reset language
  70. Carbon::setLocale('en');
  71. $service->app->removeService('events');
  72. $this->assertNull($service->boot());
  73. }
  74. public function testListenerWithoutLocaleUpdatedClass()
  75. {
  76. if (class_exists('Illuminate\Foundation\Events\LocaleUpdated')) {
  77. $this->markTestSkipped('This test cannot be run with Laravel 5.5 classes available via autoload.');
  78. }
  79. $dispatcher = new Dispatcher();
  80. $service = new ServiceProvider($dispatcher);
  81. Carbon::setLocale('en');
  82. CarbonImmutable::setLocale('en');
  83. CarbonPeriod::setLocale('en');
  84. CarbonInterval::setLocale('en');
  85. $service->boot();
  86. $service->app->register();
  87. $service->app->setLocaleWithoutEvent('fr');
  88. $dispatcher->dispatch('locale.changed');
  89. $this->assertSame('fr', Carbon::getLocale());
  90. $this->assertSame('fr', CarbonImmutable::getLocale());
  91. $this->assertSame('fr', CarbonPeriod::getLocale());
  92. $this->assertSame('fr', CarbonInterval::getLocale());
  93. }
  94. public function testListenerWithLocaleUpdatedClass()
  95. {
  96. if (!class_exists('Illuminate\Foundation\Events\LocaleUpdated')) {
  97. eval('namespace Illuminate\Foundation\Events; class LocaleUpdated {}');
  98. }
  99. $dispatcher = new Dispatcher();
  100. $service = new ServiceProvider($dispatcher);
  101. Carbon::setLocale('en');
  102. CarbonImmutable::setLocale('en');
  103. CarbonPeriod::setLocale('en');
  104. CarbonInterval::setLocale('en');
  105. $service->boot();
  106. $service->app->register();
  107. $service->app->setLocaleWithoutEvent('fr');
  108. $app = new App();
  109. $app->register();
  110. $app->setLocaleWithoutEvent('de_DE');
  111. $dispatcher->dispatch('Illuminate\Foundation\Events\LocaleUpdated');
  112. $this->assertSame('fr', Carbon::getLocale());
  113. $this->assertSame('fr', CarbonImmutable::getLocale());
  114. $this->assertSame('fr', CarbonPeriod::getLocale());
  115. $this->assertSame('fr', CarbonInterval::getLocale());
  116. $service->setAppGetter(static function () use ($app) {
  117. return $app;
  118. });
  119. $this->assertSame('fr', Carbon::getLocale());
  120. $service->updateLocale();
  121. $this->assertSame('de_DE', Carbon::getLocale());
  122. $service->setLocaleGetter(static function () {
  123. return 'ckb';
  124. });
  125. $this->assertSame('de_DE', Carbon::getLocale());
  126. $service->updateLocale();
  127. $this->assertSame('ckb', Carbon::getLocale());
  128. $service->setLocaleGetter(null);
  129. $service->setAppGetter(static function () {
  130. return null;
  131. });
  132. $service->updateLocale();
  133. $this->assertSame('ckb', Carbon::getLocale());
  134. }
  135. public function testUpdateLocale()
  136. {
  137. if (class_exists('Illuminate\Support\Carbon')) {
  138. $this->markTestSkipped('This test cannot be run with Laravel 5.5 classes available via autoload.');
  139. }
  140. eval('
  141. namespace Illuminate\Support;
  142. class Carbon
  143. {
  144. public static $locale;
  145. public static function setLocale($locale)
  146. {
  147. static::$locale = $locale;
  148. }
  149. }
  150. ');
  151. eval('
  152. namespace Illuminate\Support\Facades;
  153. use Exception;
  154. class Date
  155. {
  156. public static $locale;
  157. public static function getFacadeRoot()
  158. {
  159. return new static();
  160. }
  161. public function setLocale($locale)
  162. {
  163. static::$locale = $locale;
  164. if ($locale === "fr") {
  165. throw new Exception("stop");
  166. }
  167. }
  168. }
  169. ');
  170. $dispatcher = new Dispatcher();
  171. $service = new ServiceProvider($dispatcher);
  172. $service->boot();
  173. $service->app->register();
  174. $service->updateLocale();
  175. $this->assertSame('de', SupportCarbon::$locale);
  176. $this->assertSame('de', Date::$locale);
  177. $service->app->setLocale('fr');
  178. $service->updateLocale();
  179. $this->assertSame('fr', SupportCarbon::$locale);
  180. $this->assertSame('fr', Date::$locale);
  181. eval('
  182. use Illuminate\Events\Dispatcher;
  183. use Tests\Laravel\App;
  184. function app($id)
  185. {
  186. $app = new App();
  187. $app->setEventDispatcher(new Dispatcher());
  188. $app->register();
  189. $app->setLocale("it");
  190. return $app;
  191. }
  192. ');
  193. $service->app = new stdClass();
  194. $service->updateLocale();
  195. $this->assertSame('it', SupportCarbon::$locale);
  196. $this->assertSame('it', Date::$locale);
  197. }
  198. }