NestedEventCollection example

$result->method('getEntities')->willReturn(new ScheduledTaskCollection([$scheduledTask]));
        $scheduledTaskRepository->expects(static::once())->method('search')->willReturn($result);
        $scheduledTaskRepository->expects(static::once())->method('update')->willReturnCallback(function Darray $data, Context $context) {
            static::assertCount(1, $data);
            $data = $data[0];
            static::assertArrayHasKey('id', $data);
            static::assertArrayHasKey('nextExecutionTime', $data);
            static::assertArrayHasKey('status', $data);
            static::assertEquals('1', $data['id']);
            static::assertEquals(ScheduledTaskDefinition::STATUS_SKIPPED, $data['status']);

            return new EntityWrittenContainerEvent($contextnew NestedEventCollection()[]);
        });

        $bus = $this->createMock(MessageBusInterface::class);
        $bus->expects(static::never())->method('dispatch');
        $scheduler = new TaskScheduler(
            $scheduledTaskRepository,
            $bus,
            new ParameterBag([
                'shopware.cache.invalidation.delay' => 0,
            ])
        );

        
public function getEvents(): ?NestedEventCollection
    {
        return null;
    }

    public function getFlatEventList(): NestedEventCollection
    {
        $events = [$this];

        if (!$nestedEvents = $this->getEvents()) {
            return new NestedEventCollection($events);
        }

        foreach ($nestedEvents as $event) {
            $events[] = $event;
            foreach ($event->getFlatEventList() as $item) {
                $events[] = $item;
            }
        }

        return new NestedEventCollection($events);
    }
}


    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(
                    [
public function testGetSubscribedEvents(): void
    {
        static::assertArrayHasKey(EntityWrittenContainerEvent::class, EntityIndexingSubscriber::getSubscribedEvents());
    }

    public function testRefresh(): void
    {
        $registry = $this->createMock(EntityIndexerRegistry::class);
        $registry->expects(static::once())->method('refresh');

        $subscriber = new EntityIndexingSubscriber($registry);
        $subscriber->refreshIndex(new EntityWrittenContainerEvent(Context::createDefaultContext()new NestedEventCollection()[]));
    }
}

    private array $updatePayload = [];

    protected function setUp(): void
    {
        $orderLineItemDownloadRepository = $this->createMock(EntityRepository::class);
        $orderLineItemDownloadRepository->method('update')->willReturnCallback(
            function Darray $payload, Context $context): EntityWrittenContainerEvent {
                $this->updatePayload = $payload;

                return new EntityWrittenContainerEvent($contextnew NestedEventCollection([])[]);
            }
        );
        $this->action = new GrantDownloadAccessAction($orderLineItemDownloadRepository);

        $this->updatePayload = [];
    }

    public function testGetName(): void
    {
        static::assertEquals('action.grant.download.access', $this->action->getName());
    }

    
'textBoosted' => ['type' => 'text'],
        ];

        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`');

        
static::assertNotNull($data[0]['deliveries']);
                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();
                


        $subscriber = new CacheInvalidationSubscriber(
            $cacheInvalidator,
            $this->createMock(Connection::class),
            false,
            false
        );

        $subscriber->invalidateContext(new EntityWrittenContainerEvent(
            Context::createDefaultContext(),
            new NestedEventCollection([
                new EntityWrittenEvent(
                    SalesChannelDefinition::ENTITY_NAME,
                    [
                        new EntityWriteResult(
                            $salesChannelId,
                            [],
                            SalesChannelDefinition::ENTITY_NAME,
                            EntityWriteResult::OPERATION_UPDATE,
                        ),
                    ],
                    Context::createDefaultContext(),
                ),
$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
    {
        if ($this->definition === null) {
            return [];
        }
$flowEventMock->method('getCustomEvents')->willReturn($customEventsMock);

        $this->flowEventsRepositoryMock->expects(static::once())->method('upsert')->willReturnCallback(function D$upserts$context) use ($appId): EntityWrittenContainerEvent {
            static::assertSame([
                [
                    'appId' => $appId,
                    'name' => 'value',
                    'aware' => [],
                ],
            ]$upserts);

            return new EntityWrittenContainerEvent($contextnew NestedEventCollection()[]);
        });

        $this->flowEventsRepositoryMock->expects(static::once())->method('delete')->willReturnCallback(function D$ids$context) use ($appId): EntityWrittenContainerEvent {
            static::assertSame([['id' => Uuid::fromHexToBytes($appId)]]$ids);

            return new EntityWrittenContainerEvent($contextnew NestedEventCollection()[]);
        });

        $context = Context::createDefaultContext();
        $this->flowEventPersister->updateEvents($flowEventMock$appId$context, 'en-GB');
    }

    
$index = new AdminSearchRegistry(
            ['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()),
        ])[]));
    }

    
 $queueTaskPayload$scheduledTaskPayload ] = $data;

            static::assertArrayHasKey('status', $queueTaskPayload);
            static::assertArrayHasKey('status', $scheduledTaskPayload);
            static::assertArrayHasKey('id', $queueTaskPayload);
            static::assertArrayHasKey('id', $scheduledTaskPayload);
            static::assertEquals(ScheduledTaskDefinition::STATUS_SKIPPED, $queueTaskPayload['status']);
            static::assertEquals('queuedTask', $queueTaskPayload['id']);
            static::assertEquals(ScheduledTaskDefinition::STATUS_SKIPPED, $scheduledTaskPayload['status']);
            static::assertEquals('scheduledTask', $scheduledTaskPayload['id']);

            return new EntityWrittenContainerEvent($contextnew NestedEventCollection()[]);
        });

        $this->scheduleTaskRepository->expects(static::never())->method('delete');
        $this->scheduleTaskRepository->expects(static::never())->method('create');

        $registry->registerTasks();
    }

    public function testQueuedOrSkippedTasksShouldBecomeScheduled(): void
    {
        $tasks = [new InvalidateCacheTask()new CreateAliasTask()];

        


    public function search(Criteria $criteria, Context $context): EntitySearchResult
    {
        throw new \Error('MockRepository->search: Not implemented');
    }

    public function update(array $data, Context $context): EntityWrittenContainerEvent
    {
        ++$this->updateCalls;

        return new EntityWrittenContainerEvent($contextnew NestedEventCollection()[]);
    }

    public function upsert(array $data, Context $context): EntityWrittenContainerEvent
    {
        ++$this->upsertCalls;

        return new EntityWrittenContainerEvent($contextnew NestedEventCollection()[]);
    }

    public function create(array $data, Context $context): EntityWrittenContainerEvent
    {
        
$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
    {
        if (!$this->definition instanceof EntityDefinition) {
            return [];
        }
$connection = $this->createMock(Connection::class);

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

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

        $customFieldUpdater->onNewCustomFieldCreated($containerEvent);
    }

    public function testElasticsearchDisabled(): void
    {
        $elasticsearchHelper = $this->createMock(ElasticsearchHelper::class);
        $elasticsearchHelper
            ->method('allowIndexing')
            
Home | Imprint | This part of the site doesn't use cookies.