lateCollect example


class ConnectionProfilerTest extends TestCase
{
    protected function setUp(): void
    {
        ClockMock::withClockMock(1500000000);
    }

    public function testCollectConnections(): void
    {
        $c = $this->createCollector([]);
        $c->lateCollect();
        $c = unserialize(serialize($c));
        static::assertEquals(['default']$c->getConnections());
    }

    public function testCollectQueryCount(): void
    {
        $c = $this->createCollector([]);
        $c->lateCollect();
        $c = unserialize(serialize($c));
        static::assertEquals(0, $c->getQueryCount());

        

                'method' => 'GET',
                'url' => 'http://localhost:8057/404',
            ],
        ]);
        $httpClient3 = $this->httpClientThatHasTracedRequests([]);
        $sut = new HttpClientDataCollector();
        $sut->registerClient('http_client1', $httpClient1);
        $sut->registerClient('http_client2', $httpClient2);
        $sut->registerClient('http_client3', $httpClient3);
        $this->assertEquals(0, $sut->getRequestCount());
        $sut->lateCollect();
        $this->assertEquals(3, $sut->getRequestCount());
    }

    public function testItCollectsErrorCount()
    {
        $httpClient1 = $this->httpClientThatHasTracedRequests([
            [
                'method' => 'GET',
                'url' => 'http://localhost:8057/',
            ],
            [
                
$envelope = new Envelope($message);

        $bus = $this->createMock(MessageBusInterface::class);
        $bus->method('dispatch')->with($message)->willReturn($envelope);
        $bus = new TraceableMessageBus($bus);

        $collector = new MessengerDataCollector();
        $collector->registerBus('default', $bus);

        $bus->dispatch($message);

        $collector->lateCollect();

        $messages = $collector->getMessages();
        $this->assertCount(1, $messages);

        $file = __FILE__;
        $expected = <<<DUMP array:5 [ "bus" => "default" "stamps" => [] "stamps_after_dispatch" => [] "message" => array:2 [ "type" => "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage" "value" => Symfony\Component\Messenger\Tests\Fixtures\DummyMessage %A -message: "dummy message" } ] "caller" => array:3 [ "name" => "MessengerDataCollectorTest.php" "file" => "
$collector = new ValidatorDataCollector($validator);

        $violations = new ConstraintViolationList([
            $this->createMock(ConstraintViolation::class),
            $this->createMock(ConstraintViolation::class),
        ]);
        $originalValidator->method('validate')->willReturn($violations);

        $validator->validate(new \stdClass());

        $collector->lateCollect();

        $calls = $collector->getCalls();

        $this->assertCount(1, $calls);
        $this->assertSame(2, $collector->getViolationsCount());

        $call = $calls[0];

        $this->assertArrayHasKey('caller', $call);
        $this->assertArrayHasKey('context', $call);
        $this->assertArrayHasKey('violations', $call);
        
->willReturn($config);

        $controller = new ProfilerController($twig$profiler$connection);

        $profile = new Profile('some-token');
        $profiler->expects(static::once())
            ->method('loadProfile')
            ->with('some-token')
            ->willReturn($profile);

        $collector = new ConnectionProfiler($connection);
        $collector->lateCollect();

        $profile->addCollector($collector);

        $response = $controller->explainAction(
            'some-token',
            'app.connection_collector',
            'default',
            5
        );

        static::assertEquals('This query does not exist.', $response->getContent());
    }
return $this->storage->read($token);
    }

    /** * Saves a Profile. */
    public function saveProfile(Profile $profile): bool
    {
        // late collect         foreach ($profile->getCollectors() as $collector) {
            if ($collector instanceof LateDataCollectorInterface) {
                $collector->lateCollect();
            }
        }

        if (!($ret = $this->storage->write($profile)) && null !== $this->logger) {
            $this->logger->warning('Unable to store the profiler information.', ['configured_storage' => $this->storage::class]);
        }

        return $ret;
    }

    /** * Purges all data from the storage. * * @return void */

    private array $clients = [];

    public function registerClient(string $name, TraceableHttpClient $client): void
    {
        $this->clients[$name] = $client;
    }

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

    public function lateCollect(): void
    {
        $this->data['request_count'] = $this->data['request_count'] ?? 0;
        $this->data['error_count'] = $this->data['error_count'] ?? 0;
        $this->data += ['clients' => []];

        foreach ($this->clients as $name => $client) {
            [$errorCount$traces] = $this->collectOnClient($client);

            
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Tests\Fixtures\DataCollector\DummyController;

class RequestDataCollectorTest extends TestCase
{
    public function testCollect()
    {
        $c = new RequestDataCollector();

        $c->collect($request = $this->createRequest()$this->createResponse());
        $c->lateCollect();

        $attributes = $c->getRequestAttributes();

        $this->assertSame('request', $c->getName());
        $this->assertInstanceOf(ParameterBag::class$c->getRequestHeaders());
        $this->assertInstanceOf(ParameterBag::class$c->getRequestServer());
        $this->assertInstanceOf(ParameterBag::class$c->getRequestCookies());
        $this->assertInstanceOf(ParameterBag::class$attributes);
        $this->assertInstanceOf(ParameterBag::class$c->getRequestRequest());
        $this->assertInstanceOf(ParameterBag::class$c->getRequestQuery());
        $this->assertInstanceOf(ParameterBag::class$c->getResponseCookies());
        
$this->assertNull($collector->getFirewall());
    }

    /** @dataProvider provideRoles */
    public function testCollectAuthenticationTokenAndRoles(array $roles, array $normalizedRoles, array $inheritedRoles)
    {
        $tokenStorage = new TokenStorage();
        $tokenStorage->setToken(new UsernamePasswordToken(new InMemoryUser('hhamon', 'P4$$w0rD', $roles), 'provider', $roles));

        $collector = new SecurityDataCollector($tokenStorage$this->getRoleHierarchy(), null, null, null, null, true);
        $collector->collect(new Request()new Response());
        $collector->lateCollect();

        $this->assertTrue($collector->isEnabled());
        $this->assertTrue($collector->isAuthenticated());
        $this->assertFalse($collector->isImpersonated());
        $this->assertNull($collector->getImpersonatorUser());
        $this->assertNull($collector->getImpersonationExitPath());
        $this->assertSame(UsernamePasswordToken::class$collector->getTokenClass()->getValue());
        $this->assertTrue($collector->supportsRoleHierarchy());
        $this->assertSame($normalizedRoles$collector->getRoles()->getValue(true));
        $this->assertSame($inheritedRoles$collector->getInheritedRoles()->getValue(true));
        $this->assertSame('hhamon', $collector->getUser());
    }
public function testLateCollect()
    {
        $stopwatch = new Stopwatch();
        $stopwatch->start('test');

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

        $request = new Request();
        $request->attributes->set('_stopwatch_token', '__root__');

        $collector->collect($requestnew Response());
        $collector->lateCollect();

        $this->assertEquals(['test']array_keys($collector->getEvents()));
    }

    public function testSetEvents()
    {
        $collector = new TimeDataCollector();

        $event = $this->createMock(StopwatchEvent::class);
        $event->expects($this->once())->method('ensureStopped');

        
return $this->storage->read($token);
    }

    /** * Saves a Profile. */
    public function saveProfile(Profile $profile): bool
    {
        // late collect         foreach ($profile->getCollectors() as $collector) {
            if ($collector instanceof LateDataCollectorInterface) {
                $collector->lateCollect();
            }
        }

        if (!($ret = $this->storage->write($profile)) && null !== $this->logger) {
            $this->logger->warning('Unable to store the profiler information.', ['configured_storage' => $this->storage::class]);
        }

        return $ret;
    }

    /** * Purges all data from the storage. * * @return void */

    public function testCollectWithUnexpectedFormat()
    {
        $logger = $this
            ->getMockBuilder(DebugLoggerInterface::class)
            ->onlyMethods(['countErrors', 'getLogs', 'clear'])
            ->getMock();
        $logger->expects($this->once())->method('countErrors')->willReturn(123);
        $logger->expects($this->exactly(2))->method('getLogs')->willReturn([]);

        $c = new LoggerDataCollector($logger, __DIR__.'/');
        $c->lateCollect();
        $compilerLogs = $c->getCompilerLogs()->getValue('message');

        $this->assertSame([
            ['message' => 'Removed service "Psr\Container\ContainerInterface"; reason: private alias.'],
            ['message' => 'Removed service "Symfony\Component\DependencyInjection\ContainerInterface"; reason: private alias.'],
        ]$compilerLogs['Symfony\Component\DependencyInjection\Compiler\RemovePrivateAliasesPass']);

        $this->assertSame([
            ['message' => 'Some custom logging message'],
            ['message' => 'With ending :'],
        ]$compilerLogs['Unknown Compiler Pass']);
    }
class SerializerDataCollectorTest extends TestCase
{
    public function testCollectSerialize()
    {
        $dataCollector = new SerializerDataCollector();

        $caller = ['name' => 'Foo.php', 'file' => 'src/Foo.php', 'line' => 123];
        $dataCollector->collectSerialize('traceIdOne', 'data', 'format', ['foo' => 'bar'], 1.0, $caller);
        $dataCollector->collectDeserialize('traceIdTwo', 'data', 'type', 'format', ['foo' => 'bar'], 1.0, $caller);

        $dataCollector->lateCollect();
        $collectedData = $this->castCollectedData($dataCollector->getData());

        $this->assertSame([[
            'data' => 'data',
            'dataType' => 'string',
            'type' => null,
            'format' => 'format',
            'time' => 1.0,
            'context' => ['foo' => 'bar'],
            'normalization' => [],
            'encoding' => [],
            
use Symfony\Component\Translation\DataCollector\TranslationDataCollector;
use Symfony\Component\Translation\DataCollectorTranslator;

class TranslationDataCollectorTest extends TestCase
{
    public function testCollectEmptyMessages()
    {
        $translator = $this->getTranslator();
        $translator->expects($this->any())->method('getCollectedMessages')->willReturn([]);

        $dataCollector = new TranslationDataCollector($translator);
        $dataCollector->lateCollect();

        $this->assertEquals(0, $dataCollector->getCountMissings());
        $this->assertEquals(0, $dataCollector->getCountFallbacks());
        $this->assertEquals(0, $dataCollector->getCountDefines());
        $this->assertEquals([]$dataCollector->getMessages()->getValue());
    }

    public function testCollect()
    {
        $collectedMessages = [
            [
                
Home | Imprint | This part of the site doesn't use cookies.