Merge branch '2.8' into 3.1

* 2.8:
  [Validator][GroupSequence] fixed GroupSequence validation ignores PropertyMetadata of parent classes
  [FrameworkBundle][Security] Remove useless mocks
  [DoctrineBridge] Enhance exception message in EntityUserProvider
  added friendly exception when constraint validator does not exist or it is not enabled
  remove duplicate instruction
  [FrameworkBundle] Remove TranslatorBagInterface check
  [FrameworkBundle] Remove duplicated code in RouterDebugCommand
  [Validator] fixed duplicate constraints with parent class interfaces
  SecurityBundle:BasicAuthenticationListener: removed a default argument on getting a header value
This commit is contained in:
Nicolas Grekas 2016-08-26 14:04:02 +02:00
commit 224ebc0aff
22 changed files with 132 additions and 48 deletions

View File

@ -51,7 +51,7 @@ class EntityUserProvider implements UserProviderInterface
$user = $repository->findOneBy(array($this->property => $username));
} else {
if (!$repository instanceof UserLoaderInterface) {
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface.', get_class($repository)));
throw new \InvalidArgumentException(sprintf('You must either make the "%s" entity Doctrine Repository ("%s") implement "Symfony\Component\Security\Core\User\UserProviderInterface" or set the "property" option in the corresponding entity provider configuration.', $this->classOrAlias, get_class($repository)));
}
$user = $repository->loadUserByUsername($username);

View File

@ -38,6 +38,39 @@ class EntityUserProviderTest extends \PHPUnit_Framework_TestCase
$this->assertSame($user1, $provider->refreshUser($user1));
}
public function testLoadUserByUsername()
{
$em = DoctrineTestHelper::createTestEntityManager();
$this->createSchema($em);
$user = new User(1, 1, 'user1');
$em->persist($user);
$em->flush();
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User', 'name');
$this->assertSame($user, $provider->loadUserByUsername('user1'));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage You must either make the "Symfony\Bridge\Doctrine\Tests\Fixtures\User" entity Doctrine Repository ("Doctrine\ORM\EntityRepository") implement "Symfony\Component\Security\Core\User\UserProviderInterface" or set the "property" option in the corresponding entity provider configuration.
*/
public function testLoadUserByUsernameWithNonUserProviderRepositoryAndWithoutProperty()
{
$em = DoctrineTestHelper::createTestEntityManager();
$this->createSchema($em);
$user = new User(1, 1, 'user1');
$em->persist($user);
$em->flush();
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Bridge\Doctrine\Tests\Fixtures\User');
$provider->loadUserByUsername('user1');
}
public function testRefreshUserRequiresId()
{
$em = DoctrineTestHelper::createTestEntityManager();

View File

@ -78,10 +78,10 @@ EOF
$io = new SymfonyStyle($input, $output);
$name = $input->getArgument('name');
$helper = new DescriptorHelper();
$routes = $this->getContainer()->get('router')->getRouteCollection();
if ($name) {
$route = $this->getContainer()->get('router')->getRouteCollection()->get($name);
if (!$route) {
if (!$route = $routes->get($name)) {
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
}
@ -94,8 +94,6 @@ EOF
'output' => $io,
));
} else {
$routes = $this->getContainer()->get('router')->getRouteCollection();
foreach ($routes as $route) {
$this->convertController($route);
}

View File

@ -26,11 +26,6 @@ class LoggingTranslatorPass implements CompilerPassInterface
return;
}
// skip if the symfony/translation version is lower than 2.6
if (!interface_exists('Symfony\Component\Translation\TranslatorBagInterface')) {
return;
}
if ($container->hasParameter('translator.logging') && $container->getParameter('translator.logging')) {
$translatorAlias = $container->getAlias('translator');
$definition = $container->getDefinition((string) $translatorAlias);

View File

@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
use Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine;
use Symfony\Component\HttpFoundation\Response;
class DelegatingEngineTest extends \PHPUnit_Framework_TestCase
{
@ -60,7 +61,7 @@ class DelegatingEngineTest extends \PHPUnit_Framework_TestCase
public function testRenderResponseWithFrameworkEngine()
{
$response = $this->getMock('Symfony\Component\HttpFoundation\Response');
$response = new Response();
$engine = $this->getFrameworkEngineMock('template.php', true);
$engine->expects($this->once())
->method('renderResponse')

View File

@ -62,4 +62,19 @@ class ConstraintValidatorFactoryTest extends \PHPUnit_Framework_TestCase
$factory = new ConstraintValidatorFactory($container, array('validator_constraint_alias' => 'validator_constraint_service'));
$this->assertSame($validator, $factory->getInstance($constraint));
}
/**
* @expectedException \Symfony\Component\Validator\Exception\ValidatorException
*/
public function testGetInstanceInvalidValidatorClass()
{
$constraint = $this->getMock('Symfony\\Component\\Validator\\Constraint');
$constraint
->expects($this->once())
->method('validatedBy')
->will($this->returnValue('Fully\\Qualified\\ConstraintValidator\\Class\\Name'));
$factory = new ConstraintValidatorFactory(new Container());
$factory->getInstance($constraint);
}
}

View File

@ -15,6 +15,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
@ -61,6 +62,7 @@ class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
*
* @return ConstraintValidatorInterface A validator for the supplied constraint
*
* @throws ValidatorException When the validator class does not exist
* @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface
*/
public function getInstance(Constraint $constraint)
@ -68,6 +70,10 @@ class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
$name = $constraint->validatedBy();
if (!isset($this->validators[$name])) {
if (!class_exists($name)) {
throw new ValidatorException(sprintf('Constraint validator "%s" does not exist or it is not enabled. Check the "validatedBy" method in your constraint class "%s".', $name, get_class($constraint)));
}
$this->validators[$name] = new $name();
} elseif (is_string($this->validators[$name])) {
$this->validators[$name] = $this->container->get($this->validators[$name]);

View File

@ -95,12 +95,10 @@ class MockSplFileInfo extends \SplFileInfo
if (is_string($type)) {
switch ($type) {
case 'directory':
$this->type = self::TYPE_DIRECTORY;
case 'd':
$this->type = self::TYPE_DIRECTORY;
break;
case 'file':
$this->type = self::TYPE_FILE;
case 'f':
$this->type = self::TYPE_FILE;
break;

View File

@ -56,7 +56,7 @@ class BasicAuthenticationListener implements ListenerInterface
{
$request = $event->getRequest();
if (false === $username = $request->headers->get('PHP_AUTH_USER', false)) {
if (null === $username = $request->headers->get('PHP_AUTH_USER')) {
return;
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Security\Http\Tests\Authentication;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class DefaultAuthenticationFailureHandlerTest extends \PHPUnit_Framework_TestCase
@ -47,7 +48,7 @@ class DefaultAuthenticationFailureHandlerTest extends \PHPUnit_Framework_TestCas
->method('createRequest')->with($this->request, '/login')
->will($this->returnValue($subRequest));
$response = $this->getMock('Symfony\Component\HttpFoundation\Response');
$response = new Response();
$this->httpKernel->expects($this->once())
->method('handle')->with($subRequest, HttpKernelInterface::SUB_REQUEST)
->will($this->returnValue($response));
@ -60,7 +61,7 @@ class DefaultAuthenticationFailureHandlerTest extends \PHPUnit_Framework_TestCas
public function testRedirect()
{
$response = $this->getMock('Symfony\Component\HttpFoundation\Response');
$response = new Response();
$this->httpUtils->expects($this->once())
->method('createRedirectResponse')->with($this->request, '/login')
->will($this->returnValue($response));

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Security\Http\Tests\Authentication;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
class DefaultAuthenticationSuccessHandlerTest extends \PHPUnit_Framework_TestCase
@ -171,8 +172,7 @@ class DefaultAuthenticationSuccessHandlerTest extends \PHPUnit_Framework_TestCas
private function expectRedirectResponse($path)
{
$response = $this->getMock('Symfony\Component\HttpFoundation\Response');
$response = new Response();
$this->httpUtils->expects($this->once())
->method('createRedirectResponse')
->with($this->request, $path)

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Security\Http\Tests;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
@ -41,7 +42,7 @@ class SimpleAuthenticationHandlerTest extends \PHPUnit_Framework_TestCase
// No methods are invoked on the exception; we just assert on its class
$this->authenticationException = new AuthenticationException();
$this->response = $this->getMock('Symfony\Component\HttpFoundation\Response');
$this->response = new Response();
}
public function testOnAuthenticationSuccessFallsBackToDefaultHandlerIfSimpleIsNotASuccessHandler()

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Security\Http\Tests\EntryPoint;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\EntryPoint\FormAuthenticationEntryPoint;
use Symfony\Component\HttpKernel\HttpKernelInterface;
@ -19,7 +20,7 @@ class FormAuthenticationEntryPointTest extends \PHPUnit_Framework_TestCase
public function testStart()
{
$request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
$response = $this->getMock('Symfony\Component\HttpFoundation\Response');
$response = new Response();
$httpKernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$httpUtils = $this->getMock('Symfony\Component\Security\Http\HttpUtils');
@ -39,7 +40,7 @@ class FormAuthenticationEntryPointTest extends \PHPUnit_Framework_TestCase
{
$request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
$subRequest = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
$response = new \Symfony\Component\HttpFoundation\Response('', 200);
$response = new Response('', 200);
$httpUtils = $this->getMock('Symfony\Component\Security\Http\HttpUtils');
$httpUtils

View File

@ -11,9 +11,10 @@
namespace Symfony\Component\Security\Http\Tests;
use Symfony\Component\Security\Http\Firewall;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Http\Firewall;
class FirewallTest extends \PHPUnit_Framework_TestCase
{
@ -46,7 +47,7 @@ class FirewallTest extends \PHPUnit_Framework_TestCase
public function testOnKernelRequestStopsWhenThereIsAResponse()
{
$response = $this->getMock('Symfony\Component\HttpFoundation\Response');
$response = new Response();
$first = $this->getMock('Symfony\Component\Security\Http\Firewall\ListenerInterface');
$first

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Security\Http\Tests\Logout;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Logout\DefaultLogoutSuccessHandler;
class DefaultLogoutSuccessHandlerTest extends \PHPUnit_Framework_TestCase
@ -18,7 +19,7 @@ class DefaultLogoutSuccessHandlerTest extends \PHPUnit_Framework_TestCase
public function testLogout()
{
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
$response = $this->getMock('Symfony\Component\HttpFoundation\Response');
$response = new Response();
$httpUtils = $this->getMock('Symfony\Component\Security\Http\HttpUtils');
$httpUtils->expects($this->once())

View File

@ -15,6 +15,7 @@ use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Http\RememberMe\ResponseListener;
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\KernelEvents;
@ -81,7 +82,7 @@ class ResponseListenerTest extends \PHPUnit_Framework_TestCase
private function getResponse()
{
$response = $this->getMock('Symfony\Component\HttpFoundation\Response');
$response = new Response();
$response->headers = $this->getMock('Symfony\Component\HttpFoundation\ResponseHeaderBag');
return $response;

View File

@ -308,6 +308,7 @@ class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
$member = clone $member;
foreach ($member->getConstraints() as $constraint) {
$member->constraintsByGroup[$this->getDefaultGroup()][] = $constraint;
$constraint->addImplicitGroupName($this->getDefaultGroup());
}

View File

@ -114,9 +114,9 @@ class LazyLoadingMetadataFactory implements MetadataFactoryInterface
$metadata->mergeConstraints($this->getMetadataFor($parent->name));
}
// Include constraints from all implemented interfaces
// Include constraints from all implemented interfaces that have not been processed via parent class yet
foreach ($metadata->getReflectionClass()->getInterfaces() as $interface) {
if ('Symfony\Component\Validator\GroupSequenceProviderInterface' === $interface->name) {
if ('Symfony\Component\Validator\GroupSequenceProviderInterface' === $interface->name || ($parent && $parent->implementsInterface($interface->name))) {
continue;
}
$metadata->mergeConstraints($this->getMetadataFor($interface->name));

View File

@ -19,7 +19,7 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
* @Assert\GroupSequence({"Foo", "Entity"})
* @Assert\Callback({"Symfony\Component\Validator\Tests\Fixtures\CallbackClass", "callback"})
*/
class Entity extends EntityParent implements EntityInterface
class Entity extends EntityParent
{
/**
* @Assert\NotNull

View File

@ -13,7 +13,7 @@ namespace Symfony\Component\Validator\Tests\Fixtures;
use Symfony\Component\Validator\Constraints\NotNull;
class EntityParent
class EntityParent implements EntityInterface
{
protected $firstName;
private $internal;

View File

@ -138,16 +138,33 @@ class ClassMetadataTest extends \PHPUnit_Framework_TestCase
$this->metadata->mergeConstraints($parent);
$this->metadata->addPropertyConstraint('firstName', new ConstraintA());
$constraintA1 = new ConstraintA(array('groups' => array(
'Default',
'EntityParent',
'Entity',
)));
$constraintA2 = new ConstraintA(array('groups' => array(
'Default',
'Entity',
)));
$constraints = array(
new ConstraintA(array('groups' => array(
'Default',
'EntityParent',
'Entity',
))),
new ConstraintA(array('groups' => array(
'Default',
'Entity',
))),
$constraintA1,
$constraintA2,
);
$constraintsByGroup = array(
'Default' => array(
$constraintA1,
$constraintA2,
),
'EntityParent' => array(
$constraintA1,
),
'Entity' => array(
$constraintA1,
$constraintA2,
),
);
$members = $this->metadata->getPropertyMetadata('firstName');
@ -155,6 +172,7 @@ class ClassMetadataTest extends \PHPUnit_Framework_TestCase
$this->assertCount(1, $members);
$this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
$this->assertEquals($constraints, $members[0]->getConstraints());
$this->assertEquals($constraintsByGroup, $members[0]->constraintsByGroup);
}
public function testMemberMetadatas()

View File

@ -20,13 +20,15 @@ class LazyLoadingMetadataFactoryTest extends \PHPUnit_Framework_TestCase
{
const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent';
const INTERFACECLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityInterface';
public function testLoadClassMetadata()
public function testLoadClassMetadataWithInterface()
{
$factory = new LazyLoadingMetadataFactory(new TestLoader());
$metadata = $factory->getMetadataFor(self::PARENTCLASS);
$constraints = array(
new ConstraintA(array('groups' => array('Default', 'EntityInterface', 'EntityParent'))),
new ConstraintA(array('groups' => array('Default', 'EntityParent'))),
);
@ -41,12 +43,13 @@ class LazyLoadingMetadataFactoryTest extends \PHPUnit_Framework_TestCase
$constraints = array(
new ConstraintA(array('groups' => array(
'Default',
'EntityInterface',
'EntityParent',
'Entity',
))),
new ConstraintA(array('groups' => array(
'Default',
'EntityInterface',
'EntityParent',
'Entity',
))),
new ConstraintA(array('groups' => array(
@ -63,26 +66,36 @@ class LazyLoadingMetadataFactoryTest extends \PHPUnit_Framework_TestCase
$cache = $this->getMock('Symfony\Component\Validator\Mapping\Cache\CacheInterface');
$factory = new LazyLoadingMetadataFactory(new TestLoader(), $cache);
$constraints = array(
$parentClassConstraints = array(
new ConstraintA(array('groups' => array('Default', 'EntityInterface', 'EntityParent'))),
new ConstraintA(array('groups' => array('Default', 'EntityParent'))),
);
$interfaceConstraints = array(new ConstraintA(array('groups' => array('Default', 'EntityInterface'))));
$cache->expects($this->never())
->method('has');
$cache->expects($this->once())
$cache->expects($this->exactly(2))
->method('read')
->with($this->equalTo(self::PARENTCLASS))
->withConsecutive(
array($this->equalTo(self::PARENTCLASS)),
array($this->equalTo(self::INTERFACECLASS))
)
->will($this->returnValue(false));
$cache->expects($this->once())
$cache->expects($this->exactly(2))
->method('write')
->will($this->returnCallback(function ($metadata) use ($constraints) {
$this->assertEquals($constraints, $metadata->getConstraints());
}));
->withConsecutive(
$this->callback(function ($metadata) use ($interfaceConstraints) {
return $interfaceConstraints == $metadata->getConstraints();
}),
$this->callback(function ($metadata) use ($parentClassConstraints) {
return $parentClassConstraints == $metadata->getConstraints();
})
);
$metadata = $factory->getMetadataFor(self::PARENTCLASS);
$this->assertEquals(self::PARENTCLASS, $metadata->getClassName());
$this->assertEquals($constraints, $metadata->getConstraints());
$this->assertEquals($parentClassConstraints, $metadata->getConstraints());
}
public function testReadMetadataFromCache()