-
-
Notifications
You must be signed in to change notification settings - Fork 95
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
Add tests to ModelManager and DateFilter #368
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,9 @@ | |
namespace Sonata\DoctrineMongoDBAdminBundle\Model; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Persistence\Mapping\ClassMetadata as CommonClassMetadata; | ||
use Doctrine\ODM\MongoDB\Query\Builder; | ||
use Doctrine\Persistence\Mapping\ClassMetadata; | ||
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; | ||
use Sonata\AdminBundle\Datagrid\DatagridInterface; | ||
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; | ||
|
@@ -23,16 +25,44 @@ | |
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery; | ||
use Sonata\Exporter\Source\DoctrineODMQuerySourceIterator; | ||
use Symfony\Bridge\Doctrine\ManagerRegistry; | ||
use Symfony\Component\Form\Exception\PropertyAccessDeniedException; | ||
use Symfony\Component\PropertyAccess\PropertyAccess; | ||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | ||
|
||
class ModelManager implements ModelManagerInterface | ||
{ | ||
public const ID_SEPARATOR = '-'; | ||
|
||
/** | ||
* @var ManagerRegistry | ||
*/ | ||
protected $registry; | ||
|
||
public function __construct(ManagerRegistry $registry) | ||
/** | ||
* @var PropertyAccessorInterface | ||
*/ | ||
private $propertyAccessor; | ||
|
||
/** | ||
* NEXT_MAJOR: Make $propertyAccessor mandatory. | ||
*/ | ||
public function __construct(ManagerRegistry $registry, ?PropertyAccessorInterface $propertyAccessor = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And what about requiring the param in the next major ? (And add a deprecation in 3.x if it's not provided) |
||
{ | ||
$this->registry = $registry; | ||
|
||
// NEXT_MAJOR: Remove this block. | ||
if (!$propertyAccessor instanceof PropertyAccessorInterface) { | ||
@trigger_error(sprintf( | ||
'Not passing an object implementing "%s" as argument 2 for "%s()" is deprecated since' | ||
.' sonata-project/doctrine-mongodb-admin-bundle 3.x and will throw a %s error in 4.0.', | ||
PropertyAccessorInterface::class, | ||
__METHOD__, | ||
\TypeError::class | ||
), E_USER_DEPRECATED); | ||
|
||
$propertyAccessor = PropertyAccess::createPropertyAccessor(); | ||
} | ||
|
||
$this->propertyAccessor = $propertyAccessor; | ||
} | ||
|
||
/** | ||
|
@@ -364,6 +394,21 @@ public function getExportFields($class) | |
*/ | ||
public function getModelInstance($class) | ||
{ | ||
if (!class_exists($class)) { | ||
throw new \InvalidArgumentException(sprintf('Class "%s" not found', $class)); | ||
} | ||
|
||
$r = new \ReflectionClass($class); | ||
franmomu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ($r->isAbstract()) { | ||
throw new \InvalidArgumentException(sprintf('Cannot initialize abstract class: %s', $class)); | ||
} | ||
|
||
$constructor = $r->getConstructor(); | ||
|
||
if (null !== $constructor && (!$constructor->isPublic() || $constructor->getNumberOfRequiredParameters() > 0)) { | ||
return $r->newInstanceWithoutConstructor(); | ||
} | ||
|
||
return new $class(); | ||
} | ||
|
||
|
@@ -438,39 +483,10 @@ public function modelReverseTransform($class, array $array = []) | |
$instance = $this->getModelInstance($class); | ||
$metadata = $this->getMetadata($class); | ||
|
||
$reflClass = $metadata->reflClass; | ||
foreach ($array as $name => $value) { | ||
$reflection_property = false; | ||
// property or association ? | ||
if (\array_key_exists($name, $metadata->fieldMappings)) { | ||
$property = $metadata->fieldMappings[$name]['fieldName']; | ||
$reflection_property = $metadata->reflFields[$name]; | ||
} elseif (\array_key_exists($name, $metadata->associationMappings)) { | ||
$property = $metadata->associationMappings[$name]['fieldName']; | ||
} else { | ||
$property = $name; | ||
} | ||
$property = $this->getFieldName($metadata, $name); | ||
|
||
$setter = 'set'.$this->camelize($name); | ||
|
||
if ($reflClass->hasMethod($setter)) { | ||
if (!$reflClass->getMethod($setter)->isPublic()) { | ||
throw new PropertyAccessDeniedException(sprintf('Method "%s()" is not public in class "%s"', $setter, $reflClass->getName())); | ||
} | ||
|
||
$instance->$setter($value); | ||
} elseif ($reflClass->hasMethod('__set')) { | ||
// needed to support magic method __set | ||
$instance->$property = $value; | ||
} elseif ($reflClass->hasProperty($property)) { | ||
if (!$reflClass->getProperty($property)->isPublic()) { | ||
throw new PropertyAccessDeniedException(sprintf('Property "%s" is not public in class "%s". Maybe you should create the method "set%s()"?', $property, $reflClass->getName(), ucfirst($property))); | ||
} | ||
|
||
$instance->$property = $value; | ||
} elseif ($reflection_property) { | ||
$reflection_property->setValue($instance, $value); | ||
} | ||
$this->propertyAccessor->setValue($instance, $property, $value); | ||
franmomu marked this conversation as resolved.
Show resolved
Hide resolved
franmomu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return $instance; | ||
|
@@ -517,15 +533,40 @@ public function collectionRemoveElement(&$collection, &$element) | |
} | ||
|
||
/** | ||
* method taken from PropertyPath. | ||
* NEXT_MAJOR: Remove this method. | ||
* | ||
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'. | ||
* | ||
* @param string $property | ||
* | ||
* @return mixed | ||
*/ | ||
protected function camelize($property) | ||
{ | ||
return preg_replace(['/(^|_)+(.)/e', '/\.(.)/e'], ["strtoupper('\\2')", "'_'.strtoupper('\\1')"], $property); | ||
@trigger_error(sprintf( | ||
'Method "%s()" is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.', | ||
__METHOD__ | ||
), E_USER_DEPRECATED); | ||
|
||
return str_replace(' ', '', ucwords(str_replace('_', ' ', $property))); | ||
} | ||
|
||
/** | ||
* NEXT_MAJOR: Remove CommonClassMetadata and add ClassMetadata as type hint when dropping doctrine/mongodb-odm 1.3.x. | ||
* | ||
* @param ClassMetadata|CommonClassMetadata $metadata | ||
*/ | ||
private function getFieldName($metadata, string $name): string | ||
{ | ||
if (\array_key_exists($name, $metadata->fieldMappings)) { | ||
return $metadata->fieldMappings[$name]['fieldName']; | ||
} | ||
|
||
if (\array_key_exists($name, $metadata->associationMappings)) { | ||
return $metadata->associationMappings[$name]['fieldName']; | ||
} | ||
|
||
return $name; | ||
} | ||
|
||
private function isFieldAlreadySorted(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid): bool | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK this is a BC break, because you introduce new API methods without that MUST be implemented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #368 (comment)