StockAlteration example

use Shopware\Core\Content\Product\Stock\StockAlteration;

/** * @internal * * @covers \Shopware\Core\Content\Product\Stock\StockAlteration */
class StockAlterationTest extends TestCase
{
    public function testAccessors(): void
    {
        $alteration = new StockAlteration('12345', '67890', 10, 5);

        static::assertEquals('12345', $alteration->lineItemId);
        static::assertEquals('67890', $alteration->productId);
        static::assertEquals(10, $alteration->quantityBefore);
        static::assertEquals(5, $alteration->newQuantity);
        static::assertEquals(5, $alteration->quantityDelta());

        $alteration = new StockAlteration('12345', '67890', 3, 10);

        static::assertEquals('12345', $alteration->lineItemId);
        static::assertEquals('67890', $alteration->productId);
        
// the quantity changed         if ($lineItem['quantity'] !== $afterLine['quantity']) {
            return [$this->changeset($id$lineItem['referenced_id'](int) $lineItem['quantity'](int) $afterLine['quantity'])];
        }

        return [];
    }

    private function changeset(string $id, string $productId, int $qtyBefore, int $newQty): StockAlteration
    {
        return new StockAlteration($id$productId$qtyBefore$newQty);
    }

    /** * @return array<string> */
    private function getAffectedIds(EntityWriteEvent $event): array
    {
        return array_map(
            static fn (WriteCommand $command) => $command->getPrimaryKey()['id'],
            array_filter($event->getCommandsForEntity(OrderLineItemDefinition::ENTITY_NAME)static function DWriteCommand $command) {
                if ($command instanceof DeleteCommand || $command instanceof InsertCommand) {
                    
Home | Imprint | This part of the site doesn't use cookies.