FoundationProviderRepositoryTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Illuminate\Tests\Foundation;
  3. use Exception;
  4. use Illuminate\Contracts\Foundation\Application as ApplicationContract;
  5. use Illuminate\Filesystem\Filesystem;
  6. use Illuminate\Foundation\Application;
  7. use Illuminate\Foundation\ProviderRepository;
  8. use Illuminate\Support\ServiceProvider;
  9. use Mockery as m;
  10. use PHPUnit\Framework\TestCase;
  11. use stdClass;
  12. class FoundationProviderRepositoryTest extends TestCase
  13. {
  14. protected function tearDown(): void
  15. {
  16. m::close();
  17. }
  18. public function testServicesAreRegisteredWhenManifestIsNotRecompiled()
  19. {
  20. $app = m::mock(Application::class);
  21. $repo = m::mock(ProviderRepository::class.'[createProvider,loadManifest,shouldRecompile]', [$app, m::mock(Filesystem::class), [__DIR__.'/services.php']]);
  22. $repo->shouldReceive('loadManifest')->once()->andReturn(['eager' => ['foo'], 'deferred' => ['deferred'], 'providers' => ['providers'], 'when' => []]);
  23. $repo->shouldReceive('shouldRecompile')->once()->andReturn(false);
  24. $app->shouldReceive('register')->once()->with('foo');
  25. $app->shouldReceive('runningInConsole')->andReturn(false);
  26. $app->shouldReceive('addDeferredServices')->once()->with(['deferred']);
  27. $repo->load([]);
  28. }
  29. public function testManifestIsProperlyRecompiled()
  30. {
  31. $app = m::mock(Application::class);
  32. $repo = m::mock(ProviderRepository::class.'[createProvider,loadManifest,writeManifest,shouldRecompile]', [$app, m::mock(Filesystem::class), [__DIR__.'/services.php']]);
  33. $repo->shouldReceive('loadManifest')->once()->andReturn(['eager' => [], 'deferred' => ['deferred']]);
  34. $repo->shouldReceive('shouldRecompile')->once()->andReturn(true);
  35. // foo mock is just a deferred provider
  36. $repo->shouldReceive('createProvider')->once()->with('foo')->andReturn($fooMock = m::mock(stdClass::class));
  37. $fooMock->shouldReceive('isDeferred')->once()->andReturn(true);
  38. $fooMock->shouldReceive('provides')->once()->andReturn(['foo.provides1', 'foo.provides2']);
  39. $fooMock->shouldReceive('when')->once()->andReturn([]);
  40. // bar mock is added to eagers since it's not reserved
  41. $repo->shouldReceive('createProvider')->once()->with('bar')->andReturn($barMock = m::mock(ServiceProvider::class));
  42. $barMock->shouldReceive('isDeferred')->once()->andReturn(false);
  43. $repo->shouldReceive('writeManifest')->once()->andReturnUsing(function ($manifest) {
  44. return $manifest;
  45. });
  46. $app->shouldReceive('register')->once()->with('bar');
  47. $app->shouldReceive('runningInConsole')->andReturn(false);
  48. $app->shouldReceive('addDeferredServices')->once()->with(['foo.provides1' => 'foo', 'foo.provides2' => 'foo']);
  49. $repo->load(['foo', 'bar']);
  50. }
  51. public function testShouldRecompileReturnsCorrectValue()
  52. {
  53. $repo = new ProviderRepository(m::mock(ApplicationContract::class), new Filesystem, __DIR__.'/services.php');
  54. $this->assertTrue($repo->shouldRecompile(null, []));
  55. $this->assertTrue($repo->shouldRecompile(['providers' => ['foo']], ['foo', 'bar']));
  56. $this->assertFalse($repo->shouldRecompile(['providers' => ['foo']], ['foo']));
  57. }
  58. public function testLoadManifestReturnsParsedJSON()
  59. {
  60. $repo = new ProviderRepository(m::mock(ApplicationContract::class), $files = m::mock(Filesystem::class), __DIR__.'/services.php');
  61. $files->shouldReceive('exists')->once()->with(__DIR__.'/services.php')->andReturn(true);
  62. $files->shouldReceive('getRequire')->once()->with(__DIR__.'/services.php')->andReturn($array = ['users' => ['dayle' => true], 'when' => []]);
  63. $this->assertEquals($array, $repo->loadManifest());
  64. }
  65. public function testWriteManifestStoresToProperLocation()
  66. {
  67. $repo = new ProviderRepository(m::mock(ApplicationContract::class), $files = m::mock(Filesystem::class), __DIR__.'/services.php');
  68. $files->shouldReceive('replace')->once()->with(__DIR__.'/services.php', '<?php return '.var_export(['foo'], true).';');
  69. $result = $repo->writeManifest(['foo']);
  70. $this->assertEquals(['foo', 'when' => []], $result);
  71. }
  72. public function testWriteManifestThrowsExceptionIfManifestDirDoesntExist()
  73. {
  74. $this->expectException(Exception::class);
  75. $this->expectExceptionMessageMatches('/^The (.*) directory must be present and writable.$/');
  76. $repo = new ProviderRepository(m::mock(ApplicationContract::class), $files = m::mock(Filesystem::class), __DIR__.'/cache/services.php');
  77. $files->shouldReceive('replace')->never();
  78. $repo->writeManifest(['foo']);
  79. }
  80. }