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

Remove some Prophecy usage #6416

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 24 additions & 16 deletions tests/Controller/CRUDControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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([
Expand Down
64 changes: 46 additions & 18 deletions tests/Twig/Extension/SonataAdminExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -2959,10 +2973,24 @@ public function testCanonicalizedLocaleForMoment(?string $expected, string $orig

public function testIsGrantedAffirmative(): void
{
$this->securityChecker
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been moved where it is actually used and I changed a little because before:

$this->securityChecker->isGranted(['foo', 'bar'], null)->willReturn(false);	
$this->securityChecker->isGranted(Argument::type('string'), null)->willReturn(true);

The first line was wrong because the implementation calls isGranted always with just one $role.

->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'));
}

Expand Down