Skip to content

Commit

Permalink
adjust tests to deprecate Plugin:load()
Browse files Browse the repository at this point in the history
  • Loading branch information
saeideng committed Jul 5, 2018
1 parent 7be710d commit b78eae9
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 110 deletions.
3 changes: 1 addition & 2 deletions tests/TestCase/Core/AppTest.php
Expand Up @@ -218,12 +218,11 @@ public function shortNameProvider()
public function testPathWithPlugins()
{
$basepath = TEST_APP . 'Plugin' . DS;
$this->loadPlugins('TestPlugin');
$this->loadPlugins(['TestPlugin', 'Company/TestPluginThree']);

$result = App::path('Controller', 'TestPlugin');
$this->assertPathEquals($basepath . 'TestPlugin' . DS . 'src' . DS . 'Controller' . DS, $result[0]);

$this->loadPlugins('Company/TestPluginThree');
$result = App::path('Controller', 'Company/TestPluginThree');
$expected = $basepath . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS . 'Controller' . DS;
$this->assertPathEquals($expected, $result[0]);
Expand Down
208 changes: 120 additions & 88 deletions tests/TestCase/Core/PluginTest.php
Expand Up @@ -53,9 +53,11 @@ public function tearDown()
*/
public function testLoad()
{
Plugin::load('TestPlugin');
$expected = ['TestPlugin'];
$this->assertEquals($expected, Plugin::loaded());
$this->deprecated(function () {
Plugin::load('TestPlugin');
$expected = ['TestPlugin'];
$this->assertEquals($expected, Plugin::loaded());
});
}

/**
Expand All @@ -65,9 +67,11 @@ public function testLoad()
*/
public function testLoadConcreteClass()
{
Plugin::load('TestPlugin');
$instance = Plugin::getCollection()->get('TestPlugin');
$this->assertSame(TestPlugin::class, get_class($instance));
$this->deprecated(function () {
Plugin::load('TestPlugin');
$instance = Plugin::getCollection()->get('TestPlugin');
$this->assertSame(TestPlugin::class, get_class($instance));
});
}

/**
Expand All @@ -77,9 +81,11 @@ public function testLoadConcreteClass()
*/
public function testLoadDynamicClass()
{
Plugin::load('TestPluginTwo');
$instance = Plugin::getCollection()->get('TestPluginTwo');
$this->assertSame(BasePlugin::class, get_class($instance));
$this->deprecated(function () {
Plugin::load('TestPluginTwo');
$instance = Plugin::getCollection()->get('TestPluginTwo');
$this->assertSame(BasePlugin::class, get_class($instance));
});
}

/**
Expand All @@ -89,14 +95,14 @@ public function testLoadDynamicClass()
*/
public function testUnload()
{
Plugin::load('TestPlugin');
$this->loadPlugins(['TestPlugin' => ['bootstrap' => false, 'routes' => false]]);
$expected = ['TestPlugin'];
$this->assertEquals($expected, Plugin::loaded());

Plugin::unload('TestPlugin');
$this->assertEquals([], Plugin::loaded());

Plugin::load('TestPlugin');
$this->loadPlugins(['TestPlugin' => ['bootstrap' => false, 'routes' => false]]);
$expected = ['TestPlugin'];
$this->assertEquals($expected, Plugin::loaded());

Expand All @@ -111,14 +117,16 @@ public function testUnload()
*/
public function testLoadWithAutoload()
{
$this->assertFalse(class_exists('Company\TestPluginFive\Utility\Hello'));
Plugin::load('Company/TestPluginFive', [
'autoload' => true,
]);
$this->assertTrue(
class_exists('Company\TestPluginFive\Utility\Hello'),
'Class should be loaded'
);
$this->deprecated(function () {
$this->assertFalse(class_exists('Company\TestPluginFive\Utility\Hello'));
Plugin::load('Company/TestPluginFive', [
'autoload' => true,
]);
$this->assertTrue(
class_exists('Company\TestPluginFive\Utility\Hello'),
'Class should be loaded'
);
});
}

/**
Expand All @@ -128,19 +136,21 @@ class_exists('Company\TestPluginFive\Utility\Hello'),
*/
public function testLoadWithAutoloadAndBootstrap()
{
Plugin::load(
'Company/TestPluginFive',
[
'autoload' => true,
'bootstrap' => true
]
);
$this->assertTrue(Configure::read('PluginTest.test_plugin_five.autoload'));
$this->assertEquals('loaded plugin five bootstrap', Configure::read('PluginTest.test_plugin_five.bootstrap'));
$this->assertTrue(
class_exists('Company\TestPluginFive\Utility\Hello'),
'Class should be loaded'
);
$this->deprecated(function () {
Plugin::load(
'Company/TestPluginFive',
[
'autoload' => true,
'bootstrap' => true
]
);
$this->assertTrue(Configure::read('PluginTest.test_plugin_five.autoload'));
$this->assertEquals('loaded plugin five bootstrap', Configure::read('PluginTest.test_plugin_five.bootstrap'));
$this->assertTrue(
class_exists('Company\TestPluginFive\Utility\Hello'),
'Class should be loaded'
);
});
}

/**
Expand All @@ -150,13 +160,15 @@ class_exists('Company\TestPluginFive\Utility\Hello'),
*/
public function testLoadWithBootstrap()
{
Plugin::load('TestPlugin', ['bootstrap' => true]);
$this->assertTrue(Plugin::loaded('TestPlugin'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
$this->deprecated(function () {
Plugin::load('TestPlugin', ['bootstrap' => true]);
$this->assertTrue(Plugin::loaded('TestPlugin'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));

Plugin::load('Company/TestPluginThree', ['bootstrap' => true]);
$this->assertTrue(Plugin::loaded('Company/TestPluginThree'));
$this->assertEquals('loaded plugin three bootstrap', Configure::read('PluginTest.test_plugin_three.bootstrap'));
Plugin::load('Company/TestPluginThree', ['bootstrap' => true]);
$this->assertTrue(Plugin::loaded('Company/TestPluginThree'));
$this->assertEquals('loaded plugin three bootstrap', Configure::read('PluginTest.test_plugin_three.bootstrap'));
});
}

/**
Expand All @@ -166,12 +178,14 @@ public function testLoadWithBootstrap()
*/
public function testLoadWithBootstrapDisableBootstrapHook()
{
Plugin::load('TestPlugin', ['bootstrap' => true]);
$this->assertTrue(Plugin::loaded('TestPlugin'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
$this->deprecated(function () {
Plugin::load('TestPlugin', ['bootstrap' => true]);
$this->assertTrue(Plugin::loaded('TestPlugin'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));

$plugin = Plugin::getCollection()->get('TestPlugin');
$this->assertFalse($plugin->isEnabled('bootstrap'), 'Should be disabled as hook has been run.');
$plugin = Plugin::getCollection()->get('TestPlugin');
$this->assertFalse($plugin->isEnabled('bootstrap'), 'Should be disabled as hook has been run.');
});
}

/**
Expand Down Expand Up @@ -199,9 +213,11 @@ public function testLoadSingleWithBootstrapAndRoutes()
*/
public function testLoadSingleWithPathConfig()
{
Configure::write('plugins.TestPlugin', APP);
Plugin::load('TestPlugin');
$this->assertEquals(APP . 'src' . DS, Plugin::classPath('TestPlugin'));
$this->deprecated(function () {
Configure::write('plugins.TestPlugin', APP);
Plugin::load('TestPlugin');
$this->assertEquals(APP . 'src' . DS, Plugin::classPath('TestPlugin'));
});
}

/**
Expand All @@ -211,9 +227,11 @@ public function testLoadSingleWithPathConfig()
*/
public function testLoadMultiple()
{
Plugin::load(['TestPlugin', 'TestPluginTwo']);
$expected = ['TestPlugin', 'TestPluginTwo'];
$this->assertEquals($expected, Plugin::loaded());
$this->deprecated(function () {
Plugin::load(['TestPlugin', 'TestPluginTwo']);
$expected = ['TestPlugin', 'TestPluginTwo'];
$this->assertEquals($expected, Plugin::loaded());
});
}

/**
Expand All @@ -223,11 +241,13 @@ public function testLoadMultiple()
*/
public function testLoadMultipleWithDefaults()
{
Plugin::load(['TestPlugin', 'TestPluginTwo'], ['bootstrap' => true, 'routes' => false]);
$expected = ['TestPlugin', 'TestPluginTwo'];
$this->assertEquals($expected, Plugin::loaded());
$this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
$this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
$this->deprecated(function () {
Plugin::load(['TestPlugin', 'TestPluginTwo'], ['bootstrap' => true, 'routes' => false]);
$expected = ['TestPlugin', 'TestPluginTwo'];
$this->assertEquals($expected, Plugin::loaded());
$this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
$this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
});
}

/**
Expand All @@ -237,14 +257,16 @@ public function testLoadMultipleWithDefaults()
*/
public function testLoadMultipleWithDefaultsAndOverride()
{
Plugin::load(
['TestPlugin', 'TestPluginTwo' => ['routes' => false]],
['bootstrap' => true, 'routes' => true]
);
$expected = ['TestPlugin', 'TestPluginTwo'];
$this->assertEquals($expected, Plugin::loaded());
$this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
$this->assertNull(Configure::read('PluginTest.test_plugin_two.bootstrap'));
$this->deprecated(function () {
Plugin::load(
['TestPlugin', 'TestPluginTwo' => ['routes' => false]],
['bootstrap' => true, 'routes' => true]
);
$expected = ['TestPlugin', 'TestPluginTwo'];
$this->assertEquals($expected, Plugin::loaded());
$this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
$this->assertNull(Configure::read('PluginTest.test_plugin_two.bootstrap'));
});
}

/**
Expand Down Expand Up @@ -272,8 +294,10 @@ public function testIgnoreMissingFiles()
*/
public function testLoadNotFound()
{
$this->expectException(\Cake\Core\Exception\MissingPluginException::class);
Plugin::load('MissingPlugin');
$this->deprecated(function () {
$this->expectException(\Cake\Core\Exception\MissingPluginException::class);
Plugin::load('MissingPlugin');
});
}

/**
Expand All @@ -283,7 +307,7 @@ public function testLoadNotFound()
*/
public function testPath()
{
Plugin::load(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
$this->loadPlugins(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
$expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
$this->assertPathEquals(Plugin::path('TestPlugin'), $expected);

Expand Down Expand Up @@ -312,7 +336,7 @@ public function testPathNotFound()
*/
public function testClassPath()
{
Plugin::load(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
$this->loadPlugins(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
$expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS;
$this->assertPathEquals(Plugin::classPath('TestPlugin'), $expected);

Expand Down Expand Up @@ -341,12 +365,14 @@ public function testClassPathNotFound()
*/
public function testLoadAll()
{
Plugin::loadAll();
$expected = [
'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
'TestPluginFour', 'TestPluginTwo', 'TestTheme'
];
$this->assertEquals($expected, Plugin::loaded());
$this->deprecated(function () {
Plugin::loadAll();
$expected = [
'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
'TestPluginFour', 'TestPluginTwo', 'TestTheme'
];
$this->assertEquals($expected, Plugin::loaded());
});
}

/**
Expand All @@ -356,9 +382,11 @@ public function testLoadAll()
*/
public function testLoadAllWithPathConfig()
{
Configure::write('plugins.FakePlugin', APP);
Plugin::loadAll();
$this->assertContains('FakePlugin', Plugin::loaded());
$this->deprecated(function () {
Configure::write('plugins.FakePlugin', APP);
Plugin::loadAll();
$this->assertContains('FakePlugin', Plugin::loaded());
});
}

/**
Expand All @@ -368,9 +396,11 @@ public function testLoadAllWithPathConfig()
*/
public function testLoadAllWithPluginAlreadyLoaded()
{
Plugin::load('Company/TestPluginThree', ['bootstrap' => false]);
Plugin::loadAll(['bootstrap' => true, 'ignoreMissing' => true]);
$this->assertEmpty(Configure::read('PluginTest.test_plugin_three.bootstrap'));
$this->deprecated(function () {
Plugin::load('Company/TestPluginThree', ['bootstrap' => false]);
Plugin::loadAll(['bootstrap' => true, 'ignoreMissing' => true]);
$this->assertEmpty(Configure::read('PluginTest.test_plugin_three.bootstrap'));
});
}

/**
Expand All @@ -380,16 +410,18 @@ public function testLoadAllWithPluginAlreadyLoaded()
*/
public function testLoadAllWithDefaults()
{
$defaults = ['bootstrap' => true, 'ignoreMissing' => true];
Plugin::loadAll([$defaults]);
$expected = [
'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
'TestPluginFour', 'TestPluginTwo', 'TestTheme'
];
$this->assertEquals($expected, Plugin::loaded());
$this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
$this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
$this->deprecated(function () {
$defaults = ['bootstrap' => true, 'ignoreMissing' => true];
Plugin::loadAll([$defaults]);
$expected = [
'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
'TestPluginFour', 'TestPluginTwo', 'TestTheme'
];
$this->assertEquals($expected, Plugin::loaded());
$this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
$this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
});
}

/**
Expand Down

0 comments on commit b78eae9

Please sign in to comment.