DeleteCommand example

$ids = new IdsCollection();

        $context = Context::createDefaultContext();
        $writeContext = WriteContext::createFromContext($context);

        $registry = new StaticDefinitionInstanceRegistry(
            [new ProductDefinition()],
            $this->createMock(ValidatorInterface::class),
            $this->createMock(EntityWriteGatewayInterface::class)
        );

        $command = new DeleteCommand(
            $registry->getByEntityName('product'),
            ['id' => $ids->get('p1')],
            new EntityExistence('product', ['id' => $ids->get('p1')], true, true, true, [])
        );

        $event = EntityWriteEvent::create($writeContext[
            $command,
        ]);

        static::assertSame($writeContext$event->getWriteContext());
        static::assertSame($context$event->getContext());
        
$gateway = new EntityWriteGateway(
            1,
            $connection,
            $this->getContainer()->get('event_dispatcher'),
            $this->getContainer()->get(ExceptionHandlerRegistry::class),
            $definitionRegistry
        );

        $writeContext = WriteContext::createFromContext(Context::createDefaultContext());

        $command = new DeleteCommand(
            $definitionRegistry->getByEntityName('product'),
            $delete[0],
            new EntityExistence('product', $delete[0], true, true, true, [])
        );

        $exceptionThrown = null;

        try {
            $gateway->execute([$command]$writeContext);
        } catch (Exception $exception) {
            $exceptionThrown = $exception;
        }
static::assertFalse($command->hasField('available_stock'));
    }

    public function testThatDeleteCommandIsIgnored(): void
    {
        $context = Context::createDefaultContext();

        $subscriber = new AvailableStockMirrorSubscriber();

        $definition = $this->getDefinition();

        $command = new DeleteCommand(
            $definition,
            ['id' => $this->ids->getBytes('product-1')],
            new EntityExistence(
                ProductDefinition::ENTITY_NAME,
                ['id' => $this->ids->get('product-1')],
                true,
                false,
                false,
                []
            ),
        );

        
static::assertNotInstanceOf(ChangeSetAware::class$commands[0]);
    }

    public function testTriggerChangeSetWithDeleteCommand(): void
    {
        $definition = new OrderLineItemDefinition();
        $primaryKey = ['id' => 'some_id'];
        $existence = new EntityExistence('order_line_item', [], false, false, false, []);

        $commands = [
            new DeleteCommand(
                $definition,
                $primaryKey,
                $existence
            ),
        ];

        $writeContextMock = $this->getMockBuilder(WriteContext::class)
            ->disableOriginalConstructor()
            ->getMock();

        $context = Context::createDefaultContext();
        
public function testTriggerChangeset(): void
    {
        $fieldCollection = $this->definition->getFields();

        $oneToManyField = $fieldCollection->get('oneToMany');
        $manyToOneField = $fieldCollection->get('manyToOne');

        static::assertInstanceOf(OneToManyAssociationField::class$oneToManyField);
        static::assertInstanceOf(ManyToOneAssociationField::class$manyToOneField);

        $event = new PreWriteValidationEvent(WriteContext::createFromContext(Context::createDefaultContext())[
            new DeleteCommand($oneToManyField->getReferenceDefinition()[]$this->createMock(EntityExistence::class)),
            new UpdateCommand($manyToOneField->getReferenceDefinition()[][]$this->createMock(EntityExistence::class), ''),
            new UpdateCommand($oneToManyField->getReferenceDefinition()['rule_id' => 'foo'][]$this->createMock(EntityExistence::class), ''),
        ]);

        $this->areaUpdater->triggerChangeSet($event);

        /** @var DeleteCommand[]|UpdateCommand[] $commands */
        $commands = $event->getCommands();

        static::assertCount(3, $commands);
        static::assertTrue($commands[0]->requiresChangeSet());
        
$ids = new IdsCollection();

        $context = Context::createDefaultContext();
        $writeContext = WriteContext::createFromContext($context);

        $registry = new StaticDefinitionInstanceRegistry(
            [new ProductDefinition()],
            $this->createMock(ValidatorInterface::class),
            $this->createMock(EntityWriteGatewayInterface::class)
        );

        $command = new DeleteCommand(
            $registry->getByEntityName('product'),
            ['id' => $ids->get('p1')],
            new EntityExistence('product', ['id' => $ids->get('p1')], true, true, true, [])
        );

        $event = EntityDeleteEvent::create($writeContext[
            $command,
        ]);

        static::assertSame($writeContext$event->getWriteContext());
        static::assertSame($context$event->getContext());
        
$stockSubscriber = new OrderStockSubscriber(
            $this->createMock(Connection::class),
            $stockStorage,
            true
        );

        $definition = $this->getDefinition(ProductDefinition::class);

        $event = EntityWriteEvent::create(
            WriteContext::createFromContext($context),
            [
                new DeleteCommand(
                    $definition,
                    ['id' => $this->ids->getBytes('item-1')],
                    new EntityExistence(
                        OrderLineItemDefinition::ENTITY_NAME,
                        ['id' => $this->ids->get('item-1')],
                        true,
                        false,
                        false,
                        []
                    ),
                ),
            ],
'product_review.written' => 'createReview',
            EntityDeleteEvent::class => 'detectChangeset',
            'product_review.deleted' => 'onReviewDeleted',
        ]$this->productReviewSubscriber->getSubscribedEvents());
    }

    public function testDetectChangesetWithReviewDeleteEvent(): void
    {
        $event = EntityDeleteEvent::create(
            WriteContext::createFromContext(Context::createDefaultContext()),
            [
                new DeleteCommand(
                    new ProductReviewDefinition(),
                    [
                        'id' => 'foo',
                    ],
                    new EntityExistence(ProductReviewDefinition::ENTITY_NAME, ['id' => 'foo'], true, false, false, [])
                ),
            ]
        );

        foreach ($event->getCommands() as $command) {
            static::assertInstanceOf(ChangeSetAware::class$command);
            
$definition = self::getDefinition($entity);

        $existence = new EntityExistence('', [], false, false, false, []);

        return new InsertCommand($definition[]$primaryKey$existence, '');
    }

    private static function delete(string $entity, array $primaryKey): DeleteCommand
    {
        $definition = self::getDefinition($entity);

        return new DeleteCommand($definition$primaryKeynew EntityExistence('', [], false, false, false, []));
    }

    private static function getDefinition(string $entity): EntityDefinition
    {
        $definition = new class() extends EntityDefinition {
            private string $entityName;

            public function getEntityName(): string
            {
                return $this->entityName;
            }

            
$definition = self::getDefinition($entity);

        $existence = new EntityExistence('', [], false, false, false, []);

        return new InsertCommand($definition[]$primaryKey$existence, '');
    }

    private static function delete(string $entity, array $primaryKey): DeleteCommand
    {
        $definition = self::getDefinition($entity);

        return new DeleteCommand($definition$primaryKeynew EntityExistence('', [], false, false, false, []));
    }

    private static function getDefinition(string $entity): EntityDefinition
    {
        $definition = new class() extends EntityDefinition {
            private string $entityName;

            public function getEntityName(): string
            {
                return $this->entityName;
            }

            
$mappedBytes[$field->getStorageName()] = $field->getSerializer()->encode(
                    $field,
                    EntityExistence::createForEntity($definition->getEntityName()[$key => $value]),
                    new KeyValuePair($key$value, true),
                    $parameters,
                )->current();
            }

            $existence = $this->gateway->getExistence($definition$mappedBytes[]$commandQueue);

            if ($existence->exists()) {
                $commandQueue->add($definitionnew DeleteCommand($definition$mappedBytes$existence));

                continue;
            }

            $stripped = [];
            /** * @var string $key * @var string $value */
            foreach ($primaryKey as $key => $value) {
                $field = $definition->getFields()->get($key);

                
Home | Imprint | This part of the site doesn't use cookies.