Skip to content

Commit a63bbfd

Browse files
committed
fix(cache): Fixed issue with cache middleware not caching
1 parent fed65d4 commit a63bbfd

File tree

11 files changed

+127
-30
lines changed

11 files changed

+127
-30
lines changed

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ ENV PATH /composer/vendor/bin:$PATH
4141
# Allow Composer to be run as root
4242
ENV COMPOSER_ALLOW_SUPERUSER 1
4343

44+
ARG PHP_XDEBUG_MODE=debug
45+
ARG PHP_XDEBUG_REMOTE_PORT=9000
46+
ARG PHP_XDEBUG_REMOTE_ADDRESS=host.docker.internal
47+
ARG PHP_XDEBUG_IDEKEY=PHPSTORM
48+
ARG PHP_IDE_CONFIG=serverName=localhost
49+
50+
ENV PHP_XDEBUG_MODE=${PHP_XDEBUG_MODE}
51+
ENV PHP_XDEBUG_REMOTE_PORT=${PHP_XDEBUG_REMOTE_PORT}
52+
ENV PHP_XDEBUG_REMOTE_ADDRESS=${PHP_XDEBUG_REMOTE_ADDRESS}
53+
ENV PHP_XDEBUG_IDEKEY=${PHP_XDEBUG_IDEKEY}
54+
ENV PHP_IDE_CONFIG=${PHP_IDE_CONFIG}
55+
4456
# Setup the Composer installer
4557
RUN curl -o /tmp/composer-setup.php https://getcomposer.org/installer \
4658
&& curl -o /tmp/composer-setup.sig https://composer.github.io/installer.sig \

src/RunOpenCode/Bundle/QueryResourcesLoader/Cache/CacheMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __invoke(string $query, Parameters $parameters, Options $options
4949
$identity = $options->cache instanceof CacheIdentifiableInterface ?
5050
$options->cache->getCacheIdentity()
5151
: $options->cache;
52-
52+
5353

5454
// ensure TTL is set
5555
$identity = $identity->withTtl($identity->getTtl() ?? $this->defaultTtl);

src/RunOpenCode/Bundle/QueryResourcesLoader/DependencyInjection/CompilerPass/ConfigureCacheMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function process(ContainerBuilder $container): void
2525
$pool = $container->getParameter(Extension::CACHE_POOL);
2626
/** @var int|null $ttl */
2727
$ttl = $container->getParameter(Extension::CACHE_DEFAULT_TTL);
28-
28+
2929
// nothing to reconfigure.
3030
if (null === $pool && null === $ttl) {
3131
return;

src/RunOpenCode/Bundle/QueryResourcesLoader/Executor/Dbal/ResultProxy.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,18 @@ public function count(): int
187187
public function __serialize(): array
188188
{
189189
$columnNames = [];
190+
$rows = $this->result->fetchAllNumeric();
190191

191192
for ($index = 0; $index < $this->columnCount(); ++$index) {
192193
$columnNames[] = $this->result->getColumnName($index);
193194
}
195+
196+
// Replace result with array result so this resultset can be reused.
197+
$this->result = new ArrayResult($columnNames, $rows);
194198

195199
return [
196200
'columnNames' => $columnNames,
197-
'rows' => $this->result->fetchAllNumeric(),
201+
'rows' => $rows,
198202
];
199203
}
200204

src/RunOpenCode/Bundle/QueryResourcesLoader/Resources/config/loader.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<tag
6262
name="runopencode.query_resources_loader.middleware"
6363
priority="500"
64-
label="cache"
64+
label="loader"
6565
/>
6666
</service>
6767

tests/Cache/CacheIntegrationTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RunOpenCode\Bundle\QueryResourcesLoader\Tests\Cache;
6+
7+
use RunOpenCode\Bundle\QueryResourcesLoader\Cache\CacheIdentity;
8+
use RunOpenCode\Bundle\QueryResourcesLoader\Contract\QueryResourcesLoaderInterface;
9+
use RunOpenCode\Bundle\QueryResourcesLoader\Model\Options;
10+
use RunOpenCode\Bundle\QueryResourcesLoader\Tests\KernelTestCase;
11+
12+
final class CacheIntegrationTest extends KernelTestCase
13+
{
14+
public function testCacheIntegration(): void
15+
{
16+
$this->createFixtures();
17+
18+
$loader = $this->getContainer()->get(QueryResourcesLoaderInterface::class);
19+
$data = $loader->execute('SELECT id, title FROM bar ORDER BY id', null, Options::cached(new CacheIdentity(
20+
'foo',
21+
)));
22+
23+
$this->assertEquals([
24+
['id' => 1, 'title' => 'Bar title 1'],
25+
['id' => 2, 'title' => 'Bar title 2'],
26+
['id' => 3, 'title' => 'Bar title 3'],
27+
['id' => 4, 'title' => 'Bar title 4'],
28+
['id' => 5, 'title' => 'Bar title 5'],
29+
], $data->fetchAllAssociative());
30+
31+
$this->ensureKernelShutdown();
32+
$this->bootKernel();
33+
34+
$loader = $this->getContainer()->get(QueryResourcesLoaderInterface::class);
35+
$data = $loader->execute('SELECT * FROM bar', null, Options::cached(new CacheIdentity(
36+
'foo',
37+
)));
38+
39+
$this->assertEquals([
40+
['id' => 1, 'title' => 'Bar title 1'],
41+
['id' => 2, 'title' => 'Bar title 2'],
42+
['id' => 3, 'title' => 'Bar title 3'],
43+
['id' => 4, 'title' => 'Bar title 4'],
44+
['id' => 5, 'title' => 'Bar title 5'],
45+
], $data->fetchAllAssociative());
46+
}
47+
}

tests/KernelTestCase.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,36 @@
88
use Doctrine\DBAL\Configuration;
99
use Doctrine\DBAL\Connection;
1010
use Doctrine\DBAL\Driver\Middleware;
11+
use RunOpenCode\Bundle\QueryResourcesLoader\Cache\CacheMiddleware;
12+
use RunOpenCode\Bundle\QueryResourcesLoader\Tests\Fixtures\Fixtures;
1113
use RunOpenCode\Bundle\QueryResourcesLoader\Tests\Resources\App\TestKernel;
1214
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase as SymfonyKernelTestCase;
15+
use Symfony\Contracts\Cache\CacheInterface;
16+
use Symfony\Contracts\Cache\TagAwareCacheInterface;
1317

1418
/**
1519
* @psalm-suppress UnnecessaryVarAnnotation, PossiblyNullFunctionCall, UndefinedThisPropertyFetch
1620
*/
1721
abstract class KernelTestCase extends SymfonyKernelTestCase
1822
{
23+
protected function setUp(): void
24+
{
25+
parent::setUp();
26+
$this->clearCache();
27+
}
28+
1929
protected static function getKernelClass(): string
2030
{
2131
return TestKernel::class;
2232
}
33+
34+
protected final function createFixtures(): void
35+
{
36+
$this->getContainer()->get(Fixtures::class)->execute(); // @phpstan-ignore-line
37+
$this->clearLoggedQueryStatements();
38+
}
2339

24-
protected function clearLoggedQueryStatements(): void
40+
protected final function clearLoggedQueryStatements(): void
2541
{
2642
/** @var Configuration $configuration */
2743
$configuration = $this->getContainer()->get(Connection::class)->getConfiguration(); // @phpstan-ignore-line
@@ -37,7 +53,7 @@ protected function clearLoggedQueryStatements(): void
3753
/**
3854
* @return array<string, string[]>
3955
*/
40-
protected function getLoggedQueryStatements(): array
56+
protected final function getLoggedQueryStatements(): array
4157
{
4258
/** @var Configuration $configuration */
4359
$configuration = $this->getContainer()->get(Connection::class)->getConfiguration(); // @phpstan-ignore-line
@@ -60,4 +76,27 @@ protected function getLoggedQueryStatements(): array
6076

6177
return $result;
6278
}
79+
80+
/**
81+
* Clear cache for the current test.
82+
*/
83+
protected final function clearCache(): void
84+
{
85+
$booted = self::$booted;
86+
87+
if (!$booted) {
88+
self::bootKernel();
89+
}
90+
91+
/** @var CacheInterface&TagAwareCacheInterface $pool */
92+
$pool = $this->getContainer()->get('app.roc_test_cache');
93+
94+
$pool->invalidateTags([
95+
CacheMiddleware::TAG,
96+
]);
97+
98+
if ($booted) {
99+
self::ensureKernelShutdown();
100+
}
101+
}
63102
}

tests/Legacy/ManagerTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use RunOpenCode\Bundle\QueryResourcesLoader\Contract\ManagerInterface;
1111
use RunOpenCode\Bundle\QueryResourcesLoader\Exception\RuntimeException;
1212
use RunOpenCode\Bundle\QueryResourcesLoader\Executor\Dbal\DoctrineDbalExecutionResult;
13-
use RunOpenCode\Bundle\QueryResourcesLoader\Tests\Fixtures\Fixtures;
1413
use RunOpenCode\Bundle\QueryResourcesLoader\Tests\KernelTestCase;
1514

1615
final class ManagerTest extends KernelTestCase
@@ -23,7 +22,7 @@ protected function setUp(): void
2322

2423
$this->manager = $this->getContainer()->get(ManagerInterface::class); // @phpstan-ignore-line
2524

26-
$this->getContainer()->get(Fixtures::class)->execute(); // @phpstan-ignore-line
25+
$this->createFixtures();
2726
}
2827

2928
public function testItExecutesFromDefaultExecutor(): void

tests/QueryResourcesLoaderTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use RunOpenCode\Bundle\QueryResourcesLoader\Executor\Dbal\DbalOptions;
99
use RunOpenCode\Bundle\QueryResourcesLoader\Model\Options;
1010
use RunOpenCode\Bundle\QueryResourcesLoader\Model\Parameters;
11-
use RunOpenCode\Bundle\QueryResourcesLoader\Tests\Fixtures\Fixtures;
1211

1312
final class QueryResourcesLoaderTest extends KernelTestCase
1413
{
@@ -18,10 +17,9 @@ public function setUp(): void
1817
{
1918
parent::setUp();
2019

21-
$this->getContainer()->get(Fixtures::class)->execute(); // @phpstan-ignore-line
22-
$this->clearLoggedQueryStatements();
23-
2420
$this->loader = $this->getContainer()->get(QueryResourcesLoaderInterface::class); // @phpstan-ignore-line
21+
22+
$this->createFixtures();
2523
}
2624

2725
public function testItExecutesQuery(): void

tests/Resources/App/TestKernel.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Bundle\MonologBundle\MonologBundle;
1515
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1616
use Symfony\Component\HttpKernel\Kernel;
17-
1817
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
1918

2019
final class TestKernel extends Kernel
@@ -44,6 +43,14 @@ public function configureContainer(ContainerConfigurator $container): void
4443
'session' => [
4544
'storage_factory_id' => 'session.storage.factory.mock_file',
4645
],
46+
'cache' => [
47+
'pools' => [
48+
'app.roc_test_cache' => [
49+
'adapter' => 'cache.adapter.filesystem',
50+
'tags' => true,
51+
],
52+
],
53+
],
4754
]);
4855

4956
$container->extension('doctrine', [
@@ -69,11 +76,17 @@ public function configureContainer(ContainerConfigurator $container): void
6976
$container->extension('monolog', [
7077
'handlers' => [
7178
'main' => [
72-
'type' => 'test',
73-
'level' => 'debug',
79+
'type' => 'test',
80+
'level' => 'debug',
7481
],
7582
],
7683
]);
84+
85+
$container->extension('runopencode_query_resources_loader', [
86+
'cache' => [
87+
'pool' => 'app.roc_test_cache',
88+
],
89+
]);
7790

7891
$container
7992
->services()

tests/bootstrap.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
declare(strict_types=1);
44

5-
use RunOpenCode\Bundle\QueryResourcesLoader\Tests\Fixtures\Fixtures;
6-
use RunOpenCode\Bundle\QueryResourcesLoader\Tests\Resources\App\TestKernel;
75
use Symfony\Component\Filesystem\Filesystem;
86

97
require \dirname(__DIR__) . '/vendor/autoload.php';
@@ -19,17 +17,4 @@
1917
if (\in_array(\strtolower($bootstrap), ['yes', '1', 'true'], true)) {
2018
// Clear the cache before running the tests.
2119
(new Filesystem())->remove(__DIR__ . '/Resources/App/var/cache');
22-
23-
$kernel = new TestKernel('test', false);
24-
25-
$kernel->boot();
26-
27-
try {
28-
/**
29-
* @phpstan-ignore-next-line
30-
*/
31-
$kernel->getContainer()->get(Fixtures::class)->execute();
32-
} finally {
33-
$kernel->shutdown();
34-
}
3520
}

0 commit comments

Comments
 (0)