Skip to content

Commit

Permalink
Merge pull request #449 from jackalope/1-to-2
Browse files Browse the repository at this point in the history
1 to 2
  • Loading branch information
dbu committed May 7, 2024
2 parents 78cf8b9 + 55d3571 commit a56dba2
Show file tree
Hide file tree
Showing 25 changed files with 112 additions and 52 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

2.0.1
-----

* Fixed cache key sanitize for PSR-16 cache.
* Fixed not found detection for PSR-16 cache.

2.0.0
-----

Expand All @@ -24,6 +30,12 @@ Changelog
1.x
===

1.13.0
------

* Fixed cache key sanitize for PSR-16 cache.
* Fixed not found detection for PSR-16 cache.

1.12.0
------

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
"psr-0": { "Jackalope\\": "src/" }
},
"autoload-dev": {
"psr-4": {
"Jackalope\\Tests\\": "tests/"
},
"psr-0": {
"Jackalope\\Test\\": "tests/",
"Jackalope\\": "vendor/jackalope/jackalope/tests",
"PHPCR": "vendor/phpcr/phpcr/tests"
}
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

<testsuites>
<testsuite name="Jackalope Doctrine DBAL Tests">
<directory>tests/Jackalope/Transport</directory>
<directory>tests/Jackalope/Tools</directory>
<directory>tests/Transport</directory>
<directory>tests/Tools</directory>
<directory>vendor/jackalope/jackalope/tests</directory>
<directory>vendor/phpcr/phpcr/tests</directory>
<directory>vendor/phpcr/phpcr-utils/tests</directory>
Expand Down
16 changes: 10 additions & 6 deletions src/Jackalope/Transport/DoctrineDBAL/CachedClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ public function __construct(FactoryInterface $factory, Connection $conn, array $
}
}
$this->caches = $caches;
$this->keySanitizer = static function (string $cacheKey): string {
return str_replace(' ', '_', $cacheKey);
$this->keySanitizer = static function ($cacheKey) {
return str_replace(
['%', '.'],
['_', '|'],
\urlencode($cacheKey)
);
};
}

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

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

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

if (false !== ($result = $this->caches['nodes']->get($cacheKey))) {
if (null !== ($result = $this->caches['nodes']->get($cacheKey))) {
return $result;
}

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

if (false !== ($result = $this->caches['query']->get($cacheKey))) {
if (null !== ($result = $this->caches['query']->get($cacheKey))) {
return $result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Jackalope\Test;
namespace Jackalope\Tests;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\SqlitePlatform;
Expand All @@ -10,6 +10,7 @@
use Jackalope\Transport\DoctrineDBAL\Client;
use Jackalope\Transport\DoctrineDBAL\RepositorySchema;
use Jackalope\Transport\TransportInterface;
use Jackalope\Transport\WorkspaceManagementInterface;
use PHPCR\RepositoryException;
use PHPCR\SimpleCredentials;

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

$this->transport->createWorkspace('default');
$this->repository = new Repository(null, $this->transport);
Expand Down
8 changes: 4 additions & 4 deletions tests/ImplementationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use Jackalope\Repository;
use Jackalope\RepositoryFactoryDoctrineDBAL;
use Jackalope\Session;
use Jackalope\Test\Tester\Generic;
use Jackalope\Test\Tester\Mysql;
use Jackalope\Test\Tester\Pgsql;
use Jackalope\Tests\Test\Tester\Generic;
use Jackalope\Tests\Test\Tester\Mysql;
use Jackalope\Tests\Test\Tester\Pgsql;
use Jackalope\Transport\DoctrineDBAL\Client;
use Jackalope\Transport\Logging\Psr3Logger;
use PHPCR\RepositoryException;
Expand Down Expand Up @@ -141,8 +141,8 @@ public function getRepositoryFactoryParameters()

public function getSessionWithLastModified()
{
/** @var $session Session */
$session = $this->getSession();
\assert($session instanceof Session);
$session->setSessionOption(Session::OPTION_AUTO_LASTMODIFIED, true);

return $session;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace Jackalope;
namespace Jackalope\Tests;

use Doctrine\DBAL\Connection;
use Jackalope\RepositoryFactoryDoctrineDBAL;
use PHPCR\ConfigurationException;
use PHPUnit\Framework\TestCase;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Jackalope\Test\Fixture;
namespace Jackalope\Tests\Test\Fixture;

use PHPCR\Util\PathHelper;
use PHPCR\Util\UUIDHelper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Jackalope\Test\Fixture;
namespace Jackalope\Tests\Test\Fixture;

/**
* Jackalope Document or System Views.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Jackalope\Test\Fixture;
namespace Jackalope\Tests\Test\Fixture;

/**
* Base for Jackalope Document or System Views and PHPUnit DBUnit Fixture XML classes.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Jackalope\Test\Tester;
namespace Jackalope\Tests\Test\Tester;

use Doctrine\DBAL\Connection;
use PHPCR\Test\FixtureLoaderInterface;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Jackalope\Test\Tester;
namespace Jackalope\Tests\Test\Tester;

/**
* MySQL specific tester class.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Jackalope\Test\Tester;
namespace Jackalope\Tests\Test\Tester;

/**
* PostgreSQL specific tester class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Jackalope\Test\Tester;
namespace Jackalope\Tests\Test\Tester;

use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Index;
Expand Down
2 changes: 1 addition & 1 deletion tests/Jackalope/Test/TestCase.php → tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Jackalope\Test;
namespace Jackalope\Tests;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Jackalope\Tools\Console;
namespace Jackalope\Tests\Tools\Console;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
Expand All @@ -9,6 +9,7 @@
use Doctrine\DBAL\Schema\SchemaConfig;
use Jackalope\Tools\Console\Command\InitDoctrineDbalCommand;
use Jackalope\Tools\Console\Helper\DoctrineDbalHelper;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
Expand All @@ -22,7 +23,7 @@ class InitDoctrineDbalCommandTest extends TestCase
protected $helperSet;

/**
* @var Connection
* @var Connection&MockObject
*/
protected $connection;

Expand All @@ -32,7 +33,7 @@ class InitDoctrineDbalCommandTest extends TestCase
protected $platform;

/**
* @var AbstractSchemaManager
* @var AbstractSchemaManager&MockObject
*/
protected $schemaManager;

Expand Down
27 changes: 27 additions & 0 deletions tests/Transport/DoctrineDBAL/CachedClientFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Jackalope\Tests\Transport\DoctrineDBAL;

use Doctrine\DBAL\Connection;
use Jackalope\Factory;
use Jackalope\Transport\DoctrineDBAL\CachedClient;
use Jackalope\Transport\TransportInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Psr16Cache;

class CachedClientFunctionalTest extends ClientTest
{
protected function getClient(Connection $conn): TransportInterface
{
$nodeCacheAdapter = new ArrayAdapter();
$nodeCache = new Psr16Cache($nodeCacheAdapter);

$metaCacheAdapter = new ArrayAdapter();
$metaCache = new Psr16Cache($metaCacheAdapter);

return new CachedClient(new Factory(), $conn, [
'nodes' => $nodeCache,
'meta' => $metaCache,
]);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

namespace Jackalope\Transport\DoctrineDBAL;
namespace Jackalope\Tests\Transport\DoctrineDBAL;

use Doctrine\DBAL\Connection;
use Jackalope\Factory;
use Jackalope\Test\FunctionalTestCase;
use Jackalope\Tests\FunctionalTestCase;
use Jackalope\Transport\DoctrineDBAL\CachedClient;
use Jackalope\Transport\TransportInterface;
use Psr\SimpleCache\CacheInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
Expand All @@ -30,25 +31,28 @@ public function testArrayObjectIsConvertedToArray(): void
self::assertIsArray($namespaces);
}

public function testCacheHit()
public function testCacheHit(): void
{
$cache = new \stdClass();
$cache->foo = 'bar';
$this->cache->set('nodes:_/test,_tests', $cache);
$this->cache->set('nodes_3A+_2Ftest_2C+tests', $cache);
$this->assertEquals($cache, $this->transport->getNode('/test'));
}

/**
* The default key sanitizer replaces spaces with underscores.
* The default key sanitizer keeps the cache key compatible with PSR16.
*/
public function testDefaultKeySanitizer(): void
{
/** @var CachedClient $cachedClient */
$cachedClient = $this->transport;
$cachedClient->getNodeTypes();
$client = $this->getClient($this->getConnection());
$reflection = new \ReflectionClass($client);
$keySanitizerProperty = $reflection->getProperty('keySanitizer');
$keySanitizerProperty->setAccessible(true);
$defaultKeySanitizer = $keySanitizerProperty->getValue($client);

$result = $defaultKeySanitizer(' :{}().@/"\\'); // not allowed PSR16 keys

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

public function testCustomKeySanitizer(): void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

namespace Jackalope\Transport\DoctrineDBAL;
namespace Jackalope\Tests\Transport\DoctrineDBAL;

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

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

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

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

/** @var $value \DOMElement */
/** @var \DOMElement $value */
foreach ($values as $index => $value) {
$lengthAttribute = $value->attributes->getNamedItem('length');
if (null === $lengthAttribute) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Jackalope\Transport\DoctrineDBAL;
namespace Jackalope\Tests\Transport\DoctrineDBAL;

use Doctrine\DBAL\Platforms\SqlitePlatform;
use Jackalope\Test\FunctionalTestCase;
use Jackalope\Tests\FunctionalTestCase;
use PHPCR\PropertyType;

class DeleteCascadeTest extends FunctionalTestCase
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Jackalope\Transport\DoctrineDBAL;
namespace Jackalope\Tests\Transport\DoctrineDBAL;

use Jackalope\Test\FunctionalTestCase;
use Jackalope\Tests\FunctionalTestCase;

class PrefetchTest extends FunctionalTestCase
{
Expand Down

0 comments on commit a56dba2

Please sign in to comment.