Skip to content

Commit

Permalink
Add AdminInterface::createFieldDescription method
Browse files Browse the repository at this point in the history
This method will use the field description factory and also sets
the current admin to the field description.
  • Loading branch information
franmomu committed Feb 21, 2021
1 parent e866147 commit 11fa21f
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 98 deletions.
40 changes: 20 additions & 20 deletions src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2885,6 +2885,26 @@ final public function hasTemplateRegistry(): bool
return null !== $this->templateRegistry;
}

public function createFieldDescription(string $propertyName, array $options = []): FieldDescriptionInterface
{
$fieldDescriptionFactory = $this->getFieldDescriptionFactory();

// NEXT_MAJOR: Remove the "if" block and leave the "else" one.
if (null === $fieldDescriptionFactory) {
$fieldDescription = $this->getModelManager()->getNewFieldDescriptionInstance(
$this->getClass(),
$propertyName,
$options
);
} else {
$fieldDescription = $fieldDescriptionFactory->create($this->getClass(), $propertyName, $options);
}

$fieldDescription->setAdmin($this);

return $fieldDescription;
}

/**
* @phpstan-param T $object
*/
Expand Down Expand Up @@ -3344,26 +3364,6 @@ private function buildRoutes(): void
$extension->configureRoutes($this, $this->routes);
}
}

private function createFieldDescription(string $name, array $options = []): FieldDescriptionInterface
{
$fieldDescriptionFactory = $this->getFieldDescriptionFactory();

// NEXT_MAJOR: Remove the "if" block and leave the "else" one.
if (null === $fieldDescriptionFactory) {
$fieldDescription = $this->getModelManager()->getNewFieldDescriptionInstance(
$this->getClass(),
$name,
$options
);
} else {
$fieldDescription = $fieldDescriptionFactory->create($this->getClass(), $name, $options);
}

$fieldDescription->setAdmin($this);

return $fieldDescription;
}
}

class_exists(\Sonata\Form\Validator\ErrorElement::class);
4 changes: 4 additions & 0 deletions src/Admin/AdminInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* @method string|null getParentAssociationMapping()
* @method void reorderFormGroup(string $group, array $keys)
* @method void defineFormBuilder(FormBuilderInterface $formBuilder)
* @method FieldDescriptionInterface createFieldDescription(string $propertyName, array $options = [])
*
* @phpstan-template T of object
* @phpstan-extends AccessRegistryInterface<T>
Expand Down Expand Up @@ -789,6 +790,9 @@ public function getListMode();
// * the getFormBuilder is only call by the main admin class.
// */
// public function defineFormBuilder(FormBuilderInterface $formBuilder): void;

// NEXT_MAJOR: uncomment this method in 4.0
// public function createFieldDescription(string $propertyName, array $options = []): FieldDescriptionInterface;
}

class_exists(\Sonata\Form\Validator\ErrorElement::class);
18 changes: 13 additions & 5 deletions src/Datagrid/DatagridMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,19 @@ public function add(
$filterOptions['field_name'] = substr(strrchr('.'.$name, '.'), 1);
}

$fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
$this->admin->getClass(),
$name,
array_merge($filterOptions, $fieldDescriptionOptions)
);
// NEXT_MAJOR: Remove the check and use `createFieldDescription`.
if (method_exists($this->admin, 'createFieldDescription')) {
$fieldDescription = $this->admin->createFieldDescription(
$name,
array_merge($filterOptions, $fieldDescriptionOptions)
);
} else {
$fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
$this->admin->getClass(),
$name,
array_merge($filterOptions, $fieldDescriptionOptions)
);
}
} else {
throw new \TypeError(
'Unknown field name in datagrid mapper.'
Expand Down
18 changes: 13 additions & 5 deletions src/Datagrid/ListMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,19 @@ public function add($name, $type = null, array $fieldDescriptionOptions = [])
));
}

$fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
$this->admin->getClass(),
$name,
$fieldDescriptionOptions
);
// NEXT_MAJOR: Remove the check and use `createFieldDescription`.
if (method_exists($this->admin, 'createFieldDescription')) {
$fieldDescription = $this->admin->createFieldDescription(
$name,
$fieldDescriptionOptions
);
} else {
$fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
$this->admin->getClass(),
$name,
$fieldDescriptionOptions
);
}
} else {
throw new \TypeError(
'Unknown field name in list mapper.'
Expand Down
18 changes: 13 additions & 5 deletions src/Form/FormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,19 @@ public function add($name, $type = null, array $options = [], array $fieldDescri
$fieldDescriptionOptions['translation_domain'] = $group['translation_domain'];
}

$fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
$this->admin->getClass(),
$name instanceof FormBuilderInterface ? $name->getName() : $name,
$fieldDescriptionOptions
);
// NEXT_MAJOR: Remove the check and use `createFieldDescription`.
if (method_exists($this->admin, 'createFieldDescription')) {
$fieldDescription = $this->admin->createFieldDescription(
$name instanceof FormBuilderInterface ? $name->getName() : $name,
$fieldDescriptionOptions
);
} else {
$fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
$this->admin->getClass(),
$name instanceof FormBuilderInterface ? $name->getName() : $name,
$fieldDescriptionOptions
);
}

// Note that the builder var is actually the formContractor:
$this->builder->fixFieldDescription($this->admin, $fieldDescription);
Expand Down
19 changes: 14 additions & 5 deletions src/Show/ShowMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,20 @@ public function add($name, $type = null, array $fieldDescriptionOptions = [])
$fieldDescription->mergeOptions($fieldDescriptionOptions);
} elseif (\is_string($name)) {
if (!$this->admin->hasShowFieldDescription($name)) {
$fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
$this->admin->getClass(),
$name,
$fieldDescriptionOptions
);

// NEXT_MAJOR: Remove the check and use `createFieldDescription`.
if (method_exists($this->admin, 'createFieldDescription')) {
$fieldDescription = $this->admin->createFieldDescription(
$name,
$fieldDescriptionOptions
);
} else {
$fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
$this->admin->getClass(),
$name,
$fieldDescriptionOptions
);
}
} else {
throw new \LogicException(sprintf(
'Duplicate field name "%s" in show mapper. Names should be unique.',
Expand Down
35 changes: 20 additions & 15 deletions tests/Admin/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Sonata\AdminBundle\Datagrid\PagerInterface;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Exporter\DataSourceInterface;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionFactoryInterface;
use Sonata\AdminBundle\Filter\Persister\FilterPersisterInterface;
use Sonata\AdminBundle\Model\AuditManagerInterface;
use Sonata\AdminBundle\Model\ModelManagerInterface;
Expand Down Expand Up @@ -1693,11 +1694,11 @@ public function testFormAddPostSubmitEventForPreValidation(): void
->willReturn($this->createMock(MemberMetadata::class));
$modelAdmin->setValidator($validator);

$modelManager = $this->createMock(ModelManagerInterface::class);
$modelManager
->method('getNewFieldDescriptionInstance')
$fieldDescriptionFactory = $this->createStub(FieldDescriptionFactoryInterface::class);
$fieldDescriptionFactory
->method('create')
->willReturn(new FieldDescription('name'));
$modelAdmin->setModelManager($modelManager);
$modelAdmin->setFieldDescriptionFactory($fieldDescriptionFactory);

// a Admin class to test that preValidate is called
$testAdminPreValidate = $this->createMock(AbstractAdmin::class);
Expand Down Expand Up @@ -1763,11 +1764,11 @@ public function testCanAddInlineValidationOnlyForGenericMetadata(): void
->willReturn($metadata);
$modelAdmin->setValidator($validator);

$modelManager = $this->createStub(ModelManagerInterface::class);
$modelManager
->method('getNewFieldDescriptionInstance')
$fieldDescriptionFactory = $this->createStub(FieldDescriptionFactoryInterface::class);
$fieldDescriptionFactory
->method('create')
->willReturn(new FieldDescription('name'));
$modelAdmin->setModelManager($modelManager);
$modelAdmin->setFieldDescriptionFactory($fieldDescriptionFactory);

$event = $this->createStub(FormEvent::class);
$event
Expand Down Expand Up @@ -1902,13 +1903,10 @@ public function testGetFilterFieldDescription(): void
$barFieldDescription = new FieldDescription('bar');
$bazFieldDescription = new FieldDescription('baz');

$modelManager = $this->createMock(ModelManagerInterface::class);
$modelManager
->method('getDefaultSortValues')
->willReturn([]);

$modelManager->expects($this->exactly(3))
->method('getNewFieldDescriptionInstance')
$fieldDescriptionFactory = $this->createMock(FieldDescriptionFactoryInterface::class);
$fieldDescriptionFactory
->expects($this->exactly(3))
->method('create')
->willReturnCallback(static function ($adminClass, string $name, $filterOptions) use ($fooFieldDescription, $barFieldDescription, $bazFieldDescription) {
switch ($name) {
case 'foo':
Expand Down Expand Up @@ -1936,6 +1934,13 @@ public function testGetFilterFieldDescription(): void
return $fieldDescription;
});

$modelAdmin->setFieldDescriptionFactory($fieldDescriptionFactory);

$modelManager = $this->createStub(ModelManagerInterface::class);
$modelManager
->method('getDefaultSortValues')
->willReturn([]);

$modelAdmin->setModelManager($modelManager);

$pager = $this->createMock(PagerInterface::class);
Expand Down
18 changes: 7 additions & 11 deletions tests/Datagrid/DatagridMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Filter\Filter;
use Sonata\AdminBundle\Filter\FilterInterface;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilder;
Expand Down Expand Up @@ -60,7 +59,10 @@ protected function setUp(): void

$this->datagrid = new Datagrid($proxyQuery, $fieldDescriptionCollection, $pager, $formBuilder, []);

$admin = $this->createMock(AdminInterface::class);
// NEXT_MAJOR: Change to $this->createStub(AdminInterface::class).
$admin = $this->getMockBuilder(AdminInterface::class)
->addMethods(['createFieldDescription'])
->getMockForAbstractClass();

$datagridBuilder
->method('addFilter')
Expand All @@ -82,21 +84,15 @@ protected function setUp(): void
$datagrid->addFilter($filter);
});

$modelManager = $this->createMock(ModelManagerInterface::class);

$modelManager
->method('getNewFieldDescriptionInstance')
->willReturnCallback(function (?string $class, string $name, array $options = []): BaseFieldDescription {
$admin
->method('createFieldDescription')
->willReturnCallback(function (string $name, array $options = []): FieldDescriptionInterface {
$fieldDescription = $this->getFieldDescriptionMock($name);
$fieldDescription->setOptions($options);

return $fieldDescription;
});

$admin
->method('getModelManager')
->willReturn($modelManager);

$admin
->method('isGranted')
->willReturnCallback(static function (string $name, ?object $object = null): bool {
Expand Down
13 changes: 3 additions & 10 deletions tests/Datagrid/ListMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\Builder\ListBuilderInterface;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy;

/**
Expand Down Expand Up @@ -64,21 +63,15 @@ protected function setUp(): void
$list->add($fieldDescription);
});

$modelManager = $this->createMock(ModelManagerInterface::class);

$modelManager
->method('getNewFieldDescriptionInstance')
->willReturnCallback(function (?string $class, string $name, array $options = []): BaseFieldDescription {
$this->admin
->method('createFieldDescription')
->willReturnCallback(function (string $name, array $options = []): FieldDescriptionInterface {
$fieldDescription = $this->getFieldDescriptionMock($name);
$fieldDescription->setOptions($options);

return $fieldDescription;
});

$this->admin
->method('getModelManager')
->willReturn($modelManager);

$labelTranslatorStrategy = new NoopLabelTranslatorStrategy();

$this->admin
Expand Down
13 changes: 7 additions & 6 deletions tests/Form/FormMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\BaseFieldDescription;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\Builder\FormContractorInterface;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionFactoryInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
Expand Down Expand Up @@ -91,18 +93,17 @@ protected function setUp(): void
$this->admin->setSecurityHandler($securityHandler);
$this->admin->setFormContractor($this->contractor);

$this->modelManager = $this->getMockForAbstractClass(ModelManagerInterface::class);

$this->modelManager
->method('getNewFieldDescriptionInstance')
->willReturnCallback(function (string $class, string $name, array $options = []): BaseFieldDescription {
$fieldDescriptionFactory = $this->createStub(FieldDescriptionFactoryInterface::class);
$fieldDescriptionFactory
->method('create')
->willReturnCallback(function (string $class, string $name, array $options = []): FieldDescriptionInterface {
$fieldDescription = $this->getFieldDescriptionMock($name);
$fieldDescription->setOptions($options);

return $fieldDescription;
});

$this->admin->setModelManager($this->modelManager);
$this->admin->setFieldDescriptionFactory($fieldDescriptionFactory);

$labelTranslatorStrategy = $this->getMockForAbstractClass(LabelTranslatorStrategyInterface::class);
$this->admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
Expand Down
Loading

0 comments on commit 11fa21f

Please sign in to comment.