From c94706ed057504cb6d3fa42b2bcb6c4249f33284 Mon Sep 17 00:00:00 2001 From: Mariano Custiel Date: Tue, 9 Feb 2016 11:42:14 +0100 Subject: [PATCH] Added the ability to create instances of components from factories to allow injection of dependencies in components Fixed descriptions Added missing new lines at the end of fixture components Fixed problem with include path Fixed code as suggested. Now everything is solved through app::make Removed tests that are not needed anymore since last changes --- modules/cms/classes/ComponentManager.php | 5 ++- .../plugins/october/tester/Plugin.php | 3 +- .../plugins/october/tester/classes/Users.php | 12 ++++++ .../october/tester/components/Categories.php | 34 +++++++++++++++ .../october/tester/components/Comments.php | 43 +++++++++++++++++++ .../unit/cms/classes/ComponentManagerTest.php | 41 +++++++++++++++--- 6 files changed, 128 insertions(+), 10 deletions(-) create mode 100644 tests/fixtures/plugins/october/tester/classes/Users.php create mode 100644 tests/fixtures/plugins/october/tester/components/Categories.php create mode 100644 tests/fixtures/plugins/october/tester/components/Comments.php diff --git a/modules/cms/classes/ComponentManager.php b/modules/cms/classes/ComponentManager.php index bf26747bd..dc922cd88 100644 --- a/modules/cms/classes/ComponentManager.php +++ b/modules/cms/classes/ComponentManager.php @@ -4,6 +4,7 @@ use Str; use Illuminate\Container\Container; use System\Classes\PluginManager; use SystemException; +use Illuminate\Support\Facades\App; /** * Component manager @@ -132,7 +133,7 @@ class ComponentManager return $this->codeMap; } - /** + /** * Returns an array of all component detail definitions. * @return array Array keys are component codes, values are the details defined in the component. */ @@ -210,7 +211,7 @@ class ComponentManager )); } - $component = new $className($cmsObject, $properties); + $component = App::make($className, [$cmsObject, $properties]); $component->name = $name; return $component; diff --git a/tests/fixtures/plugins/october/tester/Plugin.php b/tests/fixtures/plugins/october/tester/Plugin.php index a0c80bac0..c3d5506fb 100644 --- a/tests/fixtures/plugins/october/tester/Plugin.php +++ b/tests/fixtures/plugins/october/tester/Plugin.php @@ -20,7 +20,8 @@ class Plugin extends PluginBase 'October\Tester\Components\Archive' => 'testArchive', 'October\Tester\Components\Post' => 'testPost', 'October\Tester\Components\MainMenu' => 'testMainMenu', - 'October\Tester\Components\ContentBlock' => 'testContentBlock' + 'October\Tester\Components\ContentBlock' => 'testContentBlock', + 'October\Tester\Components\Comments' => 'testComments', ]; } diff --git a/tests/fixtures/plugins/october/tester/classes/Users.php b/tests/fixtures/plugins/october/tester/classes/Users.php new file mode 100644 index 000000000..9456f61dd --- /dev/null +++ b/tests/fixtures/plugins/october/tester/classes/Users.php @@ -0,0 +1,12 @@ + 'Arquitecht and Importer/Exporter', + 'Carl' => 'where is he?', + ]; + } +} diff --git a/tests/fixtures/plugins/october/tester/components/Categories.php b/tests/fixtures/plugins/october/tester/components/Categories.php new file mode 100644 index 000000000..dd2ab1be5 --- /dev/null +++ b/tests/fixtures/plugins/october/tester/components/Categories.php @@ -0,0 +1,34 @@ + 'Blog Categories Dummy Component', + 'description' => 'Displays the list of categories in the blog.' + ]; + } + + public function posts() + { + return [ + ['title' => 'Lorum ipsum', 'content' => 'Post Content #1'], + ['title' => 'La Playa Nudista', 'content' => 'Second Post Content'] + ]; + } + + public function onTestAjax() + { + $this->page['var'] = 'page'; + } + +} diff --git a/tests/fixtures/plugins/october/tester/components/Comments.php b/tests/fixtures/plugins/october/tester/components/Comments.php new file mode 100644 index 000000000..227b02863 --- /dev/null +++ b/tests/fixtures/plugins/october/tester/components/Comments.php @@ -0,0 +1,43 @@ +users = $users; + } + + public function componentDetails() + { + return [ + 'name' => 'Blog Comments Dummy Component', + 'description' => 'Displays the list of comments on a post.' + ]; + } + + public function posts() + { + return [ + ['title' => 'Lorum ipsum', 'content' => 'Post Content #1'], + ['title' => 'La Playa Nudista', 'content' => 'Second Post Content'] + ]; + } + + public function onTestAjax() + { + $this->page['var'] = 'page'; + } + + public function getUsers() + { + return $this->users; + } + +} diff --git a/tests/unit/cms/classes/ComponentManagerTest.php b/tests/unit/cms/classes/ComponentManagerTest.php index 99d3601d6..a12255580 100644 --- a/tests/unit/cms/classes/ComponentManagerTest.php +++ b/tests/unit/cms/classes/ComponentManagerTest.php @@ -9,6 +9,19 @@ use Cms\Classes\ComponentManager; class ComponentManagerTest extends TestCase { + public function setUp() + { + parent::setUp(); + + include_once base_path() . '/tests/fixtures/plugins/october/tester/components/Archive.php'; + include_once base_path() . '/tests/fixtures/plugins/october/tester/components/Post.php'; + include_once base_path() . '/tests/fixtures/plugins/october/tester/components/MainMenu.php'; + include_once base_path() . '/tests/fixtures/plugins/october/tester/components/ContentBlock.php'; + include_once base_path() . '/tests/fixtures/plugins/october/tester/components/Comments.php'; + include_once base_path() . '/tests/fixtures/plugins/october/tester/classes/Users.php'; + } + + public function testListComponents() { $manager = ComponentManager::instance(); @@ -20,14 +33,9 @@ class ComponentManagerTest extends TestCase public function testListComponentDetails() { - include_once base_path() . '/tests/fixtures/plugins/october/tester/components/Archive.php'; - include_once base_path() . '/tests/fixtures/plugins/october/tester/components/Post.php'; - include_once base_path() . '/tests/fixtures/plugins/october/tester/components/MainMenu.php'; - include_once base_path() . '/tests/fixtures/plugins/october/tester/components/ContentBlock.php'; - $manager = ComponentManager::instance(); $components = $manager->listComponentDetails(); - + $this->assertArrayHasKey('testArchive', $components); $this->assertArrayHasKey('name', $components['testArchive']); $this->assertArrayHasKey('description', $components['testArchive']); @@ -41,6 +49,26 @@ class ComponentManagerTest extends TestCase $this->assertEquals('Displays a blog post.', $components['testPost']['description']); } + public function testGetComponentWithFactoryUsingAutomaticResolution() + { + $manager = ComponentManager::instance(); + $components = $manager->listComponentDetails(); + + $this->assertArrayHasKey('testComments', $components); + $this->assertArrayHasKey('name', $components['testComments']); + $this->assertArrayHasKey('description', $components['testComments']); + $this->assertEquals('Blog Comments Dummy Component', $components['testComments']['name']); + $this->assertEquals('Displays the list of comments on a post.', $components['testComments']['description']); + + $comments = $manager->makeComponent('testComments', $this->spoofPageCode(), []); + $users = $comments->getUsers()->getUsers(); + + $this->assertArrayHasKey('Art Vandelay', $users); + $this->assertArrayHasKey('Carl', $users); + $this->assertEquals('Arquitecht and Importer/Exporter', $users['Art Vandelay']); + $this->assertEquals('where is he?', $users['Carl']); + } + public function testFindByAlias() { $manager = ComponentManager::instance(); @@ -50,7 +78,6 @@ class ComponentManagerTest extends TestCase $component = $manager->resolve('testPost'); $this->assertEquals('\October\Tester\Components\Post', $component); - } public function testHasComponent()