Skip to content

Commit

Permalink
Remove Pool dependency of adminHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Aug 22, 2020
1 parent 20b6cc5 commit a3f15b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
3 changes: 3 additions & 0 deletions UPGRADE-4.0.md
Expand Up @@ -128,6 +128,9 @@ If you have implemented a custom admin extension, you must adapt the signature o
* `configureBatchActions`
* `getAccessMapping`

## AdminHelper
The `AdminHelper::__construct` method change the required `Pool` param to an optional `PropertyAccessorInterface` one.

## BreadcrumbsBuilder
The `buildBreacrumbs` method may no longer be called from outside the class.

Expand Down
20 changes: 9 additions & 11 deletions src/Admin/AdminHelper.php
Expand Up @@ -22,6 +22,8 @@
use Sonata\AdminBundle\Util\FormViewIterator;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;

/**
* @final since sonata-project/admin-bundle 3.52
Expand All @@ -36,13 +38,13 @@ class AdminHelper
private const FORM_FIELD_DELETE = '_delete';

/**
* @var Pool
* @var PropertyAccessorInterface
*/
protected $pool;
protected $propertyAccessor;

public function __construct(Pool $pool)
public function __construct(?PropertyAccessorInterface $propertyAccessor = null)
{
$this->pool = $pool;
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
}

/**
Expand Down Expand Up @@ -111,11 +113,9 @@ public function appendFormFieldElement(AdminInterface $admin, object $subject, s
//Child form not found (probably nested one)
//if childFormBuilder was not found resulted in fatal error getName() method call on non object
if (!$childFormBuilder) {
$propertyAccessor = $this->pool->getPropertyAccessor();

$path = $this->getElementAccessPath($elementId, $subject);

$collection = $propertyAccessor->getValue($subject, $path);
$collection = $this->propertyAccessor->getValue($subject, $path);

if ($collection instanceof DoctrinePersistentCollection || $collection instanceof PersistentCollection) {
//since doctrine 2.4
Expand All @@ -127,7 +127,7 @@ public function appendFormFieldElement(AdminInterface $admin, object $subject, s
}

$collection->add(new $modelClassName());
$propertyAccessor->setValue($subject, $path, $collection);
$this->propertyAccessor->setValue($subject, $path, $collection);

$fieldDescription = null;
} else {
Expand Down Expand Up @@ -195,8 +195,6 @@ public function appendFormFieldElement(AdminInterface $admin, object $subject, s
*/
public function getElementAccessPath(string $elementId, $model): string
{
$propertyAccessor = $this->pool->getPropertyAccessor();

$idWithoutIdentifier = preg_replace('/^[^_]*_/', '', $elementId);
$initialPath = preg_replace('#(_(\d+)_)#', '[$2]_', $idWithoutIdentifier);

Expand All @@ -208,7 +206,7 @@ public function getElementAccessPath(string $elementId, $model): string
$currentPath .= empty($currentPath) ? $part : '_'.$part;
$separator = empty($totalPath) ? '' : '.';

if ($propertyAccessor->isReadable($model, $totalPath.$separator.$currentPath)) {
if ($this->propertyAccessor->isReadable($model, $totalPath.$separator.$currentPath)) {
$totalPath .= $separator.$currentPath;
$currentPath = '';
}
Expand Down
19 changes: 3 additions & 16 deletions tests/Admin/AdminHelperTest.php
Expand Up @@ -17,10 +17,8 @@
use Sonata\AdminBundle\Admin\AdminHelper;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Tests\Fixtures\Entity\Bar;
use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
Expand All @@ -30,7 +28,6 @@
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PropertyAccess\PropertyAccessorBuilder;

class AdminHelperTest extends TestCase
{
Expand All @@ -41,10 +38,7 @@ class AdminHelperTest extends TestCase

protected function setUp(): void
{
$container = new Container();

$pool = new Pool($container, 'title', 'logo.png');
$this->helper = new AdminHelper($pool);
$this->helper = new AdminHelper();
}

public function testGetChildFormBuilder(): void
Expand Down Expand Up @@ -115,13 +109,6 @@ public function testItThrowsExceptionWhenDoesNotFindTheFullPath(): void

public function testAppendFormFieldElement(): void
{
$container = new Container();

$propertyAccessorBuilder = new PropertyAccessorBuilder();
$propertyAccessor = $propertyAccessorBuilder->getPropertyAccessor();
$pool = new Pool($container, 'title', 'logo.png', [], $propertyAccessor);
$helper = new AdminHelper($pool);

$admin = $this->createMock(AdminInterface::class);
$admin
->method('getClass')
Expand Down Expand Up @@ -211,7 +198,7 @@ public function testAppendFormFieldElement(): void
$associationAdmin->expects($this->atLeastOnce())->method('setSubject')->with($bar);
$admin->method('getFormBuilder')->willReturn($formBuilder);

$finalForm = $helper->appendFormFieldElement($admin, $foo, 'test_bar')[1];
$finalForm = $this->helper->appendFormFieldElement($admin, $foo, 'test_bar')[1];

foreach ($finalForm->get($childFormBuilder->getName()) as $childField) {
$this->assertFalse($childField->has('_delete'));
Expand All @@ -220,7 +207,7 @@ public function testAppendFormFieldElement(): void
$deleteFormBuilder = new FormBuilder('_delete', null, $eventDispatcher, $formFactory);
$subChildFormBuilder->add($deleteFormBuilder, CheckboxType::class, ['delete' => false]);

$finalForm = $helper->appendFormFieldElement($admin, $foo, 'test_bar')[1];
$finalForm = $this->helper->appendFormFieldElement($admin, $foo, 'test_bar')[1];

foreach ($finalForm->get($childFormBuilder->getName()) as $childField) {
$this->assertTrue($childField->has('_delete'));
Expand Down

0 comments on commit a3f15b3

Please sign in to comment.