|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Core\Tests\Bridge\Doctrine\EventListener; |
| 15 | + |
| 16 | +use ApiPlatform\Core\Bridge\Doctrine\EventListener\WriteListener; |
| 17 | +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy; |
| 18 | +use Doctrine\Common\Persistence\ManagerRegistry; |
| 19 | +use Doctrine\Common\Persistence\ObjectManager; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use Symfony\Component\HttpFoundation\Request; |
| 22 | +use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; |
| 23 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Amrouche Hamza <[email protected]> |
| 27 | + * |
| 28 | + * @group legacy |
| 29 | + */ |
| 30 | +class WriteListenerTest extends TestCase |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @expectedDeprecation The ApiPlatform\Core\Bridge\Doctrine\EventListener\WriteListener class is deprecated since version 2.2 and will be removed in 3.0. Use the ApiPlatform\Core\EventListener\WriteListener class instead. |
| 34 | + */ |
| 35 | + public function testOnKernelViewWithControllerResultAndPostMethod() |
| 36 | + { |
| 37 | + $dummy = new Dummy(); |
| 38 | + $dummy->setName('Dummyrino'); |
| 39 | + |
| 40 | + $objectManagerProphecy = $this->prophesize(ObjectManager::class); |
| 41 | + $objectManagerProphecy->persist($dummy)->shouldBeCalled(); |
| 42 | + $objectManagerProphecy->flush()->shouldBeCalled(); |
| 43 | + $managerRegistryProphecy = $this->prophesize(ManagerRegistry::class); |
| 44 | + $managerRegistryProphecy->getManagerForClass('Dummy')->willReturn($objectManagerProphecy->reveal()); |
| 45 | + |
| 46 | + $writeListener = new WriteListener($managerRegistryProphecy->reveal()); |
| 47 | + $httpKernelProphecy = $this->prophesize(HttpKernelInterface::class); |
| 48 | + $request = new Request(); |
| 49 | + $request->setMethod(Request::METHOD_POST); |
| 50 | + $request->attributes->set('_api_resource_class', 'Dummy'); |
| 51 | + $event = new GetResponseForControllerResultEvent($httpKernelProphecy->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, $dummy); |
| 52 | + |
| 53 | + $this->assertNull($writeListener->onKernelView($event)); |
| 54 | + $this->assertNotEquals($dummy, $writeListener->onKernelView($event)); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @expectedDeprecation The ApiPlatform\Core\Bridge\Doctrine\EventListener\WriteListener class is deprecated since version 2.2 and will be removed in 3.0. Use the ApiPlatform\Core\EventListener\WriteListener class instead. |
| 59 | + */ |
| 60 | + public function testOnKernelViewWithControllerResultAndDeleteMethod() |
| 61 | + { |
| 62 | + $dummy = new Dummy(); |
| 63 | + $dummy->setName('Dummyrino'); |
| 64 | + |
| 65 | + $objectManagerProphecy = $this->prophesize(ObjectManager::class); |
| 66 | + $objectManagerProphecy->remove($dummy)->shouldBeCalled(); |
| 67 | + $objectManagerProphecy->flush()->shouldBeCalled(); |
| 68 | + $managerRegistryProphecy = $this->prophesize(ManagerRegistry::class); |
| 69 | + $managerRegistryProphecy->getManagerForClass('Dummy')->willReturn($objectManagerProphecy->reveal()); |
| 70 | + |
| 71 | + $writeListener = new WriteListener($managerRegistryProphecy->reveal()); |
| 72 | + $request = new Request(); |
| 73 | + $request->setMethod(Request::METHOD_DELETE); |
| 74 | + $request->attributes->set('_api_resource_class', 'Dummy'); |
| 75 | + $event = $this->prophesize(GetResponseForControllerResultEvent::class); |
| 76 | + $event->setControllerResult(null)->shouldBeCalled(); |
| 77 | + $event->getRequest()->willReturn($request); |
| 78 | + $event->getControllerResult()->willReturn($dummy); |
| 79 | + $this->assertNull($writeListener->onKernelView($event->reveal())); |
| 80 | + $this->assertNotEquals($dummy, $writeListener->onKernelView($event->reveal())); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @expectedDeprecation The ApiPlatform\Core\Bridge\Doctrine\EventListener\WriteListener class is deprecated since version 2.2 and will be removed in 3.0. Use the ApiPlatform\Core\EventListener\WriteListener class instead. |
| 85 | + */ |
| 86 | + public function testOnKernelViewWithSafeMethod() |
| 87 | + { |
| 88 | + $dummy = new Dummy(); |
| 89 | + $dummy->setName('Dummyrino'); |
| 90 | + |
| 91 | + $managerRegistryProphecy = $this->prophesize(ManagerRegistry::class); |
| 92 | + |
| 93 | + $writeListener = new WriteListener($managerRegistryProphecy->reveal()); |
| 94 | + $httpKernelProphecy = $this->prophesize(HttpKernelInterface::class); |
| 95 | + $request = new Request(); |
| 96 | + $request->setMethod(Request::METHOD_HEAD); |
| 97 | + $event = new GetResponseForControllerResultEvent($httpKernelProphecy->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, $dummy); |
| 98 | + |
| 99 | + $this->assertNull($writeListener->onKernelView($event)); |
| 100 | + $this->assertNotEquals($dummy, $writeListener->onKernelView($event)); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @expectedDeprecation The ApiPlatform\Core\Bridge\Doctrine\EventListener\WriteListener class is deprecated since version 2.2 and will be removed in 3.0. Use the ApiPlatform\Core\EventListener\WriteListener class instead. |
| 105 | + */ |
| 106 | + public function testOnKernelViewWithNoResourceClass() |
| 107 | + { |
| 108 | + $dummy = new Dummy(); |
| 109 | + $dummy->setName('Dummyrino'); |
| 110 | + |
| 111 | + $managerRegistryProphecy = $this->prophesize(ManagerRegistry::class); |
| 112 | + |
| 113 | + $writeListener = new WriteListener($managerRegistryProphecy->reveal()); |
| 114 | + $httpKernelProphecy = $this->prophesize(HttpKernelInterface::class); |
| 115 | + $request = new Request(); |
| 116 | + $request->setMethod(Request::METHOD_POST); |
| 117 | + $event = new GetResponseForControllerResultEvent($httpKernelProphecy->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, $dummy); |
| 118 | + |
| 119 | + $this->assertNull($writeListener->onKernelView($event)); |
| 120 | + $this->assertNotEquals($dummy, $writeListener->onKernelView($event)); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @expectedDeprecation The ApiPlatform\Core\Bridge\Doctrine\EventListener\WriteListener class is deprecated since version 2.2 and will be removed in 3.0. Use the ApiPlatform\Core\EventListener\WriteListener class instead. |
| 125 | + */ |
| 126 | + public function testOnKernelViewWithNoManager() |
| 127 | + { |
| 128 | + $dummy = new Dummy(); |
| 129 | + $dummy->setName('Dummyrino'); |
| 130 | + |
| 131 | + $managerRegistryProphecy = $this->prophesize(ManagerRegistry::class); |
| 132 | + $managerRegistryProphecy->getManagerForClass('Dummy')->willReturn(null); |
| 133 | + |
| 134 | + $writeListener = new WriteListener($managerRegistryProphecy->reveal()); |
| 135 | + $httpKernelProphecy = $this->prophesize(HttpKernelInterface::class); |
| 136 | + $request = new Request(); |
| 137 | + $request->setMethod(Request::METHOD_DELETE); |
| 138 | + $request->attributes->set('_api_resource_class', 'Dummy'); |
| 139 | + $event = new GetResponseForControllerResultEvent($httpKernelProphecy->reveal(), $request, HttpKernelInterface::MASTER_REQUEST, $dummy); |
| 140 | + |
| 141 | + $this->assertNull($writeListener->onKernelView($event)); |
| 142 | + $this->assertNotEquals($dummy, $writeListener->onKernelView($event)); |
| 143 | + } |
| 144 | +} |
0 commit comments