From a51c59264939d43858e035d0a6e31fc46d212751 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Fri, 25 Sep 2020 22:14:08 +0200 Subject: [PATCH] Remove some Prophecy usage Reduces the number of warnings using PHPUnit 9. --- tests/Controller/CRUDControllerTest.php | 40 +++++++----- .../Extension/SonataAdminExtensionTest.php | 64 +++++++++++++------ 2 files changed, 70 insertions(+), 34 deletions(-) diff --git a/tests/Controller/CRUDControllerTest.php b/tests/Controller/CRUDControllerTest.php index 70198cf25e..124d607e28 100644 --- a/tests/Controller/CRUDControllerTest.php +++ b/tests/Controller/CRUDControllerTest.php @@ -174,7 +174,7 @@ protected function setUp(): void $this->parameters = []; $this->template = ''; - $this->templateRegistry = $this->prophesize(TemplateRegistryInterface::class); + $this->templateRegistry = $this->createMock(TemplateRegistryInterface::class); $templating = $this->createMock(DelegatingEngine::class); @@ -252,7 +252,7 @@ protected function setUp(): void $this->container->set('sonata.admin.pool', $this->pool); $this->container->set('request_stack', $requestStack); $this->container->set('foo.admin', $this->admin); - $this->container->set('foo.admin.template_registry', $this->templateRegistry->reveal()); + $this->container->set('foo.admin.template_registry', $this->templateRegistry); $this->container->set('templating', $templating); $this->container->set('twig', $twig); $this->container->set('session', $this->session); @@ -271,20 +271,28 @@ protected function setUp(): void ); $this->container->setParameter('sonata.admin.security.acl_user_manager', null); - $this->templateRegistry->getTemplate('ajax')->willReturn('@SonataAdmin/ajax_layout.html.twig'); - $this->templateRegistry->getTemplate('layout')->willReturn('@SonataAdmin/standard_layout.html.twig'); - $this->templateRegistry->getTemplate('show')->willReturn('@SonataAdmin/CRUD/show.html.twig'); - $this->templateRegistry->getTemplate('show_compare')->willReturn('@SonataAdmin/CRUD/show_compare.html.twig'); - $this->templateRegistry->getTemplate('edit')->willReturn('@SonataAdmin/CRUD/edit.html.twig'); - $this->templateRegistry->getTemplate('dashboard')->willReturn('@SonataAdmin/Core/dashboard.html.twig'); - $this->templateRegistry->getTemplate('search')->willReturn('@SonataAdmin/Core/search.html.twig'); - $this->templateRegistry->getTemplate('list')->willReturn('@SonataAdmin/CRUD/list.html.twig'); - $this->templateRegistry->getTemplate('preview')->willReturn('@SonataAdmin/CRUD/preview.html.twig'); - $this->templateRegistry->getTemplate('history')->willReturn('@SonataAdmin/CRUD/history.html.twig'); - $this->templateRegistry->getTemplate('acl')->willReturn('@SonataAdmin/CRUD/acl.html.twig'); - $this->templateRegistry->getTemplate('delete')->willReturn('@SonataAdmin/CRUD/delete.html.twig'); - $this->templateRegistry->getTemplate('batch')->willReturn('@SonataAdmin/CRUD/list__batch.html.twig'); - $this->templateRegistry->getTemplate('batch_confirmation')->willReturn('@SonataAdmin/CRUD/batch_confirmation.html.twig'); + $templates = [ + 'ajax' => '@SonataAdmin/ajax_layout.html.twig', + 'layout' => '@SonataAdmin/standard_layout.html.twig', + 'show' => '@SonataAdmin/CRUD/show.html.twig', + 'show_compare' => '@SonataAdmin/CRUD/show_compare.html.twig', + 'edit' => '@SonataAdmin/CRUD/edit.html.twig', + 'dashboard' => '@SonataAdmin/Core/dashboard.html.twig', + 'search' => '@SonataAdmin/Core/search.html.twig', + 'list' => '@SonataAdmin/CRUD/list.html.twig', + 'preview' => '@SonataAdmin/CRUD/preview.html.twig', + 'history' => '@SonataAdmin/CRUD/history.html.twig', + 'acl' => '@SonataAdmin/CRUD/acl.html.twig', + 'delete' => '@SonataAdmin/CRUD/delete.html.twig', + 'batch' => '@SonataAdmin/CRUD/list__batch.html.twig', + 'batch_confirmation' => '@SonataAdmin/CRUD/batch_confirmation.html.twig', + ]; + + $this->templateRegistry + ->method('getTemplate') + ->willReturnCallback(static function (string $templateName) use ($templates): string { + return $templates[$templateName]; + }); // NEXT_MAJOR: Remove this call $this->admin->method('getTemplate')->willReturnMap([ diff --git a/tests/Twig/Extension/SonataAdminExtensionTest.php b/tests/Twig/Extension/SonataAdminExtensionTest.php index 54dad24d8f..a6332ca2a1 100644 --- a/tests/Twig/Extension/SonataAdminExtensionTest.php +++ b/tests/Twig/Extension/SonataAdminExtensionTest.php @@ -14,7 +14,6 @@ namespace Sonata\AdminBundle\Tests\Twig\Extension; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; use Psr\Log\LoggerInterface; use Sonata\AdminBundle\Admin\AbstractAdmin; use Sonata\AdminBundle\Admin\AdminInterface; @@ -29,6 +28,7 @@ use Symfony\Bridge\Twig\Extension\RoutingExtension; use Symfony\Bridge\Twig\Extension\TranslationExtension; use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Generator\UrlGenerator; @@ -155,20 +155,18 @@ protected function setUp(): void $this->translator = $translator; - $this->templateRegistry = $this->prophesize(TemplateRegistryInterface::class); - $this->container = $this->prophesize(ContainerInterface::class); - $this->container->get('sonata_admin_foo_service.template_registry')->willReturn($this->templateRegistry->reveal()); + $this->templateRegistry = $this->createMock(TemplateRegistryInterface::class); + $this->container = new Container(); + $this->container->set('sonata_admin_foo_service.template_registry', $this->templateRegistry); - $this->securityChecker = $this->prophesize(AuthorizationCheckerInterface::class); - $this->securityChecker->isGranted(['foo', 'bar'], null)->willReturn(false); - $this->securityChecker->isGranted(Argument::type('string'), null)->willReturn(true); + $this->securityChecker = $this->createStub(AuthorizationCheckerInterface::class); $this->twigExtension = new SonataAdminExtension( $this->pool, $this->logger, $this->translator, - $this->container->reveal(), - $this->securityChecker->reveal() + $this->container, + $this->securityChecker ); $this->twigExtension->setXEditableTypeMapping($this->xEditableTypeMapping); @@ -318,7 +316,10 @@ public function testRenderListElement(string $expected, string $type, $value, ar ->with('base_list_field') ->willReturn('@SonataAdmin/CRUD/base_list_field.html.twig'); - $this->templateRegistry->getTemplate('base_list_field')->willReturn('@SonataAdmin/CRUD/base_list_field.html.twig'); + $this->templateRegistry + ->method('getTemplate') + ->with('base_list_field') + ->willReturn('@SonataAdmin/CRUD/base_list_field.html.twig'); $this->fieldDescription ->method('getValue') @@ -400,7 +401,10 @@ public function testRenderListElementWithAdditionalValuesInArray(): void ->with('base_list_field') ->willReturn('@SonataAdmin/CRUD/base_list_field.html.twig'); - $this->templateRegistry->getTemplate('base_list_field')->willReturn('@SonataAdmin/CRUD/base_list_field.html.twig'); + $this->templateRegistry + ->method('getTemplate') + ->with('base_list_field') + ->willReturn('@SonataAdmin/CRUD/base_list_field.html.twig'); $this->fieldDescription ->method('getTemplate') @@ -430,7 +434,10 @@ public function testRenderListElementWithNoValueException(): void ->with('base_list_field') ->willReturn('@SonataAdmin/CRUD/base_list_field.html.twig'); - $this->templateRegistry->getTemplate('base_list_field')->willReturn('@SonataAdmin/CRUD/base_list_field.html.twig'); + $this->templateRegistry + ->method('getTemplate') + ->with('base_list_field') + ->willReturn('@SonataAdmin/CRUD/base_list_field.html.twig'); $this->fieldDescription ->method('getValue') @@ -464,7 +471,10 @@ public function testDeprecatedRenderListElement(string $expected, ?string $value ->with('base_list_field') ->willReturn('@SonataAdmin/CRUD/base_list_field.html.twig'); - $this->templateRegistry->getTemplate('base_list_field')->willReturn('@SonataAdmin/CRUD/base_list_field.html.twig'); + $this->templateRegistry + ->method('getTemplate') + ->with('base_list_field') + ->willReturn('@SonataAdmin/CRUD/base_list_field.html.twig'); $this->fieldDescription ->method('getValue') @@ -1493,7 +1503,10 @@ public function testRenderListElementNonExistentTemplate(): void ->with('base_list_field') ->willReturn('@SonataAdmin/CRUD/base_list_field.html.twig'); - $this->templateRegistry->getTemplate('base_list_field')->willReturn('@SonataAdmin/CRUD/base_list_field.html.twig'); + $this->templateRegistry + ->method('getTemplate') + ->with('base_list_field') + ->willReturn('@SonataAdmin/CRUD/base_list_field.html.twig'); $this->fieldDescription->expects($this->once()) ->method('getValue') @@ -1537,15 +1550,16 @@ public function testRenderListElementErrorLoadingTemplate(): void ->with('base_list_field') ->willReturn('@SonataAdmin/CRUD/base_list_nonexistent_field.html.twig'); - $this->templateRegistry->getTemplate('base_list_field')->willReturn('@SonataAdmin/CRUD/base_list_nonexistent_field.html.twig'); + $this->templateRegistry + ->method('getTemplate') + ->with('base_list_field') + ->willReturn('@SonataAdmin/CRUD/base_list_nonexistent_field.html.twig'); $this->fieldDescription->expects($this->once()) ->method('getTemplate') ->willReturn('@SonataAdmin/CRUD/list_nonexistent_template.html.twig'); $this->twigExtension->renderListElement($this->environment, $this->object, $this->fieldDescription); - - $this->templateRegistry->getTemplate('base_list_field')->shouldHaveBeenCalled(); } /** @@ -2959,10 +2973,24 @@ public function testCanonicalizedLocaleForMoment(?string $expected, string $orig public function testIsGrantedAffirmative(): void { + $this->securityChecker + ->method('isGranted') + ->willReturnCallback(static function (string $role): bool { + if ('foo' === $role) { + return false; + } + + if ('bar' === $role) { + return true; + } + + return false; + }); + $this->assertTrue( $this->twigExtension->isGrantedAffirmative(['foo', 'bar']) ); - $this->assertTrue($this->twigExtension->isGrantedAffirmative('foo')); + $this->assertFalse($this->twigExtension->isGrantedAffirmative('foo')); $this->assertTrue($this->twigExtension->isGrantedAffirmative('bar')); }