merged 2.0

This commit is contained in:
Fabien Potencier 2011-11-08 08:37:19 +01:00
commit 290734353c
17 changed files with 212 additions and 48 deletions

View File

@ -30,13 +30,15 @@ class EntityUserProvider implements UserProviderInterface
private $class;
private $repository;
private $property;
private $metadata;
public function __construct(EntityManager $em, $class, $property = null)
{
$this->class = $class;
$this->metadata = $em->getClassMetadata($class);
if (false !== strpos($this->class, ':')) {
$this->class = $em->getClassMetadata($class)->name;
$this->class = $this->metadata->name;
}
$this->repository = $em->getRepository($class);
@ -74,7 +76,11 @@ class EntityUserProvider implements UserProviderInterface
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}
return $this->loadUserByUsername($user->getUsername());
// The user must be reloaded via the primary key as all other data
// might have changed without proper persistence in the database.
// That's the case when the user has been changed by a form with
// validation errors.
return $this->repository->find($this->metadata->getIdentifierValues($user));
}
/**

View File

@ -97,7 +97,7 @@ class HttpUtils
* Checks that a given path matches the Request.
*
* @param Request $request A Request instance
* @param string $path A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
* @param string $path A path (an absolute path (/foo) or a route name (foo))
*
* @return Boolean true if the path is the same as the one from the Request, false otherwise
*/

View File

@ -96,6 +96,9 @@ class Serializer implements SerializerInterface
if (null === $data || is_scalar($data)) {
return $data;
}
if (is_object($data) && $this->supportsNormalization($data, $format)) {
return $this->normalizeObject($data, $format);
}
if ($data instanceof \Traversable) {
$normalized = array();
foreach ($data as $key => $val) {
@ -153,17 +156,14 @@ class Serializer implements SerializerInterface
if (!$this->normalizers) {
throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
}
$class = get_class($object);
if (isset($this->normalizerCache[$class][$format])) {
// If normalization is supported, cached normalizer will exist
if ($this->supportsNormalization($object, $format)) {
return $this->normalizerCache[$class][$format]->normalize($object, $format);
}
foreach ($this->normalizers as $normalizer) {
if ($normalizer->supportsNormalization($object, $class, $format)) {
$this->normalizerCache[$class][$format] = $normalizer;
return $normalizer->normalize($object, $format);
}
}
throw new UnexpectedValueException('Could not normalize object of type '.$class.', no supporting normalizer found.');
}
@ -193,6 +193,31 @@ class Serializer implements SerializerInterface
throw new UnexpectedValueException('Could not denormalize object of type '.$class.', no supporting normalizer found.');
}
/**
* Check if normalizer cache or normalizers supports provided object, which will then be cached
*
* @param object $object Object to test for normalization support
* @param string $format Format name, needed for normalizers to pivot on
*/
private function supportsNormalization($object, $format)
{
$class = get_class($object);
if (isset($this->normalizerCache[$class][$format])) {
return true;
}
foreach ($this->normalizers as $normalizer) {
if ($normalizer->supportsNormalization($object, $format)) {
$this->normalizerCache[$class][$format] = $normalizer;
return true;
}
}
return false;
}
/**
* {@inheritdoc}
*/

View File

@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Bridge\Doctrine\Form;
namespace Symfony\Tests\Bridge\Doctrine;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;

View File

@ -1,6 +1,6 @@
<?php
namespace Symfony\Tests\Bridge\Doctrine\Form\Fixtures;
namespace Symfony\Tests\Bridge\Doctrine\Fixtures;
use Doctrine\ORM\Mapping AS ORM;

View File

@ -1,13 +1,14 @@
<?php
namespace Symfony\Tests\Bridge\Doctrine\Form\Fixtures;
namespace Symfony\Tests\Bridge\Doctrine\Fixtures;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
/** @Entity */
class CompositeIdentEntity
class CompositeIdentEntity implements UserInterface
{
/** @Id @Column(type="integer") */
protected $id1;
@ -23,4 +24,29 @@ class CompositeIdentEntity
$this->id2 = $id2;
$this->name = $name;
}
public function getRoles()
{
}
public function getPassword()
{
}
public function getSalt()
{
}
public function getUsername()
{
return $this->name;
}
public function eraseCredentials()
{
}
public function equals(UserInterface $user)
{
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace Symfony\Tests\Bridge\Doctrine\Form\Fixtures;
namespace Symfony\Tests\Bridge\Doctrine\Fixtures;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Column;

View File

@ -1,6 +1,6 @@
<?php
namespace Symfony\Tests\Bridge\Doctrine\Form\Fixtures;
namespace Symfony\Tests\Bridge\Doctrine\Fixtures;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Column;

View File

@ -1,6 +1,6 @@
<?php
namespace Symfony\Tests\Bridge\Doctrine\Form\Fixtures;
namespace Symfony\Tests\Bridge\Doctrine\Fixtures;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Column;

View File

@ -1,6 +1,6 @@
<?php
namespace Symfony\Tests\Bridge\Doctrine\Form\Fixtures;
namespace Symfony\Tests\Bridge\Doctrine\Fixtures;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Column;

View File

@ -11,22 +11,22 @@
namespace Symfony\Tests\Bridge\Doctrine\Form\ChoiceList;
require_once __DIR__.'/../DoctrineOrmTestCase.php';
require_once __DIR__.'/../../DoctrineOrmTestCase.php';
require_once __DIR__.'/../../Fixtures/ItemGroupEntity.php';
require_once __DIR__.'/../../Fixtures/SingleIdentEntity.php';
use Symfony\Tests\Bridge\Doctrine\Form\DoctrineOrmTestCase;
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\ItemGroupEntity;
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity;
use Symfony\Tests\Bridge\Doctrine\DoctrineOrmTestCase;
use Symfony\Tests\Bridge\Doctrine\Fixtures\ItemGroupEntity;
use Symfony\Tests\Bridge\Doctrine\Fixtures\SingleIdentEntity;
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList;
class EntityChoiceListTest extends DoctrineOrmTestCase
{
const ITEM_GROUP_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\ItemGroupEntity';
const ITEM_GROUP_CLASS = 'Symfony\Tests\Bridge\Doctrine\Fixtures\ItemGroupEntity';
const SINGLE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity';
const SINGLE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Fixtures\SingleIdentEntity';
const COMPOSITE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeIdentEntity';
const COMPOSITE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity';
private $em;

View File

@ -11,7 +11,7 @@
namespace Symfony\Tests\Bridge\Doctrine\Form\Type;
require_once __DIR__.'/../DoctrineOrmTestCase.php';
require_once __DIR__.'/../../DoctrineOrmTestCase.php';
require_once __DIR__.'/../../Fixtures/ItemGroupEntity.php';
require_once __DIR__.'/../../Fixtures/SingleIdentEntity.php';
require_once __DIR__.'/../../Fixtures/SingleStringIdentEntity.php';
@ -20,12 +20,12 @@ require_once __DIR__.'/../../Fixtures/CompositeStringIdentEntity.php';
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Tests\Component\Form\Extension\Core\Type\TypeTestCase;
use Symfony\Tests\Bridge\Doctrine\Form\DoctrineOrmTestCase;
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\ItemGroupEntity;
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity;
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleStringIdentEntity;
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeIdentEntity;
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeStringIdentEntity;
use Symfony\Tests\Bridge\Doctrine\DoctrineOrmTestCase;
use Symfony\Tests\Bridge\Doctrine\Fixtures\ItemGroupEntity;
use Symfony\Tests\Bridge\Doctrine\Fixtures\SingleIdentEntity;
use Symfony\Tests\Bridge\Doctrine\Fixtures\SingleStringIdentEntity;
use Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity;
use Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeStringIdentEntity;
use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\EntityManager;
@ -33,11 +33,11 @@ use Doctrine\Common\Collections\ArrayCollection;
class EntityTypeTest extends TypeTestCase
{
const ITEM_GROUP_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\ItemGroupEntity';
const SINGLE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity';
const SINGLE_STRING_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleStringIdentEntity';
const COMPOSITE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeIdentEntity';
const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeStringIdentEntity';
const ITEM_GROUP_CLASS = 'Symfony\Tests\Bridge\Doctrine\Fixtures\ItemGroupEntity';
const SINGLE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Fixtures\SingleIdentEntity';
const SINGLE_STRING_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Fixtures\SingleStringIdentEntity';
const COMPOSITE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity';
const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeStringIdentEntity';
private $em;

View File

@ -0,0 +1,51 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Bridge\Doctrine\Security\User;
require_once __DIR__.'/../../DoctrineOrmTestCase.php';
require_once __DIR__.'/../../Fixtures/CompositeIdentEntity.php';
use Symfony\Tests\Bridge\Doctrine\DoctrineOrmTestCase;
use Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity;
use Symfony\Bridge\Doctrine\Security\User\EntityUserProvider;
use Doctrine\ORM\Tools\SchemaTool;
class EntityUserProviderTest extends DoctrineOrmTestCase
{
public function testRefreshUserGetsUserByPrimaryKey()
{
$em = $this->createTestEntityManager();
$this->createSchema($em);
$user1 = new CompositeIdentEntity(1, 1, 'user1');
$user2 = new CompositeIdentEntity(1, 2, 'user2');
$em->persist($user1);
$em->persist($user2);
$em->flush();
$provider = new EntityUserProvider($em, 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');
// try to change the user identity
$user1->name = 'user2';
$this->assertSame($user1, $provider->refreshUser($user1));
}
private function createSchema($em)
{
$schemaTool = new SchemaTool($em);
$schemaTool->createSchema(array(
$em->getClassMetadata('Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity'),
));
}
}

View File

@ -11,15 +11,15 @@
namespace Symfony\Tests\Bridge\Doctrine\Validator\Constraints;
require_once __DIR__.'/../../Form/DoctrineOrmTestCase.php';
require_once __DIR__.'/../../DoctrineOrmTestCase.php';
require_once __DIR__.'/../../Fixtures/SingleIdentEntity.php';
require_once __DIR__.'/../../Fixtures/CompositeIdentEntity.php';
require_once __DIR__.'/../../Fixtures/AssociationEntity.php';
use Symfony\Tests\Bridge\Doctrine\Form\DoctrineOrmTestCase;
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity;
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeIdentEntity;
use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\AssociationEntity;
use Symfony\Tests\Bridge\Doctrine\DoctrineOrmTestCase;
use Symfony\Tests\Bridge\Doctrine\Fixtures\SingleIdentEntity;
use Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity;
use Symfony\Tests\Bridge\Doctrine\Fixtures\AssociationEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
use Symfony\Component\Validator\Mapping\ClassMetadata;
@ -64,7 +64,7 @@ class UniqueValidatorTest extends DoctrineOrmTestCase
public function createValidator($entityManagerName, $em, $validateClass = null, $uniqueFields = null)
{
if (!$validateClass) {
$validateClass = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity';
$validateClass = 'Symfony\Tests\Bridge\Doctrine\Fixtures\SingleIdentEntity';
}
if (!$uniqueFields) {
$uniqueFields = array('name');
@ -87,9 +87,9 @@ class UniqueValidatorTest extends DoctrineOrmTestCase
{
$schemaTool = new SchemaTool($em);
$schemaTool->createSchema(array(
$em->getClassMetadata('Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity'),
$em->getClassMetadata('Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeIdentEntity'),
$em->getClassMetadata('Symfony\Tests\Bridge\Doctrine\Form\Fixtures\AssociationEntity'),
$em->getClassMetadata('Symfony\Tests\Bridge\Doctrine\Fixtures\SingleIdentEntity'),
$em->getClassMetadata('Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity'),
$em->getClassMetadata('Symfony\Tests\Bridge\Doctrine\Fixtures\AssociationEntity'),
));
}
@ -171,7 +171,7 @@ class UniqueValidatorTest extends DoctrineOrmTestCase
$entityManagerName = "foo";
$em = $this->createTestEntityManager();
$this->createSchema($em);
$validator = $this->createValidator($entityManagerName, $em, 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\AssociationEntity', array('single'));
$validator = $this->createValidator($entityManagerName, $em, 'Symfony\Tests\Bridge\Doctrine\Fixtures\AssociationEntity', array('single'));
$entity1 = new SingleIdentEntity(1, 'foo');
$associated = new AssociationEntity();
@ -202,7 +202,7 @@ class UniqueValidatorTest extends DoctrineOrmTestCase
$entityManagerName = "foo";
$em = $this->createTestEntityManager();
$this->createSchema($em);
$validator = $this->createValidator($entityManagerName, $em, 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\AssociationEntity', array('composite'));
$validator = $this->createValidator($entityManagerName, $em, 'Symfony\Tests\Bridge\Doctrine\Fixtures\AssociationEntity', array('composite'));
$composite = new CompositeIdentEntity(1, 1, "test");
$associated = new AssociationEntity();

View File

@ -0,0 +1,25 @@
<?php
namespace Symfony\Tests\Component\Serializer\Fixtures;
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
use Symfony\Component\Serializer\SerializerInterface;
class NormalizableTraversableDummy extends TraversableDummy implements NormalizableInterface
{
public function normalize(SerializerInterface $serializer, $format = null)
{
return array(
'foo' => 'normalizedFoo',
'bar' => 'normalizedBar',
);
}
public function denormalize(SerializerInterface $serializer, $data, $format = null)
{
return array(
'foo' => 'denormalizedFoo',
'bar' => 'denormalizedBar',
);
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Symfony\Tests\Component\Serializer\Fixtures;
class TraversableDummy implements \IteratorAggregate
{
public $foo = 'foo';
public $bar = 'bar';
public function getIterator()
{
return new \ArrayIterator(get_object_vars($this));
}
}

View File

@ -4,6 +4,9 @@ namespace Symfony\Tests\Component\Serializer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use Symfony\Tests\Component\Serializer\Fixtures\TraversableDummy;
use Symfony\Tests\Component\Serializer\Fixtures\NormalizableTraversableDummy;
/*
* This file is part of the Symfony framework.
@ -25,6 +28,20 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
$this->serializer->normalize(new \stdClass, 'xml');
}
public function testNormalizeTraversable()
{
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
$result = $this->serializer->serialize(new TraversableDummy, 'json');
$this->assertEquals('{"foo":"foo","bar":"bar"}', $result);
}
public function testNormalizeGivesPriorityToInterfaceOverTraversable()
{
$this->serializer = new Serializer(array(new CustomNormalizer), array('json' => new JsonEncoder()));
$result = $this->serializer->serialize(new NormalizableTraversableDummy, 'json');
$this->assertEquals('{"foo":"normalizedFoo","bar":"normalizedBar"}', $result);
}
/**
* @expectedException \UnexpectedValueException
*/