Skip to content

Commit

Permalink
Merge branch '1.4' into 1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
eigan committed Jul 23, 2020
2 parents bcae6f3 + e3d8692 commit ed08f53
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 10 deletions.
2 changes: 1 addition & 1 deletion config/doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
| Configure meta-data, query and result caching here.
| Optionally you can enable second level caching.
|
| Available: apc|array|file|memcached|php_file|redis|void
| Available: apc|array|file|illuminate|memcached|php_file|redis|void
|
*/
'cache' => [
Expand Down
5 changes: 4 additions & 1 deletion src/Configuration/Cache/FileCacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\Common\Cache\FilesystemCache;
use Illuminate\Contracts\Config\Repository;
use LaravelDoctrine\ORM\Configuration\Driver;
use function storage_path;

class FileCacheProvider implements Driver
{
Expand All @@ -28,8 +29,10 @@ public function __construct(Repository $config)
*/
public function resolve(array $settings = [])
{
$path = $settings['path'] ?? $this->config->get('cache.stores.file.path', storage_path('framework/cache'));

return new FilesystemCache(
$this->config->get('cache.stores.file.path', storage_path('framework/cache'))
$path
);
}
}
16 changes: 14 additions & 2 deletions src/Configuration/Cache/IlluminateCacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace LaravelDoctrine\ORM\Configuration\Cache;

use const E_USER_DEPRECATED;
use Illuminate\Contracts\Cache\Factory;
use InvalidArgumentException;
use LaravelDoctrine\ORM\Configuration\Driver;

abstract class IlluminateCacheProvider implements Driver
class IlluminateCacheProvider implements Driver
{
/**
* @var Factory
Expand All @@ -32,8 +34,18 @@ public function __construct(Factory $cache)
*/
public function resolve(array $settings = [])
{
$store = $this->store ?? $settings['store'] ?? null;

if ($store === null) {
throw new InvalidArgumentException('Please specify the `store` when using the "illuminate" cache driver.');
}

if ($this->store && isset($settings['store'])) {
trigger_error('Using driver "' . $this->store . '" with a custom store is deprecated. Please use the "illuminate" driver.', E_USER_DEPRECATED);
}

return new IlluminateCacheAdapter(
$this->cache->store($this->store)
$this->cache->store($store)
);
}
}
4 changes: 3 additions & 1 deletion src/Configuration/Cache/PhpFileCacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ public function __construct(Repository $config)
*/
public function resolve(array $settings = [])
{
$path = $settings['path'] ?? $this->config->get('cache.stores.file.path', storage_path('framework/cache'));

return new PhpFileCache(
$this->config->get('cache.stores.file.path', storage_path('framework/cache'))
$path
);
}
}
5 changes: 3 additions & 2 deletions src/EntityManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,10 @@ private function applyNamedCacheConfiguration($cacheName)
$defaultDriver = $this->config->get('doctrine.cache.default', 'array');
$defaultNamespace = $this->config->get('doctrine.cache.namespace');

$driver = $this->config->get('doctrine.cache.' . $cacheName . '.driver', $defaultDriver);
$settings = $this->config->get('doctrine.cache.' . $cacheName, []);
$driver = $settings['driver'] ?? $defaultDriver;

$cache = $this->cache->driver($driver);
$cache = $this->cache->driver($driver, $settings);

if ($namespace = $this->config->get('doctrine.cache.' . $cacheName . '.namespace', $defaultNamespace)) {
$cache->setNamespace($namespace);
Expand Down
3 changes: 2 additions & 1 deletion src/Testing/ConfigRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LaravelDoctrine\ORM\Testing;

use Illuminate\Contracts\Config\Repository;
use Illuminate\Support\Arr;

class ConfigRepository implements Repository
{
Expand All @@ -23,7 +24,7 @@ public function all()

public function get($key, $default = null)
{
return $this->items[$key] ?? $default;
return Arr::get($this->items, $key, $default);
}

public function set($key, $value = null)
Expand Down
241 changes: 239 additions & 2 deletions tests/EntityManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Container\Container;
use LaravelDoctrine\ORM\Configuration\Cache\CacheManager;
use LaravelDoctrine\ORM\Configuration\Cache\IlluminateCacheAdapter;
use LaravelDoctrine\ORM\Configuration\Connections\ConnectionManager;
use LaravelDoctrine\ORM\Configuration\LaravelNamingStrategy;
use LaravelDoctrine\ORM\Configuration\MetaData\MetaDataManager;
Expand Down Expand Up @@ -485,6 +486,242 @@ public function test_can_set_repository_factory()
$this->assertEntityManager($manager);
}

public function test_illuminate_cache_provider_custom_store()
{
m::resetContainer();

$config = new ConfigRepository([
'database.connections.mysql' => [
'driver' => 'mysql'
],
'doctrine' => [
'meta' => 'annotations',
'connection' => 'mysql',
'paths' => ['Entities'],
'proxies' => [
'path' => 'dir',
'auto_generate' => false,
'namespace' => 'namespace'
],

'cache' => [
'metadata' => [
'driver' => 'illuminate',
'store' => 'myStoreName'
]
]
],
'doctrine.custom_datetime_functions' => [],
'doctrine.custom_numeric_functions' => [],
'doctrine.custom_string_functions' => []
]);

$container = new \Illuminate\Container\Container();
$container->singleton(Repository::class, function () use ($config) {
return $config;
});

$cache = M::mock(Illuminate\Contracts\Cache\Repository::class);

$factory = M::mock(\Illuminate\Contracts\Cache\Factory::class);
$factory->shouldReceive('store')->with('myStoreName')->andReturn($cache);

$container->singleton(Illuminate\Contracts\Cache\Factory::class, function () use ($factory) {
return $factory;
});

$factory = new EntityManagerFactory(
$container,
new Setup(),
new MetaDataManager($container),
new ConnectionManager($container),
new CacheManager($container),
$config,
new EntityListenerResolver($container)
);

$manager = $factory->create($config->get('doctrine'));

$this->assertInstanceOf(IlluminateCacheAdapter::class, $manager->getConfiguration()->getMetadataCacheImpl());
}

public function test_illuminate_cache_provider_redis()
{
m::resetContainer();

$config = new ConfigRepository([
'database.connections.mysql' => [
'driver' => 'mysql'
],
'doctrine' => [
'meta' => 'annotations',
'connection' => 'mysql',
'paths' => ['Entities'],
'proxies' => [
'path' => 'dir',
'auto_generate' => false,
'namespace' => 'namespace'
],

'cache' => [
'metadata' => [
'driver' => 'redis',
]
]
],
'doctrine.custom_datetime_functions' => [],
'doctrine.custom_numeric_functions' => [],
'doctrine.custom_string_functions' => []
]);

$container = new \Illuminate\Container\Container();
$container->singleton(Repository::class, function () use ($config) {
return $config;
});

$cache = M::mock(Illuminate\Contracts\Cache\Repository::class);

$factory = M::mock(\Illuminate\Contracts\Cache\Factory::class);
$factory->shouldReceive('store')->with('redis')->andReturn($cache);

$container->singleton(Illuminate\Contracts\Cache\Factory::class, function () use ($factory) {
return $factory;
});

$factory = new EntityManagerFactory(
$container,
new Setup(),
new MetaDataManager($container),
new ConnectionManager($container),
new CacheManager($container),
$config,
new EntityListenerResolver($container)
);

$manager = $factory->create($config->get('doctrine'));

$this->assertInstanceOf(IlluminateCacheAdapter::class, $manager->getConfiguration()->getMetadataCacheImpl());
}

public function test_illuminate_cache_provider_invalid_store()
{
m::resetContainer();

$config = new ConfigRepository([
'database.connections.mysql' => [
'driver' => 'mysql'
],
'doctrine' => [
'meta' => 'annotations',
'connection' => 'mysql',
'paths' => ['Entities'],
'proxies' => [
'path' => 'dir',
'auto_generate' => false,
'namespace' => 'namespace'
],

'cache' => [
'metadata' => [
'driver' => 'illuminate',
]
]
],
'doctrine.custom_datetime_functions' => [],
'doctrine.custom_numeric_functions' => [],
'doctrine.custom_string_functions' => []
]);

$container = new \Illuminate\Container\Container();
$container->singleton(Repository::class, function () use ($config) {
return $config;
});

$cache = M::mock(Illuminate\Contracts\Cache\Repository::class);

$factory = M::mock(\Illuminate\Contracts\Cache\Factory::class);
$factory->shouldReceive('store')->with('myStoreName')->andReturn($cache);

$container->singleton(Illuminate\Contracts\Cache\Factory::class, function () use ($factory) {
return $factory;
});

$factory = new EntityManagerFactory(
$container,
new Setup(),
new MetaDataManager($container),
new ConnectionManager($container),
new CacheManager($container),
$config,
new EntityListenerResolver($container)
);

$this->expectException(InvalidArgumentException::class);

$this->expectExceptionMessage('Please specify the `store` when using the "illuminate" cache driver.');
$factory->create($config->get('doctrine'));
}

public function test_php_file_cache_custom_path()
{
m::resetContainer();

$config = new ConfigRepository([
'database.connections.mysql' => [
'driver' => 'mysql'
],
'doctrine' => [
'meta' => 'annotations',
'connection' => 'mysql',
'paths' => ['Entities'],
'proxies' => [
'path' => 'dir',
'auto_generate' => false,
'namespace' => 'namespace'
],

'cache' => [
'metadata' => [
'driver' => 'php_file',
'path' => 'myCustomPath'
]
]
],
'doctrine.custom_datetime_functions' => [],
'doctrine.custom_numeric_functions' => [],
'doctrine.custom_string_functions' => []
]);

$container = new \Illuminate\Container\Container();
$container->singleton(Repository::class, function () use ($config) {
return $config;
});

$cache = M::mock(Illuminate\Contracts\Cache\Repository::class);

$factory = M::mock(\Illuminate\Contracts\Cache\Factory::class);
$factory->shouldReceive('store')->with('myStoreName')->andReturn($cache);

$container->singleton(Illuminate\Contracts\Cache\Factory::class, function () use ($factory) {
return $factory;
});

$factory = new EntityManagerFactory(
$container,
new Setup(),
new MetaDataManager($container),
new ConnectionManager($container),
new CacheManager($container),
$config,
new EntityListenerResolver($container)
);

$manager = $factory->create($config->get('doctrine'));

$this->assertInstanceOf(\Doctrine\Common\Cache\PhpFileCache::class, $manager->getConfiguration()->getMetadataCacheImpl());
$this->assertStringEndsWith('myCustomPath', $manager->getConfiguration()->getMetadataCacheImpl()->getDirectory());
}

public function test_wrapper_connection()
{
m::resetContainer();
Expand Down Expand Up @@ -592,8 +829,8 @@ protected function mockConfig($driverConfig = ['driver' => 'mysql'], $strictCall

foreach ($this->caches as $cache) {
$expectation = $this->config->shouldReceive('get')
->with('doctrine.cache.' . $cache . '.driver', 'array')
->andReturn('array');
->with('doctrine.cache.' . $cache, [])
->andReturn(['driver' => 'array']);

$strictCallCountChecking ? $expectation->once() : $expectation->never();
}
Expand Down

0 comments on commit ed08f53

Please sign in to comment.