Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/DataCollector/GraphQLCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function getBatches(): array
public function reset(): void
{
$this->data = [];
$this->batches = [];
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/profiler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ services:
template: "@OverblogGraphQL/profiler/panel.html.twig"
id: graphql
- { name: kernel.event_listener, event: graphql.post_executor, method: onPostExecutor }
- { name: kernel.reset, method: reset }
34 changes: 34 additions & 0 deletions tests/DataCollector/GraphQLCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,38 @@ public function testCollect(): void
],
]);
}

public function testResetClearsBatches(): void
{
$collector = new GraphQLCollector();

$request = new Request();

$collector->onPostExecutor(new ExecutorResultEvent(
new ExecutionResult(['res' => 'ok']),
ExecutorArgumentsEvent::create('test_schema', new ExtensibleSchema([]), 'query{ test{field1} }', new ArrayObject())
));

$collector->collect($request, new Response());
$this->assertCount(1, $collector->getBatches());

$collector->reset();
$collector->collect($request, new Response());

$this->assertSame([], $collector->getBatches());
$this->assertEquals(0, $collector->getCount());
$this->assertFalse($collector->getError());

$collector->onPostExecutor(new ExecutorResultEvent(
new ExecutionResult(['res' => 'ok']),
ExecutorArgumentsEvent::create('test_schema', new ExtensibleSchema([]), 'query{ other{field1, field2} }', new ArrayObject())
));

$collector->collect($request, new Response());

$batches = $collector->getBatches();
$this->assertCount(1, $batches);
$this->assertEquals('query{ other{field1, field2} }', $batches[0]['queryString']);
$this->assertEquals(1, $collector->getCount());
}
}
40 changes: 40 additions & 0 deletions tests/Functional/DataCollector/GraphQLCollectorResetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Overblog\GraphQLBundle\Tests\Functional\DataCollector;

use GraphQL\Type\Introspection;
use Overblog\GraphQLBundle\DataCollector\GraphQLCollector;
use Overblog\GraphQLBundle\Tests\Functional\TestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class GraphQLCollectorResetTest extends TestCase
{
public function testCollectorIsResetByServicesResetter(): void
{
$client = static::createClient(['test_case' => 'connection']);
static::sendRequest($client, Introspection::getIntrospectionQuery());

$container = static::getContainer();
// the premise of this test: the collector is registered while Symfony's profiler is not
$this->assertFalse($container->has('profiler'));
/** @var ContainerInterface $testContainer */
$testContainer = $container->get('test.service_container');
/** @var GraphQLCollector $collector */
$collector = $testContainer->get(GraphQLCollector::class);

$collector->collect(new Request(), new Response());
$this->assertCount(1, $collector->getBatches());

// this is what a long-running runtime does between requests, through Kernel::boot()
/** @var \Symfony\Contracts\Service\ResetInterface $resetter */
$resetter = $container->get('services_resetter');
$resetter->reset();

$collector->collect(new Request(), new Response());
$this->assertSame([], $collector->getBatches());
}
}
Loading