From 28a4144ed14f6f6cf41c394a1f2a37e41bf87b67 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Thu, 22 Aug 2019 10:06:13 +0800 Subject: [PATCH] Fix tests --- .../CommandsTest/OptionsCacheCommandTest.php | 1 + tests/ServicesTest/PluginManagerTest.php | 70 ++++++++++++++----- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/tests/CommandsTest/OptionsCacheCommandTest.php b/tests/CommandsTest/OptionsCacheCommandTest.php index a1757cdf..dd41291a 100644 --- a/tests/CommandsTest/OptionsCacheCommandTest.php +++ b/tests/CommandsTest/OptionsCacheCommandTest.php @@ -10,6 +10,7 @@ class OptionsCacheCommandTest extends TestCase public function testRun() { $this->mock(Filesystem::class, function ($mock) { + $mock->shouldReceive('exists')->andReturn(false); $mock->shouldReceive('put') ->withArgs(function ($path, $content) { $this->assertEquals(storage_path('options/cache.php'), $path); diff --git a/tests/ServicesTest/PluginManagerTest.php b/tests/ServicesTest/PluginManagerTest.php index 1e0bfc1a..e95f205b 100644 --- a/tests/ServicesTest/PluginManagerTest.php +++ b/tests/ServicesTest/PluginManagerTest.php @@ -5,6 +5,7 @@ namespace Tests; use Event; use App\Events; use ReflectionClass; +use App\Services\Option; use App\Services\Plugin; use App\Services\PluginManager; use Illuminate\Filesystem\Filesystem; @@ -25,6 +26,9 @@ class PluginManagerTest extends TestCase public function testPreventBootingAgain() { + $this->mock(Option::class, function ($mock) { + $mock->shouldReceive('get')->with('plugins_enabled', '[]')->andReturn('[]'); + }); $this->mock(Filesystem::class, function ($mock) { $mock->shouldReceive('directories')->times(1); }); @@ -45,6 +49,9 @@ class PluginManagerTest extends TestCase public function testNotLoadUnsatisfied() { + $this->mock(Option::class, function ($mock) { + $mock->shouldReceive('get')->with('plugins_enabled', '[]')->andReturn('[]'); + }); $this->mock(Filesystem::class, function ($mock) { $mock->shouldReceive('directories') ->with(base_path('plugins')) @@ -73,6 +80,9 @@ class PluginManagerTest extends TestCase public function testReportDuplicatedPlugins() { + $this->mock(Option::class, function ($mock) { + $mock->shouldReceive('get')->with('plugins_enabled', '[]')->andReturn('[]'); + }); $this->mock(Filesystem::class, function ($mock) { $mock->shouldReceive('directories') ->with(base_path('plugins')) @@ -115,8 +125,12 @@ class PluginManagerTest extends TestCase public function testDetectVersionChanged() { - option(['plugins_enabled' => json_encode([['name' => 'mayaka', 'version' => '0.0.0']])]); Event::fake(); + $this->mock(Option::class, function ($mock) { + $mock->shouldReceive('get') + ->with('plugins_enabled', '[]') + ->andReturn(json_encode([['name' => 'mayaka', 'version' => '0.0.0']])); + }); $this->mock(Filesystem::class, function ($mock) { $mock->shouldReceive('directories') ->with(base_path('plugins')) @@ -152,13 +166,15 @@ class PluginManagerTest extends TestCase return true; }); - - option(['plugins_enabled' => '[]']); } public function testLoadComposer() { - option(['plugins_enabled' => json_encode([['name' => 'mayaka', 'version' => '0.0.0']])]); + $this->mock(Option::class, function ($mock) { + $mock->shouldReceive('get') + ->with('plugins_enabled', '[]') + ->andReturn(json_encode([['name' => 'mayaka', 'version' => '0.0.0']])); + }); $this->mock(Filesystem::class, function ($mock) { $mock->shouldReceive('directories') ->with(base_path('plugins')) @@ -194,13 +210,15 @@ class PluginManagerTest extends TestCase }); $manager = $this->rebootPluginManager(app('plugins')); - - option(['plugins_enabled' => '[]']); } public function testLoadViewsAndTranslations() { - option(['plugins_enabled' => json_encode([['name' => 'mayaka', 'version' => '0.0.0']])]); + $this->mock(Option::class, function ($mock) { + $mock->shouldReceive('get') + ->with('plugins_enabled', '[]') + ->andReturn(json_encode([['name' => 'mayaka', 'version' => '0.0.0']])); + }); $this->mock(Filesystem::class, function ($mock) { $mock->shouldReceive('directories') ->with(base_path('plugins')) @@ -242,13 +260,15 @@ class PluginManagerTest extends TestCase })); $manager = $this->rebootPluginManager(app('plugins')); - - option(['plugins_enabled' => '[]']); } public function testLoadBootstrapper() { - option(['plugins_enabled' => json_encode([['name' => 'mayaka', 'version' => '0.0.0']])]); + $this->mock(Option::class, function ($mock) { + $mock->shouldReceive('get') + ->with('plugins_enabled', '[]') + ->andReturn(json_encode([['name' => 'mayaka', 'version' => '0.0.0']])); + }); $this->mock(Filesystem::class, function ($mock) { $mock->shouldReceive('directories') ->with(base_path('plugins')) @@ -287,12 +307,13 @@ class PluginManagerTest extends TestCase }); $manager = $this->rebootPluginManager(app('plugins')); - - option(['plugins_enabled' => '[]']); } public function testLifecycleHooks() { + $this->mock(Option::class, function ($mock) { + $mock->shouldReceive('get')->with('plugins_enabled', '[]')->andReturn('[]'); + }); $this->mock(Filesystem::class, function ($mock) { $mock->shouldReceive('directories') ->with(base_path('plugins')) @@ -337,14 +358,17 @@ class PluginManagerTest extends TestCase { $dir = config('plugins.directory'); config(['plugins.directory' => storage_path('mocks')]); - option(['plugins_enabled' => json_encode([['name' => 'fake', 'version' => '0.0.0']])]); + $this->mock(Option::class, function ($mock) { + $mock->shouldReceive('get') + ->with('plugins_enabled', '[]') + ->andReturn(json_encode([['name' => 'fake', 'version' => '0.0.0']])); + }); $this->assertFalse(class_exists('Fake\Faker')); $manager = $this->rebootPluginManager(app('plugins')); $this->assertTrue(class_exists('Fake\FakeServiceProvider')); config(['plugins.directory' => $dir]); - option(['plugins_enabled' => '[]']); } public function testRegisterServiceProviders() @@ -353,13 +377,16 @@ class PluginManagerTest extends TestCase $dir = config('plugins.directory'); config(['plugins.directory' => storage_path('mocks')]); - option(['plugins_enabled' => json_encode([['name' => 'fake', 'version' => '0.0.0']])]); + $this->mock(Option::class, function ($mock) { + $mock->shouldReceive('get') + ->with('plugins_enabled', '[]') + ->andReturn(json_encode([['name' => 'fake', 'version' => '0.0.0']])); + }); $manager = $this->rebootPluginManager(app('plugins')); Event::assertDispatched('provider.loaded'); config(['plugins.directory' => $dir]); - option(['plugins_enabled' => '[]']); } public function testGetUnsatisfied() @@ -468,6 +495,13 @@ class PluginManagerTest extends TestCase public function testDelete() { Event::fake(); + $this->mock(Option::class, function ($mock) { + $mock->shouldReceive('get') + ->with('plugins_enabled', '[]') + ->andReturn(json_encode([['name' => 'fake', 'version' => '0.0.0']])); + $mock->shouldReceive('set') + ->with('plugins_enabled', '[]'); + }); $this->mock(Filesystem::class, function ($mock) { $mock->shouldReceive('directories')->andReturn(collect([])); $mock->shouldReceive('deleteDirectory')->with('/fake')->once(); @@ -493,7 +527,6 @@ class PluginManagerTest extends TestCase return true; }); $this->assertFalse($manager->getEnabledPlugins()->has('fake')); - $this->assertCount(0, json_decode(resolve(\App\Services\Option::class)->get('plugins_enabled'), true)); $this->assertTrue($manager->all()->isEmpty()); } @@ -534,6 +567,9 @@ class PluginManagerTest extends TestCase $old = config('plugins.directory'); config(['plugins.directory' => '/kumiko,/reina']); + $this->mock(Option::class, function ($mock) { + $mock->shouldReceive('get')->with('plugins_enabled', '[]')->andReturn('[]'); + }); $this->mock(Filesystem::class, function ($mock) { $mock->shouldReceive('directories') ->with('/kumiko')