Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Lexer query error #519

Merged
merged 2 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Entity/CategoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ public function getPager(array $criteria, $page, $limit = 10, array $sort = [])

public function getBySlug(string $slug, $context = null, ?bool $enabled = true): ?CategoryInterface
{
$queryBuilder = $this->getObjectManager()->createQueryBuilder()
$queryBuilder = $this->getRepository()
->createQueryBuilder('c')
->select('c')
->andWhere('c.slug = :slug')->setParameter('slug', $slug);

Expand Down
6 changes: 4 additions & 2 deletions src/Entity/CollectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function getPager(array $criteria, $page, $limit = 10, array $sort = [])

public function getBySlug(string $slug, $context = null, ?bool $enabled = true): ?CollectionInterface
{
$queryBuilder = $this->getObjectManager()->createQueryBuilder()
$queryBuilder = $this->getRepository()
->createQueryBuilder('c')
->select('c')
->andWhere('c.slug = :slug')->setParameter('slug', $slug);

Expand All @@ -63,7 +64,8 @@ public function getBySlug(string $slug, $context = null, ?bool $enabled = true):

public function getByContext($context, ?bool $enabled = true): array
{
$queryBuilder = $this->getObjectManager()->createQueryBuilder()
$queryBuilder = $this->getRepository()
->createQueryBuilder('c')
->select('c')
->andWhere('c.context = :context')->setParameter('context', $context);

Expand Down
6 changes: 4 additions & 2 deletions src/Entity/TagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function getPager(array $criteria, $page, $limit = 10, array $sort = [])

public function getBySlug(string $slug, $context = null, ?bool $enabled = true): ?TagInterface
{
$queryBuilder = $this->getObjectManager()->createQueryBuilder()
$queryBuilder = $this->getRepository()
->createQueryBuilder('t')
->select('t')
->andWhere('t.slug = :slug')->setParameter('slug', $slug);

Expand All @@ -63,7 +64,8 @@ public function getBySlug(string $slug, $context = null, ?bool $enabled = true):

public function getByContext($context, ?bool $enabled = true): array
{
$queryBuilder = $this->getObjectManager()->createQueryBuilder()
$queryBuilder = $this->getRepository()
->createQueryBuilder('t')
->select('t')
->andWhere('t.context = :context')->setParameter('context', $context);

Expand Down
19 changes: 19 additions & 0 deletions tests/Entity/CategoryManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,25 @@ public function testGetRootCategoriesSplitByContexts(): void
$this->assertContains($categoryBar, $categories[$contextBar->getId()]);
}

public function testGetBySlug(): void
{
$self = $this;
$this
->getCategoryManager(static function ($qb) use ($self): void {
core23 marked this conversation as resolved.
Show resolved Hide resolved
$qb->expects($self->exactly(3))->method('andWhere')->withConsecutive(
[$self->equalTo('c.slug = :slug')],
[$self->equalTo('c.context = :context')],
[$self->equalTo('c.enabled = :enabled')]
)->willReturn($qb);
$qb->expects($self->exactly(3))->method('setParameter')->withConsecutive(
[$self->equalTo('slug'), $self->equalTo('theslug')],
[$self->equalTo('context'), $self->equalTo('contextA')],
[$self->equalTo('enabled'), $self->equalTo(false)]
)->willReturn($qb);
})
->getBySlug('theslug', 'contextA', false);
}

protected function getCategoryManager($qbCallback, $createQueryResult = null)
{
$em = $this->createEntityManagerMock($qbCallback, []);
Expand Down
46 changes: 45 additions & 1 deletion tests/Entity/CollectionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\ClassificationBundle\Tests\Entity;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\AbstractQuery;
use PHPUnit\Framework\TestCase;
use Sonata\ClassificationBundle\Entity\BaseCollection;
use Sonata\ClassificationBundle\Entity\CollectionManager;
Expand Down Expand Up @@ -63,10 +64,53 @@ public function testGetPagerWithDisabledCollections(): void
], 1);
}

protected function getCollectionManager($qbCallback)
public function testGetBySlug(): void
{
$self = $this;
$this
->getCollectionManager(static function ($qb) use ($self): void {
core23 marked this conversation as resolved.
Show resolved Hide resolved
$qb->expects($self->exactly(3))->method('andWhere')->withConsecutive(
[$self->equalTo('c.slug = :slug')],
[$self->equalTo('c.context = :context')],
[$self->equalTo('c.enabled = :enabled')]
)->willReturn($qb);
$qb->expects($self->exactly(3))->method('setParameter')->withConsecutive(
[$self->equalTo('slug'), $self->equalTo('theslug')],
[$self->equalTo('context'), $self->equalTo('contextA')],
[$self->equalTo('enabled'), $self->equalTo(false)]
)->willReturn($qb);
})
->getBySlug('theslug', 'contextA', false);
}

public function testGetByContext(): void
{
$self = $this;
$this
->getCollectionManager(static function ($qb) use ($self): void {
$qb->expects($self->exactly(2))->method('andWhere')->withConsecutive(
[$self->equalTo('c.context = :context')],
[$self->equalTo('c.enabled = :enabled')]
)->willReturn($qb);
$qb->expects($self->exactly(2))->method('setParameter')->withConsecutive(
[$self->equalTo('context'), $self->equalTo('contextA')],
[$self->equalTo('enabled'), $self->equalTo(false)]
)->willReturn($qb);
}, [])
->getByContext('contextA', false);
}

protected function getCollectionManager($qbCallback, $createQueryResult = null)
core23 marked this conversation as resolved.
Show resolved Hide resolved
{
$em = $this->createEntityManagerMock($qbCallback, []);

if (null !== $createQueryResult) {
$query = $this->createMock(AbstractQuery::class);
$query->expects($this->once())->method('execute')->willReturn($createQueryResult);
$query->expects($this->any())->method('setParameter')->willReturn($query);
core23 marked this conversation as resolved.
Show resolved Hide resolved
$em->expects($this->once())->method('createQuery')->willReturn($query);
}

$registry = $this->getMockForAbstractClass(ManagerRegistry::class);
$registry->expects($this->any())->method('getManagerForClass')->willReturn($em);

Expand Down
46 changes: 45 additions & 1 deletion tests/Entity/TagManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\ClassificationBundle\Tests\Entity;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\AbstractQuery;
use PHPUnit\Framework\TestCase;
use Sonata\ClassificationBundle\Entity\BaseTag;
use Sonata\ClassificationBundle\Entity\TagManager;
Expand Down Expand Up @@ -63,10 +64,53 @@ public function testGetPagerWithDisabledTags(): void
], 1);
}

protected function getTagManager($qbCallback)
public function testGetBySlug(): void
{
$self = $this;
$this
->getTagManager(static function ($qb) use ($self): void {
core23 marked this conversation as resolved.
Show resolved Hide resolved
$qb->expects($self->exactly(3))->method('andWhere')->withConsecutive(
[$self->equalTo('t.slug = :slug')],
[$self->equalTo('t.context = :context')],
[$self->equalTo('t.enabled = :enabled')]
)->willReturn($qb);
$qb->expects($self->exactly(3))->method('setParameter')->withConsecutive(
[$self->equalTo('slug'), $self->equalTo('theslug')],
[$self->equalTo('context'), $self->equalTo('contextA')],
[$self->equalTo('enabled'), $self->equalTo(false)]
)->willReturn($qb);
})
->getBySlug('theslug', 'contextA', false);
}

public function testGetByContext(): void
{
$self = $this;
$this
->getTagManager(static function ($qb) use ($self): void {
core23 marked this conversation as resolved.
Show resolved Hide resolved
$qb->expects($self->exactly(2))->method('andWhere')->withConsecutive(
[$self->equalTo('t.context = :context')],
[$self->equalTo('t.enabled = :enabled')]
)->willReturn($qb);
$qb->expects($self->exactly(2))->method('setParameter')->withConsecutive(
[$self->equalTo('context'), $self->equalTo('contextA')],
[$self->equalTo('enabled'), $self->equalTo(false)]
)->willReturn($qb);
}, [])
->getByContext('contextA', false);
}

protected function getTagManager($qbCallback, $createQueryResult = null)
core23 marked this conversation as resolved.
Show resolved Hide resolved
{
$em = $this->createEntityManagerMock($qbCallback, []);

if (null !== $createQueryResult) {
$query = $this->createMock(AbstractQuery::class);
$query->expects($this->once())->method('execute')->willReturn($createQueryResult);
$query->expects($this->any())->method('setParameter')->willReturn($query);
core23 marked this conversation as resolved.
Show resolved Hide resolved
$em->expects($this->once())->method('createQuery')->willReturn($query);
}

$registry = $this->getMockForAbstractClass(ManagerRegistry::class);
$registry->expects($this->any())->method('getManagerForClass')->willReturn($em);

Expand Down