EntityWrittenEvent example



            $mergedWriteResults[] = $writeResult;
        }

        $mergedWriteResults = array_filter($mergedWriteResults);

        if (empty($mergedWriteResults)) {
            return null;
        }

        return new EntityWrittenEvent(
            $writtenEvent->getEntityName(),
            $mergedWriteResults,
            $writtenEvent->getContext(),
            $writtenEvent->getErrors()
        );
    }

    private function getMergedWriteResult(
        EntityWrittenEvent $translationEvent,
        EntityWriteResult $writeResult
    ): ?EntityWriteResult {
        
'promotion' => $this->indexer],
            $connection,
            $this->createMock(MessageBusInterface::class),
            $this->createMock(EventDispatcherInterface::class),
            $client,
            $searchHelper,
            [],
            []
        );

        $index->refresh(new EntityWrittenContainerEvent(Context::createDefaultContext()new NestedEventCollection([
            new EntityWrittenEvent('promotion', [
                new EntityWriteResult(
                    'c1a28776116d4431a2208eb2960ec340',
                    [],
                    'promotion',
                    EntityWriteResult::OPERATION_INSERT
                ),
            ], Context::createDefaultContext()),
        ])[]));
    }

    public function testHandle(): void
    {
->method('getAllUsedIndices');

        $connection = $this->createMock(Connection::class);

        $customFieldUpdater = new CustomFieldUpdater(
            $indexDetector,
            $this->createMock(Client::class),
            $elasticsearchHelper,
            $connection
        );

        $event = new EntityWrittenEvent(CustomFieldDefinition::ENTITY_NAME, [], Context::createDefaultContext());

        $containerEvent = new EntityWrittenContainerEvent(
            Context::createDefaultContext(),
            new NestedEventCollection([$event]),
            []
        );

        $customFieldUpdater->onNewCustomFieldCreated($containerEvent);
    }

    public function testCustomFieldUpdatedChangesNothing(): void
    {


        static::assertEquals($expectedProperties$properties);
    }

    public function testRefresh(): void
    {
        $c = $this->getContainer()->get(Connection::class);
        static::assertEmpty($c->fetchAllAssociative('SELECT `index` FROM `admin_elasticsearch_index_task`'));

        $this->registry->refresh(new EntityWrittenContainerEvent(Context::createDefaultContext()new NestedEventCollection([
            new EntityWrittenEvent('promotion', [
                new EntityWriteResult(
                    'c1a28776116d4431a2208eb2960ec340',
                    [],
                    'promotion',
                    EntityWriteResult::OPERATION_INSERT
                ),
            ], Context::createDefaultContext()),
        ])[]));

        $index = $c->fetchOne('SELECT `index` FROM `admin_elasticsearch_index_task`');

        
private function getEntityWrittenEvent(array $ids = [], bool $invalidEntity = false): EntityWrittenEvent
    {
        $entity = $invalidEntity ? ProductDefinition::ENTITY_NAME : ProductReviewDefinition::ENTITY_NAME;

        $writtenResults = [];
        foreach ($ids as $id) {
            $writtenResult = $this->createMock(EntityWriteResult::class);
            $writtenResult->method('getPrimaryKey')->willReturn($id);
            $writtenResults[] = $writtenResult;
        }

        return new EntityWrittenEvent($entity$writtenResults, Context::createDefaultContext());
    }
}
$subscriber = new LanguageSubscriber(
            $elasticsearchHelper,
            $this->createMock(ElasticsearchRegistry::class),
            $this->createMock(Client::class),
            $this->createMock(ProductDefinition::class),
            $bus
        );

        $writeResultCreated = new EntityWriteResult('1', ['languageId' => '1'], 'sales_channel_language', EntityWriteResult::OPERATION_INSERT);
        $writeResultUpdated = new EntityWriteResult('1', ['languageId' => '1'], 'sales_channel_language', EntityWriteResult::OPERATION_UPDATE);
        $event = new EntityWrittenEvent('sales_channel_language', [$writeResultCreated$writeResultUpdated], Context::createDefaultContext());
        $subscriber->onSalesChannelWritten($event);

        static::assertCount(1, $bus->getMessages());
    }

    public function testOnNewLanguageIndexExistsAlready(): void
    {
        Feature::skipTestIfActive('ES_MULTILINGUAL_INDEX', $this);

        $bus = $this->createMock(MessageBusInterface::class);
        $bus->expects(static::never())->method('dispatch');

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

        $idA = Uuid::randomHex();
        $idB = Uuid::randomBytes();
        $idC = Uuid::randomBytes();
        $idD = Uuid::randomBytes();

        $event = new EntityWrittenContainerEvent($contextnew NestedEventCollection([
            new EntityWrittenEvent('many_to_one', [
                new EntityWriteResult($idA[], 'many_to_one', EntityWriteResult::OPERATION_INSERT),
            ]$context[]),
            new EntityWrittenEvent('one_to_many', [
                new EntityWriteResult($idA['ruleId' => $idA], 'one_to_many', EntityWriteResult::OPERATION_INSERT),
                new EntityWriteResult($idA[], 'one_to_many', EntityWriteResult::OPERATION_UPDATE, null, new ChangeSet(
                    ['rule_id' => $idB],
                    ['rule_id' => $idC],
                    false
                )),
                new EntityWriteResult($idA[], 'one_to_many', EntityWriteResult::OPERATION_DELETE, null, new ChangeSet(
                    ['rule_id' => $idD],
                    [
static::assertNotNull($data[0]['deliveries'][0]);
                static::assertSame($data[0]['deliveries'][0]['stateId']$orderEntity->getDeliveries()?->first()?->getStateId());

                static::assertSame($context->getTaxState(), CartPrice::TAX_STATE_FREE);

                /** @var CartPrice $price */
                $price = $data[0]['price'];

                static::assertSame($price->getTaxStatus(), CartPrice::TAX_STATE_FREE);

                return new EntityWrittenContainerEvent(Context::createDefaultContext()new NestedEventCollection([
                    new EntityWrittenEvent('order', [new EntityWriteResult('created-id', [], 'order', EntityWriteResult::OPERATION_INSERT)], Context::createDefaultContext()),
                ])[]);
            });

        $this->orderConverter
            ->expects(static::once())
            ->method('assembleSalesChannelContext')
            ->willReturnCallback(function DOrderEntity $order, Context $context) {
                $context->setTaxState($order->getTaxStatus());

                $salesChannel = new SalesChannelEntity();
                $salesChannel->setId(Uuid::randomHex());

                
$writeResults = [];
        foreach ($snippetIds as $snippetId) {
            $writeResults[] = new EntityWriteResult(
                $snippetId,
                [],
                SnippetDefinition::ENTITY_NAME,
                EntityWriteResult::OPERATION_INSERT
            );
        }

        $event = new EntityWrittenEvent(
            SnippetDefinition::ENTITY_NAME,
            $writeResults,
            $this->context
        );

        $this->cacheInvalidator->expects(static::once())->method('invalidate')->with([
            'translation.catalog.' . $ids->get('snippetSet1'),
            'translation.catalog.' . $ids->get('snippetSet2'),
        ], true);

        $this->translatorCacheInvalidate->invalidate($event);
    }
$writeResults[] = new EntityWriteResult(
                empty($primaryKey) ? Uuid::randomHex() : $primaryKey,
                $payload,
                $this->getDummyEntityName(),
                $operation
            );
        }

        if ($operation === EntityWriteResult::OPERATION_DELETE) {
            $event = new EntityDeletedEvent($this->getDummyEntityName()$writeResults$context);
        } else {
            $event = new EntityWrittenEvent($this->getDummyEntityName()$writeResults$context);
        }

        return new NestedEventCollection([$event]);
    }

    /** * @param mixed[] $payload * * @return mixed[] */
    private function getDummyPrimaryKeys(array $payload): array
    {

        static::assertFalse($event->isAllowed(
            Uuid::randomHex(),
            $notAllowedPermissions
        ));
    }

    private function getEntityWrittenEvent(string $entityId): EntityWrittenEvent
    {
        $context = Context::createDefaultContext();

        return new EntityWrittenEvent(
            ProductDefinition::ENTITY_NAME,
            [
                new EntityWriteResult(
                    $entityId,
                    [],
                    ProductDefinition::ENTITY_NAME,
                    EntityWriteResult::OPERATION_DELETE,
                    null,
                    null
                ),
            ],
            
public function testSkip(): void
    {
        $writeResults = [
            new EntityWriteResult(
                'created-id',
                ['id' => Uuid::randomHex(), 'salutationId' => Uuid::randomHex()],
                'customer',
                EntityWriteResult::OPERATION_INSERT
            ),
        ];

        $event = new EntityWrittenEvent(
            'customer',
            $writeResults,
            Context::createDefaultContext(),
            [],
        );

        $this->connection->expects(static::never())->method('executeUpdate');

        $this->salutationSubscriber->setDefaultSalutation($event);
    }

    
'url' => 'https://test.com',
            ],
        ]$context);

        $this->appendNewResponse(new Response(200));

        $id = Uuid::randomHex();

        $event = new EntityWrittenContainerEvent(
            $context,
            new NestedEventCollection([
                new EntityWrittenEvent(
                    ProductDefinition::ENTITY_NAME,
                    [
                        new EntityWriteResult(
                            $id,
                            [
                                'id' => $id,
                                'name' => 'testProduct',
                                'productNumber' => 'SWC-1000',
                                'stock' => 100,
                                'manufacturer' => [
                                    'name' => 'app creator',
                                ],
static::assertCount(1, $entity->getApiFilter());
        static::assertSame('range', $entity->getApiFilter()[0]['type']);
        static::assertSame([RangeFilter::GTE => 10]$entity->getApiFilter()[0]['parameters']);
        static::assertFalse($entity->isInvalid());
    }

    private function createWrittenEvent(string $id): EntityWrittenContainerEvent
    {
        return new EntityWrittenContainerEvent(
            $this->context,
            new NestedEventCollection([
                new EntityWrittenEvent(
                    ProductStreamDefinition::ENTITY_NAME,
                    [new EntityWriteResult($id[], ProductStreamDefinition::ENTITY_NAME, EntityWriteResult::OPERATION_INSERT, null)],
                    Context::createDefaultContext()
                ),
            ]),
            []
        );
    }
}
->method('persistFileToMedia')
            ->willReturnCallback(function DMediaFile $m, string $dest, string $id) use ($expectedMediaFile$expectedDestination$mediaId): void {
                $this->assertSame($expectedMediaFile$m);
                $this->assertSame($expectedDestination$dest);
                $this->assertSame($mediaId$id);
            });

        $result = $mediaSerializer->deserialize(new Config([][][])$mediaDefinition$record);
        $result = \is_array($result) ? $result : iterator_to_array($result);

        $writtenResult = new EntityWriteResult($mediaId$result, 'media', 'insert');
        $writtenEvent = new EntityWrittenEvent('media', [$writtenResult]$context);
        $eventDispatcher->dispatch($writtenEvent, 'media.written');
    }

    public function testExistingMediaWithSameUrlDoesNotDownload(): void
    {
        $context = Context::createDefaultContext();
        $serializerRegistry = $this->getContainer()->get(SerializerRegistry::class);
        $mediaDefinition = $this->getContainer()->get(MediaDefinition::class);

        $mediaService = $this->createMock(MediaService::class);
        $fileSaver = $this->createMock(FileSaver::class);

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