2

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.

7
  • Did you clear the test sandbox after enabling your module?
    – Alex
    Commented Oct 4, 2021 at 10:08
  • 1
    @Alex yes, I cleaned it.
    – kolaente
    Commented Oct 4, 2021 at 12:42
  • I also faced such issue in 2.4.3 while using preference it wasn't working so I changed it with the plugin that works......try using preference
    – Asad Ullah
    Commented Oct 4, 2021 at 20:59
  • 1
    @AsadUllah But preference is not a good practice to do.
    – kolaente
    Commented Oct 5, 2021 at 10:54
  • the first option is a plugin if that is not working then the preference
    – Asad Ullah
    Commented Oct 5, 2021 at 11:33

1 Answer 1

-1

$request->setPathInfo('checkout/cart/index');

$this->dispatch($request);

1
  • dispatch expects a string
    – Alex
    Commented Oct 7, 2021 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.