getEvent example

class StopwatchTest extends TestCase
{
    private const DELTA = 20;

    public function testStart()
    {
        $stopwatch = new Stopwatch();
        $event = $stopwatch->start('foo', 'cat');

        $this->assertInstanceOf(StopwatchEvent::class$event);
        $this->assertEquals('cat', $event->getCategory());
        $this->assertSame($event$stopwatch->getEvent('foo'));
    }

    public function testStartWithoutCategory()
    {
        $stopwatch = new Stopwatch();
        $stopwatchEvent = $stopwatch->start('bar');
        $this->assertSame('default', $stopwatchEvent->getCategory());
        $this->assertSame($stopwatchEvent$stopwatch->getEvent('bar'));
    }

    public function testIsStarted()
    {
public function updateWebhooks(Manifest $manifest, string $appId, string $defaultLocale, Context $context): void
    {
        $existingWebhooks = $this->getExistingWebhooks($appId$context);

        $webhooks = $manifest->getWebhooks() ? $manifest->getWebhooks()->getWebhooks() : [];
        $upserts = [];

        foreach ($webhooks as $webhook) {
            $payload = $webhook->toArray($defaultLocale);
            $payload['appId'] = $appId;
            $payload['eventName'] = $webhook->getEvent();

            /** @var WebhookEntity|null $existing */
            $existing = $existingWebhooks->filterByProperty('name', $webhook->getName())->first();
            if ($existing) {
                $payload['id'] = $existing->getId();
                $existingWebhooks->remove($existing->getId());
            }

            $upserts[] = $payload;
        }

        
public function __invoke(ConsumeRemoteEventMessage $message): void
    {
        if (!$this->consumers->has($message->getType())) {
            throw new LogicException(sprintf('Unable to find a consumer for message of type "%s".', $message->getType()));
        }
        $consumer = $this->consumers->get($message->getType());

        if (!$consumer instanceof ConsumerInterface) {
            throw new LogicException(sprintf('The consumer "%s" for message of type "%s" must implement "%s".', get_debug_type($consumer)$message->getType(), ConsumerInterface::class));
        }

        $consumer->consume($message->getEvent());
    }
}

    public function testFromXml(): void
    {
        $manifest = Manifest::createFromXmlFile(__DIR__ . '/../_fixtures/test/manifest.xml');

        static::assertNotNull($manifest->getWebhooks());
        static::assertCount(2, $manifest->getWebhooks()->getWebhooks());

        $firstWebhook = $manifest->getWebhooks()->getWebhooks()[0];
        static::assertEquals('hook1', $firstWebhook->getName());
        static::assertEquals('https://test.com/hook', $firstWebhook->getUrl());
        static::assertEquals('checkout.customer.before.login', $firstWebhook->getEvent());

        $secondWebhook = $manifest->getWebhooks()->getWebhooks()[1];
        static::assertEquals('hook2', $secondWebhook->getName());
        static::assertEquals('https://test.com/hook2', $secondWebhook->getUrl());
        static::assertEquals('checkout.order.placed', $secondWebhook->getEvent());
    }
}

                KernelEvents::REQUEST => [['setDefaultLocale', 100]['onKernelRequest', 16]],
                KernelEvents::FINISH_REQUEST => [['onKernelFinishRequest', 0]],
            ],
            LocaleListener::getSubscribedEvents()
        );
    }

    public function testDefaultLocale()
    {
        $listener = new LocaleListener($this->requestStack, 'fr');
        $event = $this->getEvent($request = Request::create('/'));

        $listener->setDefaultLocale($event);
        $this->assertEquals('fr', $request->getLocale());
    }

    public function testLocaleFromRequestAttribute()
    {
        $request = Request::create('/');
        $request->cookies->set(session_name(), 'value');

        $request->attributes->set('_locale', 'es');
        

    public function lap(string $name): StopwatchEvent
    {
        return end($this->activeSections)->stopEvent($name)->start();
    }

    /** * Returns a specific event by name. */
    public function getEvent(string $name): StopwatchEvent
    {
        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() : [];
    }

    

    public function testTimingsInSupports()
    {
        $stopwatch = new Stopwatch();
        $resolver = new TraceableValueResolver(new ResolverStub()$stopwatch);
        $argument = new ArgumentMetadata('dummy', 'string', false, false, null);
        $request = new Request();

        $this->assertTrue($resolver->supports($request$argument));

        $event = $stopwatch->getEvent(ResolverStub::class.'::supports');
        $this->assertCount(1, $event->getPeriods());
    }

    public function testTimingsInResolve()
    {
        $stopwatch = new Stopwatch();
        $resolver = new TraceableValueResolver(new ResolverStub()$stopwatch);
        $argument = new ArgumentMetadata('dummy', 'string', false, false, null);
        $request = new Request();

        $iterable = $resolver->resolve($request$argument);

        
 $actions);

        if (!$hasAppSecret) {
            /** @phpstan-ignore-next-line - return typehint with active: bool, errorCount: int does not work here because active will always be true and errorCount will always be 0 */
            return $webhooks;
        }

        $manifestWebhooks = $manifest->getWebhooks()?->getWebhooks() ?? [];
        $webhooks = array_merge($webhooksarray_map(function D$webhook) use ($defaultLocale$appId) {
            $payload = $webhook->toArray($defaultLocale);
            $payload['appId'] = $appId;
            $payload['eventName'] = $webhook->getEvent();

            return $payload;
        }$manifestWebhooks));

        return $webhooks;
    }

    private function getIcon(Manifest $manifest): ?string
    {
        if (!$iconPath = $manifest->getMetadata()->getIcon()) {
            return null;
        }
/** * @param Backlog[] $backlogs * * @return string[] */
    private function getBacklogNumbers($backlogs)
    {
        $numbers = [];
        foreach ($backlogs as $backlog) {
            $payload = $backlog->getPayload();
            switch ($backlog->getEvent()) {
                case ORMBacklogSubscriber::EVENT_ARTICLE_DELETED:
                case ORMBacklogSubscriber::EVENT_ARTICLE_INSERTED:
                case ORMBacklogSubscriber::EVENT_ARTICLE_UPDATED:
                    $query = $this->queryFactory->createProductIdQuery([$payload['id']]);
                    $numbers = array_merge($numbers$query->fetch());
                    break;

                case ORMBacklogSubscriber::EVENT_VOTE_DELETED:
                case ORMBacklogSubscriber::EVENT_VOTE_INSERTED:
                case ORMBacklogSubscriber::EVENT_VOTE_UPDATED:
                    $query = $this->queryFactory->createProductIdQuery([$payload['articleId']]);
                    
/** * @param Backlog[] $backlogs * * @return int[] */
    private function getPropertyIdsOfBacklog($backlogs)
    {
        $ids = [];
        foreach ($backlogs as $backlog) {
            $payload = $backlog->getPayload();

            switch ($backlog->getEvent()) {
                case ORMBacklogSubscriber::EVENT_PROPERTY_GROUP_DELETED:
                case ORMBacklogSubscriber::EVENT_PROPERTY_GROUP_INSERTED:
                case ORMBacklogSubscriber::EVENT_PROPERTY_GROUP_UPDATED:
                    $ids[] = $payload['id'];
                    break;

                case ORMBacklogSubscriber::EVENT_PROPERTY_OPTION_DELETED:
                case ORMBacklogSubscriber::EVENT_PROPERTY_OPTION_INSERTED:
                case ORMBacklogSubscriber::EVENT_PROPERTY_OPTION_UPDATED:
                    $ids[] = $payload['groupId'];
                    break;
            }

        ];
    }

    /** * @dataProvider provideExecuteAndEndTransactionMethods */
    public function testGlobalDoctrineDuration(callable $sqlMethod, callable $endTransactionMethod)
    {
        $this->init();

        $periods = $this->stopwatch->getEvent('doctrine')->getPeriods();
        $this->assertCount(1, $periods);

        $this->conn->beginTransaction();

        $this->assertFalse($this->stopwatch->getEvent('doctrine')->isStarted());
        $this->assertCount(2, $this->stopwatch->getEvent('doctrine')->getPeriods());

        $sqlMethod($this->conn, 'SELECT * FROM products');

        $this->assertFalse($this->stopwatch->getEvent('doctrine')->isStarted());
        $this->assertCount(3, $this->stopwatch->getEvent('doctrine')->getPeriods());

        

class SendWebhookHandler
{
    public function __construct(
        private readonly TransportInterface $transport,
    ) {
    }

    public function __invoke(SendWebhookMessage $message): void
    {
        $this->transport->send($message->getSubscriber()$message->getEvent());
    }
}

    private function writeBacklog(array $backlogs)
    {
        $statement = $this->connection->prepare(' INSERT IGNORE INTO s_es_backlog (`event`, `payload`, `time`) VALUES (:event, :payload, :time); ');

        foreach ($backlogs as $backlog) {
            $statement->execute([
                ':event' => $backlog->getEvent(),
                ':payload' => json_encode($backlog->getPayload()),
                ':time' => $backlog->getTime()->format('Y-m-d H:i:s'),
            ]);
        }
    }
}

    public function testFromXml(): void
    {
        $manifest = Manifest::createFromXmlFile(__DIR__ . '/../_fixtures/test-manifest.xml');

        static::assertNotNull($manifest->getWebhooks());
        static::assertCount(2, $manifest->getWebhooks()->getWebhooks());

        $firstWebhook = $manifest->getWebhooks()->getWebhooks()[0];
        static::assertEquals('hook1', $firstWebhook->getName());
        static::assertEquals('https://test.com/hook', $firstWebhook->getUrl());
        static::assertEquals('checkout.customer.before.login', $firstWebhook->getEvent());

        $secondWebhook = $manifest->getWebhooks()->getWebhooks()[1];
        static::assertEquals('hook2', $secondWebhook->getName());
        static::assertEquals('https://test.com/hook2', $secondWebhook->getUrl());
        static::assertEquals('checkout.order.placed', $secondWebhook->getEvent());
    }
}


        $appPrivileges = $manifest->getPermissions();
        $appPrivileges = $appPrivileges ? $appPrivileges->asParsedPrivileges() : [];
        $hookableEventNamesWithPrivileges = $this->hookableEventCollector->getHookableEventNamesWithPrivileges($context);
        $hookableEventNames = array_keys($hookableEventNamesWithPrivileges);

        $notHookable = [];
        $missingPermissions = [];
        foreach ($webhooks as $webhook) {
            // validate supported webhooks             if (!\in_array($webhook->getEvent()$hookableEventNames, true)) {
                $notHookable[] = $webhook->getName() . ': ' . $webhook->getEvent();

                continue;
            }

            // validate permissions             foreach ($hookableEventNamesWithPrivileges[$webhook->getEvent()]['privileges'] as $privilege) {
                if (\in_array($privilege$appPrivileges, true)) {
                    continue;
                }

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