mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-23 18:03:17 +00:00
Fix BasketElementRepository findByRecordIds
This commit is contained in:
@@ -163,8 +163,9 @@ class BasketElementRepository extends EntityRepository
|
|||||||
public function findByRecords(array $records, $basketId = null)
|
public function findByRecords(array $records, $basketId = null)
|
||||||
{
|
{
|
||||||
$perDataboxLookup = [];
|
$perDataboxLookup = [];
|
||||||
|
|
||||||
foreach ($records as $record) {
|
foreach ($records as $record) {
|
||||||
if (!isset($record['databox_id']) || !isset($record['record_id'])) {
|
if (!isset($record['databox_id'], $record['record_id'])) {
|
||||||
throw new \LogicException('Each record should have a databox_id AND record_id key');
|
throw new \LogicException('Each record should have a databox_id AND record_id key');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,7 +178,7 @@ class BasketElementRepository extends EntityRepository
|
|||||||
$perDataboxLookup[$databoxId][] = $recordId;
|
$perDataboxLookup[$databoxId][] = $recordId;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($perDataboxLookup)) {
|
if (!$perDataboxLookup) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,8 +187,8 @@ class BasketElementRepository extends EntityRepository
|
|||||||
$parameters = new ArrayCollection();
|
$parameters = new ArrayCollection();
|
||||||
|
|
||||||
if ($basketId) {
|
if ($basketId) {
|
||||||
$builder->where('e.basket_id = :basket_id');
|
$builder->where('e.basket = :basket_id');
|
||||||
$parameters->add(new Parameter('basket_id', $basketId));
|
$parameters->add(new Parameter('basket_id', $basketId, \PDO::PARAM_INT));
|
||||||
}
|
}
|
||||||
|
|
||||||
$parameterGroup = 1;
|
$parameterGroup = 1;
|
||||||
|
|||||||
@@ -0,0 +1,119 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Phraseanet
|
||||||
|
*
|
||||||
|
* (c) 2005-2016 Alchemy
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Alchemy\Tests\Phrasea\Model\Repositories;
|
||||||
|
|
||||||
|
use Alchemy\Phrasea\Model\Entities\BasketElement;
|
||||||
|
use Alchemy\Phrasea\Model\Repositories\BasketElementRepository;
|
||||||
|
use Doctrine\DBAL\Connection;
|
||||||
|
use Doctrine\ORM\AbstractQuery;
|
||||||
|
use Doctrine\ORM\EntityManager;
|
||||||
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||||
|
use Doctrine\ORM\Query;
|
||||||
|
use Doctrine\ORM\Query\Expr;
|
||||||
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
use Prophecy\Argument;
|
||||||
|
|
||||||
|
class BasketElementRepositoryTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function testFindByRecordIdsWithBasketSucceed()
|
||||||
|
{
|
||||||
|
$result = [(object)['fake_object' => 'basket_element']];
|
||||||
|
|
||||||
|
$entityManager = $this->prophesize(EntityManager::class);
|
||||||
|
|
||||||
|
$builder = new QueryBuilder($entityManager->reveal());
|
||||||
|
$entityManager->createQueryBuilder()->willReturn($builder);
|
||||||
|
|
||||||
|
$entityManager->getExpressionBuilder()->willReturn(new Expr());
|
||||||
|
|
||||||
|
$query = $this->prophesize(StubQuery::class);
|
||||||
|
$query->setDql(Argument::any())->will(function ($arguments) {
|
||||||
|
$this->getDql()->willReturn($arguments[0]);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
});
|
||||||
|
$query->setParameters(Argument::any())->will(function ($arguments) {
|
||||||
|
$this->getParameters()->willReturn($arguments[0]);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
});
|
||||||
|
$query->setFirstResult(Argument::any())->will(function ($arguments) {
|
||||||
|
$this->getFirstResult()->willReturn($arguments[0]);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
});
|
||||||
|
$query->setMaxResults(Argument::any())->will(function ($arguments) {
|
||||||
|
$this->getMaxResults()->willReturn($arguments[0]);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
});
|
||||||
|
$query->getResult()->willReturn($result);
|
||||||
|
|
||||||
|
|
||||||
|
$entityManager->createQuery(Argument::any())->will(function ($arguments) use ($query) {
|
||||||
|
$query->reveal()->setDql($arguments[0]);
|
||||||
|
|
||||||
|
return $query->reveal();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
$sut = new BasketElementRepository($entityManager->reveal(), new ClassMetadata(BasketElement::class));
|
||||||
|
|
||||||
|
$records = [
|
||||||
|
['databox_id' => 1, 'record_id' => 42],
|
||||||
|
];
|
||||||
|
$basketId = 2;
|
||||||
|
|
||||||
|
$this->assertSame($result, $sut->findByRecords($records, $basketId));
|
||||||
|
$this->assertSame(sprintf('SELECT e FROM %s e WHERE e.basket = :basket_id AND (e.sbas_id = :databoxId1 AND e.record_id IN (:recordIds1))', BasketElement::class), $query->reveal()->getDql());
|
||||||
|
|
||||||
|
$parameters = $query->reveal()->getParameters();
|
||||||
|
$this->assertCount(3, $parameters);
|
||||||
|
$this->assertEquals(new Query\Parameter('basket_id', 2, \PDO::PARAM_INT), $parameters[0]);
|
||||||
|
$this->assertEquals(new Query\Parameter('databoxId1', 1, \PDO::PARAM_INT), $parameters[1]);
|
||||||
|
$this->assertEquals(new Query\Parameter('recordIds1', [42], Connection::PARAM_INT_ARRAY), $parameters[2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class StubQuery extends AbstractQuery
|
||||||
|
{
|
||||||
|
public function setDql($dql = '')
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDql()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setFirstResult($firstResult)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFirstResult()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setMaxResults($maxResult)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMaxResults()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSQL()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function _doExecute()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user