Skip to content

Commit a56dba2

Browse files
authored
Merge pull request #449 from jackalope/1-to-2
1 to 2
2 parents 78cf8b9 + 55d3571 commit a56dba2

25 files changed

+112
-52
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changelog
22
=========
33

4+
2.0.1
5+
-----
6+
7+
* Fixed cache key sanitize for PSR-16 cache.
8+
* Fixed not found detection for PSR-16 cache.
9+
410
2.0.0
511
-----
612

@@ -24,6 +30,12 @@ Changelog
2430
1.x
2531
===
2632

33+
1.13.0
34+
------
35+
36+
* Fixed cache key sanitize for PSR-16 cache.
37+
* Fixed not found detection for PSR-16 cache.
38+
2739
1.12.0
2840
------
2941

composer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@
4343
"psr-0": { "Jackalope\\": "src/" }
4444
},
4545
"autoload-dev": {
46+
"psr-4": {
47+
"Jackalope\\Tests\\": "tests/"
48+
},
4649
"psr-0": {
47-
"Jackalope\\Test\\": "tests/",
4850
"Jackalope\\": "vendor/jackalope/jackalope/tests",
4951
"PHPCR": "vendor/phpcr/phpcr/tests"
5052
}

phpunit.xml.dist

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727

2828
<testsuites>
2929
<testsuite name="Jackalope Doctrine DBAL Tests">
30-
<directory>tests/Jackalope/Transport</directory>
31-
<directory>tests/Jackalope/Tools</directory>
30+
<directory>tests/Transport</directory>
31+
<directory>tests/Tools</directory>
3232
<directory>vendor/jackalope/jackalope/tests</directory>
3333
<directory>vendor/phpcr/phpcr/tests</directory>
3434
<directory>vendor/phpcr/phpcr-utils/tests</directory>

src/Jackalope/Transport/DoctrineDBAL/CachedClient.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ public function __construct(FactoryInterface $factory, Connection $conn, array $
5050
}
5151
}
5252
$this->caches = $caches;
53-
$this->keySanitizer = static function (string $cacheKey): string {
54-
return str_replace(' ', '_', $cacheKey);
53+
$this->keySanitizer = static function ($cacheKey) {
54+
return str_replace(
55+
['%', '.'],
56+
['_', '|'],
57+
\urlencode($cacheKey)
58+
);
5559
};
5660
}
5761

@@ -219,7 +223,7 @@ public function getNode(string $path): \stdClass
219223
$cacheKey = "nodes: $path, ".$this->workspaceName;
220224
$cacheKey = $this->sanitizeKey($cacheKey);
221225

222-
if (false !== ($result = $this->caches['nodes']->get($cacheKey))) {
226+
if (null !== ($result = $this->caches['nodes']->get($cacheKey))) {
223227
if ('ItemNotFoundException' === $result) {
224228
throw new ItemNotFoundException("Item '$path' not found in workspace '$this->workspaceName'");
225229
}
@@ -354,7 +358,7 @@ public function getNodePathForIdentifier($uuid, $workspace = null): string
354358
$cacheKey = "nodes by uuid: $uuid, $this->workspaceName";
355359
$cacheKey = $this->sanitizeKey($cacheKey);
356360

357-
if (false !== ($result = $this->caches['nodes']->get($cacheKey))) {
361+
if (null !== ($result = $this->caches['nodes']->get($cacheKey))) {
358362
if ('ItemNotFoundException' === $result) {
359363
throw new ItemNotFoundException("no item found with uuid $uuid");
360364
}
@@ -407,7 +411,7 @@ public function getReferences($path, $name = null): array
407411
$cacheKey = "nodes references: $path, $name, ".$this->workspaceName;
408412
$cacheKey = $this->sanitizeKey($cacheKey);
409413

410-
if (false !== ($result = $this->caches['nodes']->get($cacheKey))) {
414+
if (null !== ($result = $this->caches['nodes']->get($cacheKey))) {
411415
return $result;
412416
}
413417

@@ -449,7 +453,7 @@ public function query(Query $query): array
449453
$cacheKey = "query: {$query->getStatement()}, {$query->getLimit()}, {$query->getOffset()}, {$query->getLanguage()}, ".$this->workspaceName;
450454
$cacheKey = $this->sanitizeKey($cacheKey);
451455

452-
if (false !== ($result = $this->caches['query']->get($cacheKey))) {
456+
if (null !== ($result = $this->caches['query']->get($cacheKey))) {
453457
return $result;
454458
}
455459

tests/Jackalope/Test/FunctionalTestCase.php tests/FunctionalTestCase.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Jackalope\Test;
3+
namespace Jackalope\Tests;
44

55
use Doctrine\DBAL\Connection;
66
use Doctrine\DBAL\Platforms\SqlitePlatform;
@@ -10,6 +10,7 @@
1010
use Jackalope\Transport\DoctrineDBAL\Client;
1111
use Jackalope\Transport\DoctrineDBAL\RepositorySchema;
1212
use Jackalope\Transport\TransportInterface;
13+
use Jackalope\Transport\WorkspaceManagementInterface;
1314
use PHPCR\RepositoryException;
1415
use PHPCR\SimpleCredentials;
1516

@@ -29,6 +30,7 @@ public function setUp(): void
2930
$conn = $this->getConnection();
3031
$this->loadFixtures($conn);
3132
$this->transport = $this->getClient($conn);
33+
$this->assertInstanceOf(WorkspaceManagementInterface::class, $this->transport);
3234

3335
$this->transport->createWorkspace('default');
3436
$this->repository = new Repository(null, $this->transport);

tests/ImplementationLoader.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
use Jackalope\Repository;
99
use Jackalope\RepositoryFactoryDoctrineDBAL;
1010
use Jackalope\Session;
11-
use Jackalope\Test\Tester\Generic;
12-
use Jackalope\Test\Tester\Mysql;
13-
use Jackalope\Test\Tester\Pgsql;
11+
use Jackalope\Tests\Test\Tester\Generic;
12+
use Jackalope\Tests\Test\Tester\Mysql;
13+
use Jackalope\Tests\Test\Tester\Pgsql;
1414
use Jackalope\Transport\DoctrineDBAL\Client;
1515
use Jackalope\Transport\Logging\Psr3Logger;
1616
use PHPCR\RepositoryException;
@@ -141,8 +141,8 @@ public function getRepositoryFactoryParameters()
141141

142142
public function getSessionWithLastModified()
143143
{
144-
/** @var $session Session */
145144
$session = $this->getSession();
145+
\assert($session instanceof Session);
146146
$session->setSessionOption(Session::OPTION_AUTO_LASTMODIFIED, true);
147147

148148
return $session;

tests/Jackalope/RepositoryFactoryDoctrineDBALTest.php tests/RepositoryFactoryDoctrineDBALTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

3-
namespace Jackalope;
3+
namespace Jackalope\Tests;
44

55
use Doctrine\DBAL\Connection;
6+
use Jackalope\RepositoryFactoryDoctrineDBAL;
67
use PHPCR\ConfigurationException;
78
use PHPUnit\Framework\TestCase;
89

tests/Jackalope/Test/Fixture/DBUnitFixtureXML.php tests/Test/Fixture/DBUnitFixtureXML.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Jackalope\Test\Fixture;
3+
namespace Jackalope\Tests\Test\Fixture;
44

55
use PHPCR\Util\PathHelper;
66
use PHPCR\Util\UUIDHelper;

tests/Jackalope/Test/Fixture/JCRSystemXML.php tests/Test/Fixture/JCRSystemXML.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Jackalope\Test\Fixture;
3+
namespace Jackalope\Tests\Test\Fixture;
44

55
/**
66
* Jackalope Document or System Views.

tests/Jackalope/Test/Fixture/XMLDocument.php tests/Test/Fixture/XMLDocument.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Jackalope\Test\Fixture;
3+
namespace Jackalope\Tests\Test\Fixture;
44

55
/**
66
* Base for Jackalope Document or System Views and PHPUnit DBUnit Fixture XML classes.

tests/Jackalope/Test/Tester/Generic.php tests/Test/Tester/Generic.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Jackalope\Test\Tester;
3+
namespace Jackalope\Tests\Test\Tester;
44

55
use Doctrine\DBAL\Connection;
66
use PHPCR\Test\FixtureLoaderInterface;

tests/Jackalope/Test/Tester/Mysql.php tests/Test/Tester/Mysql.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Jackalope\Test\Tester;
3+
namespace Jackalope\Tests\Test\Tester;
44

55
/**
66
* MySQL specific tester class.

tests/Jackalope/Test/Tester/Pgsql.php tests/Test/Tester/Pgsql.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Jackalope\Test\Tester;
3+
namespace Jackalope\Tests\Test\Tester;
44

55
/**
66
* PostgreSQL specific tester class.

tests/Jackalope/Test/Tester/XmlDataSet.php tests/Test/Tester/XmlDataSet.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Jackalope\Test\Tester;
5+
namespace Jackalope\Tests\Test\Tester;
66

77
use Doctrine\DBAL\Schema\Column;
88
use Doctrine\DBAL\Schema\Index;

tests/Jackalope/Test/TestCase.php tests/TestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Jackalope\Test;
3+
namespace Jackalope\Tests;
44

55
use Doctrine\DBAL\Connection;
66
use Doctrine\DBAL\DriverManager;

tests/Jackalope/Tools/Console/InitDoctrineDbalCommandTest.php tests/Tools/Console/InitDoctrineDbalCommandTest.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Jackalope\Tools\Console;
3+
namespace Jackalope\Tests\Tools\Console;
44

55
use Doctrine\DBAL\Connection;
66
use Doctrine\DBAL\Platforms\AbstractPlatform;
@@ -9,6 +9,7 @@
99
use Doctrine\DBAL\Schema\SchemaConfig;
1010
use Jackalope\Tools\Console\Command\InitDoctrineDbalCommand;
1111
use Jackalope\Tools\Console\Helper\DoctrineDbalHelper;
12+
use PHPUnit\Framework\MockObject\MockObject;
1213
use PHPUnit\Framework\TestCase;
1314
use Symfony\Component\Console\Application;
1415
use Symfony\Component\Console\Helper\HelperSet;
@@ -22,7 +23,7 @@ class InitDoctrineDbalCommandTest extends TestCase
2223
protected $helperSet;
2324

2425
/**
25-
* @var Connection
26+
* @var Connection&MockObject
2627
*/
2728
protected $connection;
2829

@@ -32,7 +33,7 @@ class InitDoctrineDbalCommandTest extends TestCase
3233
protected $platform;
3334

3435
/**
35-
* @var AbstractSchemaManager
36+
* @var AbstractSchemaManager&MockObject
3637
*/
3738
protected $schemaManager;
3839

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Jackalope\Tests\Transport\DoctrineDBAL;
4+
5+
use Doctrine\DBAL\Connection;
6+
use Jackalope\Factory;
7+
use Jackalope\Transport\DoctrineDBAL\CachedClient;
8+
use Jackalope\Transport\TransportInterface;
9+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
10+
use Symfony\Component\Cache\Psr16Cache;
11+
12+
class CachedClientFunctionalTest extends ClientTest
13+
{
14+
protected function getClient(Connection $conn): TransportInterface
15+
{
16+
$nodeCacheAdapter = new ArrayAdapter();
17+
$nodeCache = new Psr16Cache($nodeCacheAdapter);
18+
19+
$metaCacheAdapter = new ArrayAdapter();
20+
$metaCache = new Psr16Cache($metaCacheAdapter);
21+
22+
return new CachedClient(new Factory(), $conn, [
23+
'nodes' => $nodeCache,
24+
'meta' => $metaCache,
25+
]);
26+
}
27+
}

tests/Jackalope/Transport/DoctrineDBAL/CachedClientTest.php tests/Transport/DoctrineDBAL/CachedClientTest.php

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22

3-
namespace Jackalope\Transport\DoctrineDBAL;
3+
namespace Jackalope\Tests\Transport\DoctrineDBAL;
44

55
use Doctrine\DBAL\Connection;
66
use Jackalope\Factory;
7-
use Jackalope\Test\FunctionalTestCase;
7+
use Jackalope\Tests\FunctionalTestCase;
8+
use Jackalope\Transport\DoctrineDBAL\CachedClient;
89
use Jackalope\Transport\TransportInterface;
910
use Psr\SimpleCache\CacheInterface;
1011
use Symfony\Component\Cache\Adapter\ArrayAdapter;
@@ -30,25 +31,28 @@ public function testArrayObjectIsConvertedToArray(): void
3031
self::assertIsArray($namespaces);
3132
}
3233

33-
public function testCacheHit()
34+
public function testCacheHit(): void
3435
{
3536
$cache = new \stdClass();
3637
$cache->foo = 'bar';
37-
$this->cache->set('nodes:_/test,_tests', $cache);
38+
$this->cache->set('nodes_3A+_2Ftest_2C+tests', $cache);
3839
$this->assertEquals($cache, $this->transport->getNode('/test'));
3940
}
4041

4142
/**
42-
* The default key sanitizer replaces spaces with underscores.
43+
* The default key sanitizer keeps the cache key compatible with PSR16.
4344
*/
4445
public function testDefaultKeySanitizer(): void
4546
{
46-
/** @var CachedClient $cachedClient */
47-
$cachedClient = $this->transport;
48-
$cachedClient->getNodeTypes();
47+
$client = $this->getClient($this->getConnection());
48+
$reflection = new \ReflectionClass($client);
49+
$keySanitizerProperty = $reflection->getProperty('keySanitizer');
50+
$keySanitizerProperty->setAccessible(true);
51+
$defaultKeySanitizer = $keySanitizerProperty->getValue($client);
52+
53+
$result = $defaultKeySanitizer(' :{}().@/"\\'); // not allowed PSR16 keys
4954

50-
$this->assertTrue($this->cache->has('node_types'));
51-
$this->assertTrue($this->cache->has('nodetypes:_a:0:{}'));
55+
$this->assertEquals('+_3A_7B_7D_28_29|_40_2F_22_5C', $result);
5256
}
5357

5458
public function testCustomKeySanitizer(): void

tests/Jackalope/Transport/DoctrineDBAL/ClientTest.php tests/Transport/DoctrineDBAL/ClientTest.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
22

3-
namespace Jackalope\Transport\DoctrineDBAL;
3+
namespace Jackalope\Tests\Transport\DoctrineDBAL;
44

55
use Doctrine\DBAL\Platforms\MySQLPlatform;
6-
use Jackalope\Test\FunctionalTestCase;
6+
use Jackalope\Tests\FunctionalTestCase;
7+
use Jackalope\Transport\DoctrineDBAL\Client;
8+
use Jackalope\Transport\WritingInterface;
79
use PHPCR\PropertyType;
810
use PHPCR\Query\QOM\QueryObjectModelConstantsInterface;
911
use PHPCR\Query\QueryInterface;
@@ -157,6 +159,7 @@ public function testDepthOnMove(): void
157159
$topic3->addNode('page3');
158160
$this->session->save();
159161

162+
$this->assertInstanceOf(WritingInterface::class, $this->transport);
160163
$this->transport->moveNodeImmediately('/topic2/page2', '/topic1/page1/page2');
161164

162165
$this->transport->moveNodeImmediately('/topic3', '/topic1/page1/page2/topic3');
@@ -323,7 +326,7 @@ public function testPropertyLengthAttribute(): void
323326

324327
$values = $xpath->query('sv:value', $propertyElement->item(0));
325328

326-
/** @var $value \DOMElement */
329+
/** @var \DOMElement $value */
327330
foreach ($values as $index => $value) {
328331
$lengthAttribute = $value->attributes->getNamedItem('length');
329332
if (null === $lengthAttribute) {

tests/Jackalope/Transport/DoctrineDBAL/DeleteCascadeTest.php tests/Transport/DoctrineDBAL/DeleteCascadeTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Jackalope\Transport\DoctrineDBAL;
3+
namespace Jackalope\Tests\Transport\DoctrineDBAL;
44

55
use Doctrine\DBAL\Platforms\SqlitePlatform;
6-
use Jackalope\Test\FunctionalTestCase;
6+
use Jackalope\Tests\FunctionalTestCase;
77
use PHPCR\PropertyType;
88

99
class DeleteCascadeTest extends FunctionalTestCase

tests/Jackalope/Transport/DoctrineDBAL/PrefetchTest.php tests/Transport/DoctrineDBAL/PrefetchTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Jackalope\Transport\DoctrineDBAL;
3+
namespace Jackalope\Tests\Transport\DoctrineDBAL;
44

5-
use Jackalope\Test\FunctionalTestCase;
5+
use Jackalope\Tests\FunctionalTestCase;
66

77
class PrefetchTest extends FunctionalTestCase
88
{

0 commit comments

Comments
 (0)