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