/tests/Foundation/FoundationProviderRepositoryTest.php

https://gitlab.com/tillkruss/framework · PHP · 87 lines · 64 code · 20 blank · 3 comment · 0 complexity · 2012773fa226d94c0c65e696e09390f6 MD5 · raw file

  1. <?php
  2. use Mockery as m;
  3. class FoundationProviderRepositoryTest extends PHPUnit_Framework_TestCase
  4. {
  5. public function tearDown()
  6. {
  7. m::close();
  8. }
  9. public function testServicesAreRegisteredWhenManifestIsNotRecompiled()
  10. {
  11. $app = m::mock('Illuminate\Foundation\Application');
  12. $repo = m::mock('Illuminate\Foundation\ProviderRepository[createProvider,loadManifest,shouldRecompile]', [$app, m::mock('Illuminate\Filesystem\Filesystem'), [__DIR__.'/services.php']]);
  13. $repo->shouldReceive('loadManifest')->once()->andReturn(['eager' => ['foo'], 'deferred' => ['deferred'], 'providers' => ['providers'], 'when' => []]);
  14. $repo->shouldReceive('shouldRecompile')->once()->andReturn(false);
  15. $provider = m::mock('Illuminate\Support\ServiceProvider');
  16. $repo->shouldReceive('createProvider')->once()->with('foo')->andReturn($provider);
  17. $app->shouldReceive('register')->once()->with($provider);
  18. $app->shouldReceive('runningInConsole')->andReturn(false);
  19. $app->shouldReceive('addDeferredServices')->once()->with(['deferred']);
  20. $repo->load([]);
  21. }
  22. public function testManifestIsProperlyRecompiled()
  23. {
  24. $app = m::mock('Illuminate\Foundation\Application');
  25. $repo = m::mock('Illuminate\Foundation\ProviderRepository[createProvider,loadManifest,writeManifest,shouldRecompile]', [$app, m::mock('Illuminate\Filesystem\Filesystem'), [__DIR__.'/services.php']]);
  26. $repo->shouldReceive('loadManifest')->once()->andReturn(['eager' => [], 'deferred' => ['deferred']]);
  27. $repo->shouldReceive('shouldRecompile')->once()->andReturn(true);
  28. // foo mock is just a deferred provider
  29. $repo->shouldReceive('createProvider')->once()->with('foo')->andReturn($fooMock = m::mock('StdClass'));
  30. $fooMock->shouldReceive('isDeferred')->once()->andReturn(true);
  31. $fooMock->shouldReceive('provides')->once()->andReturn(['foo.provides1', 'foo.provides2']);
  32. $fooMock->shouldReceive('when')->once()->andReturn([]);
  33. // bar mock is added to eagers since it's not reserved
  34. $repo->shouldReceive('createProvider')->once()->with('bar')->andReturn($barMock = m::mock('Illuminate\Support\ServiceProvider'));
  35. $barMock->shouldReceive('isDeferred')->once()->andReturn(false);
  36. $repo->shouldReceive('writeManifest')->once()->andReturnUsing(function ($manifest) {
  37. return $manifest;
  38. });
  39. // bar mock should be registered with the application since it's eager
  40. $repo->shouldReceive('createProvider')->once()->with('bar')->andReturn($barMock);
  41. $app->shouldReceive('register')->once()->with($barMock);
  42. $app->shouldReceive('runningInConsole')->andReturn(false);
  43. $app->shouldReceive('addDeferredServices')->once()->with(['foo.provides1' => 'foo', 'foo.provides2' => 'foo']);
  44. $manifest = $repo->load(['foo', 'bar']);
  45. }
  46. public function testShouldRecompileReturnsCorrectValue()
  47. {
  48. $repo = new Illuminate\Foundation\ProviderRepository(m::mock('Illuminate\Contracts\Foundation\Application'), new Illuminate\Filesystem\Filesystem, __DIR__.'/services.php');
  49. $this->assertTrue($repo->shouldRecompile(null, []));
  50. $this->assertTrue($repo->shouldRecompile(['providers' => ['foo']], ['foo', 'bar']));
  51. $this->assertFalse($repo->shouldRecompile(['providers' => ['foo']], ['foo']));
  52. }
  53. public function testLoadManifestReturnsParsedJSON()
  54. {
  55. $repo = new Illuminate\Foundation\ProviderRepository(m::mock('Illuminate\Contracts\Foundation\Application'), $files = m::mock('Illuminate\Filesystem\Filesystem'), __DIR__.'/services.php');
  56. $files->shouldReceive('exists')->once()->with(__DIR__.'/services.php')->andReturn(true);
  57. $files->shouldReceive('getRequire')->once()->with(__DIR__.'/services.php')->andReturn($array = ['users' => ['dayle' => true], 'when' => []]);
  58. $this->assertEquals($array, $repo->loadManifest());
  59. }
  60. public function testWriteManifestStoresToProperLocation()
  61. {
  62. $repo = new Illuminate\Foundation\ProviderRepository(m::mock('Illuminate\Contracts\Foundation\Application'), $files = m::mock('Illuminate\Filesystem\Filesystem'), __DIR__.'/services.php');
  63. $files->shouldReceive('put')->once()->with(__DIR__.'/services.php', '<?php return '.var_export(['foo'], true).';');
  64. $result = $repo->writeManifest(['foo']);
  65. $this->assertEquals(['foo', 'when' => []], $result);
  66. }
  67. }