Browse Source

Fix bugs and add tests

Mark Story 1 year ago
parent
commit
d781f0156f

+ 3 - 0
src/Controller/ComponentRegistry.php

@@ -65,6 +65,9 @@ class ComponentRegistry extends ObjectRegistry implements EventDispatcherInterfa
             $this->setController($controller);
         }
         $this->container = $container;
+        if ($this->container) {
+            $this->container->add(self::class, $this);
+        }
     }
 
     /**

+ 30 - 0
tests/TestCase/Controller/ComponentRegistryTest.php

@@ -21,6 +21,7 @@ use Cake\Controller\Component\FormProtectionComponent;
 use Cake\Controller\ComponentRegistry;
 use Cake\Controller\Controller;
 use Cake\Controller\Exception\MissingComponentException;
+use Cake\Core\Container;
 use Cake\Http\ServerRequest;
 use Cake\TestSuite\TestCase;
 use Countable;
@@ -34,6 +35,7 @@ class ComponentRegistryTest extends TestCase
      * @var \Cake\Controller\ComponentRegistry
      */
     protected $Components;
+    private bool $created = false;
 
     /**
      * setUp
@@ -72,6 +74,34 @@ class ComponentRegistryTest extends TestCase
     }
 
     /**
+     * test load() with the container set
+     */
+    public function testLoadWithContainer(): void
+    {
+        $controller = new Controller(new ServerRequest());
+        $container = new Container();
+        $components = new ComponentRegistry($controller, $container);
+        $this->assertEquals([], $components->loaded());
+
+        $container->add(FlashComponent::class, function (ComponentRegistry $registry, array $config) {
+            $this->created = true;
+
+            return new FlashComponent($registry, $config);
+        })
+        ->addArgument(ComponentRegistry::class)
+        ->addArgument(['key' => 'customFlash']);
+
+        $flash = $components->load('Flash');
+
+        // Container was modified for the current registry and our factory was called
+        $this->assertTrue($container->has(ComponentRegistry::class));
+        $this->assertTrue($this->created);
+
+        $this->assertInstanceOf(FlashComponent::class, $flash);
+        $this->assertSame('customFlash', $flash->getConfig('key'));
+    }
+
+    /**
      * Tests loading as an alias
      */
     public function testLoadWithAlias(): void

+ 30 - 0
tests/TestCase/Controller/ControllerFactoryTest.php

@@ -16,6 +16,8 @@ declare(strict_types=1);
  */
 namespace Cake\Test\TestCase\Controller;
 
+use Cake\Controller\Component\FlashComponent;
+use Cake\Controller\ComponentRegistry;
 use Cake\Controller\ControllerFactory;
 use Cake\Controller\Exception\InvalidParameterException;
 use Cake\Core\Container;
@@ -899,6 +901,34 @@ class ControllerFactoryTest extends TestCase
         $this->assertSame(['one' => '1'], $data);
     }
 
+    /**
+     * Test that default values work for typed parameters
+     */
+    public function testInvokeComponentFromContainer(): void
+    {
+        $this->container->add(FlashComponent::class, function (ComponentRegistry $registry, array $config) {
+            return new FlashComponent($registry, $config);
+        })
+        ->addArgument(ComponentRegistry::class)
+        ->addArgument(['key' => 'customFlash']);
+
+        $request = new ServerRequest([
+            'url' => 'test_plugin_three/component-test/flash',
+            'params' => [
+                'plugin' => null,
+                'controller' => 'ComponentTest',
+                'action' => 'flash',
+                'pass' => [],
+            ],
+        ]);
+        $controller = $this->factory->create($request);
+
+        $result = $this->factory->invoke($controller);
+        $data = json_decode((string)$result->getBody(), true);
+
+        $this->assertSame(['flashKey' => 'customFlash'], $data);
+    }
+
     public function testMiddleware(): void
     {
         $request = new ServerRequest([

+ 26 - 0
tests/TestCase/Controller/ControllerTest.php

@@ -17,9 +17,12 @@ declare(strict_types=1);
 namespace Cake\Test\TestCase\Controller;
 
 use AssertionError;
+use Cake\Controller\Component\FlashComponent;
+use Cake\Controller\ComponentRegistry;
 use Cake\Controller\Controller;
 use Cake\Controller\Exception\MissingActionException;
 use Cake\Core\Configure;
+use Cake\Core\Container;
 use Cake\Datasource\Paging\PaginatedInterface;
 use Cake\Event\Event;
 use Cake\Event\EventInterface;
@@ -981,6 +984,29 @@ class ControllerTest extends TestCase
     }
 
     /**
+     * Test adding a component with container passed to controller
+     */
+    public function testLoadComponentWithContainer(): void
+    {
+        $container = new Container();
+        $container->add(FlashComponent::class, function (ComponentRegistry $registry, array $config) {
+            return new FlashComponent($registry, $config);
+        })
+        ->addArgument(ComponentRegistry::class)
+        ->addArgument(['key' => 'customFlash']);
+
+        $request = new ServerRequest(['url' => '/']);
+
+        $controller = new TestController($request);
+        $result = $controller->loadComponent('Flash');
+        $this->assertInstanceOf(FlashComponent::class, $result);
+        $this->assertSame($result, $controller->Flash);
+
+        $registry = $controller->components();
+        $this->assertTrue(isset($registry->Flash));
+    }
+
+    /**
      * Test the isAction method.
      */
     public function testIsAction(): void

+ 13 - 6
tests/test_app/TestApp/Controller/ComponentTestController.php

@@ -22,10 +22,17 @@ use Cake\Controller\Controller;
  */
 class ComponentTestController extends Controller
 {
-    /**
-     * uses property
-     *
-     * @var array
-     */
-    public $uses = [];
+    public function initialize(): void
+    {
+        parent::initialize();
+
+        $this->loadComponent('Flash');
+    }
+
+    public function flash()
+    {
+        return $this->response->withStringBody(json_encode([
+            'flashKey' => $this->Flash->getConfig('key'),
+        ]));
+    }
 }