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

Remove deprecations constructing FieldDescription #459

Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"doctrine/mongodb-odm": "^2.1",
"doctrine/mongodb-odm-bundle": "^4.0",
"doctrine/persistence": "^1.3.4 || ^2.0",
"sonata-project/admin-bundle": "^3.75",
"sonata-project/admin-bundle": "^3.78",
"sonata-project/form-extensions": "^0.1 || ^1.4",
"symfony/config": "^4.4",
"symfony/dependency-injection": "^4.4",
Expand Down
5 changes: 0 additions & 5 deletions src/Admin/FieldDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
*/
class FieldDescription extends BaseFieldDescription
{
public function __construct()
{
$this->parentAssociationMappings = [];
}

public function setAssociationMapping($associationMapping)
{
if (!\is_array($associationMapping)) {
Expand Down
4 changes: 1 addition & 3 deletions src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ public function getNewFieldDescriptionInstance($class, $name, array $options = [

[$metadata, $propertyName, $parentAssociationMappings] = $this->getParentMetadataForProperty($class, $name);

$fieldDescription = new FieldDescription();
$fieldDescription->setName($name);
Copy link
Member

Choose a reason for hiding this comment

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

You can remove this line and the next one

$fieldDescription->setOptions($options);
$fieldDescription = new FieldDescription($name, $options);
$fieldDescription->setParentAssociationMappings($parentAssociationMappings);

/* @var ClassMetadata */
Expand Down
43 changes: 21 additions & 22 deletions tests/Admin/FieldDescriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FieldDescriptionTest extends TestCase
{
public function testOptions(): void
{
$field = new FieldDescription();
$field = new FieldDescription('name');
$field->setOptions([
'template' => 'foo',
'type' => 'bar',
Expand Down Expand Up @@ -87,7 +87,7 @@ public function testOptions(): void

public function testAssociationMapping(): void
{
$field = new FieldDescription();
$field = new FieldDescription('name');
$field->setAssociationMapping([
'type' => 'integer',
'fieldName' => 'position',
Expand All @@ -112,23 +112,22 @@ public function testAssociationMapping(): void

public function testSetName(): void
{
$field = new FieldDescription();
$field = new FieldDescription('name');
$field->setName('New field description name');

$this->assertSame($field->getName(), 'New field description name');
}

public function testSetNameSetFieldNameToo(): void
{
$field = new FieldDescription();
$field->setName('New field description name');
$field = new FieldDescription('New field description name');

$this->assertSame($field->getFieldName(), 'New field description name');
}

public function testSetNameDoesNotSetFieldNameWhenSetBefore(): void
{
$field = new FieldDescription();
$field = new FieldDescription('name');
$field->setFieldName('field name');
$field->setName('New field description name');

Expand All @@ -138,7 +137,7 @@ public function testSetNameDoesNotSetFieldNameWhenSetBefore(): void
public function testGetParent(): void
{
$adminMock = $this->createMock(AdminInterface::class);
$field = new FieldDescription();
$field = new FieldDescription('name');
$field->setParent($adminMock);

$this->assertSame($adminMock, $field->getParent());
Expand All @@ -147,7 +146,7 @@ public function testGetParent(): void
public function testGetAdmin(): void
{
$adminMock = $this->createMock(AdminInterface::class);
$field = new FieldDescription();
$field = new FieldDescription('name');
$field->setAdmin($adminMock);

$this->assertSame($adminMock, $field->getAdmin());
Expand All @@ -160,7 +159,7 @@ public function testGetAssociationAdmin(): void
->method('setParentFieldDescription')
->with($this->isInstanceOf(FieldDescriptionInterface::class));

$field = new FieldDescription();
$field = new FieldDescription('name');
$field->setAssociationAdmin($adminMock);

$this->assertSame($adminMock, $field->getAssociationAdmin());
Expand All @@ -173,7 +172,7 @@ public function testHasAssociationAdmin(): void
->method('setParentFieldDescription')
->with($this->isInstanceOf(FieldDescriptionInterface::class));

$field = new FieldDescription();
$field = new FieldDescription('name');

$this->assertFalse($field->hasAssociationAdmin());

Expand All @@ -191,7 +190,7 @@ public function myMethod()
}
};

$field = new FieldDescription();
$field = new FieldDescription('name');
$field->setOption('code', 'myMethod');

$this->assertSame($field->getValue($object), 'myMethodValue');
Expand All @@ -206,7 +205,7 @@ public function myMethod()
}
};

$field = new FieldDescription();
$field = new FieldDescription('name');

$this->expectException(NoValueException::class);

Expand All @@ -220,7 +219,7 @@ public function testGetAssociationMapping(): void
'fieldName' => 'position',
];

$field = new FieldDescription();
$field = new FieldDescription('name');
$field->setAssociationMapping($assocationMapping);

$this->assertSame($assocationMapping, $field->getAssociationMapping());
Expand All @@ -230,15 +229,15 @@ public function testSetAssociationMappingAllowOnlyForArray(): void
{
$this->expectException(\RuntimeException::class);

$field = new FieldDescription();
$field = new FieldDescription('name');
$field->setAssociationMapping('test');
}

public function testSetFieldMappingAllowOnlyForArray(): void
{
$this->expectException(\RuntimeException::class);

$field = new FieldDescription();
$field = new FieldDescription('name');
$field->setFieldMapping('test');
}

Expand All @@ -249,7 +248,7 @@ public function testSetFieldMappingSetType(): void
'fieldName' => 'position',
];

$field = new FieldDescription();
$field = new FieldDescription('name');
$field->setFieldMapping($fieldMapping);

$this->assertSame('integer', $field->getType());
Expand All @@ -262,7 +261,7 @@ public function testSetFieldMappingSetMappingType(): void
'fieldName' => 'position',
];

$field = new FieldDescription();
$field = new FieldDescription('name');
$field->setFieldMapping($fieldMapping);

$this->assertSame('integer', $field->getMappingType());
Expand All @@ -275,7 +274,7 @@ public function testSetFieldMappingSetFieldName(): void
'fieldName' => 'position',
];

$field = new FieldDescription();
$field = new FieldDescription('position');
$field->setFieldMapping($fieldMapping);

$this->assertSame('position', $field->getFieldName());
Expand All @@ -294,7 +293,7 @@ public function testGetTargetEntity(): void
'targetDocument' => 'someValue',
];

$field = new FieldDescription();
$field = new FieldDescription('name');

$this->assertNull($field->getTargetEntity());

Expand All @@ -311,7 +310,7 @@ public function testGetTargetModel(): void
'targetDocument' => 'someValue',
];

$field = new FieldDescription();
$field = new FieldDescription('name');

$this->assertNull($field->getTargetModel());

Expand All @@ -328,7 +327,7 @@ public function testIsIdentifierFromFieldMapping(): void
'id' => true,
];

$field = new FieldDescription();
$field = new FieldDescription('name');
$field->setFieldMapping($fieldMapping);

$this->assertTrue($field->isIdentifier());
Expand All @@ -342,7 +341,7 @@ public function testGetFieldMapping(): void
'id' => 'someId',
];

$field = new FieldDescription();
$field = new FieldDescription('name');
$field->setFieldMapping($fieldMapping);

$this->assertSame($fieldMapping, $field->getFieldMapping());
Expand Down
12 changes: 4 additions & 8 deletions tests/Builder/DatagridBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ public function testFixFieldDescription(): void
{
$classMetadata = $this->getMetadataForDocumentWithAnnotations(DocumentWithReferences::class);

$fieldDescription = new FieldDescription();
$fieldDescription->setName('name');
$fieldDescription = new FieldDescription('name');
$fieldDescription->setFieldMapping($classMetadata->fieldMappings['name']);

$this->modelManager->method('hasMetadata')->willReturn(true);
Expand All @@ -134,8 +133,7 @@ public function testFixFieldDescriptionWithAssociationMapping(): void
{
$classMetadata = $this->getMetadataForDocumentWithAnnotations(DocumentWithReferences::class);

$fieldDescription = new FieldDescription();
$fieldDescription->setName('associatedDocument');
$fieldDescription = new FieldDescription('associatedDocument');
$fieldDescription->setMappingType(ClassMetadata::ONE);
$fieldDescription->setFieldMapping($classMetadata->fieldMappings['associatedDocument']);
$fieldDescription->setAssociationMapping($classMetadata->associationMappings['associatedDocument']);
Expand Down Expand Up @@ -170,8 +168,7 @@ public function testAddFilterNoType(): void
],
], Guess::VERY_HIGH_CONFIDENCE);

$fieldDescription = new FieldDescription();
$fieldDescription->setName('test');
$fieldDescription = new FieldDescription('test');

$this->typeGuesser->method('guessType')->willReturn($guessType);

Expand Down Expand Up @@ -214,8 +211,7 @@ public function testAddFilterWithType(): void

$datagrid = $this->createMock(DatagridInterface::class);

$fieldDescription = new FieldDescription();
$fieldDescription->setName('test');
$fieldDescription = new FieldDescription('test');

$this->filterFactory->method('create')->willReturn(new ModelFilter());

Expand Down
6 changes: 2 additions & 4 deletions tests/Builder/FormContractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,8 @@ public function testFixFieldDescriptionForFieldMapping(): void
$admin = $this->createMock(AdminInterface::class);
$admin->method('getModelManager')->willReturn($modelManager);

$fieldDescription = new FieldDescription();
$fieldDescription = new FieldDescription('name');
$fieldDescription->setMappingType(ClassMetadata::ONE);
$fieldDescription->setName('name');
$fieldDescription->setFieldMapping($classMetadata->fieldMappings['name']);

$this->formContractor->fixFieldDescription($admin, $fieldDescription);
Expand All @@ -182,9 +181,8 @@ public function testFixFieldDescriptionForAssociationMapping(): void
$admin = $this->createMock(AdminInterface::class);
$admin->method('getModelManager')->willReturn($modelManager);

$fieldDescription = new FieldDescription();
$fieldDescription = new FieldDescription('associatedDocument');
$fieldDescription->setMappingType(ClassMetadata::ONE);
$fieldDescription->setName('associatedDocument');
$fieldDescription->setFieldMapping($classMetadata->fieldMappings['associatedDocument']);
$fieldDescription->setAssociationMapping($classMetadata->associationMappings['associatedDocument']);

Expand Down
17 changes: 6 additions & 11 deletions tests/Builder/ListBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ protected function setUp(): void

public function testAddListActionField(): void
{
$fieldDescription = new FieldDescription();
$fieldDescription->setName('foo');
$fieldDescription = new FieldDescription('foo');

$list = $this->listBuilder->getBaseList();

Expand All @@ -97,8 +96,7 @@ public function testCorrectFixedActionsFieldType(): void
new TypeGuess('actions', [], Guess::LOW_CONFIDENCE)
);

$fieldDescription = new FieldDescription();
$fieldDescription->setName('_action');
$fieldDescription = new FieldDescription('_action');

$list = $this->listBuilder->getBaseList();

Expand All @@ -119,8 +117,7 @@ public function testFixFieldDescriptionWithFieldMapping(): void
{
$classMetadata = $this->getMetadataForDocumentWithAnnotations(DocumentWithReferences::class);

$fieldDescription = new FieldDescription();
$fieldDescription->setName('name');
$fieldDescription = new FieldDescription('name');
$fieldDescription->setOption('sortable', true);
$fieldDescription->setType('string');
$fieldDescription->setFieldMapping($classMetadata->fieldMappings['name']);
Expand All @@ -142,8 +139,7 @@ public function testFixFieldDescriptionWithAssociationMapping(string $type, stri
{
$classMetadata = $this->getMetadataForDocumentWithAnnotations(DocumentWithReferences::class);

$fieldDescription = new FieldDescription();
$fieldDescription->setName('associatedDocument');
$fieldDescription = new FieldDescription('associatedDocument');
$fieldDescription->setOption('sortable', true);
$fieldDescription->setType($type);
$fieldDescription->setMappingType($type);
Expand Down Expand Up @@ -184,8 +180,7 @@ public function fixFieldDescriptionData(): array
public function testFixFieldDescriptionFixesType(string $expectedType, string $type): void
{
$this->modelManager->method('hasMetadata')->willReturn(false);
$fieldDescription = new FieldDescription();
$fieldDescription->setName('test');
$fieldDescription = new FieldDescription('test');
$fieldDescription->setType($type);

$this->listBuilder->fixFieldDescription($this->admin, $fieldDescription);
Expand All @@ -204,6 +199,6 @@ public function fixFieldDescriptionTypes(): array
public function testFixFieldDescriptionException(): void
{
$this->expectException(\RuntimeException::class);
$this->listBuilder->fixFieldDescription($this->admin, new FieldDescription());
$this->listBuilder->fixFieldDescription($this->admin, new FieldDescription('name'));
}
}
14 changes: 5 additions & 9 deletions tests/Builder/ShowBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public function testAddFieldNoType(): void
{
$typeGuess = $this->createStub(TypeGuess::class);

$fieldDescription = new FieldDescription();
$fieldDescription->setName('FakeName');
$fieldDescription = new FieldDescription('FakeName');
$fieldDescription->setMappingType(ClassMetadata::ONE);

$this->admin->expects($this->once())->method('attachAdminClass');
Expand All @@ -101,8 +100,7 @@ public function testAddFieldNoType(): void

public function testAddFieldWithType(): void
{
$fieldDescription = new FieldDescription();
$fieldDescription->setName('FakeName');
$fieldDescription = new FieldDescription('FakeName');

$this->admin->expects($this->once())->method('addShowFieldDescription');

Expand All @@ -125,8 +123,7 @@ public function testFixFieldDescription(string $type, string $mappingType, strin
{
$classMetadata = $this->getMetadataForDocumentWithAnnotations(DocumentWithReferences::class);

$fieldDescription = new FieldDescription();
$fieldDescription->setName('name');
$fieldDescription = new FieldDescription('name');
$fieldDescription->setType($type);
$fieldDescription->setMappingType($mappingType);
$fieldDescription->setFieldMapping($classMetadata->fieldMappings['name']);
Expand Down Expand Up @@ -163,8 +160,7 @@ public function fixFieldDescriptionData(): iterable
*/
public function testFixFieldDescriptionFixesType(string $expectedType, string $type): void
{
$fieldDescription = new FieldDescription();
$fieldDescription->setName('FakeName');
$fieldDescription = new FieldDescription('FakeName');
$fieldDescription->setType($type);

$this->showBuilder->fixFieldDescription($this->admin, $fieldDescription);
Expand All @@ -183,6 +179,6 @@ public function fixFieldDescriptionTypes(): iterable
public function testFixFieldDescriptionException(): void
{
$this->expectException(\RuntimeException::class);
$this->showBuilder->fixFieldDescription($this->admin, new FieldDescription());
$this->showBuilder->fixFieldDescription($this->admin, new FieldDescription('name'));
}
}
Loading