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

Prevent load entities when pass empty choices for ModelType #6548

Merged
merged 2 commits into from
Oct 31, 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
14 changes: 7 additions & 7 deletions src/Form/ChoiceList/ModelChoiceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ModelChoiceLoader implements ChoiceLoaderInterface
private $query;

/**
* @var object[]
* @var object[]|null
*/
private $choices;

Expand All @@ -76,10 +76,10 @@ class ModelChoiceLoader implements ChoiceLoaderInterface
private $choiceList;

/**
* @param string $class
* @param string|null $property
* @param object|null $query
* @param object[] $choices
* @param string $class
* @param string|null $property
* @param object|null $query
* @param object[]|null $choices
*
* @phpstan-param class-string $class
*/
Expand All @@ -88,7 +88,7 @@ public function __construct(
$class,
$property = null,
$query = null,
$choices = [],
$choices = null,
?PropertyAccessorInterface $propertyAccessor = null
) {
$this->modelManager = $modelManager;
Expand Down Expand Up @@ -128,7 +128,7 @@ public function loadChoiceList($value = null)
if (!$this->choiceList) {
if ($this->query) {
$entities = $this->modelManager->executeQuery($this->query);
} elseif (\is_array($this->choices) && \count($this->choices) > 0) {
} elseif (\is_array($this->choices)) {
Copy link
Member

Choose a reason for hiding this comment

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

Given what is currently checked in the test, I think this is the only change required in this PR.

Copy link
Contributor Author

@kirya-dev kirya-dev Oct 29, 2020

Choose a reason for hiding this comment

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

Not only. If we keep ModelType with default option choices with empty than on default entities dosent loaded

Copy link
Member

Choose a reason for hiding this comment

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

Then, that isn't checked in the test. I mean, if I change only this line and run simple-phpunit --filter ModelTypeTest::testGetOptions, the test is passing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Must no passing becouse require method findBy calls once or never

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image
proofs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

When you're passing explicitly null in the test, it's working indeed.
But what if you're not passing a choice.

$options = $optionResolver->resolve(['model_manager' => $modelManager]);

It should keep calling the findBy, but it won't anymore @phansys.

That's why I agree with @kirya-dev.

Copy link
Member

@phansys phansys Oct 30, 2020

Choose a reason for hiding this comment

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

I didn't analyzed the change, but if that is fine, I think the test must cover the case (the test provided in this PR is passing even if these changes are not applied).

Copy link
Member

Choose a reason for hiding this comment

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

@kirya-dev Can you add a test with $options = $optionResolver->resolve(['model_manager' => $modelManager]); ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course. Added!

$entities = $this->choices;
} else {
$entities = $this->modelManager->findBy($this->class);
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Type/ModelType.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function configureOptions(OptionsResolver $resolver)
'class' => null,
'property' => null,
'query' => null,
'choices' => [],
'choices' => null,
'preferred_choices' => [],
'btn_add' => 'link_add',
'btn_list' => 'link_list',
Expand Down
59 changes: 38 additions & 21 deletions tests/Form/Type/ModelTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,48 @@ protected function setUp(): void
$this->type = new ModelType(PropertyAccess::createPropertyAccessor());
}

public function testGetDefaultOptions(): void
/**
* @dataProvider getGetOptionsTests
*/
public function testGetOptions(array $options, int $expectedModelManagerFindCalls): void
{
$modelManager = $this->getMockForAbstractClass(ModelManagerInterface::class);

$optionResolver = new OptionsResolver();

$this->type->configureOptions($optionResolver);

$options = $optionResolver->resolve(['model_manager' => $modelManager, 'choices' => []]);
$optionsResolver = new OptionsResolver();

$this->type->configureOptions($optionsResolver);

$resolvedOptions = $optionsResolver->resolve(['model_manager' => $modelManager] + $options);

$this->assertFalse($resolvedOptions['compound']);
$this->assertSame('choice', $resolvedOptions['template']);
$this->assertFalse($resolvedOptions['multiple']);
$this->assertFalse($resolvedOptions['expanded']);
$this->assertInstanceOf(ModelManagerInterface::class, $resolvedOptions['model_manager']);
$this->assertNull($resolvedOptions['class']);
$this->assertNull($resolvedOptions['property']);
$this->assertNull($resolvedOptions['query']);
$this->assertSame($options['choices'] ?? null, $resolvedOptions['choices']);
$this->assertCount(0, $resolvedOptions['preferred_choices']);
$this->assertSame('link_add', $resolvedOptions['btn_add']);
$this->assertSame('link_list', $resolvedOptions['btn_list']);
$this->assertSame('link_delete', $resolvedOptions['btn_delete']);
$this->assertSame('SonataAdminBundle', $resolvedOptions['btn_catalogue']);
$this->assertInstanceOf(ModelChoiceLoader::class, $resolvedOptions['choice_loader']);

$modelManager->expects($this->exactly($expectedModelManagerFindCalls))
->method('findBy')
->willReturn([]);
$resolvedOptions['choice_loader']->loadChoiceList();
}

$this->assertFalse($options['compound']);
$this->assertSame('choice', $options['template']);
$this->assertFalse($options['multiple']);
$this->assertFalse($options['expanded']);
$this->assertInstanceOf(ModelManagerInterface::class, $options['model_manager']);
$this->assertNull($options['class']);
$this->assertNull($options['property']);
$this->assertNull($options['query']);
$this->assertCount(0, $options['choices']);
$this->assertCount(0, $options['preferred_choices']);
$this->assertSame('link_add', $options['btn_add']);
$this->assertSame('link_list', $options['btn_list']);
$this->assertSame('link_delete', $options['btn_delete']);
$this->assertSame('SonataAdminBundle', $options['btn_catalogue']);
$this->assertInstanceOf(ModelChoiceLoader::class, $options['choice_loader']);
public function getGetOptionsTests(): iterable
{
return [
[[], 1],
[['choices' => null], 1],
[['choices' => []], 0],
];
}

/**
Expand Down