Skip to content

Commit e840349

Browse files
committed
Tests: cover caching
1 parent c9b4379 commit e840349

File tree

3 files changed

+111
-12
lines changed

3 files changed

+111
-12
lines changed

.docs/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ nettrine.orm:
202202
managers:
203203
default:
204204
# Create cache manually
205-
defaultCache: App\CacheService(%tempDir%/cache/orm)
205+
defaultCache: App\CacheService(%tempDir%/cache/doctrine/orm)
206206
207207
# Use registered cache service
208208
defaultCache: @cacheService
@@ -214,10 +214,10 @@ Or you can define each cache separately.
214214
nettrine.orm:
215215
managers:
216216
default:
217-
queryCache: App\CacheService(%tempDir%/cache/orm-query)
218-
resultCache: App\CacheService(%tempDir%/cache/orm-result)
219-
hydrationCache: App\CacheService(%tempDir%/cache/orm-hydration)
220-
metadataCache: App\CacheService(%tempDir%/cache/orm-metadata)
217+
queryCache: App\CacheService(%tempDir%/cache/doctrine/orm/query)
218+
resultCache: App\CacheService(%tempDir%/cache/doctrine/orm/result)
219+
hydrationCache: App\CacheService(%tempDir%/cache/doctrine/orm/hydration)
220+
metadataCache: App\CacheService(%tempDir%/cache/doctrine/orm/metadata)
221221
```
222222

223223
Second level cache is a bit different. Be sure you know what you are doing, lear more in official [Doctrine documentation](https://www.doctrine-project.org/projects/doctrine-orm/en/3.3/reference/second-level-cache.html).
@@ -228,7 +228,7 @@ nettrine.orm:
228228
default:
229229
secondLevelCache:
230230
enable: true
231-
cache: App\CacheService(%tempDir%/cache/orm-slc)
231+
cache: App\CacheService(%tempDir%/cache/doctrine/orm/slc)
232232
logger: App\LoggerService()
233233
regions:
234234
region1:
@@ -246,13 +246,13 @@ nettrine.orm:
246246
managers:
247247
default:
248248
# Use default cache
249-
defaultCache: Symfony\Component\Cache\Adapter\FilesystemAdapter(namespace: doctrine-orm, defaultLifetime: 0, directory: %tempDir%/cache/orm)
249+
defaultCache: Symfony\Component\Cache\Adapter\FilesystemAdapter(namespace: orm, defaultLifetime: 0, directory: %tempDir%/cache/doctrine/orm)
250250
251251
# Or use separate caches
252-
queryCache: Symfony\Component\Cache\Adapter\FilesystemAdapter(namespace: doctrine-orm-query, defaultLifetime: 0, directory: %tempDir%/cache/orm-query)
253-
resultCache: Symfony\Component\Cache\Adapter\FilesystemAdapter(namespace: doctrine-orm-result, defaultLifetime: 0, directory: %tempDir%/cache/orm-result)
254-
hydrationCache: Symfony\Component\Cache\Adapter\FilesystemAdapter(namespace: doctrine-orm-hydration, defaultLifetime: 0, directory: %tempDir%/cache/orm-hydration)
255-
metadataCache: Symfony\Component\Cache\Adapter\FilesystemAdapter(namespace: doctrine-orm-metadata, defaultLifetime: 0, directory: %tempDir%/cache/orm-metadata)
252+
queryCache: Symfony\Component\Cache\Adapter\FilesystemAdapter(namespace: orm-query, defaultLifetime: 0, directory: %tempDir%/cache/doctrine/orm/query)
253+
resultCache: Symfony\Component\Cache\Adapter\FilesystemAdapter(namespace: orm-result, defaultLifetime: 0, directory: %tempDir%/cache/doctrine/orm/result)
254+
hydrationCache: Symfony\Component\Cache\Adapter\FilesystemAdapter(namespace: orm-hydration, defaultLifetime: 0, directory: %tempDir%/cache/doctrine/orm/hydration)
255+
metadataCache: Symfony\Component\Cache\Adapter\FilesystemAdapter(namespace: orm-metadata, defaultLifetime: 0, directory: %tempDir%/cache/doctrine/orm/metadata)
256256
```
257257

258258
If you like [`nette/caching`](https://github.com/nette/caching) you can use it as well. Be aware that `nette/caching` is not PSR-6 + PSR-16 compatible, you need `contributte/psr16-caching`.
@@ -264,7 +264,7 @@ nettrine.orm:
264264
defaultCache: Contributte\Psr6\CachePool(
265265
Nette\Caching\Cache(
266266
Nette\Caching\Storages\FileStorage(%tempDir%/cache)
267-
doctrine/dbal
267+
doctrine/orm
268268
)
269269
)
270270
```

tests/Cases/E2E/QueryTest.phpt

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22

33
namespace Tests\Cases\E2E;
44

5+
use Contributte\Tester\Environment;
56
use Contributte\Tester\Toolkit;
67
use Contributte\Tester\Utils\ContainerBuilder;
78
use Contributte\Tester\Utils\Neonkit;
89
use Doctrine\DBAL\Connection;
10+
use Doctrine\ORM\EntityManager;
11+
use Doctrine\ORM\EntityManagerInterface;
912
use Nette\DI\Compiler;
1013
use Nettrine\DBAL\DI\DbalExtension;
14+
use Nettrine\ORM\DI\OrmExtension;
1115
use Tester\Assert;
16+
use Tests\Mocks\Entity\DummyEntity;
1217

1318
require_once __DIR__ . '/../../bootstrap.php';
1419

20+
// DBAL
1521
Toolkit::test(function (): void {
1622
$container = ContainerBuilder::of()
1723
->withCompiler(function (Compiler $compiler): void {
@@ -73,3 +79,83 @@ Toolkit::test(function (): void {
7379
actual: $result
7480
);
7581
});
82+
83+
// DBAL + ORM
84+
Toolkit::test(function (): void {
85+
$container = ContainerBuilder::of()
86+
->withCompiler(function (Compiler $compiler): void {
87+
$compiler->addExtension('nettrine.dbal', new DbalExtension());
88+
$compiler->addExtension('nettrine.orm', new OrmExtension());
89+
$compiler->addConfig(Neonkit::load(<<<'NEON'
90+
nettrine.dbal:
91+
connections:
92+
default:
93+
driver: pdo_sqlite
94+
password: test
95+
user: test
96+
path: ":memory:"
97+
resultCache: Symfony\Component\Cache\Adapter\FilesystemAdapter(namespace: dbal, defaultLifetime: 0, directory: %tempDir%/cache/nettrine)
98+
nettrine.orm:
99+
managers:
100+
default:
101+
connection: default
102+
defaultCache: Symfony\Component\Cache\Adapter\FilesystemAdapter(namespace: orm, defaultLifetime: 0, directory: %tempDir%/cache/nettrine)
103+
secondLevelCache:
104+
enabled: true
105+
cache: Symfony\Component\Cache\Adapter\FilesystemAdapter(namespace: orm, defaultLifetime: 0, directory: %tempDir%/cache/nettrine/slc/region1)
106+
regions:
107+
region1:
108+
lifetime: 3600
109+
lockLifetime: 60
110+
region2:
111+
lifetime: 86000
112+
lockLifetime: 60
113+
mapping:
114+
App:
115+
type: attributes
116+
directories: [%appDir%/Mocks]
117+
namespace: Tests\Mocks
118+
119+
NEON
120+
));
121+
$compiler->addConfig([
122+
'parameters' => [
123+
'tempDir' => Environment::getTestDir(),
124+
'appDir' => Environment::getTmpDir(),
125+
],
126+
]);
127+
})
128+
->build();
129+
130+
/** @var Connection $connection */
131+
$connection = $container->getByType(Connection::class);
132+
$connection->executeQuery('CREATE TABLE dummy_entity (id integer primary key autoincrement, username string)');
133+
134+
/** @var EntityManager $em */
135+
$em = $container->getByType(EntityManagerInterface::class);
136+
137+
$em->persist(new DummyEntity('John'));
138+
$em->persist(new DummyEntity('Doe'));
139+
$em->flush();
140+
141+
$result = $em->createQueryBuilder()
142+
->from(DummyEntity::class, 'd')
143+
->addSelect('d.id', 'd.username')
144+
->getQuery()
145+
->enableResultCache(3600, 'dummy')
146+
->getArrayResult();
147+
148+
Assert::equal(
149+
expected: [
150+
[
151+
'id' => 1,
152+
'username' => 'John',
153+
],
154+
[
155+
'id' => 2,
156+
'username' => 'Doe',
157+
],
158+
],
159+
actual: $result
160+
);
161+
});

tests/Mocks/Entity/DummyEntity.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,22 @@ class DummyEntity implements DummyIdentity
1717
#[Id]
1818
private int $id;
1919

20+
#[Column(type: 'text', nullable: false)]
21+
private string $username;
22+
23+
public function __construct(string $username)
24+
{
25+
$this->username = $username;
26+
}
27+
2028
public function getId(): int
2129
{
2230
return $this->id;
2331
}
2432

33+
public function getUsername(): string
34+
{
35+
return $this->username;
36+
}
37+
2538
}

0 commit comments

Comments
 (0)