Browse Source

Add constructor injection for the eventManager.

This should help enable the ActionDispatcher inside the present day
Dispatcher.
Mark Story 10 years ago
parent
commit
5a90c3dec2
2 changed files with 21 additions and 1 deletions
  1. 6 1
      src/Http/ActionDispatcher.php
  2. 15 0
      tests/TestCase/Http/ActionDispatcherTest.php

+ 6 - 1
src/Http/ActionDispatcher.php

@@ -54,9 +54,14 @@ class ActionDispatcher
      * Constructor
      *
      * @param \Cake\Http\ControllerFactory $factory A controller factory instance.
+     * @param \Cake\Event\EventManager $eventManager An event manager if you want to inject one.
      */
-    public function __construct($factory = null)
+    public function __construct($factory = null, $eventManager = null)
     {
+        if ($eventManager) {
+            $this->eventManager($eventManager);
+        }
+
         // Compatibility with DispatcherFilters.
         foreach (DispatcherFactory::filters() as $filter) {
             $this->addFilter($filter);

+ 15 - 0
tests/TestCase/Http/ActionDispatcherTest.php

@@ -55,6 +55,21 @@ class ActionDispatcherTest extends TestCase
     }
 
     /**
+     * Ensure the constructor args end up on the right protected properties.
+     *
+     * @return void
+     */
+    public function testConstructorArgs()
+    {
+        $factory = $this->getMock('Cake\Http\ControllerFactory');
+        $events = $this->getMock('Cake\Event\EventManager');
+        $dispatcher = new ActionDispatcher($factory, $events);
+
+        $this->assertAttributeSame($events, '_eventManager', $dispatcher);
+        $this->assertAttributeSame($factory, 'factory', $dispatcher);
+    }
+
+    /**
      * Ensure that filters connected to the DispatcherFactory are
      * also applied
      */