-
-
Notifications
You must be signed in to change notification settings - Fork 847
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: complete doctrine bridge tests (#676)
- Loading branch information
Showing
3 changed files
with
279 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
tests/Bridge/Doctrine/EventListener/WriteListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ApiPlatform\Core\Tests\Doctrine; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\EventListener\WriteListener; | ||
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy; | ||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
use Doctrine\Common\Persistence\ObjectManager; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
|
||
/** | ||
* @author Amrouche Hamza <hamza.simperfit@gmail.com> | ||
*/ | ||
class WriteListenerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testOnKernelViewWithControllerResultAndPostMethod() | ||
{ | ||
$dummy = new Dummy(); | ||
$dummy->setName('Dummyrino'); | ||
|
||
$objectManagerProphecy = $this->prophesize(ObjectManager::class); | ||
$objectManagerProphecy->persist($dummy)->shouldBeCalled(); | ||
$objectManagerProphecy->flush()->shouldBeCalled(); | ||
$managerRegistryProphecy = $this->prophesize(ManagerRegistry::class); | ||
$managerRegistryProphecy->getManagerForClass('Dummy')->willReturn($objectManagerProphecy->reveal()); | ||
|
||
$writeListener = new WriteListener($managerRegistryProphecy->reveal()); | ||
$httpKernelProphecy = $this->prophesize(HttpKernelInterface::class); | ||
$request = new Request(); | ||
$request->setMethod(Request::METHOD_POST); | ||
$request->attributes->set('_api_resource_class', 'Dummy'); | ||
$event = new GetResponseForControllerResultEvent($httpKernelProphecy->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, $dummy); | ||
|
||
$this->assertNull($writeListener->onKernelView($event)); | ||
$this->assertNotEquals($dummy, $writeListener->onKernelView($event)); | ||
} | ||
|
||
public function testOnKernelViewWithControllerResultAndDeleteMethod() | ||
{ | ||
$dummy = new Dummy(); | ||
$dummy->setName('Dummyrino'); | ||
|
||
$objectManagerProphecy = $this->prophesize(ObjectManager::class); | ||
$objectManagerProphecy->remove($dummy)->shouldBeCalled(); | ||
$objectManagerProphecy->flush()->shouldBeCalled(); | ||
$managerRegistryProphecy = $this->prophesize(ManagerRegistry::class); | ||
$managerRegistryProphecy->getManagerForClass('Dummy')->willReturn($objectManagerProphecy->reveal()); | ||
|
||
$writeListener = new WriteListener($managerRegistryProphecy->reveal()); | ||
$request = new Request(); | ||
$request->setMethod(Request::METHOD_DELETE); | ||
$request->attributes->set('_api_resource_class', 'Dummy'); | ||
$event = $this->prophesize(GetResponseForControllerResultEvent::class); | ||
$event->setControllerResult(null)->shouldBeCalled(); | ||
$event->getRequest()->willReturn($request); | ||
$event->getControllerResult()->willReturn($dummy); | ||
$this->assertNull($writeListener->onKernelView($event->reveal())); | ||
$this->assertNotEquals($dummy, $writeListener->onKernelView($event->reveal())); | ||
} | ||
|
||
public function testOnKernelViewWithSafeMethod() | ||
{ | ||
$dummy = new Dummy(); | ||
$dummy->setName('Dummyrino'); | ||
|
||
$managerRegistryProphecy = $this->prophesize(ManagerRegistry::class); | ||
|
||
$writeListener = new WriteListener($managerRegistryProphecy->reveal()); | ||
$httpKernelProphecy = $this->prophesize(HttpKernelInterface::class); | ||
$request = new Request(); | ||
$request->setMethod(Request::METHOD_HEAD); | ||
$event = new GetResponseForControllerResultEvent($httpKernelProphecy->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, $dummy); | ||
|
||
$this->assertNull($writeListener->onKernelView($event)); | ||
$this->assertNotEquals($dummy, $writeListener->onKernelView($event)); | ||
} | ||
|
||
public function testOnKernelViewWithNoResourceClass() | ||
{ | ||
$dummy = new Dummy(); | ||
$dummy->setName('Dummyrino'); | ||
|
||
$managerRegistryProphecy = $this->prophesize(ManagerRegistry::class); | ||
|
||
$writeListener = new WriteListener($managerRegistryProphecy->reveal()); | ||
$httpKernelProphecy = $this->prophesize(HttpKernelInterface::class); | ||
$request = new Request(); | ||
$request->setMethod(Request::METHOD_POST); | ||
$event = new GetResponseForControllerResultEvent($httpKernelProphecy->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, $dummy); | ||
|
||
$this->assertNull($writeListener->onKernelView($event)); | ||
$this->assertNotEquals($dummy, $writeListener->onKernelView($event)); | ||
} | ||
|
||
public function testOnKernelViewWithNoManager() | ||
{ | ||
$dummy = new Dummy(); | ||
$dummy->setName('Dummyrino'); | ||
|
||
$managerRegistryProphecy = $this->prophesize(ManagerRegistry::class); | ||
$managerRegistryProphecy->getManagerForClass('Dummy')->willReturn(null); | ||
|
||
$writeListener = new WriteListener($managerRegistryProphecy->reveal()); | ||
$httpKernelProphecy = $this->prophesize(HttpKernelInterface::class); | ||
$request = new Request(); | ||
$request->setMethod(Request::METHOD_DELETE); | ||
$request->attributes->set('_api_resource_class', 'Dummy'); | ||
$event = new GetResponseForControllerResultEvent($httpKernelProphecy->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, $dummy); | ||
|
||
$this->assertNull($writeListener->onKernelView($event)); | ||
$this->assertNotEquals($dummy, $writeListener->onKernelView($event)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ApiPlatform\Core\Tests\Doctrine\Util; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryChecker; | ||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
use Doctrine\Common\Persistence\ObjectManager; | ||
use Doctrine\ORM\Mapping\ClassMetadata; | ||
use Doctrine\ORM\Query\Expr\Join; | ||
use Doctrine\ORM\Query\Expr\OrderBy; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
class QueryCheckerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testHasHavingClauseWithHavingClause() | ||
{ | ||
$queryBuilder = $this->prophesize(QueryBuilder::class); | ||
$queryBuilder->getDQLPart('having')->willReturn(['having' => 'toto']); | ||
$this->assertTrue(QueryChecker::hasHavingClause($queryBuilder->reveal())); | ||
} | ||
|
||
public function testHasHavingClauseWithEmptyHavingClause() | ||
{ | ||
$queryBuilder = $this->prophesize(QueryBuilder::class); | ||
$queryBuilder->getDQLPart('having')->willReturn([]); | ||
$this->assertFalse(QueryChecker::hasHavingClause($queryBuilder->reveal())); | ||
} | ||
|
||
public function testHasMaxResult() | ||
{ | ||
$queryBuilder = $this->prophesize(QueryBuilder::class); | ||
$queryBuilder->getMaxResults()->willReturn(10); | ||
$this->assertTrue(QueryChecker::hasMaxResults($queryBuilder->reveal())); | ||
} | ||
|
||
public function testHasMaxResultWithNoMaxResult() | ||
{ | ||
$queryBuilder = $this->prophesize(QueryBuilder::class); | ||
$queryBuilder->getMaxResults()->willReturn(null); | ||
$this->assertFalse(QueryChecker::hasMaxResults($queryBuilder->reveal())); | ||
} | ||
|
||
public function testHasRootEntityWithCompositeIdentifier() | ||
{ | ||
$queryBuilder = $this->prophesize(QueryBuilder::class); | ||
$queryBuilder->getRootEntities()->willReturn(['Dummy']); | ||
$queryBuilder->getRootAliases()->willReturn(['d']); | ||
$classMetadata = new ClassMetadata('Dummy'); | ||
$classMetadata->containsForeignIdentifier = true; | ||
$objectManager = $this->prophesize(ObjectManager::class); | ||
$objectManager->getClassMetadata('Dummy')->willReturn($classMetadata); | ||
$managerRegistry = $this->prophesize(ManagerRegistry::class); | ||
$managerRegistry->getManagerForClass('Dummy')->willReturn($objectManager->reveal()); | ||
$this->assertTrue(QueryChecker::hasRootEntityWithCompositeIdentifier($queryBuilder->reveal(), $managerRegistry->reveal())); | ||
} | ||
|
||
public function testHasRootEntityWithNoCompositeIdentifier() | ||
{ | ||
$queryBuilder = $this->prophesize(QueryBuilder::class); | ||
$queryBuilder->getRootEntities()->willReturn(['Dummy']); | ||
$queryBuilder->getRootAliases()->willReturn(['d']); | ||
$classMetadata = new ClassMetadata('Dummy'); | ||
$classMetadata->containsForeignIdentifier = false; | ||
$objectManager = $this->prophesize(ObjectManager::class); | ||
$objectManager->getClassMetadata('Dummy')->willReturn($classMetadata); | ||
$managerRegistry = $this->prophesize(ManagerRegistry::class); | ||
$managerRegistry->getManagerForClass('Dummy')->willReturn($objectManager->reveal()); | ||
$this->assertFalse(QueryChecker::hasRootEntityWithCompositeIdentifier($queryBuilder->reveal(), $managerRegistry->reveal())); | ||
} | ||
|
||
public function testHasRootEntityWithForeignKeyIdentifier() | ||
{ | ||
$queryBuilder = $this->prophesize(QueryBuilder::class); | ||
$queryBuilder->getRootEntities()->willReturn(['Dummy']); | ||
$queryBuilder->getRootAliases()->willReturn(['d']); | ||
$classMetadata = new ClassMetadata('Dummy'); | ||
$classMetadata->setIdentifier(['id', 'name']); | ||
$objectManager = $this->prophesize(ObjectManager::class); | ||
$objectManager->getClassMetadata('Dummy')->willReturn($classMetadata); | ||
$managerRegistry = $this->prophesize(ManagerRegistry::class); | ||
$managerRegistry->getManagerForClass('Dummy')->willReturn($objectManager->reveal()); | ||
$this->assertTrue(QueryChecker::hasRootEntityWithForeignKeyIdentifier($queryBuilder->reveal(), $managerRegistry->reveal())); | ||
} | ||
|
||
public function testHasRootEntityWithNoForeignKeyIdentifier() | ||
{ | ||
$queryBuilder = $this->prophesize(QueryBuilder::class); | ||
$queryBuilder->getRootEntities()->willReturn(['Dummy']); | ||
$queryBuilder->getRootAliases()->willReturn(['d']); | ||
$classMetadata = new ClassMetadata('Dummy'); | ||
$objectManager = $this->prophesize(ObjectManager::class); | ||
$objectManager->getClassMetadata('Dummy')->willReturn($classMetadata); | ||
$managerRegistry = $this->prophesize(ManagerRegistry::class); | ||
$managerRegistry->getManagerForClass('Dummy')->willReturn($objectManager->reveal()); | ||
$this->assertFalse(QueryChecker::hasRootEntityWithForeignKeyIdentifier($queryBuilder->reveal(), $managerRegistry->reveal())); | ||
} | ||
|
||
public function testHasOrderByOnToManyJoinWithoutJoin() | ||
{ | ||
$queryBuilder = $this->prophesize(QueryBuilder::class); | ||
$queryBuilder->getRootEntities()->willReturn(['Dummy']); | ||
$queryBuilder->getRootAliases()->willReturn(['d']); | ||
$queryBuilder->getDQLPart('join')->willReturn([]); | ||
$queryBuilder->getDQLPart('orderBy')->willReturn(['name' => new OrderBy('name', 'asc')]); | ||
$classMetadata = $this->prophesize(ClassMetadata::class); | ||
$objectManager = $this->prophesize(ObjectManager::class); | ||
$objectManager->getClassMetadata('Dummy')->willReturn($classMetadata->reveal()); | ||
$managerRegistry = $this->prophesize(ManagerRegistry::class); | ||
$managerRegistry->getManagerForClass('Dummy')->willReturn($objectManager->reveal()); | ||
|
||
$this->assertFalse(QueryChecker::hasOrderByOnToManyJoin($queryBuilder->reveal(), $managerRegistry->reveal())); | ||
} | ||
|
||
public function testHasOrderByOnToManyJoinWithoutOrderBy() | ||
{ | ||
$queryBuilder = $this->prophesize(QueryBuilder::class); | ||
$queryBuilder->getRootEntities()->willReturn(['Dummy']); | ||
$queryBuilder->getRootAliases()->willReturn(['d']); | ||
$queryBuilder->getDQLPart('join')->willReturn(['a_1' => new Join('INNER_JOIN', 'relatedDummy', 'a_1', null, 'a_1.name = r.name')]); | ||
$queryBuilder->getDQLPart('orderBy')->willReturn([]); | ||
$classMetadata = $this->prophesize(ClassMetadata::class); | ||
$objectManager = $this->prophesize(ObjectManager::class); | ||
$objectManager->getClassMetadata('Dummy')->willReturn($classMetadata->reveal()); | ||
$managerRegistry = $this->prophesize(ManagerRegistry::class); | ||
$managerRegistry->getManagerForClass('Dummy')->willReturn($objectManager->reveal()); | ||
|
||
$this->assertFalse(QueryChecker::hasOrderByOnToManyJoin($queryBuilder->reveal(), $managerRegistry->reveal())); | ||
} | ||
|
||
public function testHasOrderByOnToManyJoinWithoutJoinAndWithoutOrderBy() | ||
{ | ||
$queryBuilder = $this->prophesize(QueryBuilder::class); | ||
$queryBuilder->getRootEntities()->willReturn(['Dummy']); | ||
$queryBuilder->getRootAliases()->willReturn(['d']); | ||
$queryBuilder->getDQLPart('join')->willReturn([]); | ||
$queryBuilder->getDQLPart('orderBy')->willReturn([]); | ||
$classMetadata = $this->prophesize(ClassMetadata::class); | ||
$objectManager = $this->prophesize(ObjectManager::class); | ||
$objectManager->getClassMetadata('Dummy')->willReturn($classMetadata->reveal()); | ||
$managerRegistry = $this->prophesize(ManagerRegistry::class); | ||
$managerRegistry->getManagerForClass('Dummy')->willReturn($objectManager->reveal()); | ||
|
||
$this->assertFalse(QueryChecker::hasOrderByOnToManyJoin($queryBuilder->reveal(), $managerRegistry->reveal())); | ||
} | ||
} |