Browse Source

Tests for widgets from file

Denys Kyselov 11 years ago
parent
commit
3f2b2b9551

+ 19 - 3
src/View/Widget/WidgetRegistry.php

@@ -74,15 +74,31 @@ class WidgetRegistry {
 		$this->_templates = $templates;
 		if (!empty($widgets)) {
 			if (is_string($widgets)) {
-				$loader = new PhpConfig();
-				$widgets = $loader->read($widgets);
+				$this->load($widgets);
+			} else {
+				$this->add($widgets);
 			}
-			$this->add($widgets);
 		}
 		$this->add(['_view' => $view]);
 	}
 
 /**
+ * Load a config file containing widgets.
+ *
+ * Widget files should define a `$config` variable containing
+ * all the widgets to load. Loaded widgets will be merged with existing
+ * widgets.
+ *
+ * @param string $file The file to load
+ * @return void
+ */
+	public function load($file) {
+		$loader = new PhpConfig();
+		$widgets = $loader->read($file);
+		$this->add($widgets);
+	}
+
+/**
  * Adds or replaces existing widget instances/configuration with new ones.
  *
  * Widget arrays can either be descriptions or instances. For example:

+ 22 - 0
tests/TestCase/View/Widget/WidgetRegistryTest.php

@@ -18,6 +18,7 @@ use Cake\TestSuite\TestCase;
 use Cake\View\StringTemplate;
 use Cake\View\View;
 use Cake\View\Widget\WidgetRegistry;
+use Cake\Core\Plugin;
 
 /**
  * WidgetRegistry test case
@@ -50,6 +51,27 @@ class WidgetRegistryTestCase extends TestCase {
 	}
 
 /**
+ * Test loading widgets files in the app.
+ *
+ * @return void
+ */
+	public function testLoadInConstructor() {
+		$inputs = new WidgetRegistry($this->templates, $this->view, 'test_widgets');
+		$this->assertInstanceOf('Cake\View\Widget\Label', $inputs->get('text'));
+	}
+
+/**
+ * Test loading templates files from a plugin
+ *
+ * @return void
+ */
+	public function testLoadPluginInConstuctor() {
+		Plugin::load('TestPlugin');
+		$inputs = new WidgetRegistry($this->templates, $this->view, 'TestPlugin.test_widgets');
+		$this->assertInstanceOf('Cake\View\Widget\Label', $inputs->get('text'));
+	}
+
+/**
  * Test adding new widgets.
  *
  * @return void

+ 7 - 0
tests/test_app/Plugin/TestPlugin/config/test_widgets.php

@@ -0,0 +1,7 @@
+<?php
+/**
+ * Widgets list for testing.
+ */
+$config = [
+	'text' => ['Cake\View\Widget\Label'],
+];

+ 7 - 0
tests/test_app/config/test_widgets.php

@@ -0,0 +1,7 @@
+<?php
+/**
+ * Widgets list for testing.
+ */
+$config = [
+	'text' => ['Cake\View\Widget\Label'],
+];