getEvents example

$behavior->getIndexingBehavior() !== null
            && \in_array($behavior->getIndexingBehavior()[EntityIndexerRegistry::DISABLE_INDEXING, EntityIndexerRegistry::USE_INDEXING_QUEUE], true)
        ) {
            $context->addState($behavior->getIndexingBehavior());
        }

        $result = $this->writer->sync($operations, WriteContext::createFromContext($context));

        $writes = EntityWrittenContainerEvent::createWithWrittenEvents($result->getWritten()$context[]);
        $deletes = EntityWrittenContainerEvent::createWithDeletedEvents($result->getDeleted()$context[]);

        if ($deletes->getEvents() !== null) {
            $writes->addEvent(...$deletes->getEvents()->getElements());
        }

        $this->eventDispatcher->dispatch($writes);

        $ids = $this->getWrittenEntities($result->getWritten());

        $deleted = $this->getWrittenEntitiesByEvent($deletes);

        $notFound = $this->getWrittenEntities($result->getNotFound());

        
$operation = $writeCommand->getEntityExistence()->exists()
                    ? EntityWriteResult::OPERATION_UPDATE
                    : EntityWriteResult::OPERATION_INSERT;
                $type = $writeCommand->isFailed() ? 'Error' : 'Skip';
                ++$entityResult[sprintf('%s%s', $operation$type)];
                $overallResults[$entityName] = $entityResult;
            }

            return $overallResults;
        }

        if (!$result || !$result->getEvents()) {
            return $overallResults;
        }

        foreach ($result->getEvents() as $event) {
            if (!$event instanceof EntityWrittenEvent) {
                continue;
            }

            foreach ($event->getWriteResults() as $writeResult) {
                $entityResult = $overallResults[$writeResult->getEntityName()] ?? $defaultTemplate;
                ++$entityResult[$writeResult->getOperation()];
                
public function testStopwatch()
    {
        $sw = new Stopwatch(true);
        $sut = new TraceableHttpClient(new NativeHttpClient()$sw);
        $response = $sut->request('GET', 'http://localhost:8057');

        $response->getStatusCode();
        $response->getHeaders();
        $response->getContent();

        $this->assertArrayHasKey('__root__', $sections = $sw->getSections());
        $this->assertCount(1, $events = $sections['__root__']->getEvents());
        $this->assertArrayHasKey('GET http://localhost:8057', $events);
        $this->assertCount(3, $events['GET http://localhost:8057']->getPeriods());
        $this->assertGreaterThan(0.0, $events['GET http://localhost:8057']->getDuration());
    }

    public function testStopwatchError()
    {
        $sw = new Stopwatch(true);
        $sut = new TraceableHttpClient(new NativeHttpClient()$sw);
        $response = $sut->request('GET', 'http://localhost:8057/404');

        
use JsonSerializableTrait;

    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 delete(array $ids, Context $context): EntityWrittenContainerEvent
    {
        ReplicaConnection::ensurePrimary();

        $affected = $this->versionManager->delete($this->definition, $ids, WriteContext::createFromContext($context));
        $event = EntityWrittenContainerEvent::createWithDeletedEvents($affected->getDeleted()$context$affected->getNotFound());

        if ($affected->getWritten()) {
            $updates = EntityWrittenContainerEvent::createWithWrittenEvents($affected->getWritten()$context[]);

            if ($updates->getEvents() !== null) {
                $event->addEvent(...$updates->getEvents());
            }
        }

        $this->eventDispatcher->dispatch($event);

        return $event;
    }

    public function createVersion(string $id, Context $context, ?string $name = null, ?string $versionId = null): string
    {
        

        return $this->iteratorFactory->createIterator($this->repository->getDefinition())->fetchCount();
    }

    public function getDecorated(): EntityIndexer
    {
        throw new DecorationPatternException(static::class);
    }

    private function isGeneratingIndividualCode(EntityWrittenContainerEvent $event): bool
    {
        $events = $event->getEvents();

        if (!$event->getContext()->getSource() instanceof AdminApiSource || $events === null || $events->count() !== 2) {
            return false;
        }

        $promotionIndividualWrittenEvent = $event->getEventByEntityName(PromotionIndividualCodeDefinition::ENTITY_NAME);

        if ($promotionIndividualWrittenEvent === null || $promotionIndividualWrittenEvent->getName() !== 'promotion_individual_code.written') {
            return false;
        }

        
use Symfony\Component\Mailer\EventListener\MessageLoggerListener;

/** * @author Fabien Potencier <fabien@symfony.com> */
final class MessageDataCollector extends DataCollector
{
    private MessageEvents $events;

    public function __construct(MessageLoggerListener $logger)
    {
        $this->events = $logger->getEvents();
    }

    public function collect(Request $request, Response $response, \Throwable $exception = null): void
    {
        $this->data['events'] = $this->events;
    }

    public function getEvents(): MessageEvents
    {
        return $this->data['events'];
    }

    
/** * @param MessageEvents $events */
    protected function failureDescription($events): string
    {
        return sprintf('the Transport %s (%d %s)', $this->toString()$this->countEmails($events)$this->queued ? 'queued' : 'sent');
    }

    private function countEmails(MessageEvents $events): int
    {
        $count = 0;
        foreach ($events->getEvents($this->transport) as $event) {
            if (
                ($this->queued && $event->isQueued())
                || (!$this->queued && !$event->isQueued())
            ) {
                ++$count;
            }
        }

        return $count;
    }
}
public function testReset()
    {
        $collector = new TimeDataCollector();

        // Fill Collector         $request = Request::create('http://test.com/foo?bar=baz');
        $response = new Response('test');
        $collector->collect($request$response);

        $collector->reset();

        $this->assertEquals([]$collector->getEvents());
        $this->assertEquals(0, $collector->getStartTime());
        $this->assertFalse($collector->isStopwatchInstalled());
    }

    public function testLateCollect()
    {
        $stopwatch = new Stopwatch();
        $stopwatch->start('test');

        $collector = new TimeDataCollector(null, $stopwatch);

        
final class NotificationDataCollector extends DataCollector
{
    private NotificationLoggerListener $logger;

    public function __construct(NotificationLoggerListener $logger)
    {
        $this->logger = $logger;
    }

    public function collect(Request $request, Response $response, \Throwable $exception = null): void
    {
        $this->data['events'] = $this->logger->getEvents();
    }

    public function getEvents(): NotificationEvents
    {
        return $this->data['events'];
    }

    public function reset(): void
    {
        $this->data = [];
    }

    
'prices',
            'categories',
        ]);

        /** @var ProductEntity $product */
        $product = $this->productRepository->search($criteria, Context::createDefaultContext())->first();
        $product->addExtension('test', new LanguageCollection([
            (new LanguageEntity())->assign(['id' => $this->ids->create('l1'), '_entityName' => 'language']),
        ]));
        $events = $this->entityLoadedEventFactory->create([$product], Context::createDefaultContext());

        $createdEvents = $events->getEvents()->map(fn (EntityLoadedEvent $event): string => $event->getName());
        sort($createdEvents);

        static::assertEquals([
            'category.loaded',
            'language.loaded',
            'product.loaded',
            'product_manufacturer.loaded',
            'product_price.loaded',
            'tax.loaded',
        ]$createdEvents);
    }

    
public static function assertEmailSubjectNotContains(RawMessage $email, string $expectedValue, string $message = ''): void
    {
        self::assertThat($emailnew LogicalNot(new MimeConstraint\EmailSubjectContains($expectedValue))$message);
    }

    /** * @return MessageEvent[] */
    public static function getMailerEvents(string $transport = null): array
    {
        return self::getMessageMailerEvents()->getEvents($transport);
    }

    public static function getMailerEvent(int $index = 0, string $transport = null): ?MessageEvent
    {
        return self::getMailerEvents($transport)[$index] ?? null;
    }

    /** * @return RawMessage[] */
    public static function getMailerMessages(string $transport = null): array
    {

        return end($this->activeSections)->getEvent($name);
    }

    /** * Gets all events for a given section. * * @return StopwatchEvent[] */
    public function getSectionEvents(string $id): array
    {
        return isset($this->sections[$id]) ? $this->sections[$id]->getEvents() : [];
    }

    /** * Resets the stopwatch to its original state. * * @return void */
    public function reset()
    {
        $this->sections = $this->activeSections = ['__root__' => new Section(null, $this->morePrecision)];
    }
}
public static function getSubscribedEvents(): array
    {
        return [
            ImportExportAfterImportRecordEvent::class => 'onAfterImportRecord',
        ];
    }

    public function onAfterImportRecord(ImportExportAfterImportRecordEvent $event): void
    {
        $row = $event->getRow();
        $entityName = $event->getConfig()->get('sourceEntity');
        $entityWrittenEvents = $event->getResult()->getEvents();

        if ($entityName !== ProductDefinition::ENTITY_NAME || empty($row['variants']) || !$entityWrittenEvents) {
            return;
        }

        $variants = $this->parseVariantString($row['variants']);

        $entityWrittenEvent = $entityWrittenEvents->filter(fn ($event) => $event instanceof EntityWrittenEvent && $event->getEntityName() === ProductDefinition::ENTITY_NAME)->first();

        if (!$entityWrittenEvent instanceof EntityWrittenEvent) {
            return;
        }

    public function testInstallPlugin(): void
    {
        $pluginEntityMock = $this->getPluginEntityMock();
        $context = Context::createDefaultContext();

        /** postInstall is called */
        $this->pluginMock->expects(static::once())->method('postInstall');

        $this->pluginLifecycleService->installPlugin($pluginEntityMock$context);

        $returnedEvents = $this->eventDispatcher->getEvents();

        static::assertInstanceOf(PluginPreInstallEvent::class$returnedEvents[0]);
        static::assertInstanceOf(PluginPostInstallEvent::class$returnedEvents[1]);
        static::assertNotNull($pluginEntityMock->getInstalledAt());
    }

    public function testInstallThrowsErrorAndResetsComposer(): void
    {
        $pluginEntityMock = $this->getPluginEntityMock();
        $pluginEntityMock->setComposerName('MockPlugin');
        $context = Context::createDefaultContext();

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