I have a plugin that plugs into the execute
method of \Magento\Checkout\Controller\Cart\Index
and redirects under some condition.
di.xml:
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Controller\Cart\Index">
<plugin sortOrder="1" name="do_stuff_with_cart" type="MyVendor\MyModule\Plugin\CartPlugin"/>
</type>
</config>
The Plugin itself:
<?php
namespace MyVendor\MyModule\Plugin;
use MyVendor\MyModule\Helper\Data;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\ActionFlag;
use Magento\Framework\App\Response\RedirectInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Event\Observer;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Pricing\Render;
class CartPlugin
{
protected Data $helper;
protected RedirectInterface $redirect;
protected ResultFactory $resultFactory;
public function __construct(
Data $helper,
RedirectInterface $redirect,
\Magento\Framework\Controller\ResultFactory $resultFactory
) {
$this->helper = $helper;
$this->resultFactory = $resultFactory;
$this->redirect = $redirect;
}
public function aroundExecute($subject, callable $proceed)
{
if ($this->helper->isEnabled()) {
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setPath('vendor/module/index');
return $resultRedirect;
}
return $proceed();
}
}
When I access the /checkout/index/
route locally in my dev environment, everything works as expected. However, this integration test fails:
<?php
namespace MyVendor\MyModule\Test\Integration;
use Magento\TestFramework\TestCase\AbstractController;
class FormTest extends AbstractController
{
/**
* @magentoConfigFixture current_store vendor/module/enabled 1
*/
public function testShouldRedirectToForm()
{
$this->dispatch('checkout/cart/index');
$this->assertRedirect($this->equalTo('vendor/module/index/'));
}
}
I debugged into my plugin and it looks like the method is never called, even a die('HERE')
in the aroundExecute
method does not work. I've tried all the usual stuff (clearing cache, sandbox, generated) already.
When I dump $this->getResponse()
in the test, it has a status code of 200 but the response content itself is empty.
EDIT: I've tried moving the plugin configuration from etc/frontend/di.xml
to etc/di.xml
with no success.