From 6ac2550dc8ad9a1f67c2851f75b46b5b24cdcc6f Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Wed, 6 Feb 2019 21:50:10 +0100 Subject: [PATCH] replace mocks with real objects in tests --- .../Form/Tests/AbstractRequestHandlerTest.php | 16 +- .../DependencyInjection/FormPassTest.php | 22 +- .../DataMapper/PropertyPathMapperTest.php | 251 +++++++----------- .../FixUrlProtocolListenerTest.php | 9 +- ...MergeCollectionListenerArrayObjectTest.php | 4 +- .../MergeCollectionListenerArrayTest.php | 4 +- ...ollectionListenerCustomArrayObjectTest.php | 4 +- .../MergeCollectionListenerTest.php | 6 - .../EventListener/ResizeFormListenerTest.php | 49 +--- .../Core/EventListener/TrimListenerTest.php | 7 +- .../Extension/Core/Type/FileTypeTest.php | 23 +- .../CsrfValidationListenerTest.php | 17 +- .../DependencyInjectionExtensionTest.php | 108 ++++---- .../HttpFoundationRequestHandlerTest.php | 2 +- .../Constraints/FormValidatorTest.php | 132 ++++----- .../EventListener/ValidationListenerTest.php | 167 ++++++------ .../Type/FormTypeValidatorExtensionTest.php | 9 - .../Type/UploadValidatorExtensionTest.php | 34 ++- .../Validator/Util/ServerParamsTest.php | 28 +- .../Validator/ValidatorExtensionTest.php | 42 +-- .../Validator/ValidatorTypeGuesserTest.php | 11 +- .../ViolationMapper/ViolationMapperTest.php | 17 +- .../Form/Tests/NativeRequestHandlerTest.php | 2 +- 23 files changed, 438 insertions(+), 526 deletions(-) diff --git a/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php index dd73dcde23..1a1a24e2b2 100644 --- a/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php @@ -200,7 +200,7 @@ abstract class AbstractRequestHandlerTest extends TestCase $form = $this->createForm('param1', $method, true); $form->add($this->createForm('field1')); $form->add($this->createBuilder('field2', false, ['allow_file_upload' => true])->getForm()); - $file = $this->getMockFile(); + $file = $this->getUploadedFile(); $this->setRequestData($method, [ 'param1' => [ @@ -225,7 +225,7 @@ abstract class AbstractRequestHandlerTest extends TestCase public function testParamTakesPrecedenceOverFile($method) { $form = $this->createForm('param1', $method); - $file = $this->getMockFile(); + $file = $this->getUploadedFile(); $this->setRequestData($method, [ 'param1' => 'DATA', @@ -247,7 +247,7 @@ abstract class AbstractRequestHandlerTest extends TestCase $form = $this->createBuilder('param1', false, ['allow_file_upload' => true]) ->setMethod($method) ->getForm(); - $file = $this->getMockFile(); + $file = $this->getUploadedFile(); $this->setRequestData($method, [ 'param1' => null, @@ -269,14 +269,14 @@ abstract class AbstractRequestHandlerTest extends TestCase $form = $this->createBuilder('param1', false, ['allow_file_upload' => true]) ->setMethod($method) ->getForm(); - $file = $this->getMockFile(); + $file = $this->getUploadedFile(); $this->setRequestData($method, [ 'param1' => null, ], [ - 'param2' => $this->getMockFile('2'), + 'param2' => $this->getUploadedFile('2'), 'param1' => $file, - 'param3' => $this->getMockFile('3'), + 'param3' => $this->getUploadedFile('3'), ]); $this->requestHandler->handleRequest($form, $this->request); @@ -332,7 +332,7 @@ abstract class AbstractRequestHandlerTest extends TestCase public function testUploadedFilesAreAccepted() { - $this->assertTrue($this->requestHandler->isFileUpload($this->getMockFile())); + $this->assertTrue($this->requestHandler->isFileUpload($this->getUploadedFile())); } public function testInvalidFilesAreRejected() @@ -344,7 +344,7 @@ abstract class AbstractRequestHandlerTest extends TestCase abstract protected function getRequestHandler(); - abstract protected function getMockFile($suffix = ''); + abstract protected function getUploadedFile($suffix = ''); abstract protected function getInvalidFile(); diff --git a/src/Symfony/Component/Form/Tests/DependencyInjection/FormPassTest.php b/src/Symfony/Component/Form/Tests/DependencyInjection/FormPassTest.php index 52fc78aed9..a23607412c 100644 --- a/src/Symfony/Component/Form/Tests/DependencyInjection/FormPassTest.php +++ b/src/Symfony/Component/Form/Tests/DependencyInjection/FormPassTest.php @@ -19,8 +19,9 @@ use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ServiceLocator; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Command\DebugCommand; use Symfony\Component\Form\DependencyInjection\FormPass; -use Symfony\Component\Form\FormRegistryInterface; +use Symfony\Component\Form\FormRegistry; /** * @author Bernhard Schussek @@ -70,8 +71,12 @@ class FormPassTest extends TestCase { $container = $this->createContainerBuilder(); + $container->register('form.registry', FormRegistry::class); + $commandDefinition = new Definition(DebugCommand::class, [new Reference('form.registry')]); + $commandDefinition->setPublic(true); + $container->setDefinition('form.extension', $this->createExtensionDefinition()); - $container->setDefinition('console.command.form_debug', $this->createDebugCommandDefinition()); + $container->setDefinition('console.command.form_debug', $commandDefinition); $container->register('my.type1', __CLASS__.'_Type1')->addTag('form.type')->setPublic(true); $container->register('my.type2', __CLASS__.'_Type2')->addTag('form.type')->setPublic(true); @@ -259,19 +264,6 @@ class FormPassTest extends TestCase return $definition; } - private function createDebugCommandDefinition() - { - $definition = new Definition('Symfony\Component\Form\Command\DebugCommand'); - $definition->setPublic(true); - $definition->setArguments([ - $formRegistry = $this->getMockBuilder(FormRegistryInterface::class)->getMock(), - [], - ['Symfony\Component\Form\Extension\Core\Type'], - ]); - - return $definition; - } - private function createContainerBuilder() { $container = new ContainerBuilder(); diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php index 901af61ce9..da351295c3 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php @@ -12,9 +12,14 @@ namespace Symfony\Component\Form\Tests\Extension\Core\DataMapper; use PHPUnit\Framework\TestCase; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; +use Symfony\Component\Form\Form; use Symfony\Component\Form\FormConfigBuilder; -use Symfony\Component\Form\FormConfigInterface; +use Symfony\Component\PropertyAccess\PropertyAccess; +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; +use Symfony\Component\PropertyAccess\PropertyPath; class PropertyPathMapperTest extends TestCase { @@ -24,78 +29,36 @@ class PropertyPathMapperTest extends TestCase private $mapper; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var EventDispatcherInterface */ private $dispatcher; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var PropertyAccessorInterface */ private $propertyAccessor; protected function setUp() { - $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); - $this->propertyAccessor = $this->getMockBuilder('Symfony\Component\PropertyAccess\PropertyAccessorInterface')->getMock(); + $this->dispatcher = new EventDispatcher(); + $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); $this->mapper = new PropertyPathMapper($this->propertyAccessor); } - /** - * @return \PHPUnit_Framework_MockObject_MockObject - */ - private function getPropertyPath($path) - { - return $this->getMockBuilder('Symfony\Component\PropertyAccess\PropertyPath') - ->setConstructorArgs([$path]) - ->setMethods(['getValue', 'setValue']) - ->getMock(); - } - - /** - * @param FormConfigInterface $config - * @param bool $synchronized - * @param bool $submitted - * - * @return \PHPUnit_Framework_MockObject_MockObject - */ - private function getForm(FormConfigInterface $config, $synchronized = true, $submitted = true) - { - $form = $this->getMockBuilder('Symfony\Component\Form\Form') - ->setConstructorArgs([$config]) - ->setMethods(['isSynchronized', 'isSubmitted']) - ->getMock(); - - $form->expects($this->any()) - ->method('isSynchronized') - ->will($this->returnValue($synchronized)); - - $form->expects($this->any()) - ->method('isSubmitted') - ->will($this->returnValue($submitted)); - - return $form; - } - public function testMapDataToFormsPassesObjectRefIfByReference() { $car = new \stdClass(); $engine = new \stdClass(); - $propertyPath = $this->getPropertyPath('engine'); - - $this->propertyAccessor->expects($this->once()) - ->method('getValue') - ->with($car, $propertyPath) - ->will($this->returnValue($engine)); + $car->engine = $engine; + $propertyPath = new PropertyPath('engine'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); - $form = $this->getForm($config); + $form = new Form($config); $this->mapper->mapDataToForms($car, [$form]); - // Can't use isIdentical() above because mocks always clone their - // arguments which can't be disabled in PHPUnit 3.6 $this->assertSame($engine, $form->getData()); } @@ -103,17 +66,14 @@ class PropertyPathMapperTest extends TestCase { $car = new \stdClass(); $engine = new \stdClass(); - $propertyPath = $this->getPropertyPath('engine'); - - $this->propertyAccessor->expects($this->once()) - ->method('getValue') - ->with($car, $propertyPath) - ->will($this->returnValue($engine)); + $engine->brand = 'Rolls-Royce'; + $car->engine = $engine; + $propertyPath = new PropertyPath('engine'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(false); $config->setPropertyPath($propertyPath); - $form = $this->getForm($config); + $form = new Form($config); $this->mapper->mapDataToForms($car, [$form]); @@ -127,7 +87,7 @@ class PropertyPathMapperTest extends TestCase $config = new FormConfigBuilder(null, '\stdClass', $this->dispatcher); $config->setByReference(true); - $form = $this->getForm($config); + $form = new Form($config); $this->assertNull($form->getPropertyPath()); @@ -139,16 +99,14 @@ class PropertyPathMapperTest extends TestCase public function testMapDataToFormsIgnoresUnmapped() { $car = new \stdClass(); - $propertyPath = $this->getPropertyPath('engine'); - - $this->propertyAccessor->expects($this->never()) - ->method('getValue'); + $car->engine = new \stdClass(); + $propertyPath = new PropertyPath('engine'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setMapped(false); $config->setPropertyPath($propertyPath); - $form = $this->getForm($config); + $form = new Form($config); $this->mapper->mapDataToForms($car, [$form]); @@ -158,204 +116,188 @@ class PropertyPathMapperTest extends TestCase public function testMapDataToFormsSetsDefaultDataIfPassedDataIsNull() { $default = new \stdClass(); - $propertyPath = $this->getPropertyPath('engine'); - - $this->propertyAccessor->expects($this->never()) - ->method('getValue'); + $propertyPath = new PropertyPath('engine'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData($default); - $form = $this->getMockBuilder('Symfony\Component\Form\Form') - ->setConstructorArgs([$config]) - ->setMethods(['setData']) - ->getMock(); - - $form->expects($this->once()) - ->method('setData') - ->with($default); + $form = new Form($config); $this->mapper->mapDataToForms(null, [$form]); + + $this->assertSame($default, $form->getData()); } public function testMapDataToFormsSetsDefaultDataIfPassedDataIsEmptyArray() { $default = new \stdClass(); - $propertyPath = $this->getPropertyPath('engine'); - - $this->propertyAccessor->expects($this->never()) - ->method('getValue'); + $propertyPath = new PropertyPath('engine'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData($default); - $form = $this->getMockBuilder('Symfony\Component\Form\Form') - ->setConstructorArgs([$config]) - ->setMethods(['setData']) - ->getMock(); - - $form->expects($this->once()) - ->method('setData') - ->with($default); + $form = new Form($config); $this->mapper->mapDataToForms([], [$form]); + + $this->assertSame($default, $form->getData()); } public function testMapFormsToDataWritesBackIfNotByReference() { $car = new \stdClass(); + $car->engine = new \stdClass(); $engine = new \stdClass(); - $propertyPath = $this->getPropertyPath('engine'); - - $this->propertyAccessor->expects($this->once()) - ->method('setValue') - ->with($car, $propertyPath, $engine); + $engine->brand = 'Rolls-Royce'; + $propertyPath = new PropertyPath('engine'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(false); $config->setPropertyPath($propertyPath); $config->setData($engine); - $form = $this->getForm($config); + $form = new SubmittedForm($config); $this->mapper->mapFormsToData([$form], $car); + + $this->assertEquals($engine, $car->engine); + $this->assertNotSame($engine, $car->engine); } public function testMapFormsToDataWritesBackIfByReferenceButNoReference() { $car = new \stdClass(); + $car->engine = new \stdClass(); $engine = new \stdClass(); - $propertyPath = $this->getPropertyPath('engine'); - - $this->propertyAccessor->expects($this->once()) - ->method('setValue') - ->with($car, $propertyPath, $engine); + $propertyPath = new PropertyPath('engine'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData($engine); - $form = $this->getForm($config); + $form = new SubmittedForm($config); $this->mapper->mapFormsToData([$form], $car); + + $this->assertSame($engine, $car->engine); } public function testMapFormsToDataWritesBackIfByReferenceAndReference() { $car = new \stdClass(); - $engine = new \stdClass(); - $propertyPath = $this->getPropertyPath('engine'); + $car->engine = 'BMW'; + $propertyPath = new PropertyPath('engine'); - // $car already contains the reference of $engine - $this->propertyAccessor->expects($this->once()) - ->method('getValue') - ->with($car, $propertyPath) - ->will($this->returnValue($engine)); - - $this->propertyAccessor->expects($this->never()) - ->method('setValue'); - - $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); + $config = new FormConfigBuilder('engine', null, $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); - $config->setData($engine); - $form = $this->getForm($config); + $config->setData('Rolls-Royce'); + $form = new SubmittedForm($config); + + $car->engine = 'Rolls-Royce'; $this->mapper->mapFormsToData([$form], $car); + + $this->assertSame('Rolls-Royce', $car->engine); } public function testMapFormsToDataIgnoresUnmapped() { + $initialEngine = new \stdClass(); $car = new \stdClass(); + $car->engine = $initialEngine; $engine = new \stdClass(); - $propertyPath = $this->getPropertyPath('engine'); - - $this->propertyAccessor->expects($this->never()) - ->method('setValue'); + $propertyPath = new PropertyPath('engine'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData($engine); $config->setMapped(false); - $form = $this->getForm($config); + $form = new SubmittedForm($config); $this->mapper->mapFormsToData([$form], $car); + + $this->assertSame($initialEngine, $car->engine); } public function testMapFormsToDataIgnoresUnsubmittedForms() { + $initialEngine = new \stdClass(); $car = new \stdClass(); + $car->engine = $initialEngine; $engine = new \stdClass(); - $propertyPath = $this->getPropertyPath('engine'); - - $this->propertyAccessor->expects($this->never()) - ->method('setValue'); + $propertyPath = new PropertyPath('engine'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData($engine); - $form = $this->getForm($config, true, false); + $form = new Form($config); $this->mapper->mapFormsToData([$form], $car); + + $this->assertSame($initialEngine, $car->engine); } public function testMapFormsToDataIgnoresEmptyData() { + $initialEngine = new \stdClass(); $car = new \stdClass(); - $propertyPath = $this->getPropertyPath('engine'); - - $this->propertyAccessor->expects($this->never()) - ->method('setValue'); + $car->engine = $initialEngine; + $propertyPath = new PropertyPath('engine'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData(null); - $form = $this->getForm($config); + $form = new Form($config); $this->mapper->mapFormsToData([$form], $car); + + $this->assertSame($initialEngine, $car->engine); } public function testMapFormsToDataIgnoresUnsynchronized() { + $initialEngine = new \stdClass(); $car = new \stdClass(); + $car->engine = $initialEngine; $engine = new \stdClass(); - $propertyPath = $this->getPropertyPath('engine'); - - $this->propertyAccessor->expects($this->never()) - ->method('setValue'); + $propertyPath = new PropertyPath('engine'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData($engine); - $form = $this->getForm($config, false); + $form = new NotSynchronizedForm($config); $this->mapper->mapFormsToData([$form], $car); + + $this->assertSame($initialEngine, $car->engine); } public function testMapFormsToDataIgnoresDisabled() { + $initialEngine = new \stdClass(); $car = new \stdClass(); + $car->engine = $initialEngine; $engine = new \stdClass(); - $propertyPath = $this->getPropertyPath('engine'); - - $this->propertyAccessor->expects($this->never()) - ->method('setValue'); + $propertyPath = new PropertyPath('engine'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData($engine); $config->setDisabled(true); - $form = $this->getForm($config); + $form = new Form($config); $this->mapper->mapFormsToData([$form], $car); + + $this->assertSame($initialEngine, $car->engine); } /** @@ -365,24 +307,19 @@ class PropertyPathMapperTest extends TestCase { $article = []; $publishedAt = $date; - $article['publishedAt'] = clone $publishedAt; - $propertyPath = $this->getPropertyPath('[publishedAt]'); - - $this->propertyAccessor->expects($this->once()) - ->method('getValue') - ->willReturn($article['publishedAt']) - ; - $this->propertyAccessor->expects($this->never()) - ->method('setValue') - ; + $publishedAtValue = clone $publishedAt; + $article['publishedAt'] = $publishedAtValue; + $propertyPath = new PropertyPath('[publishedAt]'); $config = new FormConfigBuilder('publishedAt', \get_class($publishedAt), $this->dispatcher); $config->setByReference(false); $config->setPropertyPath($propertyPath); $config->setData($publishedAt); - $form = $this->getForm($config); + $form = new SubmittedForm($config); $this->mapper->mapFormsToData([$form], $article); + + $this->assertSame($publishedAtValue, $article['publishedAt']); } public function provideDate() @@ -393,3 +330,19 @@ class PropertyPathMapperTest extends TestCase ]; } } + +class SubmittedForm extends Form +{ + public function isSubmitted() + { + return true; + } +} + +class NotSynchronizedForm extends Form +{ + public function isSynchronized() + { + return false; + } +} diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/FixUrlProtocolListenerTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/FixUrlProtocolListenerTest.php index 0182aa23d3..e00cb9e9e1 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/FixUrlProtocolListenerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/FixUrlProtocolListenerTest.php @@ -12,9 +12,10 @@ namespace Symfony\Component\Form\Tests\Extension\Core\EventListener; use PHPUnit\Framework\TestCase; +use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Form\Extension\Core\EventListener\FixUrlProtocolListener; use Symfony\Component\Form\Form; -use Symfony\Component\Form\FormConfigInterface; +use Symfony\Component\Form\FormConfigBuilder; use Symfony\Component\Form\FormEvent; class FixUrlProtocolListenerTest extends TestCase @@ -22,7 +23,7 @@ class FixUrlProtocolListenerTest extends TestCase public function testFixHttpUrl() { $data = 'www.symfony.com'; - $form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock()); + $form = new Form(new FormConfigBuilder('name', null, new EventDispatcher())); $event = new FormEvent($form, $data); $filter = new FixUrlProtocolListener('http'); @@ -34,7 +35,7 @@ class FixUrlProtocolListenerTest extends TestCase public function testSkipKnownUrl() { $data = 'http://www.symfony.com'; - $form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock()); + $form = new Form(new FormConfigBuilder('name', null, new EventDispatcher())); $event = new FormEvent($form, $data); $filter = new FixUrlProtocolListener('http'); @@ -59,7 +60,7 @@ class FixUrlProtocolListenerTest extends TestCase */ public function testSkipOtherProtocol($url) { - $form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock()); + $form = new Form(new FormConfigBuilder('name', null, new EventDispatcher())); $event = new FormEvent($form, $url); $filter = new FixUrlProtocolListener('http'); diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerArrayObjectTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerArrayObjectTest.php index 6f46c9d7fa..5944537927 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerArrayObjectTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerArrayObjectTest.php @@ -11,7 +11,9 @@ namespace Symfony\Component\Form\Tests\Extension\Core\EventListener; +use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Form\FormBuilder; +use Symfony\Component\Form\FormFactoryBuilder; class MergeCollectionListenerArrayObjectTest extends MergeCollectionListenerTest { @@ -22,6 +24,6 @@ class MergeCollectionListenerArrayObjectTest extends MergeCollectionListenerTest protected function getBuilder($name = 'name') { - return new FormBuilder($name, '\ArrayObject', $this->dispatcher, $this->factory); + return new FormBuilder($name, '\ArrayObject', new EventDispatcher(), (new FormFactoryBuilder())->getFormFactory()); } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerArrayTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerArrayTest.php index c0f3d59734..4f19a3ff8e 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerArrayTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerArrayTest.php @@ -11,7 +11,9 @@ namespace Symfony\Component\Form\Tests\Extension\Core\EventListener; +use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Form\FormBuilder; +use Symfony\Component\Form\FormFactoryBuilder; class MergeCollectionListenerArrayTest extends MergeCollectionListenerTest { @@ -22,6 +24,6 @@ class MergeCollectionListenerArrayTest extends MergeCollectionListenerTest protected function getBuilder($name = 'name') { - return new FormBuilder($name, null, $this->dispatcher, $this->factory); + return new FormBuilder($name, null, new EventDispatcher(), (new FormFactoryBuilder())->getFormFactory()); } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerCustomArrayObjectTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerCustomArrayObjectTest.php index 76bdc76823..4be3b4baba 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerCustomArrayObjectTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerCustomArrayObjectTest.php @@ -11,7 +11,9 @@ namespace Symfony\Component\Form\Tests\Extension\Core\EventListener; +use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Form\FormBuilder; +use Symfony\Component\Form\FormFactoryBuilder; use Symfony\Component\Form\Tests\Fixtures\CustomArrayObject; class MergeCollectionListenerCustomArrayObjectTest extends MergeCollectionListenerTest @@ -23,6 +25,6 @@ class MergeCollectionListenerCustomArrayObjectTest extends MergeCollectionListen protected function getBuilder($name = 'name') { - return new FormBuilder($name, 'Symfony\Component\Form\Tests\Fixtures\CustomArrayObject', $this->dispatcher, $this->factory); + return new FormBuilder($name, 'Symfony\Component\Form\Tests\Fixtures\CustomArrayObject', new EventDispatcher(), (new FormFactoryBuilder())->getFormFactory()); } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerTest.php index 98cd78385a..8c691dffc1 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/MergeCollectionListenerTest.php @@ -17,21 +17,15 @@ use Symfony\Component\Form\FormEvent; abstract class MergeCollectionListenerTest extends TestCase { - protected $dispatcher; - protected $factory; protected $form; protected function setUp() { - $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); - $this->factory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock(); $this->form = $this->getForm('axes'); } protected function tearDown() { - $this->dispatcher = null; - $this->factory = null; $this->form = null; } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/ResizeFormListenerTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/ResizeFormListenerTest.php index 84a8c1db88..ae7d2db467 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/ResizeFormListenerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/ResizeFormListenerTest.php @@ -13,36 +13,37 @@ namespace Symfony\Component\Form\Tests\Extension\Core\EventListener; use Doctrine\Common\Collections\ArrayCollection; use PHPUnit\Framework\TestCase; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; use Symfony\Component\Form\Extension\Core\EventListener\ResizeFormListener; +use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilder; use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormFactoryBuilder; class ResizeFormListenerTest extends TestCase { - private $dispatcher; private $factory; private $form; protected function setUp() { - $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); - $this->factory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock(); + $this->factory = (new FormFactoryBuilder())->getFormFactory(); $this->form = $this->getBuilder() ->setCompound(true) - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->getForm(); } protected function tearDown() { - $this->dispatcher = null; $this->factory = null; $this->form = null; } protected function getBuilder($name = 'name') { - return new FormBuilder($name, null, $this->dispatcher, $this->factory); + return new FormBuilder($name, null, new EventDispatcher(), $this->factory); } protected function getForm($name = 'name') @@ -50,31 +51,14 @@ class ResizeFormListenerTest extends TestCase return $this->getBuilder($name)->getForm(); } - /** - * @return \PHPUnit_Framework_MockObject_MockObject - */ - private function getDataMapper() - { - return $this->getMockBuilder('Symfony\Component\Form\DataMapperInterface')->getMock(); - } - public function testPreSetDataResizesForm() { $this->form->add($this->getForm('0')); $this->form->add($this->getForm('1')); - $this->factory->expects($this->at(0)) - ->method('createNamed') - ->with(1, 'text', null, ['property_path' => '[1]', 'attr' => ['maxlength' => 10], 'auto_initialize' => false]) - ->will($this->returnValue($this->getForm('1'))); - $this->factory->expects($this->at(1)) - ->method('createNamed') - ->with(2, 'text', null, ['property_path' => '[2]', 'attr' => ['maxlength' => 10], 'auto_initialize' => false]) - ->will($this->returnValue($this->getForm('2'))); - $data = [1 => 'string', 2 => 'string']; $event = new FormEvent($this->form, $data); - $listener = new ResizeFormListener('text', ['attr' => ['maxlength' => 10]], false, false); + $listener = new ResizeFormListener(TextType::class, ['attr' => ['maxlength' => 10]], false, false); $listener->preSetData($event); $this->assertFalse($this->form->has('0')); @@ -95,26 +79,21 @@ class ResizeFormListenerTest extends TestCase public function testPreSetDataDealsWithNullData() { - $this->factory->expects($this->never())->method('createNamed'); - $data = null; $event = new FormEvent($this->form, $data); - $listener = new ResizeFormListener('text', [], false, false); + $listener = new ResizeFormListener(TextType::class, [], false, false); $listener->preSetData($event); + + $this->assertSame(0, $this->form->count()); } public function testPreSubmitResizesUpIfAllowAdd() { $this->form->add($this->getForm('0')); - $this->factory->expects($this->once()) - ->method('createNamed') - ->with(1, 'text', null, ['property_path' => '[1]', 'attr' => ['maxlength' => 10], 'auto_initialize' => false]) - ->will($this->returnValue($this->getForm('1'))); - $data = [0 => 'string', 1 => 'string']; $event = new FormEvent($this->form, $data); - $listener = new ResizeFormListener('text', ['attr' => ['maxlength' => 10]], true, false); + $listener = new ResizeFormListener(TextType::class, ['attr' => ['maxlength' => 10]], true, false); $listener->preSubmit($event); $this->assertTrue($this->form->has('0')); @@ -293,12 +272,12 @@ class ResizeFormListenerTest extends TestCase $this->form->setData(['0' => ['name' => 'John'], '1' => ['name' => 'Jane']]); $form1 = $this->getBuilder('0') ->setCompound(true) - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->getForm(); $form1->add($this->getForm('name')); $form2 = $this->getBuilder('1') ->setCompound(true) - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->getForm(); $form2->add($this->getForm('name')); $this->form->add($form1); diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/TrimListenerTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/TrimListenerTest.php index 78dae1bd82..a61fb86a39 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/TrimListenerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/TrimListenerTest.php @@ -12,9 +12,10 @@ namespace Symfony\Component\Form\Tests\Extension\Core\EventListener; use PHPUnit\Framework\TestCase; +use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Form\Extension\Core\EventListener\TrimListener; use Symfony\Component\Form\Form; -use Symfony\Component\Form\FormConfigInterface; +use Symfony\Component\Form\FormConfigBuilder; use Symfony\Component\Form\FormEvent; class TrimListenerTest extends TestCase @@ -22,7 +23,7 @@ class TrimListenerTest extends TestCase public function testTrim() { $data = ' Foo! '; - $form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock()); + $form = new Form(new FormConfigBuilder('name', null, new EventDispatcher())); $event = new FormEvent($form, $data); $filter = new TrimListener(); @@ -34,7 +35,7 @@ class TrimListenerTest extends TestCase public function testTrimSkipNonStrings() { $data = 1234; - $form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock()); + $form = new Form(new FormConfigBuilder('name', null, new EventDispatcher())); $event = new FormEvent($form, $data); $filter = new TrimListener(); diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php index c1e0571ec7..ea012c451e 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php @@ -14,6 +14,7 @@ namespace Symfony\Component\Form\Tests\Extension\Core\Type; use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler; use Symfony\Component\Form\NativeRequestHandler; use Symfony\Component\Form\RequestHandlerInterface; +use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\UploadedFile; class FileTypeTest extends BaseTypeTest @@ -24,9 +25,7 @@ class FileTypeTest extends BaseTypeTest public function testSetData() { $form = $this->factory->createBuilder(static::TESTED_TYPE)->getForm(); - $data = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File') - ->setConstructorArgs([__DIR__.'/../../../Fixtures/foo', 'foo']) - ->getMock(); + $data = new File(__DIR__.'/../../../Fixtures/foo', false); $form->setData($data); @@ -40,7 +39,7 @@ class FileTypeTest extends BaseTypeTest public function testSubmit(RequestHandlerInterface $requestHandler) { $form = $this->factory->createBuilder(static::TESTED_TYPE)->setRequestHandler($requestHandler)->getForm(); - $data = $this->createUploadedFileMock($requestHandler, __DIR__.'/../../../Fixtures/foo', 'foo.jpg'); + $data = $this->createUploadedFile($requestHandler, __DIR__.'/../../../Fixtures/foo', 'foo.jpg'); $form->submit($data); @@ -57,8 +56,8 @@ class FileTypeTest extends BaseTypeTest ])->setRequestHandler($requestHandler)->getForm(); $data = [ - $this->createUploadedFileMock($requestHandler, __DIR__.'/../../../Fixtures/foo', 'foo.jpg'), - $this->createUploadedFileMock($requestHandler, __DIR__.'/../../../Fixtures/foo2', 'foo2.jpg'), + $this->createUploadedFile($requestHandler, __DIR__.'/../../../Fixtures/foo', 'foo.jpg'), + $this->createUploadedFile($requestHandler, __DIR__.'/../../../Fixtures/foo2', 'foo2.jpg'), ]; $form->setData($data); @@ -75,8 +74,8 @@ class FileTypeTest extends BaseTypeTest ])->setRequestHandler($requestHandler)->getForm(); $data = [ - $this->createUploadedFileMock($requestHandler, __DIR__.'/../../../Fixtures/foo', 'foo.jpg'), - $this->createUploadedFileMock($requestHandler, __DIR__.'/../../../Fixtures/foo2', 'foo2.jpg'), + $this->createUploadedFile($requestHandler, __DIR__.'/../../../Fixtures/foo', 'foo.jpg'), + $this->createUploadedFile($requestHandler, __DIR__.'/../../../Fixtures/foo2', 'foo2.jpg'), ]; $form->submit($data); @@ -94,7 +93,7 @@ class FileTypeTest extends BaseTypeTest { $form = $this->factory->createBuilder(static::TESTED_TYPE)->setRequestHandler($requestHandler)->getForm(); $form->submit([ - 'file' => $this->createUploadedFileMock($requestHandler, __DIR__.'/../../../Fixtures/foo', 'foo.jpg'), + 'file' => $this->createUploadedFile($requestHandler, __DIR__.'/../../../Fixtures/foo', 'foo.jpg'), ]); $this->assertEquals('', $form->createView()->vars['value']); @@ -152,8 +151,8 @@ class FileTypeTest extends BaseTypeTest ->getForm(); $form->submit([ 'file:///etc/passwd', - $this->createUploadedFileMock(new HttpFoundationRequestHandler(), __DIR__.'/../../../Fixtures/foo', 'foo.jpg'), - $this->createUploadedFileMock(new NativeRequestHandler(), __DIR__.'/../../../Fixtures/foo2', 'foo2.jpg'), + $this->createUploadedFile(new HttpFoundationRequestHandler(), __DIR__.'/../../../Fixtures/foo', 'foo.jpg'), + $this->createUploadedFile(new NativeRequestHandler(), __DIR__.'/../../../Fixtures/foo2', 'foo2.jpg'), ]); $this->assertCount(1, $form->getData()); @@ -185,7 +184,7 @@ class FileTypeTest extends BaseTypeTest ]; } - private function createUploadedFileMock(RequestHandlerInterface $requestHandler, $path, $originalName) + private function createUploadedFile(RequestHandlerInterface $requestHandler, $path, $originalName) { if ($requestHandler instanceof HttpFoundationRequestHandler) { return new UploadedFile($path, $originalName, null, 10, null, true); diff --git a/src/Symfony/Component/Form/Tests/Extension/Csrf/EventListener/CsrfValidationListenerTest.php b/src/Symfony/Component/Form/Tests/Extension/Csrf/EventListener/CsrfValidationListenerTest.php index 1b409a3903..5876b092b9 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Csrf/EventListener/CsrfValidationListenerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Csrf/EventListener/CsrfValidationListenerTest.php @@ -12,9 +12,13 @@ namespace Symfony\Component\Form\Tests\Extension\Csrf\EventListener; use PHPUnit\Framework\TestCase; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; use Symfony\Component\Form\Extension\Csrf\EventListener\CsrfValidationListener; use Symfony\Component\Form\FormBuilder; use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormFactoryBuilder; +use Symfony\Component\Security\Csrf\CsrfTokenManager; class CsrfValidationListenerTest extends TestCase { @@ -25,11 +29,11 @@ class CsrfValidationListenerTest extends TestCase protected function setUp() { - $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); - $this->factory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock(); - $this->tokenManager = $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock(); + $this->dispatcher = new EventDispatcher(); + $this->factory = (new FormFactoryBuilder())->getFormFactory(); + $this->tokenManager = new CsrfTokenManager(); $this->form = $this->getBuilder() - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->getForm(); } @@ -46,11 +50,6 @@ class CsrfValidationListenerTest extends TestCase return new FormBuilder('post', null, $this->dispatcher, $this->factory, ['compound' => true]); } - protected function getDataMapper() - { - return $this->getMockBuilder('Symfony\Component\Form\DataMapperInterface')->getMock(); - } - // https://github.com/symfony/symfony/pull/5838 public function testStringFormData() { diff --git a/src/Symfony/Component/Form/Tests/Extension/DependencyInjection/DependencyInjectionExtensionTest.php b/src/Symfony/Component/Form/Tests/Extension/DependencyInjection/DependencyInjectionExtensionTest.php index b78bd67ae0..375fbfc702 100644 --- a/src/Symfony/Component/Form/Tests/Extension/DependencyInjection/DependencyInjectionExtensionTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/DependencyInjection/DependencyInjectionExtensionTest.php @@ -12,7 +12,8 @@ namespace Symfony\Component\Form\Tests\Extension\DependencyInjection; use PHPUnit\Framework\TestCase; -use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\Form\AbstractTypeExtension; use Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension; use Symfony\Component\Form\FormTypeGuesserChain; use Symfony\Component\Form\FormTypeGuesserInterface; @@ -21,19 +22,16 @@ class DependencyInjectionExtensionTest extends TestCase { public function testGetTypeExtensions() { - $container = $this->createContainerMock(); - $container->expects($this->never())->method('get'); - - $typeExtension1 = $this->createFormTypeExtensionMock('test'); - $typeExtension2 = $this->createFormTypeExtensionMock('test'); - $typeExtension3 = $this->createFormTypeExtensionMock('other'); + $typeExtension1 = new DummyExtension('test'); + $typeExtension2 = new DummyExtension('test'); + $typeExtension3 = new DummyExtension('other'); $extensions = [ 'test' => new \ArrayIterator([$typeExtension1, $typeExtension2]), 'other' => new \ArrayIterator([$typeExtension3]), ]; - $extension = new DependencyInjectionExtension($container, $extensions, []); + $extension = new DependencyInjectionExtension(new ContainerBuilder(), $extensions, []); $this->assertTrue($extension->hasTypeExtensions('test')); $this->assertTrue($extension->hasTypeExtensions('other')); @@ -47,14 +45,11 @@ class DependencyInjectionExtensionTest extends TestCase */ public function testThrowExceptionForInvalidExtendedType() { - $container = $this->getMockBuilder('Psr\Container\ContainerInterface')->getMock(); - $container->expects($this->never())->method('get'); - $extensions = [ - 'test' => new \ArrayIterator([$this->createFormTypeExtensionMock('unmatched')]), + 'test' => new \ArrayIterator([new DummyExtension('unmatched')]), ]; - $extension = new DependencyInjectionExtension($container, $extensions, []); + $extension = new DependencyInjectionExtension(new ContainerBuilder(), $extensions, []); $extension->getTypeExtensions('test'); } @@ -65,23 +60,15 @@ class DependencyInjectionExtensionTest extends TestCase */ public function testLegacyGetTypeExtensions() { - $container = $this->createContainerMock(); + $container = new ContainerBuilder(); - $services = [ - 'extension1' => $typeExtension1 = $this->createFormTypeExtensionMock('test'), - 'extension2' => $typeExtension2 = $this->createFormTypeExtensionMock('test'), - 'extension3' => $typeExtension3 = $this->createFormTypeExtensionMock('other'), - ]; + $typeExtension1 = new DummyExtension('test'); + $typeExtension2 = new DummyExtension('test'); + $typeExtension3 = new DummyExtension('other'); - $container->expects($this->any()) - ->method('get') - ->willReturnCallback(function ($id) use ($services) { - if (isset($services[$id])) { - return $services[$id]; - } - - throw new ServiceNotFoundException($id); - }); + $container->set('extension1', $typeExtension1); + $container->set('extension2', $typeExtension2); + $container->set('extension3', $typeExtension3); $extension = new DependencyInjectionExtension($container, [], ['test' => ['extension1', 'extension2'], 'other' => ['extension3']], []); @@ -97,14 +84,10 @@ class DependencyInjectionExtensionTest extends TestCase */ public function testLegacyThrowExceptionForInvalidExtendedType() { - $formTypeExtension = $this->createFormTypeExtensionMock('unmatched'); + $formTypeExtension = new DummyExtension('unmatched'); - $container = $this->createContainerMock(); - - $container->expects($this->any()) - ->method('get') - ->with('extension') - ->willReturn($formTypeExtension); + $container = new ContainerBuilder(); + $container->set('extension', $formTypeExtension); $extension = new DependencyInjectionExtension($container, [], ['test' => ['extension']], []); @@ -116,16 +99,14 @@ class DependencyInjectionExtensionTest extends TestCase public function testGetTypeGuesser() { - $container = $this->createContainerMock(); - $extension = new DependencyInjectionExtension($container, [], [$this->getMockBuilder(FormTypeGuesserInterface::class)->getMock()]); + $extension = new DependencyInjectionExtension(new ContainerBuilder(), [], [$this->getMockBuilder(FormTypeGuesserInterface::class)->getMock()]); $this->assertInstanceOf(FormTypeGuesserChain::class, $extension->getTypeGuesser()); } public function testGetTypeGuesserReturnsNullWhenNoTypeGuessersHaveBeenConfigured() { - $container = $this->createContainerMock(); - $extension = new DependencyInjectionExtension($container, [], []); + $extension = new DependencyInjectionExtension(new ContainerBuilder(), [], []); $this->assertNull($extension->getTypeGuesser()); } @@ -135,12 +116,9 @@ class DependencyInjectionExtensionTest extends TestCase */ public function testLegacyGetTypeGuesser() { - $container = $this->createContainerMock(); - $container - ->expects($this->once()) - ->method('get') - ->with('foo') - ->willReturn($this->getMockBuilder(FormTypeGuesserInterface::class)->getMock()); + $container = new ContainerBuilder(); + $container->set('foo', new DummyTypeGuesser()); + $extension = new DependencyInjectionExtension($container, [], [], ['foo']); $this->assertInstanceOf(FormTypeGuesserChain::class, $extension->getTypeGuesser()); @@ -151,24 +129,42 @@ class DependencyInjectionExtensionTest extends TestCase */ public function testLegacyGetTypeGuesserReturnsNullWhenNoTypeGuessersHaveBeenConfigured() { - $container = $this->createContainerMock(); - $extension = new DependencyInjectionExtension($container, [], [], []); + $extension = new DependencyInjectionExtension(new ContainerBuilder(), [], [], []); $this->assertNull($extension->getTypeGuesser()); } +} - private function createContainerMock() +class DummyExtension extends AbstractTypeExtension +{ + private $extendedType; + + public function __construct($extendedType) { - return $this->getMockBuilder('Psr\Container\ContainerInterface') - ->setMethods(['get', 'has']) - ->getMock(); + $this->extendedType = $extendedType; } - private function createFormTypeExtensionMock($extendedType) + public function getExtendedType() + { + return $this->extendedType; + } +} + +class DummyTypeGuesser implements FormTypeGuesserInterface +{ + public function guessType($class, $property) + { + } + + public function guessRequired($class, $property) + { + } + + public function guessMaxLength($class, $property) + { + } + + public function guessPattern($class, $property) { - $extension = $this->getMockBuilder('Symfony\Component\Form\FormTypeExtensionInterface')->getMock(); - $extension->expects($this->any())->method('getExtendedType')->willReturn($extendedType); - - return $extension; } } diff --git a/src/Symfony/Component/Form/Tests/Extension/HttpFoundation/HttpFoundationRequestHandlerTest.php b/src/Symfony/Component/Form/Tests/Extension/HttpFoundation/HttpFoundationRequestHandlerTest.php index 5597e83554..2b13451183 100644 --- a/src/Symfony/Component/Form/Tests/Extension/HttpFoundation/HttpFoundationRequestHandlerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/HttpFoundation/HttpFoundationRequestHandlerTest.php @@ -47,7 +47,7 @@ class HttpFoundationRequestHandlerTest extends AbstractRequestHandlerTest return new HttpFoundationRequestHandler($this->serverParams); } - protected function getMockFile($suffix = '') + protected function getUploadedFile($suffix = '') { return new UploadedFile(__DIR__.'/../../Fixtures/foo'.$suffix, 'foo'.$suffix); } diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php index 89e491b63f..1dbac519fe 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php @@ -11,12 +11,16 @@ namespace Symfony\Component\Form\Tests\Extension\Validator\Constraints; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\Exception\TransformationFailedException; use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; use Symfony\Component\Form\Extension\Validator\Constraints\Form; use Symfony\Component\Form\Extension\Validator\Constraints\FormValidator; use Symfony\Component\Form\FormBuilder; +use Symfony\Component\Form\FormFactoryBuilder; +use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\SubmitButtonBuilder; use Symfony\Component\Translation\IdentityTranslator; @@ -34,39 +38,28 @@ use Symfony\Component\Validator\Validation; class FormValidatorTest extends ConstraintValidatorTestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var EventDispatcherInterface */ private $dispatcher; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var FormFactoryInterface */ private $factory; - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $serverParams; - protected function setUp() { - $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); - $this->factory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock(); - $this->serverParams = $this->getMockBuilder('Symfony\Component\Form\Extension\Validator\Util\ServerParams')->setMethods(['getNormalizedIniPostMaxSize', 'getContentLength'])->getMock(); + $this->dispatcher = new EventDispatcher(); + $this->factory = (new FormFactoryBuilder())->getFormFactory(); parent::setUp(); $this->constraint = new Form(); } - protected function createValidator() - { - return new FormValidator($this->serverParams); - } - public function testValidate() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $options = ['validation_groups' => ['group1', 'group2']]; $form = $this->getBuilder('name', '\stdClass', $options) ->setData($object) @@ -81,7 +74,7 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testValidateConstraints() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $constraint1 = new NotNull(['groups' => ['group1', 'group2']]); $constraint2 = new NotBlank(['groups' => 'group2']); @@ -107,11 +100,11 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testValidateChildIfValidConstraint() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $parent = $this->getBuilder('parent') ->setCompound(true) - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->getForm(); $options = [ 'validation_groups' => ['group1', 'group2'], @@ -131,11 +124,11 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testDontValidateIfParentWithoutValidConstraint() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $parent = $this->getBuilder('parent', null) ->setCompound(true) - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->getForm(); $options = ['validation_groups' => ['group1', 'group2']]; $form = $this->getBuilder('name', '\stdClass', $options)->getForm(); @@ -165,13 +158,13 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testValidateConstraintsOptionEvenIfNoValidConstraint() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $constraint1 = new NotNull(['groups' => ['group1', 'group2']]); $constraint2 = new NotBlank(['groups' => 'group2']); $parent = $this->getBuilder('parent', null) ->setCompound(true) - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->getForm(); $options = [ 'validation_groups' => ['group1', 'group2'], @@ -192,7 +185,7 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testDontValidateIfNoValidationGroups() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $form = $this->getBuilder('name', '\stdClass', [ 'validation_groups' => [], @@ -211,13 +204,11 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testDontValidateConstraintsIfNoValidationGroups() { - $object = $this->getMockBuilder('\stdClass')->getMock(); - $constraint1 = $this->getMockBuilder('Symfony\Component\Validator\Constraint')->getMock(); - $constraint2 = $this->getMockBuilder('Symfony\Component\Validator\Constraint')->getMock(); + $object = new \stdClass(); $options = [ 'validation_groups' => [], - 'constraints' => [$constraint1, $constraint2], + 'constraints' => [new NotBlank(), new NotNull()], ]; $form = $this->getBuilder('name', '\stdClass', $options) ->setData($object) @@ -235,7 +226,7 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testDontValidateIfNotSynchronized() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $form = $this->getBuilder('name', '\stdClass', [ 'invalid_message' => 'invalid_message_key', @@ -269,7 +260,7 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testAddInvalidErrorEvenIfNoValidationGroups() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $form = $this->getBuilder('name', '\stdClass', [ 'invalid_message' => 'invalid_message_key', @@ -304,14 +295,12 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testDontValidateConstraintsIfNotSynchronized() { - $object = $this->getMockBuilder('\stdClass')->getMock(); - $constraint1 = $this->getMockBuilder('Symfony\Component\Validator\Constraint')->getMock(); - $constraint2 = $this->getMockBuilder('Symfony\Component\Validator\Constraint')->getMock(); + $object = new \stdClass(); $options = [ 'invalid_message' => 'invalid_message_key', 'validation_groups' => ['group1', 'group2'], - 'constraints' => [$constraint1, $constraint2], + 'constraints' => [new NotBlank(), new NotBlank()], ]; $form = $this->getBuilder('name', '\stdClass', $options) ->setData($object) @@ -339,7 +328,8 @@ class FormValidatorTest extends ConstraintValidatorTestCase // https://github.com/symfony/symfony/issues/4359 public function testDontMarkInvalidIfAnyChildIsNotSynchronized() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); + $object->child = 'bar'; $failingTransformer = new CallbackTransformer( function ($data) { return $data; }, @@ -350,7 +340,7 @@ class FormValidatorTest extends ConstraintValidatorTestCase ->setData($object) ->addViewTransformer($failingTransformer) ->setCompound(true) - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->add( $this->getBuilder('child') ->addViewTransformer($failingTransformer) @@ -369,7 +359,7 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testHandleGroupSequenceValidationGroups() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $options = ['validation_groups' => new GroupSequence(['group1', 'group2'])]; $form = $this->getBuilder('name', '\stdClass', $options) ->setData($object) @@ -385,7 +375,7 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testHandleCallbackValidationGroups() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $options = ['validation_groups' => [$this, 'getValidationGroups']]; $form = $this->getBuilder('name', '\stdClass', $options) ->setData($object) @@ -400,7 +390,7 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testDontExecuteFunctionNames() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $options = ['validation_groups' => 'header']; $form = $this->getBuilder('name', '\stdClass', $options) ->setData($object) @@ -415,7 +405,7 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testHandleClosureValidationGroups() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $options = ['validation_groups' => function (FormInterface $form) { return ['group1', 'group2']; }]; @@ -432,11 +422,11 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testUseValidationGroupOfClickedButton() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $parent = $this->getBuilder('parent') ->setCompound(true) - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->getForm(); $form = $this->getForm('name', '\stdClass', [ 'validation_groups' => 'form_group', @@ -459,11 +449,11 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testDontUseValidationGroupOfUnclickedButton() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $parent = $this->getBuilder('parent') ->setCompound(true) - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->getForm(); $form = $this->getForm('name', '\stdClass', [ 'validation_groups' => 'form_group', @@ -486,12 +476,12 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testUseInheritedValidationGroup() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $parentOptions = ['validation_groups' => 'group']; $parent = $this->getBuilder('parent', null, $parentOptions) ->setCompound(true) - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->getForm(); $formOptions = ['constraints' => [new Valid()]]; $form = $this->getBuilder('name', '\stdClass', $formOptions)->getForm(); @@ -508,12 +498,12 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testUseInheritedCallbackValidationGroup() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $parentOptions = ['validation_groups' => [$this, 'getValidationGroups']]; $parent = $this->getBuilder('parent', null, $parentOptions) ->setCompound(true) - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->getForm(); $formOptions = ['constraints' => [new Valid()]]; $form = $this->getBuilder('name', '\stdClass', $formOptions)->getForm(); @@ -530,7 +520,7 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testUseInheritedClosureValidationGroup() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $parentOptions = [ 'validation_groups' => function (FormInterface $form) { @@ -539,7 +529,7 @@ class FormValidatorTest extends ConstraintValidatorTestCase ]; $parent = $this->getBuilder('parent', null, $parentOptions) ->setCompound(true) - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->getForm(); $formOptions = ['constraints' => [new Valid()]]; $form = $this->getBuilder('name', '\stdClass', $formOptions)->getForm(); @@ -556,7 +546,7 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testAppendPropertyPath() { - $object = $this->getMockBuilder('\stdClass')->getMock(); + $object = new \stdClass(); $form = $this->getBuilder('name', '\stdClass') ->setData($object) ->getForm(); @@ -585,7 +575,7 @@ class FormValidatorTest extends ConstraintValidatorTestCase { $form = $this->getBuilder('parent', null, ['extra_fields_message' => 'Extra!']) ->setCompound(true) - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->add($this->getBuilder('child')) ->getForm(); @@ -606,7 +596,7 @@ class FormValidatorTest extends ConstraintValidatorTestCase { $form = $this->getBuilder('parent', null, ['extra_fields_message' => 'Extra!']) ->setCompound(true) - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->add($this->getBuilder('child')) ->getForm(); @@ -625,22 +615,21 @@ class FormValidatorTest extends ConstraintValidatorTestCase public function testNoViolationIfAllowExtraData() { - $context = $this->getMockExecutionContext(); - $form = $this ->getBuilder('parent', null, ['allow_extra_fields' => true]) ->setCompound(true) - ->setDataMapper($this->getDataMapper()) + ->setDataMapper(new PropertyPathMapper()) ->add($this->getBuilder('child')) ->getForm(); - $form->submit(['foo' => 'bar']); + $context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator()); - $context->expects($this->never()) - ->method('addViolation'); + $form->submit(['foo' => 'bar']); $this->validator->initialize($context); $this->validator->validate($form, new Form()); + + $this->assertCount(0, $context->getViolations()); } /** @@ -674,22 +663,9 @@ class FormValidatorTest extends ConstraintValidatorTestCase $this->assertSame($constraint, $context->getViolations()->get(0)->getConstraint()); } - private function getMockExecutionContext() + protected function createValidator() { - $context = $this->getMockBuilder('Symfony\Component\Validator\Context\ExecutionContextInterface')->getMock(); - $validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')->getMock(); - $contextualValidator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ContextualValidatorInterface')->getMock(); - - $validator->expects($this->any()) - ->method('inContext') - ->with($context) - ->will($this->returnValue($contextualValidator)); - - $context->expects($this->any()) - ->method('getValidator') - ->will($this->returnValue($validator)); - - return $context; + return new FormValidator(); } /** @@ -720,12 +696,4 @@ class FormValidatorTest extends ConstraintValidatorTestCase return $builder->getForm(); } - - /** - * @return \PHPUnit_Framework_MockObject_MockObject - */ - private function getDataMapper() - { - return $this->getMockBuilder('Symfony\Component\Form\DataMapperInterface')->getMock(); - } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/EventListener/ValidationListenerTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/EventListener/ValidationListenerTest.php index 855a90d2e3..76bc07b2ee 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/EventListener/ValidationListenerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/EventListener/ValidationListenerTest.php @@ -12,40 +12,41 @@ namespace Symfony\Component\Form\Tests\Extension\Validator\EventListener; use PHPUnit\Framework\TestCase; +use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; use Symfony\Component\Form\Extension\Validator\Constraints\Form as FormConstraint; use Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener; +use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper; use Symfony\Component\Form\Form; use Symfony\Component\Form\FormBuilder; +use Symfony\Component\Form\FormConfigBuilder; use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormFactoryBuilder; use Symfony\Component\Form\FormFactoryInterface; -use Symfony\Component\PropertyAccess\PropertyPath; use Symfony\Component\Validator\ConstraintViolation; -use Symfony\Component\Validator\ConstraintViolationList; +use Symfony\Component\Validator\ConstraintViolationInterface; +use Symfony\Component\Validator\Context\ExecutionContextInterface; +use Symfony\Component\Validator\Validation; +use Symfony\Component\Validator\Validator\ValidatorInterface; class ValidationListenerTest extends TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var EventDispatcherInterface */ private $dispatcher; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var FormFactoryInterface */ private $factory; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var ValidatorInterface */ private $validator; - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - private $violationMapper; - /** * @var ValidationListener */ @@ -59,40 +60,18 @@ class ValidationListenerTest extends TestCase protected function setUp() { - $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); - $this->factory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock(); - $this->validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')->getMock(); - $this->violationMapper = $this->getMockBuilder('Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapperInterface')->getMock(); - $this->listener = new ValidationListener($this->validator, $this->violationMapper); + $this->dispatcher = new EventDispatcher(); + $this->factory = (new FormFactoryBuilder())->getFormFactory(); + $this->validator = Validation::createValidator(); + $this->listener = new ValidationListener($this->validator, new ViolationMapper()); $this->message = 'Message'; $this->messageTemplate = 'Message template'; $this->params = ['foo' => 'bar']; } - private function getConstraintViolation($code = null) - { - return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code, new FormConstraint()); - } - - private function getBuilder($name = 'name', $propertyPath = null, $dataClass = null) - { - $builder = new FormBuilder($name, $dataClass, $this->dispatcher, $this->factory); - $builder->setPropertyPath(new PropertyPath($propertyPath ?: $name)); - $builder->setAttribute('error_mapping', []); - $builder->setErrorBubbling(false); - $builder->setMapped(true); - - return $builder; - } - - private function getForm($name = 'name', $propertyPath = null, $dataClass = null) - { - return $this->getBuilder($name, $propertyPath, $dataClass)->getForm(); - } - private function createForm($name = '', $compound = false) { - $config = new FormBuilder($name, null, $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), $this->getMockBuilder(FormFactoryInterface::class)->getMock()); + $config = new FormBuilder($name, null, new EventDispatcher(), (new FormFactoryBuilder())->getFormFactory()); $config->setCompound($compound); if ($compound) { @@ -105,35 +84,29 @@ class ValidationListenerTest extends TestCase // More specific mapping tests can be found in ViolationMapperTest public function testMapViolation() { - $violation = $this->getConstraintViolation(); - $form = $this->getForm('street'); + $violation = new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'data', null, null, null, new FormConstraint()); + $form = new Form(new FormConfigBuilder('street', null, new EventDispatcher())); + $form->submit(null); - $this->validator->expects($this->once()) - ->method('validate') - ->will($this->returnValue([$violation])); + $validator = new DummyValidator($violation); + $listener = new ValidationListener($validator, new ViolationMapper()); + $listener->validateForm(new FormEvent($form, null)); - $this->violationMapper->expects($this->once()) - ->method('mapViolation') - ->with($violation, $form, false); - - $this->listener->validateForm(new FormEvent($form, null)); + $this->assertCount(1, $form->getErrors()); + $this->assertSame($violation, $form->getErrors()[0]->getCause()); } public function testMapViolationAllowsNonSyncIfInvalid() { - $violation = $this->getConstraintViolation(FormConstraint::NOT_SYNCHRONIZED_ERROR); - $form = $this->getForm('street'); + $violation = new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'data', null, null, FormConstraint::NOT_SYNCHRONIZED_ERROR, new FormConstraint()); + $form = new SubmittedNotSynchronizedForm(new FormConfigBuilder('street', null, new EventDispatcher())); - $this->validator->expects($this->once()) - ->method('validate') - ->will($this->returnValue([$violation])); + $validator = new DummyValidator($violation); + $listener = new ValidationListener($validator, new ViolationMapper()); + $listener->validateForm(new FormEvent($form, null)); - $this->violationMapper->expects($this->once()) - ->method('mapViolation') - // pass true now - ->with($violation, $form, true); - - $this->listener->validateForm(new FormEvent($form, null)); + $this->assertCount(1, $form->getErrors()); + $this->assertSame($violation, $form->getErrors()[0]->getCause()); } public function testValidateIgnoresNonRoot() @@ -143,36 +116,72 @@ class ValidationListenerTest extends TestCase $form = $this->createForm('', true); $form->add($childForm); - $this->validator->expects($this->never()) - ->method('validate'); - - $this->violationMapper->expects($this->never()) - ->method('mapViolation'); + $form->submit(['child' => null]); $this->listener->validateForm(new FormEvent($childForm, null)); + + $this->assertTrue($childForm->isValid()); } public function testValidateWithEmptyViolationList() { $form = $this->createForm(); - - $this->validator - ->expects($this->once()) - ->method('validate') - ->will($this->returnValue(new ConstraintViolationList())); - - $this->violationMapper - ->expects($this->never()) - ->method('mapViolation'); + $form->submit(null); $this->listener->validateForm(new FormEvent($form, null)); - } - public function testValidatorInterface() - { - $validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')->getMock(); - - $listener = new ValidationListener($validator, $this->violationMapper); - $this->assertAttributeSame($validator, 'validator', $listener); + $this->assertTrue($form->isValid()); + } +} + +class SubmittedNotSynchronizedForm extends Form +{ + public function isSubmitted() + { + return true; + } + + public function isSynchronized() + { + return false; + } +} + +class DummyValidator implements ValidatorInterface +{ + private $violation; + + public function __construct(ConstraintViolationInterface $violation) + { + $this->violation = $violation; + } + + public function getMetadataFor($value) + { + } + + public function hasMetadataFor($value) + { + } + + public function validate($value, $constraints = null, $groups = null) + { + return [$this->violation]; + } + + public function validateProperty($object, $propertyName, $groups = null) + { + } + + public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null) + { + } + + public function startContext() + { + } + + public function inContext(ExecutionContextInterface $context) + { } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php index 8437d6f097..53d29a1911 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Form\Tests\Extension\Validator\Type; -use Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension; use Symfony\Component\Form\Extension\Validator\ValidatorExtension; use Symfony\Component\Form\Forms; use Symfony\Component\Form\Test\Traits\ValidatorExtensionTrait; @@ -56,14 +55,6 @@ class FormTypeValidatorExtensionTest extends BaseValidatorExtensionTest $this->assertSame([$valid], $form->getConfig()->getOption('constraints')); } - public function testValidatorInterface() - { - $validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')->getMock(); - - $formTypeValidatorExtension = new FormTypeValidatorExtension($validator); - $this->assertAttributeSame($validator, 'validator', $formTypeValidatorExtension); - } - public function testGroupSequenceWithConstraintsOption() { $form = Forms::createFormFactoryBuilder() diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/UploadValidatorExtensionTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Type/UploadValidatorExtensionTest.php index 7d4178bc83..c4d8e8d948 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/UploadValidatorExtensionTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Type/UploadValidatorExtensionTest.php @@ -15,23 +15,17 @@ use Symfony\Component\Form\Extension\Validator\Type\UploadValidatorExtension; use Symfony\Component\Form\Test\TypeTestCase; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Translation\TranslatorInterface; class UploadValidatorExtensionTest extends TypeTestCase { public function testPostMaxSizeTranslation() { - $translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock(); - - $translator->expects($this->any()) - ->method('trans') - ->with($this->equalTo('old max {{ max }}!')) - ->willReturn('translated max {{ max }}!'); - - $extension = new UploadValidatorExtension($translator); + $extension = new UploadValidatorExtension(new DummyTranslator()); $resolver = new OptionsResolver(); $resolver->setDefault('post_max_size_message', 'old max {{ max }}!'); - $resolver->setDefault('upload_max_size_message', function (Options $options, $message) { + $resolver->setDefault('upload_max_size_message', function (Options $options) { return function () use ($options) { return $options['post_max_size_message']; }; @@ -43,3 +37,25 @@ class UploadValidatorExtensionTest extends TypeTestCase $this->assertEquals('translated max {{ max }}!', \call_user_func($options['upload_max_size_message'])); } } + +class DummyTranslator implements TranslatorInterface +{ + public function trans($id, array $parameters = [], $domain = null, $locale = null) + { + return 'translated max {{ max }}!'; + } + + public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null) + { + return 'translated max {{ max }}!'; + } + + public function setLocale($locale) + { + } + + public function getLocale() + { + return 'en'; + } +} diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Util/ServerParamsTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Util/ServerParamsTest.php index 0803e18de9..049876246c 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Util/ServerParamsTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Util/ServerParamsTest.php @@ -14,6 +14,7 @@ namespace Symfony\Component\Form\Tests\Extension\Validator\Util; use PHPUnit\Framework\TestCase; use Symfony\Component\Form\Extension\Validator\Util\ServerParams; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; class ServerParamsTest extends TestCase { @@ -32,8 +33,8 @@ class ServerParamsTest extends TestCase public function testGetContentLengthFromRequest() { $request = Request::create('http://foo', 'GET', [], [], [], ['CONTENT_LENGTH' => 1024]); - $requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->setMethods(['getCurrentRequest'])->getMock(); - $requestStack->expects($this->once())->method('getCurrentRequest')->will($this->returnValue($request)); + $requestStack = new RequestStack(); + $requestStack->push($request); $serverParams = new ServerParams($requestStack); $this->assertEquals(1024, $serverParams->getContentLength()); @@ -42,11 +43,7 @@ class ServerParamsTest extends TestCase /** @dataProvider getGetPostMaxSizeTestData */ public function testGetPostMaxSize($size, $bytes) { - $serverParams = $this->getMockBuilder('Symfony\Component\Form\Extension\Validator\Util\ServerParams')->setMethods(['getNormalizedIniPostMaxSize'])->getMock(); - $serverParams - ->expects($this->any()) - ->method('getNormalizedIniPostMaxSize') - ->will($this->returnValue(strtoupper($size))); + $serverParams = new DummyServerParams($size); $this->assertEquals($bytes, $serverParams->getPostMaxSize()); } @@ -70,3 +67,20 @@ class ServerParamsTest extends TestCase ]; } } + +class DummyServerParams extends ServerParams +{ + private $size; + + public function __construct($size) + { + parent::__construct(); + + $this->size = $size; + } + + public function getNormalizedIniPostMaxSize() + { + return $this->size; + } +} diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorExtensionTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorExtensionTest.php index ce18d0b6c5..b526650ea0 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorExtensionTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorExtensionTest.php @@ -12,37 +12,37 @@ namespace Symfony\Component\Form\Tests\Extension\Validator; use PHPUnit\Framework\TestCase; +use Symfony\Component\Form\Extension\Validator\Constraints\Form as FormConstraint; use Symfony\Component\Form\Extension\Validator\ValidatorExtension; +use Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser; +use Symfony\Component\Form\Form; +use Symfony\Component\Validator\Mapping\CascadingStrategy; +use Symfony\Component\Validator\Mapping\ClassMetadata; +use Symfony\Component\Validator\Mapping\TraversalStrategy; +use Symfony\Component\Validator\Tests\Fixtures\FakeMetadataFactory; +use Symfony\Component\Validator\Validation; class ValidatorExtensionTest extends TestCase { public function test2Dot5ValidationApi() { - $validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\RecursiveValidator') - ->disableOriginalConstructor() - ->getMock(); - $metadata = $this->getMockBuilder('Symfony\Component\Validator\Mapping\ClassMetadata') - ->setMethods(['addConstraint', 'addPropertyConstraint']) - ->disableOriginalConstructor() - ->getMock(); + $metadata = new ClassMetadata(Form::class); - $validator->expects($this->once()) - ->method('getMetadataFor') - ->with($this->identicalTo('Symfony\Component\Form\Form')) - ->will($this->returnValue($metadata)); + $metadataFactory = new FakeMetadataFactory(); + $metadataFactory->addMetadata($metadata); - // Verify that the constraints are added - $metadata->expects($this->once()) - ->method('addConstraint') - ->with($this->isInstanceOf('Symfony\Component\Form\Extension\Validator\Constraints\Form')); - - $metadata->expects($this->once()) - ->method('addPropertyConstraint') - ->with('children', $this->isInstanceOf('Symfony\Component\Validator\Constraints\Valid')); + $validator = Validation::createValidatorBuilder() + ->setMetadataFactory($metadataFactory) + ->getValidator(); $extension = new ValidatorExtension($validator); - $guesser = $extension->loadTypeGuesser(); - $this->assertInstanceOf('Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser', $guesser); + $this->assertInstanceOf(ValidatorTypeGuesser::class, $extension->loadTypeGuesser()); + + $this->assertCount(1, $metadata->getConstraints()); + $this->assertInstanceOf(FormConstraint::class, $metadata->getConstraints()[0]); + + $this->assertSame(CascadingStrategy::CASCADE, $metadata->getPropertyMetadata('children')[0]->cascadingStrategy); + $this->assertSame(TraversalStrategy::IMPLICIT, $metadata->getPropertyMetadata('children')[0]->traversalStrategy); } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php index d14c89c2cb..878bbfad21 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/ValidatorTypeGuesserTest.php @@ -23,6 +23,8 @@ use Symfony\Component\Validator\Constraints\NotNull; use Symfony\Component\Validator\Constraints\Range; use Symfony\Component\Validator\Constraints\Type; use Symfony\Component\Validator\Mapping\ClassMetadata; +use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; +use Symfony\Component\Validator\Tests\Fixtures\FakeMetadataFactory; /** * @author franek @@ -45,18 +47,15 @@ class ValidatorTypeGuesserTest extends TestCase private $metadata; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var MetadataFactoryInterface */ private $metadataFactory; protected function setUp() { $this->metadata = new ClassMetadata(self::TEST_CLASS); - $this->metadataFactory = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface')->getMock(); - $this->metadataFactory->expects($this->any()) - ->method('getMetadataFor') - ->with(self::TEST_CLASS) - ->will($this->returnValue($this->metadata)); + $this->metadataFactory = new FakeMetadataFactory(); + $this->metadataFactory->addMetadata($this->metadata); $this->guesser = new ValidatorTypeGuesser($this->metadataFactory); } diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php index 1278a86e22..5d0afa238c 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php @@ -12,8 +12,11 @@ namespace Symfony\Component\Form\Tests\Extension\Validator\ViolationMapper; use PHPUnit\Framework\TestCase; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\Exception\TransformationFailedException; +use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper; use Symfony\Component\Form\Form; use Symfony\Component\Form\FormConfigBuilder; @@ -34,7 +37,7 @@ class ViolationMapperTest extends TestCase const LEVEL_2 = 3; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var EventDispatcherInterface */ private $dispatcher; @@ -60,7 +63,7 @@ class ViolationMapperTest extends TestCase protected function setUp() { - $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); + $this->dispatcher = new EventDispatcher(); $this->mapper = new ViolationMapper(); $this->message = 'Message'; $this->messageTemplate = 'Message template'; @@ -76,7 +79,7 @@ class ViolationMapperTest extends TestCase $config->setInheritData($inheritData); $config->setPropertyPath($propertyPath); $config->setCompound(true); - $config->setDataMapper($this->getDataMapper()); + $config->setDataMapper(new PropertyPathMapper()); if (!$synchronized) { $config->addViewTransformer(new CallbackTransformer( @@ -88,14 +91,6 @@ class ViolationMapperTest extends TestCase return new Form($config); } - /** - * @return \PHPUnit_Framework_MockObject_MockObject - */ - private function getDataMapper() - { - return $this->getMockBuilder('Symfony\Component\Form\DataMapperInterface')->getMock(); - } - /** * @param $propertyPath * diff --git a/src/Symfony/Component/Form/Tests/NativeRequestHandlerTest.php b/src/Symfony/Component/Form/Tests/NativeRequestHandlerTest.php index 077f477d4a..29b1156aa6 100644 --- a/src/Symfony/Component/Form/Tests/NativeRequestHandlerTest.php +++ b/src/Symfony/Component/Form/Tests/NativeRequestHandlerTest.php @@ -201,7 +201,7 @@ class NativeRequestHandlerTest extends AbstractRequestHandlerTest return new NativeRequestHandler($this->serverParams); } - protected function getMockFile($suffix = '') + protected function getUploadedFile($suffix = '') { return [ 'name' => 'upload'.$suffix.'.txt',