mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-18 15:33:15 +00:00
Refactor DataboxRepository
This commit is contained in:
@@ -18,7 +18,7 @@ interface Cache extends DoctrineCache
|
|||||||
/**
|
/**
|
||||||
* Sets the namespace
|
* Sets the namespace
|
||||||
*
|
*
|
||||||
* @param type $namespace
|
* @param string $namespace
|
||||||
*/
|
*/
|
||||||
public function setNamespace($namespace);
|
public function setNamespace($namespace);
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ interface Cache extends DoctrineCache
|
|||||||
* @return string The cached data.
|
* @return string The cached data.
|
||||||
* @return FALSE, if no cache entry exists for the given id.
|
* @return FALSE, if no cache entry exists for the given id.
|
||||||
*
|
*
|
||||||
* @throws Alchemy\Phrasea\Cache\Exception if provided key does not exist
|
* @throws Exception if provided key does not exist
|
||||||
*/
|
*/
|
||||||
public function get($key);
|
public function get($key);
|
||||||
|
|
||||||
@@ -61,7 +61,7 @@ interface Cache extends DoctrineCache
|
|||||||
* Delete multi cache entries
|
* Delete multi cache entries
|
||||||
*
|
*
|
||||||
* @param array $keys contains all keys to delete
|
* @param array $keys contains all keys to delete
|
||||||
* @return Alchemy\Phrasea\Cache\Cache
|
* @return Cache
|
||||||
*/
|
*/
|
||||||
public function deleteMulti(array $keys);
|
public function deleteMulti(array $keys);
|
||||||
}
|
}
|
||||||
|
@@ -12,7 +12,10 @@
|
|||||||
namespace Alchemy\Phrasea\Core\Provider;
|
namespace Alchemy\Phrasea\Core\Provider;
|
||||||
|
|
||||||
use Alchemy\Phrasea\Application as PhraseaApplication;
|
use Alchemy\Phrasea\Application as PhraseaApplication;
|
||||||
use Alchemy\Phrasea\Databox\DataboxRepository;
|
use Alchemy\Phrasea\Databox\CachedDataboxRepository;
|
||||||
|
use Alchemy\Phrasea\Databox\DataboxFactory;
|
||||||
|
use Alchemy\Phrasea\Databox\DataboxHydrator;
|
||||||
|
use Alchemy\Phrasea\Databox\DbalDataboxRepository;
|
||||||
use Silex\Application;
|
use Silex\Application;
|
||||||
use Silex\ServiceProviderInterface;
|
use Silex\ServiceProviderInterface;
|
||||||
|
|
||||||
@@ -124,7 +127,11 @@ class RepositoriesServiceProvider implements ServiceProviderInterface
|
|||||||
});
|
});
|
||||||
|
|
||||||
$app['repo.databoxes'] = $app->share(function (PhraseaApplication $app) {
|
$app['repo.databoxes'] = $app->share(function (PhraseaApplication $app) {
|
||||||
return new DataboxRepository($app, $app['phraseanet.appbox']);
|
$hydrator = new DataboxHydrator(new DataboxFactory($app));
|
||||||
|
$appbox = $app->getApplicationBox();
|
||||||
|
$repository = new DbalDataboxRepository($appbox->get_connection(), $hydrator);
|
||||||
|
|
||||||
|
return new CachedDataboxRepository($repository, $appbox, $hydrator);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
84
lib/Alchemy/Phrasea/Databox/CachedDataboxRepository.php
Normal file
84
lib/Alchemy/Phrasea/Databox/CachedDataboxRepository.php
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Phraseanet
|
||||||
|
*
|
||||||
|
* (c) 2005-2015 Alchemy
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
namespace Alchemy\Phrasea\Databox;
|
||||||
|
|
||||||
|
use Alchemy\Phrasea\Application;
|
||||||
|
use Alchemy\Phrasea\Cache\Exception;
|
||||||
|
|
||||||
|
class CachedDataboxRepository implements DataboxRepositoryInterface
|
||||||
|
{
|
||||||
|
/** @var DataboxRepositoryInterface */
|
||||||
|
private $repository;
|
||||||
|
/** @var \appbox */
|
||||||
|
private $appbox;
|
||||||
|
/** @var DataboxHydrator */
|
||||||
|
private $hydrator;
|
||||||
|
|
||||||
|
public function __construct(DataboxRepositoryInterface $repository, \appbox $appbox, DataboxHydrator $hydrator)
|
||||||
|
{
|
||||||
|
$this->repository = $repository;
|
||||||
|
$this->appbox = $appbox;
|
||||||
|
$this->hydrator = $hydrator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function find($id)
|
||||||
|
{
|
||||||
|
$rows = $this->fetchRows();
|
||||||
|
|
||||||
|
if (isset($rows[$id])) {
|
||||||
|
return $this->hydrator->hydrateRow($id, $rows[$id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->repository->find($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findAll()
|
||||||
|
{
|
||||||
|
$rows = $this->fetchRows();
|
||||||
|
|
||||||
|
if (is_array($rows)) {
|
||||||
|
return $this->hydrator->hydrateRows($rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
$databoxes = $this->repository->findAll();
|
||||||
|
|
||||||
|
$this->saveRows($databoxes);
|
||||||
|
|
||||||
|
return $databoxes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool|array false on cache miss
|
||||||
|
*/
|
||||||
|
private function fetchRows()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$rows = $this->appbox->get_data_from_cache(\appbox::CACHE_LIST_BASES);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$rows = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \databox[] $databoxes
|
||||||
|
*/
|
||||||
|
private function saveRows(array $databoxes)
|
||||||
|
{
|
||||||
|
$rows = array();
|
||||||
|
|
||||||
|
foreach ($databoxes as $databox) {
|
||||||
|
$rows[$databox->get_sbas_id()] = $databox->getAsRow();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->appbox->set_data_to_cache($rows, \appbox::CACHE_LIST_BASES);
|
||||||
|
}
|
||||||
|
}
|
@@ -10,11 +10,26 @@
|
|||||||
namespace Alchemy\Phrasea\Databox;
|
namespace Alchemy\Phrasea\Databox;
|
||||||
|
|
||||||
use Alchemy\Phrasea\Application;
|
use Alchemy\Phrasea\Application;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
|
|
||||||
class DataboxFactory
|
class DataboxFactory
|
||||||
{
|
{
|
||||||
public function create(Application $app, $id, array $raw = null)
|
/** @var Application */
|
||||||
|
private $app;
|
||||||
|
|
||||||
|
public function __construct(Application $app)
|
||||||
{
|
{
|
||||||
return new \databox($app, $id, $raw);
|
$this->app = $app;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @param array $raw
|
||||||
|
* @throws NotFoundHttpException when Databox could not be retrieved from Persistence layer
|
||||||
|
* @return \databox
|
||||||
|
*/
|
||||||
|
public function create($id, array $raw)
|
||||||
|
{
|
||||||
|
return new \databox($this->app, $id, $raw);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
47
lib/Alchemy/Phrasea/Databox/DataboxHydrator.php
Normal file
47
lib/Alchemy/Phrasea/Databox/DataboxHydrator.php
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Phraseanet
|
||||||
|
*
|
||||||
|
* (c) 2005-2015 Alchemy
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
namespace Alchemy\Phrasea\Databox;
|
||||||
|
|
||||||
|
class DataboxHydrator
|
||||||
|
{
|
||||||
|
/** @var DataboxFactory */
|
||||||
|
private $factory;
|
||||||
|
|
||||||
|
public function __construct(DataboxFactory $factory)
|
||||||
|
{
|
||||||
|
$this->factory = $factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @param array $row
|
||||||
|
* @return \databox
|
||||||
|
*/
|
||||||
|
public function hydrateRow($id, array $row)
|
||||||
|
{
|
||||||
|
return $this->factory->create($id, $row);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hydrate a list of databoxes keyed by their sbas_id
|
||||||
|
* @param array $rows
|
||||||
|
* @return \databox[]
|
||||||
|
*/
|
||||||
|
public function hydrateRows($rows)
|
||||||
|
{
|
||||||
|
$instances = array();
|
||||||
|
|
||||||
|
foreach ($rows as $id => $row) {
|
||||||
|
$instances[$id] = $this->hydrateRow($id, $row);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $instances;
|
||||||
|
}
|
||||||
|
}
|
@@ -1,78 +0,0 @@
|
|||||||
<?php
|
|
||||||
/*
|
|
||||||
* This file is part of Phraseanet
|
|
||||||
*
|
|
||||||
* (c) 2005-2015 Alchemy
|
|
||||||
*
|
|
||||||
* For the full copyright and license information, please view the LICENSE
|
|
||||||
* file that was distributed with this source code.
|
|
||||||
*/
|
|
||||||
namespace Alchemy\Phrasea\Databox;
|
|
||||||
|
|
||||||
use Alchemy\Phrasea\Application;
|
|
||||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
||||||
|
|
||||||
class DataboxRepository implements DataboxRepositoryInterface
|
|
||||||
{
|
|
||||||
/** @var Application */
|
|
||||||
private $app;
|
|
||||||
/** @var \appbox */
|
|
||||||
private $appbox;
|
|
||||||
/** @var DataboxFactory */
|
|
||||||
private $factory;
|
|
||||||
|
|
||||||
public function __construct(Application $app, \appbox $appbox, DataboxFactory $factory = null)
|
|
||||||
{
|
|
||||||
$this->app = $app;
|
|
||||||
$this->appbox = $appbox;
|
|
||||||
$this->factory = $factory ?: new DataboxFactory();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $id
|
|
||||||
* @return \databox|null
|
|
||||||
*/
|
|
||||||
public function find($id)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$databox = $this->factory->create($this->app, (int)$id);
|
|
||||||
} catch (NotFoundHttpException $exception) {
|
|
||||||
$databox = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $databox;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return \databox[]
|
|
||||||
*/
|
|
||||||
public function findAll()
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$rows = $this->appbox->get_data_from_cache(\appbox::CACHE_LIST_BASES);
|
|
||||||
if (!is_array($rows)) {
|
|
||||||
throw new \UnexpectedValueException('Expects rows to be an array');
|
|
||||||
}
|
|
||||||
} catch(\Exception $e) {
|
|
||||||
$connection = $this->appbox->get_connection();
|
|
||||||
|
|
||||||
$query = 'SELECT sbas_id, ord, viewname, label_en, label_fr, label_de, label_nl FROM sbas';
|
|
||||||
$statement = $connection->prepare($query);
|
|
||||||
$statement->execute();
|
|
||||||
$rows = $statement->fetchAll(\PDO::FETCH_ASSOC);
|
|
||||||
$statement->closeCursor();
|
|
||||||
|
|
||||||
$this->appbox->set_data_to_cache($rows, \appbox::CACHE_LIST_BASES);
|
|
||||||
}
|
|
||||||
|
|
||||||
$databoxes = array();
|
|
||||||
|
|
||||||
foreach ($rows as $row) {
|
|
||||||
$databox = $this->factory->create($this->app, (int)$row['sbas_id'], $row);
|
|
||||||
|
|
||||||
$databoxes[$databox->get_sbas_id()] = $databox;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $databoxes;
|
|
||||||
}
|
|
||||||
}
|
|
86
lib/Alchemy/Phrasea/Databox/DbalDataboxRepository.php
Normal file
86
lib/Alchemy/Phrasea/Databox/DbalDataboxRepository.php
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Phraseanet
|
||||||
|
*
|
||||||
|
* (c) 2005-2015 Alchemy
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
namespace Alchemy\Phrasea\Databox;
|
||||||
|
|
||||||
|
use Alchemy\Phrasea\Application;
|
||||||
|
use Doctrine\DBAL\Connection;
|
||||||
|
|
||||||
|
class DbalDataboxRepository implements DataboxRepositoryInterface
|
||||||
|
{
|
||||||
|
/** @var Connection */
|
||||||
|
private $connection;
|
||||||
|
/** @var DataboxHydrator */
|
||||||
|
private $hydrator;
|
||||||
|
|
||||||
|
public function __construct(Connection $connection, DataboxHydrator $hydrator)
|
||||||
|
{
|
||||||
|
$this->connection = $connection;
|
||||||
|
$this->hydrator = $hydrator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return \databox|null
|
||||||
|
*/
|
||||||
|
public function find($id)
|
||||||
|
{
|
||||||
|
$row = $this->fetchRow($id);
|
||||||
|
|
||||||
|
if (is_array($row)) {
|
||||||
|
return $this->hydrator->hydrateRow($id, $row);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \databox[]
|
||||||
|
*/
|
||||||
|
public function findAll()
|
||||||
|
{
|
||||||
|
return $this->hydrator->hydrateRows($this->fetchRows());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return false|array
|
||||||
|
* @throws \Doctrine\DBAL\DBALException
|
||||||
|
*/
|
||||||
|
private function fetchRow($id)
|
||||||
|
{
|
||||||
|
$query = 'SELECT ord, viewname, label_en, label_fr, label_de, label_nl FROM sbas WHERE sbas_id = :id';
|
||||||
|
$statement = $this->connection->prepare($query);
|
||||||
|
$statement->execute(['id' => $id]);
|
||||||
|
$row = $statement->fetch(\PDO::FETCH_ASSOC);
|
||||||
|
$statement->closeCursor();
|
||||||
|
|
||||||
|
return $row;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws \Doctrine\DBAL\DBALException
|
||||||
|
*/
|
||||||
|
private function fetchRows()
|
||||||
|
{
|
||||||
|
$query = 'SELECT sbas_id, ord, viewname, label_en, label_fr, label_de, label_nl FROM sbas';
|
||||||
|
$statement = $this->connection->prepare($query);
|
||||||
|
$statement->execute();
|
||||||
|
$rows = [];
|
||||||
|
while ($row = $statement->fetch(\PDO::FETCH_ASSOC)) {
|
||||||
|
$id = $row['sbas_id'];
|
||||||
|
unset($row['sbas_id']);
|
||||||
|
$rows[$id] = $row;
|
||||||
|
}
|
||||||
|
$statement->closeCursor();
|
||||||
|
|
||||||
|
return $rows;
|
||||||
|
}
|
||||||
|
}
|
@@ -85,6 +85,9 @@ abstract class base implements cache_cacheableInterface
|
|||||||
return $this->connection;
|
return $this->connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \Alchemy\Phrasea\Cache\Cache
|
||||||
|
*/
|
||||||
public function get_cache()
|
public function get_cache()
|
||||||
{
|
{
|
||||||
return $this->app['cache'];
|
return $this->app['cache'];
|
||||||
@@ -118,7 +121,6 @@ abstract class base implements cache_cacheableInterface
|
|||||||
phrasea::reset_sbasDatas($appbox);
|
phrasea::reset_sbasDatas($appbox);
|
||||||
phrasea::reset_baseDatas($appbox);
|
phrasea::reset_baseDatas($appbox);
|
||||||
phrasea::clear_sbas_params($this->app);
|
phrasea::clear_sbas_params($this->app);
|
||||||
$keys[] = $this->get_cache_key(appbox::CACHE_SBAS_IDS);
|
|
||||||
|
|
||||||
return $this->get_cache()->deleteMulti($keys);
|
return $this->get_cache()->deleteMulti($keys);
|
||||||
}
|
}
|
||||||
@@ -133,6 +135,11 @@ abstract class base implements cache_cacheableInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $option
|
||||||
|
* @throws Exception
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function get_cache_key($option = null)
|
public function get_cache_key($option = null)
|
||||||
{
|
{
|
||||||
throw new Exception(__METHOD__ . ' must be defined in extended class');
|
throw new Exception(__METHOD__ . ' must be defined in extended class');
|
||||||
|
1
lib/classes/cache/databox.php
vendored
1
lib/classes/cache/databox.php
vendored
@@ -102,7 +102,6 @@ class cache_databox
|
|||||||
break;
|
break;
|
||||||
case 'structure':
|
case 'structure':
|
||||||
$app->getApplicationBox()->delete_data_from_cache(\appbox::CACHE_LIST_BASES);
|
$app->getApplicationBox()->delete_data_from_cache(\appbox::CACHE_LIST_BASES);
|
||||||
$app->getApplicationBox()->delete_data_from_cache(\appbox::CACHE_SBAS_IDS);
|
|
||||||
|
|
||||||
$sql = 'DELETE FROM memcached
|
$sql = 'DELETE FROM memcached
|
||||||
WHERE site_id = :site_id AND type="structure" AND value = :value';
|
WHERE site_id = :site_id AND type="structure" AND value = :value';
|
||||||
|
@@ -95,12 +95,12 @@ class databox extends base
|
|||||||
$this->dbname = $params['dbname'];
|
$this->dbname = $params['dbname'];
|
||||||
|
|
||||||
if (empty($row)) {
|
if (empty($row)) {
|
||||||
$row = $this->loadRow();
|
$row = $this->fetchRow();
|
||||||
}
|
}
|
||||||
$this->loadFromRow($row);
|
$this->loadFromRow($row);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function loadRow()
|
private function fetchRow()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$row = $this->get_data_from_cache(static::CACHE_BASE_DATABOX);
|
$row = $this->get_data_from_cache(static::CACHE_BASE_DATABOX);
|
||||||
@@ -482,7 +482,6 @@ class databox extends base
|
|||||||
$stmt->closeCursor();
|
$stmt->closeCursor();
|
||||||
|
|
||||||
$this->get_appbox()->delete_data_from_cache(appbox::CACHE_LIST_BASES);
|
$this->get_appbox()->delete_data_from_cache(appbox::CACHE_LIST_BASES);
|
||||||
$this->get_appbox()->delete_data_from_cache(appbox::CACHE_SBAS_IDS);
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -633,7 +632,6 @@ class databox extends base
|
|||||||
$databox = $app->findDataboxById($sbas_id);
|
$databox = $app->findDataboxById($sbas_id);
|
||||||
|
|
||||||
$databox->delete_data_from_cache(databox::CACHE_COLLECTIONS);
|
$databox->delete_data_from_cache(databox::CACHE_COLLECTIONS);
|
||||||
$app->getApplicationBox()->delete_data_from_cache(appbox::CACHE_SBAS_IDS);
|
|
||||||
|
|
||||||
phrasea::reset_sbasDatas($app['phraseanet.appbox']);
|
phrasea::reset_sbasDatas($app['phraseanet.appbox']);
|
||||||
|
|
||||||
@@ -1527,4 +1525,21 @@ class databox extends base
|
|||||||
$this->labels['de'] = $row['label_de'];
|
$this->labels['de'] = $row['label_de'];
|
||||||
$this->labels['nl'] = $row['label_nl'];
|
$this->labels['nl'] = $row['label_nl'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an array that can be used to restore databox.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getAsRow()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'ord' => $this->ord,
|
||||||
|
'viewname' => $this->viewname,
|
||||||
|
'label_en' => $this->labels['en'],
|
||||||
|
'label_fr' => $this->labels['fr'],
|
||||||
|
'label_de' => $this->labels['de'],
|
||||||
|
'label_nl' => $this->labels['nl'],
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,111 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Phraseanet
|
||||||
|
*
|
||||||
|
* (c) 2005-2015 Alchemy
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
namespace Alchemy\Tests\Phrasea\Databox;
|
||||||
|
|
||||||
|
use Alchemy\Phrasea\Cache\Exception;
|
||||||
|
use Alchemy\Phrasea\Databox\CachedDataboxRepository;
|
||||||
|
use Alchemy\Phrasea\Databox\DataboxHydrator;
|
||||||
|
use Alchemy\Phrasea\Databox\DataboxRepositoryInterface;
|
||||||
|
use Prophecy\Argument;
|
||||||
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
|
|
||||||
|
final class CachedDataboxRepositoryTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/** @var ObjectProphecy */
|
||||||
|
private $appbox;
|
||||||
|
/** @var ObjectProphecy */
|
||||||
|
private $hydrator;
|
||||||
|
/** @var ObjectProphecy */
|
||||||
|
private $repository;
|
||||||
|
|
||||||
|
/** @var CachedDataboxRepository */
|
||||||
|
private $sut;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->appbox = $this->prophesize(\appbox::class);
|
||||||
|
$this->repository = $this->prophesize(DataboxRepositoryInterface::class);
|
||||||
|
$this->hydrator = $this->prophesize(DataboxHydrator::class);
|
||||||
|
|
||||||
|
$this->sut = new CachedDataboxRepository($this->repository->reveal(), $this->appbox->reveal(), $this->hydrator->reveal());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItImplementsDataboxRepositoryInterface()
|
||||||
|
{
|
||||||
|
$this->assertInstanceOf(DataboxRepositoryInterface::class, $this->sut);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItFindsASpecificDataboxWhenNotInCache()
|
||||||
|
{
|
||||||
|
$databox = $this->prophesize(\databox::class);
|
||||||
|
|
||||||
|
$this->appbox->get_data_from_cache(\appbox::CACHE_LIST_BASES)
|
||||||
|
->willReturn(false);
|
||||||
|
$this->repository->find(42)
|
||||||
|
->willReturn($databox->reveal());
|
||||||
|
|
||||||
|
$this->assertSame($databox->reveal(), $this->sut->find(42));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItHydrateDataboxWhenInCache()
|
||||||
|
{
|
||||||
|
$databox = $this->prophesize(\databox::class);
|
||||||
|
|
||||||
|
$this->appbox->get_data_from_cache(\appbox::CACHE_LIST_BASES)
|
||||||
|
->willReturn([42 => ['foo' => 'bar']]);
|
||||||
|
$this->repository->find(42)
|
||||||
|
->shouldNotBeCalled();
|
||||||
|
$this->hydrator->hydrateRow(42, ['foo' => 'bar'])
|
||||||
|
->willReturn($databox->reveal());
|
||||||
|
|
||||||
|
$this->assertSame($databox->reveal(), $this->sut->find(42));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItProperlySaveCacheOnFindAll()
|
||||||
|
{
|
||||||
|
$databox = $this->prophesize(\databox::class);
|
||||||
|
$databox->get_sbas_id()
|
||||||
|
->willReturn(42);
|
||||||
|
$databox->getAsRow()
|
||||||
|
->willReturn(['foo' => 'bar']);
|
||||||
|
|
||||||
|
$cache_data = [42 => ['foo' => 'bar']];
|
||||||
|
$databoxes = [42 => $databox->reveal()];
|
||||||
|
|
||||||
|
$this->appbox->get_data_from_cache(\appbox::CACHE_LIST_BASES)
|
||||||
|
->willThrow(new Exception());
|
||||||
|
$this->repository->findAll()
|
||||||
|
->willReturn($databoxes);
|
||||||
|
$this->appbox->set_data_to_cache($cache_data, \appbox::CACHE_LIST_BASES)
|
||||||
|
->shouldBeCalled();
|
||||||
|
|
||||||
|
$this->hydrator->hydrateRows(Argument::any())
|
||||||
|
->shouldNotBeCalled();
|
||||||
|
|
||||||
|
$this->assertSame($databoxes, $this->sut->findAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItFindsAllDeclaredDataboxesFromCache()
|
||||||
|
{
|
||||||
|
$databox = $this->prophesize(\databox::class);
|
||||||
|
|
||||||
|
$cache_data = [42 => ['foo' => 'bar']];
|
||||||
|
$databoxes = [42 => $databox->reveal()];
|
||||||
|
|
||||||
|
$this->appbox->get_data_from_cache(\appbox::CACHE_LIST_BASES)
|
||||||
|
->willReturn($cache_data);
|
||||||
|
$this->repository->findAll()
|
||||||
|
->shouldNotBeCalled();
|
||||||
|
$this->hydrator->hydrateRows($cache_data)
|
||||||
|
->willReturn($databoxes);
|
||||||
|
|
||||||
|
$this->assertSame($databoxes, $this->sut->findAll());
|
||||||
|
}
|
||||||
|
}
|
@@ -1,123 +0,0 @@
|
|||||||
<?php
|
|
||||||
/*
|
|
||||||
* This file is part of Phraseanet
|
|
||||||
*
|
|
||||||
* (c) 2005-2015 Alchemy
|
|
||||||
*
|
|
||||||
* For the full copyright and license information, please view the LICENSE
|
|
||||||
* file that was distributed with this source code.
|
|
||||||
*/
|
|
||||||
namespace Alchemy\Tests\Phrasea\Databox;
|
|
||||||
|
|
||||||
use Alchemy\Phrasea\Application;
|
|
||||||
use Alchemy\Phrasea\Databox\DataboxFactory;
|
|
||||||
use Alchemy\Phrasea\Databox\DataboxRepository;
|
|
||||||
use Alchemy\Phrasea\Databox\DataboxRepositoryInterface;
|
|
||||||
use Doctrine\DBAL\Driver\Connection;
|
|
||||||
use Doctrine\DBAL\Driver\Statement;
|
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
|
||||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
||||||
|
|
||||||
final class DataboxRepositoryTest extends \PHPUnit_Framework_TestCase
|
|
||||||
{
|
|
||||||
/** @var ObjectProphecy */
|
|
||||||
private $app;
|
|
||||||
/** @var ObjectProphecy */
|
|
||||||
private $appbox;
|
|
||||||
/** @var ObjectProphecy */
|
|
||||||
private $factory;
|
|
||||||
|
|
||||||
/** @var DataboxRepository */
|
|
||||||
private $sut;
|
|
||||||
|
|
||||||
protected function setUp()
|
|
||||||
{
|
|
||||||
$this->app = $this->prophesize(Application::class);
|
|
||||||
$this->appbox = $this->prophesize(\appbox::class);
|
|
||||||
$this->factory = $this->prophesize(DataboxFactory::class);
|
|
||||||
|
|
||||||
$this->sut = new DataboxRepository($this->app->reveal(), $this->appbox->reveal(), $this->factory->reveal());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItImplementsDataboxRepositoryInterface()
|
|
||||||
{
|
|
||||||
$this->assertInstanceOf(DataboxRepositoryInterface::class, $this->sut);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItFindsDataboxProperly()
|
|
||||||
{
|
|
||||||
$databox = $this->prophesize(\databox::class);
|
|
||||||
$this->factory->create($this->app->reveal(), 42)->willReturn($databox->reveal());
|
|
||||||
|
|
||||||
$this->assertSame($databox->reveal(), $this->sut->find(42));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItReturnsNullOnNonExistentDatabox()
|
|
||||||
{
|
|
||||||
$this->factory->create($this->app->reveal(), 42)->willThrow(new NotFoundHttpException());
|
|
||||||
|
|
||||||
$this->assertNull($this->sut->find(42));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItFindsAllDeclaredDataboxes()
|
|
||||||
{
|
|
||||||
$databox1 = $this->prophesize(\databox::class);
|
|
||||||
$databox1->get_sbas_id()
|
|
||||||
->willReturn(1);
|
|
||||||
$databox1_cache = ['sbas_id' => 1, 'foo' => 'bar'];
|
|
||||||
|
|
||||||
$databox2 = $this->prophesize(\databox::class);
|
|
||||||
$databox2->get_sbas_id()
|
|
||||||
->willReturn(2);
|
|
||||||
$databox2_cache = ['sbas_id' => 2, 'bar' => 'baz'];
|
|
||||||
|
|
||||||
$this->appbox->get_data_from_cache(\appbox::CACHE_LIST_BASES)
|
|
||||||
->willReturn([
|
|
||||||
$databox1_cache,
|
|
||||||
$databox2_cache,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->factory->create($this->app->reveal(), 1, $databox1_cache)
|
|
||||||
->willReturn($databox1->reveal());
|
|
||||||
|
|
||||||
$this->factory->create($this->app->reveal(), 2, $databox2_cache)
|
|
||||||
->willReturn($databox2->reveal());
|
|
||||||
|
|
||||||
$this->assertEquals([
|
|
||||||
1 => $databox1->reveal(),
|
|
||||||
2 => $databox2->reveal(),
|
|
||||||
], $this->sut->findAll());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItFindsAllDeclaredDataboxesWhenNoCacheIsPresent()
|
|
||||||
{
|
|
||||||
$this->appbox->get_data_from_cache(\appbox::CACHE_LIST_BASES)
|
|
||||||
->willReturn(false);
|
|
||||||
|
|
||||||
$connection = $this->prophesize(Connection::class);
|
|
||||||
$this->appbox->get_connection()
|
|
||||||
->willReturn($connection->reveal());
|
|
||||||
|
|
||||||
$statement = $this->prophesize(Statement::class);
|
|
||||||
$connection->prepare('SELECT sbas_id, ord, viewname, label_en, label_fr, label_de, label_nl FROM sbas')
|
|
||||||
->willReturn($statement->reveal());
|
|
||||||
|
|
||||||
$databox1 = $this->prophesize(\databox::class);
|
|
||||||
$databox1->get_sbas_id()
|
|
||||||
->willReturn(1);
|
|
||||||
$cache = [['sbas_id' => 1, 'foo' => 'bar']];
|
|
||||||
|
|
||||||
$statement->execute()->shouldBeCalled();
|
|
||||||
$statement->fetchAll(\PDO::FETCH_ASSOC)
|
|
||||||
->willReturn($cache);
|
|
||||||
$statement->closeCursor()->shouldBeCalled();
|
|
||||||
|
|
||||||
$this->appbox->set_data_to_cache($cache, \appbox::CACHE_LIST_BASES)
|
|
||||||
->shouldBeCalled();
|
|
||||||
|
|
||||||
$this->factory->create($this->app->reveal(), 1, $cache[0])
|
|
||||||
->willReturn($databox1->reveal());
|
|
||||||
|
|
||||||
$this->assertEquals([1 => $databox1->reveal()], $this->sut->findAll());
|
|
||||||
}
|
|
||||||
}
|
|
@@ -0,0 +1,102 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Phraseanet
|
||||||
|
*
|
||||||
|
* (c) 2005-2015 Alchemy
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
namespace Alchemy\Tests\Phrasea\Databox;
|
||||||
|
|
||||||
|
use Alchemy\Phrasea\Databox\DataboxHydrator;
|
||||||
|
use Alchemy\Phrasea\Databox\DataboxRepositoryInterface;
|
||||||
|
use Alchemy\Phrasea\Databox\DbalDataboxRepository;
|
||||||
|
use Doctrine\DBAL\Connection;
|
||||||
|
use Doctrine\DBAL\Driver\Statement;
|
||||||
|
use Prophecy\Argument;
|
||||||
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
|
|
||||||
|
final class DbalDataboxRepositoryTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/** @var ObjectProphecy */
|
||||||
|
private $connection;
|
||||||
|
/** @var ObjectProphecy */
|
||||||
|
private $hydrator;
|
||||||
|
/** @var DbalDataboxRepository */
|
||||||
|
private $sut;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->connection = $this->prophesize(Connection::class);
|
||||||
|
$this->hydrator = $this->prophesize(DataboxHydrator::class);
|
||||||
|
|
||||||
|
$this->sut = new DbalDataboxRepository($this->connection->reveal(), $this->hydrator->reveal());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItImplementsDataboxRepositoryInterface()
|
||||||
|
{
|
||||||
|
$this->assertInstanceOf(DataboxRepositoryInterface::class, $this->sut);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItFindsDataboxProperly()
|
||||||
|
{
|
||||||
|
$databox = $this->prophesize(\databox::class);
|
||||||
|
|
||||||
|
$statement = $this->prophesize(Statement::class);
|
||||||
|
$this->connection
|
||||||
|
->prepare('SELECT ord, viewname, label_en, label_fr, label_de, label_nl FROM sbas WHERE sbas_id = :id')
|
||||||
|
->willReturn($statement->reveal());
|
||||||
|
$statement->execute(['id' => 42])
|
||||||
|
->shouldBeCalled();
|
||||||
|
$statement->fetch(\PDO::FETCH_ASSOC)
|
||||||
|
->willReturn(['foo' => 'bar']);
|
||||||
|
$statement->closeCursor()
|
||||||
|
->shouldBeCalled();
|
||||||
|
|
||||||
|
$this->hydrator->hydrateRow(42, ['foo' => 'bar'])
|
||||||
|
->willReturn($databox->reveal());
|
||||||
|
|
||||||
|
$this->assertSame($databox->reveal(), $this->sut->find(42));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItReturnsNullOnNonExistentDatabox()
|
||||||
|
{
|
||||||
|
$statement = $this->prophesize(Statement::class);
|
||||||
|
$this->connection
|
||||||
|
->prepare('SELECT ord, viewname, label_en, label_fr, label_de, label_nl FROM sbas WHERE sbas_id = :id')
|
||||||
|
->willReturn($statement->reveal());
|
||||||
|
$statement->execute(['id' => 42])
|
||||||
|
->shouldBeCalled();
|
||||||
|
$statement->fetch(\PDO::FETCH_ASSOC)
|
||||||
|
->willReturn(false);
|
||||||
|
$statement->closeCursor()
|
||||||
|
->shouldBeCalled();
|
||||||
|
|
||||||
|
$this->hydrator->hydrateRow(42, Argument::any())
|
||||||
|
->shouldNotBeCalled();
|
||||||
|
|
||||||
|
$this->assertNull($this->sut->find(42));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItFindsAllDataboxes()
|
||||||
|
{
|
||||||
|
$databox = $this->prophesize(\databox::class);
|
||||||
|
|
||||||
|
$statement = $this->prophesize(Statement::class);
|
||||||
|
$this->connection
|
||||||
|
->prepare('SELECT sbas_id, ord, viewname, label_en, label_fr, label_de, label_nl FROM sbas')
|
||||||
|
->willReturn($statement->reveal());
|
||||||
|
$statement->execute()
|
||||||
|
->shouldBeCalled();
|
||||||
|
$statement->fetch(\PDO::FETCH_ASSOC)
|
||||||
|
->willReturn(['sbas_id' => 42, 'foo' => 'bar'], false);
|
||||||
|
$statement->closeCursor()
|
||||||
|
->shouldBeCalled();
|
||||||
|
|
||||||
|
$this->hydrator->hydrateRows([42 => ['foo' => 'bar']])
|
||||||
|
->willReturn([$databox->reveal()]);
|
||||||
|
|
||||||
|
$this->assertSame([$databox->reveal()], $this->sut->findAll());
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user