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 prophecy usage #6421

Merged
merged 4 commits into from
Sep 27, 2020
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"symfony/css-selector": "^4.4 || ^5.1",
"symfony/filesystem": "^4.4 || ^5.1",
"symfony/maker-bundle": "^1.17",
"symfony/phpunit-bridge": "^5.1.1",
"symfony/phpunit-bridge": "^5.1.4",
"symfony/yaml": "^4.4 || ^5.1",
"vimeo/psalm": "^3.13.1"
},
Expand Down
6 changes: 3 additions & 3 deletions src/Form/DataTransformerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ private function hasFieldDescriptionAssociationWithClass(
FieldDescriptionInterface $fieldDescription,
string $class
): bool {
return (
method_exists($fieldDescription, 'getTargetModel') && $class === $fieldDescription->getTargetModel()
) || $class === $fieldDescription->getTargetEntity();
return method_exists($fieldDescription, 'getTargetModel') ?
Copy link
Member Author

Choose a reason for hiding this comment

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

I found this while changing all the tests, this can be done on a separate PR if you want.

Copy link
Member

Choose a reason for hiding this comment

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

This PR could take some times before being merged. So maybe it's indeed a good idea to create a separate PR

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's see how this goes, if we see a lot of review process I will revert it from this PR and open a new one. If not, I will change it to patch and add a changelog

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but at least a separate commit seems needed.

$class === $fieldDescription->getTargetModel() :
$class === $fieldDescription->getTargetEntity();
}
}
58 changes: 25 additions & 33 deletions tests/Action/AppendFormFieldElementActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Sonata\AdminBundle\Tests\Action;

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Sonata\AdminBundle\Action\AppendFormFieldElementAction;
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction;
use Sonata\AdminBundle\Admin\AbstractAdmin;
Expand All @@ -27,7 +26,6 @@
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Twig\Environment;

final class AppendFormFieldElementActionTest extends TestCase
Expand All @@ -52,29 +50,23 @@ final class AppendFormFieldElementActionTest extends TestCase
*/
private $admin;

/**
* @var ValidatorInterface
*/
private $validator;

/**
* @var AdminHelper
*/
private $helper;

protected function setUp(): void
{
$this->twig = $this->prophesize(Environment::class);
$this->pool = $this->prophesize(Pool::class);
$this->admin = $this->prophesize(AbstractAdmin::class);
$this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
$this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
$this->validator = $this->prophesize(ValidatorInterface::class);
$this->helper = $this->prophesize(AdminHelper::class);
$this->twig = $this->createStub(Environment::class);
$this->pool = $this->createStub(Pool::class);
$this->admin = $this->createMock(AbstractAdmin::class);
$this->pool->method('getInstance')->willReturn($this->admin);
$this->admin->expects($this->once())->method('setRequest');
$this->helper = $this->createStub(AdminHelper::class);
$this->action = new AppendFormFieldElementAction(
$this->twig->reveal(),
$this->pool->reveal(),
$this->helper->reveal()
$this->twig,
$this->pool,
$this->helper
);
}

Expand All @@ -89,26 +81,26 @@ public function testAppendFormFieldElementAction(): void
'context' => 'list',
], [], [], [], [], ['REQUEST_METHOD' => Request::METHOD_POST]);

$modelManager = $this->prophesize(ModelManagerInterface::class);
$modelManager = $this->createStub(ModelManagerInterface::class);
$formView = new FormView();
$form = $this->prophesize(Form::class);
$form = $this->createStub(Form::class);

$renderer = $this->configureFormRenderer();

$this->admin->getObject(42)->willReturn($object);
$this->admin->getClass()->willReturn(\get_class($object));
$this->admin->setSubject($object)->shouldBeCalled();
$this->admin->getFormTheme()->willReturn($formView);
$this->helper->appendFormFieldElement($this->admin->reveal(), $object, null)->willReturn([
$this->prophesize(FieldDescriptionInterface::class),
$form->reveal(),
$this->admin->method('getObject')->with(42)->willReturn($object);
$this->admin->method('getClass')->willReturn(\get_class($object));
$this->admin->expects($this->once())->method('setSubject')->with($object);
$this->admin->method('getFormTheme')->willReturn($formView);
$this->helper->method('appendFormFieldElement')->with($this->admin, $object, null)->willReturn([
$this->createStub(FieldDescriptionInterface::class),
$form,
]);
$this->helper->getChildFormView($formView, null)
$this->helper->method('getChildFormView')->with($formView, null)
->willReturn($formView);
$modelManager->find(\get_class($object), 42)->willReturn($object);
$form->createView()->willReturn($formView);
$renderer->setTheme($formView, $formView)->shouldBeCalled();
$renderer->searchAndRenderBlock($formView, 'widget')->willReturn('block');
$modelManager->method('find')->with(\get_class($object), 42)->willReturn($object);
$form->method('createView')->willReturn($formView);
$renderer->expects($this->once())->method('setTheme')->with($formView);
$renderer->method('searchAndRenderBlock')->with($formView, 'widget')->willReturn('block');

$response = ($this->action)($request);

Expand All @@ -118,9 +110,9 @@ public function testAppendFormFieldElementAction(): void

private function configureFormRenderer()
{
$runtime = $this->prophesize(FormRenderer::class);
$runtime = $this->createStub(FormRenderer::class);
jordisala1991 marked this conversation as resolved.
Show resolved Hide resolved

$this->twig->getRuntime(FormRenderer::class)->willReturn($runtime->reveal());
$this->twig->method('getRuntime')->with(FormRenderer::class)->willReturn($runtime);

return $runtime;
}
Expand Down
26 changes: 19 additions & 7 deletions tests/Action/DashboardActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@

class DashboardActionTest extends TestCase
{
/**
* @var MutableTemplateRegistryInterface
*/
private $templateRegistry;

/**
* @var DashboardAction
*/
Expand All @@ -34,22 +39,19 @@ protected function setUp(): void
{
$container = new Container();

$templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
$templateRegistry->getTemplate('ajax')->willReturn('ajax.html');
$templateRegistry->getTemplate('dashboard')->willReturn('dashboard.html');
$templateRegistry->getTemplate('layout')->willReturn('layout.html');
$this->templateRegistry = $this->createStub(MutableTemplateRegistryInterface::class);

$pool = new Pool($container, 'title', 'logo.png');
$pool->setTemplateRegistry($templateRegistry->reveal());
$pool->setTemplateRegistry($this->templateRegistry);

$twig = $this->createMock(Environment::class);

$breadcrumbsBuilder = $this->getMockForAbstractClass(BreadcrumbsBuilderInterface::class);
$breadcrumbsBuilder = $this->createStub(BreadcrumbsBuilderInterface::class);

$this->action = new DashboardAction(
[],
$breadcrumbsBuilder,
$templateRegistry->reveal(),
$this->templateRegistry,
$pool,
$twig
);
Expand All @@ -59,6 +61,11 @@ public function testdashboardActionStandardRequest(): void
{
$request = new Request();

$this->templateRegistry->method('getTemplate')->willReturnMap([
['layout', 'layout.html'],
['dashboard', 'dashboard.html'],
]);

$this->assertInstanceOf(Response::class, ($this->action)($request));
}

Expand All @@ -67,6 +74,11 @@ public function testDashboardActionAjaxLayout(): void
$request = new Request();
$request->headers->set('X-Requested-With', 'XMLHttpRequest');

$this->templateRegistry->method('getTemplate')->willReturnMap([
['ajax', 'ajax.html'],
['dashboard', 'dashboard.html'],
]);

$this->assertInstanceOf(Response::class, ($this->action)($request));
}
}
53 changes: 28 additions & 25 deletions tests/Action/GetShortObjectDescriptionActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Sonata\AdminBundle\Tests\Action;

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Admin\Pool;
Expand Down Expand Up @@ -50,13 +49,12 @@ final class GetShortObjectDescriptionActionTest extends TestCase
protected function setUp(): void
{
$this->twig = new Environment(new ArrayLoader(['template' => 'renderedTemplate']));
$this->pool = $this->prophesize(Pool::class);
$this->admin = $this->prophesize(AbstractAdmin::class);
$this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
$this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
$this->pool = $this->createStub(Pool::class);
$this->admin = $this->createMock(AbstractAdmin::class);
$this->pool->method('getInstance')->willReturn($this->admin);
$this->action = new GetShortObjectDescriptionAction(
$this->twig,
$this->pool->reveal()
$this->pool
);
}

Expand All @@ -71,8 +69,8 @@ public function testGetShortObjectDescriptionActionInvalidAdmin(): void
'uniqid' => 'asdasd123',
]);

$this->pool->getInstance($code)->willThrow(\InvalidArgumentException::class);
$this->admin->setRequest(Argument::type(Request::class))->shouldNotBeCalled();
$this->pool->method('getInstance')->with($code)->willThrowException(new \InvalidArgumentException());
$this->admin->expects($this->never())->method('setRequest');

($this->action)($request);
}
Expand All @@ -94,8 +92,9 @@ public function testGetShortObjectDescriptionActionObjectDoesNotExist(): void
'uniqid' => 'asdasd123',
]);

$this->admin->setUniqid('asdasd123')->shouldBeCalled();
$this->admin->getObject(42)->willReturn(false);
$this->admin->expects($this->once())->method('setRequest')->with($request);
$this->admin->expects($this->once())->method('setUniqid')->with('asdasd123');
$this->admin->method('getObject')->with(42)->willReturn(false);

($this->action)($request);
}
Expand All @@ -114,8 +113,9 @@ public function testGetShortObjectDescriptionActionEmptyObjectId(): void
'_format' => 'html',
]);

$this->admin->setUniqid('asdasd123')->shouldBeCalled();
$this->admin->getObject(null)->willReturn(null);
$this->admin->expects($this->once())->method('setRequest')->with($request);
$this->admin->expects($this->once())->method('setUniqid')->with('asdasd123');
$this->admin->method('getObject')->with(null)->willReturn(null);

$this->assertInstanceOf(Response::class, ($this->action)($request));
}
Expand All @@ -130,10 +130,11 @@ public function testGetShortObjectDescriptionActionObject(): void
]);
$object = new \stdClass();

$this->admin->setUniqid('asdasd123')->shouldBeCalled();
$this->admin->getObject(42)->willReturn($object);
$this->admin->getTemplate('short_object_description')->willReturn('template');
$this->admin->toString($object)->willReturn('bar');
$this->admin->expects($this->once())->method('setRequest')->with($request);
$this->admin->expects($this->once())->method('setUniqid')->with('asdasd123');
$this->admin->method('getObject')->with(42)->willReturn($object);
$this->admin->method('getTemplate')->with('short_object_description')->willReturn('template');
$this->admin->method('toString')->with($object)->willReturn('bar');

$response = ($this->action)($request);

Expand All @@ -154,10 +155,11 @@ public function testGetShortObjectDescriptionActionEmptyObjectIdAsJson(): void
'_format' => 'json',
]);

$this->admin->setUniqid('asdasd123')->shouldBeCalled();
$this->admin->getObject(null)->willReturn(null);
$this->admin->id(null)->willReturn('');
$this->admin->toString(null)->willReturn('');
$this->admin->expects($this->once())->method('setRequest')->with($request);
$this->admin->expects($this->once())->method('setUniqid')->with('asdasd123');
$this->admin->method('getObject')->with(null)->willReturn(null);
$this->admin->method('id')->with(null)->willReturn('');
$this->admin->method('toString')->with(null)->willReturn('');

$response = ($this->action)($request);

Expand All @@ -175,11 +177,12 @@ public function testGetShortObjectDescriptionActionObjectAsJson(): void
]);
$object = new \stdClass();

$this->admin->setUniqid('asdasd123')->shouldBeCalled();
$this->admin->id($object)->willReturn(42);
$this->admin->getObject(42)->willReturn($object);
$this->admin->getTemplate('short_object_description')->willReturn('template');
$this->admin->toString($object)->willReturn('bar');
$this->admin->expects($this->once())->method('setRequest')->with($request);
$this->admin->expects($this->once())->method('setUniqid')->with('asdasd123');
$this->admin->method('id')->with($object)->willReturn(42);
$this->admin->method('getObject')->with(42)->willReturn($object);
$this->admin->method('getTemplate')->with('short_object_description')->willReturn('template');
$this->admin->method('toString')->with($object)->willReturn('bar');

$response = ($this->action)($request);

Expand Down
Loading