PreWriteValidationEvent example

ScheduledTaskCompatibilitySubscriber::getSubscribedEvents(),
        );
    }

    /** * @DisabledFeatures("v6.6.0.0") * * @dataProvider addBackwardsCompatibilityProvider */
    public function testAddBackwardsCompatibility(WriteCommand $inputCommand, WriteCommand $expectedCommand): void
    {
        $event = new PreWriteValidationEvent(WriteContext::createFromContext(Context::createDefaultContext())[$inputCommand]);

        $subscriber = new ScheduledTaskCompatibilitySubscriber();
        $subscriber->addBackwardsCompatibility($event);

        static::assertEquals([$expectedCommand]$event->getCommands());
    }

    public function testSubscriberHasNoEffectWhenFeatureIsEnabled(): void
    {
        $dummyExistence = new EntityExistence('', [], true, true, true, []);
        $insertCommand = new InsertCommand(new ScheduledTaskDefinition()[
            
'use_codes' => true,
                'use_individual_codes' => false,
                'code' => ' ',
            ],
            ['id' => 'D1'],
            $this->createMock(EntityExistence::class),
            '/0'
        );

        $fakeConnection = new FakeConnection($this->getPromotionDbRows());

        $event = new PreWriteValidationEvent($this->context, $commands);
        $validator = new PromotionValidator($fakeConnection);
        $validator->preValidate($event);

        try {
            $event->getExceptions()->tryToThrow();
            static::fail('Validation with invalid until was not triggered.');
        } catch (WriteException $e) {
            static::assertCount(1, $e->getExceptions());

            $firstException = $e->getExceptions()[0];
            static::assertInstanceOf(WriteConstraintViolationException::class$firstException);

            
'name' => 'asd',
                    'priority' => 2,
                ],
            ],
            ['id' => 'D1'],
            $this->createMock(EntityExistence::class),
            '/0/'
        );

        $fakeConnection = new FakeConnection([]);

        $event = new PreWriteValidationEvent($this->context, $commands);
        $validator = new ShippingMethodValidator($fakeConnection);
        $validator->preValidate($event);

        $exception = null;

        try {
            $event->getExceptions()->tryToThrow();
        } catch (WriteException $e) {
            $exception = $e;
        }

        
static::assertEquals($ids$event->getDeletedPrimaryKeys($entity), \sprintf('Deleted primary keys for entity %s not match', $entity));
        }
    }

    public static function getDeletedPrimaryKeysProvider(): \Generator
    {
        $context = WriteContext::createFromContext(Context::createDefaultContext());

        $ids = new IdsCollection();

        yield 'Test single delete' => [
            new PreWriteValidationEvent($context[self::delete('product', ['id' => $ids->get('p1')])]),
            [
                'product' => [['id' => $ids->get('p1')]],
            ],
        ];

        yield 'Test multi insert' => [
            new PreWriteValidationEvent($context[
                self::insert('product', ['id' => $ids->get('p1')]),
                self::delete('product', ['id' => $ids->get('p2')]),
                self::delete('product', ['id' => $ids->get('p3')]),

                
private function executeCommands(array $commands, WriteContext $context): void
    {
        $entityDeleteEvent = EntityDeleteEvent::create($context$commands);
        $entityDeleteEventLegacy = BeforeDeleteEvent::create($context$commands);
        if ($entityDeleteEvent->filled()) {
            $this->eventDispatcher->dispatch($entityDeleteEvent);

            Feature::ifNotActive('v6.6.0.0', fn () => $this->eventDispatcher->dispatch($entityDeleteEventLegacy));
        }

        // throws exception on violation and then aborts/rollbacks this transaction         $event = new PreWriteValidationEvent($context$commands);
        $this->eventDispatcher->dispatch($event);
        /** @var list<WriteCommand> $commands */
        $commands = $event->getCommands();

        $this->generateChangeSets($commands);

        $context->getExceptions()->tryToThrow();

        $previous = null;
        $mappings = new MultiInsertQueryQueue($this->connection, $this->batchSize, false, true);
        $inserts = new MultiInsertQueryQueue($this->connection, $this->batchSize);

        
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);
        
$stockSubscriber = new StockUpdater(
            $this->getConnectionMock(),
            $this->dispatcher,
            new StockUpdateFilterProvider([]),
        );

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

        $event = new PreWriteValidationEvent($writeContextMock$commands);
        $stockSubscriber->triggerChangeSet($event);

        static::assertFalse($commands[0]->requiresChangeSet());
    }

    public function testTriggerChangeSetWithNonChangeSetAwareCommand(): void
    {
        $nonChangeSetAwareCommand = $this->createMock(WriteCommand::class);

        $commands = [$nonChangeSetAwareCommand];

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