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

Cast field name to match parameter requirements #7983

Closed
wants to merge 2 commits 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
2 changes: 1 addition & 1 deletion src/Util/FormBuilderIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function next(): void

public function current(): FormBuilderInterface
{
return $this->formBuilder->get($this->iterator->current());
return $this->formBuilder->get((string) $this->iterator->current());
}

public function getChildren(): self
Expand Down
35 changes: 25 additions & 10 deletions tests/Util/FormBuilderIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,26 @@
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Util\FormBuilderIterator;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\Form\FormRegistryInterface;

/**
* @author Mike Meier <mike.meier@ibrows.ch>
*/
final class FormBuilderIteratorTest extends TestCase
{
private EventDispatcherInterface $dispatcher;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've removed dispatcher and factory properties from the class and replaced them with vars inside setUp since they are not used anywhere else.


private FormFactoryInterface $factory;

private FormBuilder $builder;

protected function setUp(): void
{
$this->dispatcher = $this->createStub(EventDispatcherInterface::class);
$this->factory = $this->createStub(FormFactoryInterface::class);
$this->builder = new FormBuilder('name', null, $this->dispatcher, $this->factory);
$this->factory->method('createNamedBuilder')->willReturn($this->builder);
$dispatcher = $this->createStub(EventDispatcherInterface::class);
$registry = $this->createStub(FormRegistryInterface::class);
$factory = new FormFactory($registry);
$this->builder = new FormBuilder('name', null, $dispatcher, $factory);
}

public function testGetChildren(): void
Expand All @@ -51,6 +50,22 @@ public function testHasChildren(): void
{
$this->builder->add('name', TextType::class);
$iterator = new FormBuilderIterator($this->builder);
static::assertTrue($iterator->hasChildren());
static::assertFalse($iterator->hasChildren());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed from using a stub of FormFactoryInterface to using an instance of FormFactory. This resulted in this test to fail. After checking what hasChildren does, I don't think this test was ever correct and only resulted in the expected result because it was a stub.
hasChildren checks if the children of current have children. This is not the case if we have one single TextType.

Copy link
Member

Choose a reason for hiding this comment

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

Then can you add a hasChildren test when we have the true response too ?

}

public function testCurrentCasting(): void
{
$this->builder->add('hungry', ChoiceType::class, [
'multiple' => true,
'expanded' => true,
'choices' => [
'Maybe' => null,
'Yes' => true,
'No' => false,
],
]);

$iterator = new FormBuilderIterator($this->builder->get('hungry'));
static::assertInstanceOf(FormBuilderInterface::class, $iterator->current());
}
}