From cc3e5a9609824a683abd146fdb5167d63f0b6d27 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Tue, 10 Dec 2019 10:57:03 +0800 Subject: [PATCH] Fix tests --- .../ControllersTest/PluginControllerTest.php | 46 ++++++------------- tests/ServicesTest/PluginManagerTest.php | 34 +++++++++++++- 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/tests/HttpTest/ControllersTest/PluginControllerTest.php b/tests/HttpTest/ControllersTest/PluginControllerTest.php index 60a21302..7336c084 100644 --- a/tests/HttpTest/ControllersTest/PluginControllerTest.php +++ b/tests/HttpTest/ControllersTest/PluginControllerTest.php @@ -138,30 +138,23 @@ class PluginControllerTest extends TestCase ->with('fake2') ->once() ->andReturn(new Plugin('', ['name' => 'fake2'])); - $mock->shouldReceive('get') - ->with('dep') - ->once() - ->andReturn(new Plugin('', ['title' => 'dep'])); - $mock->shouldReceive('get') - ->with('whatever') - ->once() - ->andReturn(null); - $mock->shouldReceive('get') - ->with('conf') - ->once() - ->andReturn(new Plugin('', ['title' => 'conf'])); + $unresolvedInfo = [ + 'unsatisfied' => collect([ + 'dep' => ['version' => '0.0.0', 'constraint' => '^6.6.6'], + 'whatever' => ['version' => null, 'constraint' => '^1.2.3'], + ]), + 'conflicts' => collect([ + 'conf' => ['version' => '1.2.3', 'constraint' => '^1.0.0'], + ]), + ]; $mock->shouldReceive('enable') ->with('fake2') ->once() - ->andReturn([ - 'unsatisfied' => collect([ - 'dep' => ['version' => '0.0.0', 'constraint' => '^6.6.6'], - 'whatever' => ['version' => null, 'constraint' => '^1.2.3'], - ]), - 'conflicts' => collect([ - 'conf' => ['version' => '1.2.3', 'constraint' => '^1.0.0'], - ]), - ]); + ->andReturn($unresolvedInfo); + $mock->shouldReceive('formatUnresolved') + ->with($unresolvedInfo['unsatisfied'], $unresolvedInfo['conflicts']) + ->once() + ->andReturn(['dep', 'whatever', 'conf']); $mock->shouldReceive('get') ->with('fake3') @@ -210,16 +203,7 @@ class PluginControllerTest extends TestCase ])->assertJson([ 'code' => 1, 'message' => trans('admin.plugins.operations.unsatisfied.notice'), - 'data' => [ - 'reason' => [ - trans('admin.plugins.operations.unsatisfied.version', [ - 'title' => 'dep', - 'constraint' => '^6.6.6', - ]), - trans('admin.plugins.operations.unsatisfied.disabled', ['name' => 'whatever']), - trans('admin.plugins.operations.unsatisfied.conflict', ['title' => 'conf']), - ], - ], + 'data' => ['reason' => ['dep', 'whatever', 'conf']], ]); // Enable a plugin diff --git a/tests/ServicesTest/PluginManagerTest.php b/tests/ServicesTest/PluginManagerTest.php index 3d6e981b..8bf4bd6e 100644 --- a/tests/ServicesTest/PluginManagerTest.php +++ b/tests/ServicesTest/PluginManagerTest.php @@ -511,7 +511,7 @@ class PluginManagerTest extends TestCase public function testGetConflicts() { - $manager = app('plugins'); + $manager = resolve(PluginManager::class); $plugin = new Plugin('/', ['enchants' => ['conflicts' => ['a' => '*', 'b' => '^1.2.0']]]); $reflection = new ReflectionClass($manager); @@ -529,6 +529,38 @@ class PluginManagerTest extends TestCase $this->assertNull($manager->getConflicts($plugin)->get('b')); } + public function testFormatUnresolved() + { + app()->forgetInstance(PluginManager::class); + $manager = resolve(PluginManager::class); + $reflection = new ReflectionClass($manager); + $property = $reflection->getProperty('plugins'); + $property->setAccessible(true); + $property->setValue($manager, collect([ + 'dep' => new Plugin('', ['title' => 'dep', 'version' => '0.0.0']), + 'conf' => new Plugin('', ['title' => 'conf', 'version' => '1.2.3']), + ])); + + $unsatisfied = collect([ + 'dep' => ['version' => '0.0.0', 'constraint' => '^6.6.6'], + 'whatever' => ['version' => null, 'constraint' => '^1.2.3'], + ]); + $conflicts = collect([ + 'conf' => ['version' => '1.2.3', 'constraint' => '^1.0.0'], + ]); + + $received = $manager->formatUnresolved($unsatisfied, $conflicts); + $expected = [ + trans('admin.plugins.operations.unsatisfied.version', [ + 'title' => 'dep', + 'constraint' => '^6.6.6', + ]), + trans('admin.plugins.operations.unsatisfied.disabled', ['name' => 'whatever']), + trans('admin.plugins.operations.unsatisfied.conflict', ['title' => 'conf']), + ]; + $this->assertEquals($expected, $received); + } + public function testEnable() { Event::fake();