Skip to content

Commit

Permalink
Merge 3.x into master
Browse files Browse the repository at this point in the history
  • Loading branch information
SonataCI committed Mar 25, 2020
2 parents 025cb9d + 76570e9 commit 25cbcd3
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [3.11.1](https://github.com/sonata-project/SonataClassificationBundle/compare/3.11.0...3.11.1) - 2020-03-24
### Fixed
- Fix Lexer query error in managers

## [3.11.0](https://github.com/sonata-project/SonataClassificationBundle/compare/3.10.1...3.11.0) - 2020-03-18
### Added
- Add public aliases to all manager interface
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -26,7 +26,7 @@
"sonata-project/admin-bundle": "^3.31",
"sonata-project/core-bundle": "^3.14",
"sonata-project/datagrid-bundle": "^2.3",
"sonata-project/doctrine-extensions": "^1.5.1",
"sonata-project/doctrine-extensions": "^1.6.0",
"sonata-project/doctrine-orm-admin-bundle": "^3.4",
"sonata-project/easy-extends-bundle": "^2.5",
"symfony/config": "^3.4 || ^4.2",
Expand Down
3 changes: 2 additions & 1 deletion src/Entity/CategoryManager.php
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
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
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
44 changes: 32 additions & 12 deletions tests/Entity/CategoryManagerTest.php
Expand Up @@ -15,6 +15,7 @@

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\AbstractQuery;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\ClassificationBundle\Entity\BaseCategory;
use Sonata\ClassificationBundle\Entity\CategoryManager;
Expand All @@ -29,7 +30,7 @@ public function testGetPager(): void
{
$self = $this;
$this
->getCategoryManager(static function ($qb) use ($self): void {
->getCategoryManager(static function (MockObject $qb) use ($self): void {
$qb->expects($self->once())->method('getRootAliases')->will($self->returnValue([]));
$qb->expects($self->exactly(1))->method('andWhere')->withConsecutive(
[$self->equalTo('c.context = :context')]
Expand All @@ -43,7 +44,7 @@ public function testGetPagerWithEnabledCategories(): void
{
$self = $this;
$this
->getCategoryManager(static function ($qb) use ($self): void {
->getCategoryManager(static function (MockObject $qb) use ($self): void {
$qb->expects($self->once())->method('getRootAliases')->will($self->returnValue([]));
$qb->expects($self->exactly(2))->method('andWhere')->withConsecutive(
[$self->equalTo('c.context = :context')],
Expand All @@ -61,7 +62,7 @@ public function testGetPagerWithDisabledCategories(): void
{
$self = $this;
$this
->getCategoryManager(static function ($qb) use ($self): void {
->getCategoryManager(static function (MockObject $qb) use ($self): void {
$qb->expects($self->once())->method('getRootAliases')->will($self->returnValue([]));
$qb->expects($self->exactly(2))->method('andWhere')->withConsecutive(
[$self->equalTo('c.context = :context')],
Expand Down Expand Up @@ -101,7 +102,7 @@ public function testGetCategoriesWithMultipleRootsInContext(): void

$categories = [$categoryFoo, $categoryBar];

$categoryManager = $this->getCategoryManager(static function ($qb): void {
$categoryManager = $this->getCategoryManager(static function (MockObject $qb): void {
}, $categories);

$this->assertSame($categoryManager->getCategories($context), $categories);
Expand Down Expand Up @@ -131,7 +132,7 @@ public function testGetRootCategoryWithChildren(): void
$categoryBar->setParent($categoryFoo);
$categoryBar->setEnabled(true);

$categoryManager = $this->getCategoryManager(static function ($qb): void {
$categoryManager = $this->getCategoryManager(static function (MockObject $qb): void {
}, [$categoryFoo, $categoryBar]);

$categoryFoo = $categoryManager->getRootCategoryWithChildren($categoryFoo);
Expand All @@ -154,7 +155,7 @@ public function testGetRootCategory(): void
$categoryFoo->setParent(null);
$categoryFoo->setEnabled(true);

$categoryManager = $this->getCategoryManager(static function ($qb): void {
$categoryManager = $this->getCategoryManager(static function (MockObject $qb): void {
}, [$categoryFoo]);

$categoryBar = $categoryManager->getRootCategory($context);
Expand Down Expand Up @@ -185,7 +186,7 @@ public function testGetRootCategoriesForContext(): void
$categoryBar->setParent($categoryFoo);
$categoryBar->setEnabled(true);

$categoryManager = $this->getCategoryManager(static function ($qb): void {
$categoryManager = $this->getCategoryManager(static function (MockObject $qb): void {
}, [$categoryFoo, $categoryBar]);

$categories = $categoryManager->getRootCategoriesForContext($context);
Expand Down Expand Up @@ -223,7 +224,7 @@ public function testGetRootCategories(): void
$categoryBar->setParent(null);
$categoryBar->setEnabled(true);

$categoryManager = $this->getCategoryManager(static function ($qb): void {
$categoryManager = $this->getCategoryManager(static function (MockObject $qb): void {
}, [$categoryFoo, $categoryBar]);

$categories = $categoryManager->getRootCategories(false);
Expand Down Expand Up @@ -263,7 +264,7 @@ public function testGetRootCategoriesSplitByContexts(): void
$categoryBar->setParent(null);
$categoryBar->setEnabled(true);

$categoryManager = $this->getCategoryManager(static function ($qb): void {
$categoryManager = $this->getCategoryManager(static function (MockObject $qb): void {
}, [$categoryFoo, $categoryBar]);

$categories = $categoryManager->getRootCategoriesSplitByContexts(false);
Expand All @@ -273,19 +274,38 @@ public function testGetRootCategoriesSplitByContexts(): void
$this->assertContains($categoryBar, $categories[$contextBar->getId()]);
}

protected function getCategoryManager($qbCallback, $createQueryResult = null)
public function testGetBySlug(): void
{
$self = $this;
$this
->getCategoryManager(static function (MockObject $qb) use ($self): void {
$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);
}

private function getCategoryManager($qbCallback, $createQueryResult = null): CategoryManager
{
$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);
$query->method('setParameter')->willReturn($query);
$em->expects($this->once())->method('createQuery')->willReturn($query);
}

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

$contextManager = $this->createMock(ContextManagerInterface::class);

Expand Down
47 changes: 42 additions & 5 deletions tests/Entity/CollectionManagerTest.php
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\ClassificationBundle\Tests\Entity;

use Doctrine\Common\Persistence\ManagerRegistry;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\ClassificationBundle\Entity\BaseCollection;
use Sonata\ClassificationBundle\Entity\CollectionManager;
Expand All @@ -27,7 +28,7 @@ public function testGetPager(): void
{
$self = $this;
$this
->getCollectionManager(static function ($qb) use ($self): void {
->getCollectionManager(static function (MockObject $qb) use ($self): void {
$qb->expects($self->once())->method('getRootAliases')->will($self->returnValue([]));
$qb->expects($self->never())->method('andWhere');
$qb->expects($self->once())->method('setParameters')->with([]);
Expand All @@ -39,7 +40,7 @@ public function testGetPagerWithEnabledCollections(): void
{
$self = $this;
$this
->getCollectionManager(static function ($qb) use ($self): void {
->getCollectionManager(static function (MockObject $qb) use ($self): void {
$qb->expects($self->once())->method('getRootAliases')->will($self->returnValue([]));
$qb->expects($self->once())->method('andWhere')->with($self->equalTo('c.enabled = :enabled'));
$qb->expects($self->once())->method('setParameters')->with(['enabled' => true]);
Expand All @@ -53,7 +54,7 @@ public function testGetPagerWithDisabledCollections(): void
{
$self = $this;
$this
->getCollectionManager(static function ($qb) use ($self): void {
->getCollectionManager(static function (MockObject $qb) use ($self): void {
$qb->expects($self->once())->method('getRootAliases')->will($self->returnValue([]));
$qb->expects($self->once())->method('andWhere')->with($self->equalTo('c.enabled = :enabled'));
$qb->expects($self->once())->method('setParameters')->with(['enabled' => false]);
Expand All @@ -63,12 +64,48 @@ public function testGetPagerWithDisabledCollections(): void
], 1);
}

protected function getCollectionManager($qbCallback)
public function testGetBySlug(): void
{
$self = $this;
$this
->getCollectionManager(static function (MockObject $qb) use ($self): void {
$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 (MockObject $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);
}

private function getCollectionManager($qbCallback): CollectionManager
{
$em = $this->createEntityManagerMock($qbCallback, []);

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

return new CollectionManager(BaseCollection::class, $registry);
}
Expand Down
11 changes: 6 additions & 5 deletions tests/Entity/ContextManagerTest.php
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\ClassificationBundle\Tests\Entity;

use Doctrine\Common\Persistence\ManagerRegistry;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\ClassificationBundle\Entity\BaseContext;
use Sonata\ClassificationBundle\Entity\ContextManager;
Expand All @@ -27,7 +28,7 @@ public function testGetPager(): void
{
$self = $this;
$this
->getContextManager(static function ($qb) use ($self): void {
->getContextManager(static function (MockObject $qb) use ($self): void {
$qb->expects($self->once())->method('getRootAliases')->will($self->returnValue([]));
$qb->expects($self->never())->method('andWhere');
$qb->expects($self->once())->method('setParameters')->with([]);
Expand All @@ -39,7 +40,7 @@ public function testGetPagerWithEnabledContexts(): void
{
$self = $this;
$this
->getContextManager(static function ($qb) use ($self): void {
->getContextManager(static function (MockObject $qb) use ($self): void {
$qb->expects($self->once())->method('getRootAliases')->will($self->returnValue([]));
$qb->expects($self->once())->method('andWhere')->with($self->equalTo('c.enabled = :enabled'));
$qb->expects($self->once())->method('setParameters')->with(['enabled' => true]);
Expand All @@ -53,7 +54,7 @@ public function testGetPagerWithDisabledContexts(): void
{
$self = $this;
$this
->getContextManager(static function ($qb) use ($self): void {
->getContextManager(static function (MockObject $qb) use ($self): void {
$qb->expects($self->once())->method('getRootAliases')->will($self->returnValue([]));
$qb->expects($self->once())->method('andWhere')->with($self->equalTo('c.enabled = :enabled'));
$qb->expects($self->once())->method('setParameters')->with(['enabled' => false]);
Expand All @@ -63,12 +64,12 @@ public function testGetPagerWithDisabledContexts(): void
], 1);
}

protected function getContextManager($qbCallback)
private function getContextManager($qbCallback): ContextManager
{
$em = $this->createEntityManagerMock($qbCallback, []);

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

return new ContextManager(BaseContext::class, $registry);
}
Expand Down

0 comments on commit 25cbcd3

Please sign in to comment.