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

Support doctrine/mongodb-odm ^2.x & doctrine/mongodb-odm-bundle ^4.x #321

Merged
merged 6 commits into from
Oct 15, 2019
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
],
"require": {
"php": "^7.1",
"doctrine/mongodb-odm": "^1.0",
"doctrine/mongodb-odm-bundle": "^3.0",
"doctrine/mongodb-odm": "^1.0 || ^2.0",
"doctrine/mongodb-odm-bundle": "^3.0 || ^4.0",
"sonata-project/admin-bundle": "^3.31",
"sonata-project/core-bundle": "^3.8",
"symfony/config": "^2.8 || ^3.2 || ^4.0",
Expand Down
6 changes: 3 additions & 3 deletions src/Builder/FormContractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sonata\DoctrineMongoDBAdminBundle\Builder;

use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
Copy link
Member

Choose a reason for hiding this comment

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

This class exists in odm 1.x and 2.x. You could support both version inside the composer.json file

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\Builder\FormContractorInterface;
Expand Down Expand Up @@ -74,7 +74,7 @@ public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInter
$fieldDescription->setAdmin($admin);
$fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));

if (\in_array($fieldDescription->getMappingType(), [ClassMetadataInfo::ONE, ClassMetadataInfo::MANY], true)) {
if (\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) {
$admin->attachAdminClass($fieldDescription);
}
}
Expand Down Expand Up @@ -142,7 +142,7 @@ public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescrip
));
}

if (!\in_array($fieldDescription->getMappingType(), [ClassMetadataInfo::ONE, ClassMetadataInfo::MANY], true)) {
if (!\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) {
throw new \RuntimeException(sprintf(
'You are trying to add `sonata_type_admin` field `%s` which is not One-To-One or Many-To-One.'
.' Maybe you want `sonata_type_collection` instead?',
Expand Down
8 changes: 4 additions & 4 deletions src/Builder/ListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sonata\DoctrineMongoDBAdminBundle\Builder;

use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
Expand Down Expand Up @@ -118,17 +118,17 @@ public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInter
$template = $this->getTemplate($fieldDescription->getType());

if (null === $template) {
if (ClassMetadataInfo::ONE === $fieldDescription->getMappingType()) {
if (ClassMetadata::ONE === $fieldDescription->getMappingType()) {
$template = '@SonataAdmin/CRUD/Association/list_many_to_one.html.twig';
} elseif (ClassMetadataInfo::MANY === $fieldDescription->getMappingType()) {
} elseif (ClassMetadata::MANY === $fieldDescription->getMappingType()) {
$template = '@SonataAdmin/CRUD/Association/list_many_to_many.html.twig';
}
}

$fieldDescription->setTemplate($template);
}

if (\in_array($fieldDescription->getMappingType(), [ClassMetadataInfo::ONE, ClassMetadataInfo::MANY], true)) {
if (\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) {
$admin->attachAdminClass($fieldDescription);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Builder/ShowBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sonata\DoctrineMongoDBAdminBundle\Builder;

use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
Expand Down Expand Up @@ -101,17 +101,17 @@ public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInter
$template = $this->getTemplate($fieldDescription->getType());

if (null === $template) {
if (ClassMetadataInfo::ONE === $fieldDescription->getMappingType()) {
if (ClassMetadata::ONE === $fieldDescription->getMappingType()) {
$template = '@SonataAdmin/CRUD/Association/show_many_to_one.html.twig';
} elseif (ClassMetadataInfo::MANY === $fieldDescription->getMappingType()) {
} elseif (ClassMetadata::MANY === $fieldDescription->getMappingType()) {
$template = '@SonataAdmin/CRUD/Association/show_many_to_many.html.twig';
}
}

$fieldDescription->setTemplate($template);
}

if (\in_array($fieldDescription->getMappingType(), [ClassMetadataInfo::ONE, ClassMetadataInfo::MANY], true)) {
if (\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) {
$admin->attachAdminClass($fieldDescription);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Filter/AbstractDateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
return $this->applyTypeIsEqual($queryBuilder, $field, $data);

case DateType::TYPE_GREATER_THAN:
if (!array_key_exists('value', $data) || !$data['value']) {
if (!\array_key_exists('value', $data) || !$data['value']) {
return;
}

return $this->applyTypeIsGreaterThan($queryBuilder, $field, $data);

case DateType::TYPE_LESS_EQUAL:
if (!array_key_exists('value', $data) || !$data['value']) {
if (!\array_key_exists('value', $data) || !$data['value']) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Filter/BooleanFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class BooleanFilter extends Filter
*/
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
{
if (!$data || !\is_array($data) || !array_key_exists('type', $data) || !array_key_exists('value', $data)) {
if (!$data || !\is_array($data) || !\array_key_exists('type', $data) || !\array_key_exists('value', $data)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Filter/ChoiceFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ChoiceFilter extends Filter
*/
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
{
if (!$data || !\is_array($data) || !array_key_exists('type', $data) || !array_key_exists('value', $data)) {
if (!$data || !\is_array($data) || !\array_key_exists('type', $data) || !\array_key_exists('value', $data)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Filter/ModelFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ModelFilter extends Filter
*/
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
{
if (!$data || !\is_array($data) || !array_key_exists('value', $data)) {
if (!$data || !\is_array($data) || !\array_key_exists('value', $data)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Filter/NumberFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class NumberFilter extends Filter
*/
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
{
if (!$data || !\is_array($data) || !array_key_exists('value', $data) || !is_numeric($data['value'])) {
if (!$data || !\is_array($data) || !\array_key_exists('value', $data) || !is_numeric($data['value'])) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Filter/StringFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class StringFilter extends Filter
*/
public function filter(ProxyQueryInterface $queryBuilder, $name, $field, $data)
{
if (!$data || !\is_array($data) || !array_key_exists('value', $data) || null === $data['value']) {
if (!$data || !\is_array($data) || !\array_key_exists('value', $data) || null === $data['value']) {
return;
}

Expand Down
11 changes: 4 additions & 7 deletions src/Guesser/FilterTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Sonata\DoctrineMongoDBAdminBundle\Guesser;

use Doctrine\Bundle\MongoDBBundle\Form\Type\DocumentType;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\CoreBundle\Form\Type\BooleanType;
use Sonata\CoreBundle\Form\Type\EqualType;
Expand Down Expand Up @@ -49,11 +49,8 @@ public function guessType($class, $property, ModelManagerInterface $modelManager
$mapping = $metadata->fieldMappings[$propertyName];

switch ($mapping['type']) {
case ClassMetadataInfo::ONE:
case ClassMetadataInfo::MANY:
//case ClassMetadataInfo::MANY_TO_ONE:
//case ClassMetadataInfo::MANY_TO_MANY:

case ClassMetadata::ONE:
case ClassMetadata::MANY:
$options['operator_type'] = EqualType::class;
$options['operator_options'] = [];

Expand All @@ -69,7 +66,7 @@ public function guessType($class, $property, ModelManagerInterface $modelManager
}
}

if (!array_key_exists($propertyName, $metadata->fieldMappings)) {
if (!\array_key_exists($propertyName, $metadata->fieldMappings)) {
throw new MissingPropertyMetadataException($class, $property);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Guesser/TypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sonata\DoctrineMongoDBAdminBundle\Guesser;

use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
Expand All @@ -35,10 +35,10 @@ public function guessType($class, $property, ModelManagerInterface $modelManager
$mapping = $metadata->fieldMappings[$propertyName];

switch ($mapping['type']) {
case ClassMetadataInfo::ONE:
case ClassMetadata::ONE:
return new TypeGuess('mongo_one', [], Guess::HIGH_CONFIDENCE);

case ClassMetadataInfo::MANY:
case ClassMetadata::MANY:
return new TypeGuess('mongo_many', [], Guess::HIGH_CONFIDENCE);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,10 @@ public function modelReverseTransform($class, array $array = [])
foreach ($array as $name => $value) {
$reflection_property = false;
// property or association ?
if (array_key_exists($name, $metadata->fieldMappings)) {
if (\array_key_exists($name, $metadata->fieldMappings)) {
$property = $metadata->fieldMappings[$name]['fieldName'];
$reflection_property = $metadata->reflFields[$name];
} elseif (array_key_exists($name, $metadata->associationMappings)) {
} elseif (\array_key_exists($name, $metadata->associationMappings)) {
$property = $metadata->associationMappings[$name]['fieldName'];
} else {
$property = $name;
Expand Down
4 changes: 2 additions & 2 deletions tests/Admin/FieldDescriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function testGetValue()
->getMock();
$mockedObject->expects($this->once())
->method('myMethod')
->will($this->returnValue('myMethodValue'));
->willReturn('myMethodValue');

$field = new FieldDescription();
$field->setOption('code', 'myMethod');
Expand All @@ -218,7 +218,7 @@ public function testGetValueWhenCannotRetrieve()
->getMock();
$mockedObject->expects($this->never())
->method('myMethod')
->will($this->returnValue('myMethodValue'));
->willReturn('myMethodValue');

$field = new FieldDescription();

Expand Down
6 changes: 3 additions & 3 deletions tests/Builder/FormContractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Builder;

use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
Expand Down Expand Up @@ -107,7 +107,7 @@ public function testDefaultOptionsForSonataFormTypes()
}

// admin type
$fieldDescription->method('getMappingType')->willReturn(ClassMetadataInfo::ONE);
$fieldDescription->method('getMappingType')->willReturn(ClassMetadata::ONE);
foreach ($adminTypes as $formType) {
$options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
$this->assertSame($fieldDescription, $options['sonata_field_description']);
Expand All @@ -117,7 +117,7 @@ public function testDefaultOptionsForSonataFormTypes()
}

// collection type
$fieldDescription->method('getMappingType')->willReturn(ClassMetadataInfo::MANY);
$fieldDescription->method('getMappingType')->willReturn(ClassMetadata::MANY);
foreach ($collectionTypes as $index => $formType) {
$options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
$this->assertSame($fieldDescription, $options['sonata_field_description']);
Expand Down
6 changes: 3 additions & 3 deletions tests/Filter/FilterWithQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ public function setUp()
$this->queryBuilder
->expects($this->any())
->method('field')
->will($this->returnSelf())
->willReturnSelf()
;
$this->expr = $this->createMock('Doctrine\ODM\MongoDB\Query\Expr');
$this->expr
->expects($this->any())
->method('field')
->will($this->returnSelf())
->willReturnSelf()
;
$this->queryBuilder
->expects($this->any())
->method('expr')
->will($this->returnValue($this->expr))
->willReturn($this->expr)
;
}

Expand Down
12 changes: 6 additions & 6 deletions tests/Filter/ModelFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Filter;

use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Sonata\CoreBundle\Form\Type\EqualType;
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
use Sonata\DoctrineMongoDBAdminBundle\Filter\ModelFilter;
Expand All @@ -34,8 +34,8 @@ class ModelFilterTest extends FilterWithQueryBuilderTest
public function getFieldDescription(array $options)
{
$fieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
$fieldDescription->expects($this->once())->method('getOptions')->will($this->returnValue($options));
$fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('field_name'));
$fieldDescription->expects($this->once())->method('getOptions')->willReturn($options);
$fieldDescription->expects($this->once())->method('getName')->willReturn('field_name');

return $fieldDescription;
}
Expand Down Expand Up @@ -98,7 +98,7 @@ public function testAssociationWithValidMappingAndEmptyFieldName()
$this->expectException(\RuntimeException::class);

$filter = new ModelFilter();
$filter->initialize('field_name', ['mapping_type' => ClassMetadataInfo::ONE, 'field_mapping' => true]);
$filter->initialize('field_name', ['mapping_type' => ClassMetadata::ONE, 'field_mapping' => true]);

$builder = new ProxyQuery($this->getQueryBuilder());

Expand All @@ -110,7 +110,7 @@ public function testAssociationWithValidMapping()
{
$filter = new ModelFilter();
$filter->initialize('field_name', [
'mapping_type' => ClassMetadataInfo::ONE,
'mapping_type' => ClassMetadata::ONE,
'field_name' => 'field_name',
'association_mapping' => [
'fieldName' => 'association_mapping',
Expand All @@ -128,7 +128,7 @@ public function testAssociationWithValidParentAssociationMappings()
{
$filter = new ModelFilter();
$filter->initialize('field_name', [
'mapping_type' => ClassMetadataInfo::ONE,
'mapping_type' => ClassMetadata::ONE,
'field_name' => 'field_name',
'parent_association_mappings' => [
[
Expand Down
3 changes: 3 additions & 0 deletions tests/Guesser/FilterTypeGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function testThrowsOnMissingField()

$class = 'My\Model';
$property = 'whatever';

$this->metadata->hasAssociation($property)->willReturn(false);

greg0ire marked this conversation as resolved.
Show resolved Hide resolved
$this->modelManager->getParentMetadataForProperty($class, $property)->willReturn([
$this->metadata->reveal(),
$property,
Expand Down