CartHook example

$this->script = new Script('test', '', new \DateTimeImmutable());
    }

    /** * @dataProvider addProductProvider */
    public function testAddProduct(string $input, ?string $expected): void
    {
        $context = $this->getContainer()->get(SalesChannelContextFactory::class)
            ->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL, []);

        $hook = new CartHook($this->createCart()$context);

        $service = $this->getContainer()->get(CartFacadeHookFactory::class)
            ->factory($hook$this->script);

        $service->products()->add($this->ids->get($input));
        $service->calculate();

        $item = $service->products()->get($this->ids->get($input));

        if ($expected === null) {
            static::assertNull($item);

            
                foreach ($original->getLineItems()->getFlat() as $item) {
                    $item->markUnModifiedByApp();
                }
            }

            // move data from previous calculation into new cart             $cart->setData($original->getData());

            $this->runProcessors($original$cart$context$behavior);

            if ($behavior->hookAware()) {
                $this->executor->execute(new CartHook($cart$context));
            }

            $this->calculateAmount($context$cart);

            $cart->addErrors(
                ...$this->validator->validate($cart$context)
            );

            $cart->setTransactions(
                $this->transactionProcessor->process($cart$context)
            );

            
/** * @internal * * @covers \Shopware\Core\Checkout\Cart\Hook\CartHook */
class CartHookTest extends TestCase
{
    public function testNameRespectsCartSource(): void
    {
        $cart = new Cart('test');
        $cart->setSource('test');
        $hook = new CartHook($cart$this->createMock(SalesChannelContext::class));

        static::assertEquals('cart-test', $hook->getName());
    }

    public function testNameWithoutCartSource(): void
    {
        $cart = new Cart('test');
        $hook = new CartHook($cart$this->createMock(SalesChannelContext::class));

        static::assertEquals('cart', $hook->getName());
    }
}
Home | Imprint | This part of the site doesn't use cookies.