Skip to content

Commit

Permalink
Deprecate passing other type than string|null as prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu authored and greg0ire committed Dec 11, 2020
1 parent cf1ec4d commit fd63992
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/Util/FormBuilderIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,20 @@ class FormBuilderIterator extends \RecursiveArrayIterator
*
* @param string|false $prefix
*/
public function __construct(FormBuilderInterface $formBuilder, $prefix = false)
public function __construct(FormBuilderInterface $formBuilder, $prefix = null)
{
parent::__construct();
$this->formBuilder = $formBuilder;

// NEXT_MAJOR: Remove this block.
if (null !== $prefix && !\is_string($prefix)) {
@trigger_error(sprintf(
'Passing other type than string or null as argument 2 for method %s() is deprecated since'
.' sonata-project/admin-bundle 3.x. It will accept only string and null in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);
}

// NEXT_MAJOR: Remove next line.
$this->prefix = \is_string($prefix) ? $prefix : $formBuilder->getName();
// NEXT_MAJOR: Uncomment next line.
Expand Down
4 changes: 2 additions & 2 deletions tests/Admin/AdminHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public function testGetChildFormBuilder(): void

public function testGetGrandChildFormBuilder(): void
{
$formFactory = $this->createMock(FormFactoryInterface::class);
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$formFactory = $this->createStub(FormFactoryInterface::class);
$eventDispatcher = $this->createStub(EventDispatcherInterface::class);

$formBuilder = new FormBuilder('parent', \stdClass::class, $eventDispatcher, $formFactory);
$childFormBuilder = new FormBuilder('child', \stdClass::class, $eventDispatcher, $formFactory);
Expand Down
18 changes: 18 additions & 0 deletions tests/Util/FormBuilderIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Util\FormBuilderIterator;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilder;
Expand All @@ -25,6 +26,8 @@
*/
class FormBuilderIteratorTest extends TestCase
{
use ExpectDeprecationTrait;

/**
* @var EventDispatcherInterface
*/
Expand Down Expand Up @@ -69,4 +72,19 @@ public function testHasChildren(): void
$iterator = new FormBuilderIterator($this->builder);
$this->assertTrue($iterator->hasChildren());
}

/**
* NEXT_MAJOR: Remove this test.
*
* @group legacy
*/
public function testTriggersADeprecationWithWrongPrefixType(): void
{
$this->expectDeprecation('Passing other type than string or null as argument 2 for method Sonata\AdminBundle\Util\FormBuilderIterator::__construct() is deprecated since sonata-project/admin-bundle 3.x. It will accept only string and null in version 4.0.');

$this->builder->add('name', TextType::class);
$iterator = new FormBuilderIterator($this->builder, new \stdClass());

$this->assertSame($iterator->key(), 'name_name');
}
}

0 comments on commit fd63992

Please sign in to comment.