[Form] ensure compatibility with older PHPUnit mocks

This commit is contained in:
Christian Flothmann 2019-01-18 18:22:25 +01:00 committed by Nicolas Grekas
parent 0c323028d2
commit 0d9de7e7e3
18 changed files with 291 additions and 338 deletions

View File

@ -15,6 +15,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormConfigInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\JsonResponse;
@ -487,8 +488,7 @@ abstract class ControllerTraitTest extends TestCase
public function testCreateForm()
{
$config = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')->getMock();
$form = new Form($config);
$form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock());
$formFactory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock();
$formFactory->expects($this->once())->method('create')->willReturn($form);

View File

@ -13,6 +13,7 @@ namespace Symfony\Bundle\WebProfilerBundle\Tests\Profiler;
use Symfony\Bundle\WebProfilerBundle\Profiler\TemplateManager;
use Symfony\Bundle\WebProfilerBundle\Tests\TestCase;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Twig\Environment;
/**
@ -57,8 +58,7 @@ class TemplateManagerTest extends TestCase
*/
public function testGetNameOfInvalidTemplate()
{
$profile = $this->mockProfile();
$this->templateManager->getName($profile, 'notexistingpanel');
$this->templateManager->getName(new Profile('token'), 'notexistingpanel');
}
/**
@ -71,12 +71,7 @@ class TemplateManagerTest extends TestCase
->withAnyParameters()
->will($this->returnCallback([$this, 'profilerHasCallback']));
$profile = $this->mockProfile();
$profile->expects($this->any())
->method('hasCollector')
->will($this->returnCallback([$this, 'profileHasCollectorCallback']));
$this->assertEquals('FooBundle:Collector:foo.html.twig', $this->templateManager->getName($profile, 'foo'));
$this->assertEquals('FooBundle:Collector:foo.html.twig', $this->templateManager->getName(new ProfileDummy(), 'foo'));
}
/**
@ -85,17 +80,12 @@ class TemplateManagerTest extends TestCase
*/
public function testGetTemplates()
{
$profile = $this->mockProfile();
$profile->expects($this->any())
->method('hasCollector')
->will($this->returnCallback([$this, 'profilerHasCallback']));
$this->profiler->expects($this->any())
->method('has')
->withAnyParameters()
->will($this->returnCallback([$this, 'profileHasCollectorCallback']));
$result = $this->templateManager->getTemplates($profile);
$result = $this->templateManager->getTemplates(new ProfileDummy());
$this->assertArrayHasKey('foo', $result);
$this->assertArrayNotHasKey('bar', $result);
$this->assertArrayNotHasKey('baz', $result);
@ -155,3 +145,22 @@ class TemplateManagerTest extends TestCase
return $this->profiler;
}
}
class ProfileDummy extends Profile
{
public function __construct()
{
parent::__construct('token');
}
public function hasCollector($name)
{
switch ($name) {
case 'foo':
case 'bar':
return true;
default:
return false;
}
}
}

View File

@ -532,9 +532,11 @@ class Form implements \IteratorAggregate, FormInterface
$submittedData = null;
} elseif (is_scalar($submittedData)) {
$submittedData = (string) $submittedData;
} elseif (!$this->config->getOption('allow_file_upload') && $this->config->getRequestHandler()->isFileUpload($submittedData)) {
$submittedData = null;
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, file upload given.');
} elseif ($this->config->getRequestHandler()->isFileUpload($submittedData)) {
if (!$this->config->getOption('allow_file_upload')) {
$submittedData = null;
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, file upload given.');
}
} elseif (\is_array($submittedData) && !$this->config->getCompound() && !$this->config->hasOption('multiple')) {
$submittedData = null;
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, array given.');

View File

@ -65,26 +65,6 @@ abstract class AbstractFormTest extends TestCase
return new FormBuilder($name, $dataClass, $dispatcher ?: $this->dispatcher, $this->factory, $options);
}
/**
* @param string $name
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
protected function getMockForm($name = 'name')
{
$form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
$config = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')->getMock();
$form->expects($this->any())
->method('getName')
->will($this->returnValue($name));
$form->expects($this->any())
->method('getConfig')
->will($this->returnValue($config));
return $form;
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject
*/

View File

@ -12,6 +12,10 @@
namespace Symfony\Component\Form\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\Form\Forms;
@ -66,17 +70,16 @@ abstract class AbstractRequestHandlerTest extends TestCase
*/
public function testSubmitIfNameInRequest($method)
{
$form = $this->getMockForm('param1', $method);
$form = $this->createForm('param1', $method);
$this->setRequestData($method, [
'param1' => 'DATA',
]);
$form->expects($this->once())
->method('submit')
->with('DATA', 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request);
$this->assertTrue($form->isSubmitted());
$this->assertSame('DATA', $form->getData());
}
/**
@ -84,7 +87,7 @@ abstract class AbstractRequestHandlerTest extends TestCase
*/
public function testDoNotSubmitIfWrongRequestMethod($method)
{
$form = $this->getMockForm('param1', $method);
$form = $this->createForm('param1', $method);
$otherMethod = 'POST' === $method ? 'PUT' : 'POST';
@ -92,10 +95,9 @@ abstract class AbstractRequestHandlerTest extends TestCase
'param1' => 'DATA',
]);
$form->expects($this->never())
->method('submit');
$this->requestHandler->handleRequest($form, $this->request);
$this->assertFalse($form->isSubmitted());
}
/**
@ -103,16 +105,15 @@ abstract class AbstractRequestHandlerTest extends TestCase
*/
public function testDoNoSubmitSimpleFormIfNameNotInRequestAndNotGetRequest($method)
{
$form = $this->getMockForm('param1', $method, false);
$form = $this->createForm('param1', $method, false);
$this->setRequestData($method, [
'paramx' => [],
]);
$form->expects($this->never())
->method('submit');
$this->requestHandler->handleRequest($form, $this->request);
$this->assertFalse($form->isSubmitted());
}
/**
@ -120,30 +121,28 @@ abstract class AbstractRequestHandlerTest extends TestCase
*/
public function testDoNotSubmitCompoundFormIfNameNotInRequestAndNotGetRequest($method)
{
$form = $this->getMockForm('param1', $method, true);
$form = $this->createForm('param1', $method, true);
$this->setRequestData($method, [
'paramx' => [],
]);
$form->expects($this->never())
->method('submit');
$this->requestHandler->handleRequest($form, $this->request);
$this->assertFalse($form->isSubmitted());
}
public function testDoNotSubmitIfNameNotInRequestAndGetRequest()
{
$form = $this->getMockForm('param1', 'GET');
$form = $this->createForm('param1', 'GET');
$this->setRequestData('GET', [
'paramx' => [],
]);
$form->expects($this->never())
->method('submit');
$this->requestHandler->handleRequest($form, $this->request);
$this->assertFalse($form->isSubmitted());
}
/**
@ -151,24 +150,28 @@ abstract class AbstractRequestHandlerTest extends TestCase
*/
public function testSubmitFormWithEmptyNameIfAtLeastOneFieldInRequest($method)
{
$form = $this->getMockForm('', $method);
$form->expects($this->any())
->method('all')
->will($this->returnValue([
'param1' => $this->getMockForm('param1'),
'param2' => $this->getMockForm('param2'),
]));
$form = $this->createForm('', $method, true);
$form->add($this->createForm('param1'));
$form->add($this->createForm('param2'));
$this->setRequestData($method, $requestData = [
'param1' => 'submitted value',
'paramx' => 'submitted value',
]);
$form->expects($this->once())
->method('submit')
->with($requestData, 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request);
$this->assertTrue($form->isSubmitted());
$this->assertTrue($form->get('param1')->isSubmitted());
$this->assertSame('submitted value', $form->get('param1')->getData());
if ('PATCH' === $method) {
$this->assertFalse($form->get('param2')->isSubmitted());
} else {
$this->assertTrue($form->get('param2')->isSubmitted());
}
$this->assertNull($form->get('param2')->getData());
}
/**
@ -176,22 +179,17 @@ abstract class AbstractRequestHandlerTest extends TestCase
*/
public function testDoNotSubmitFormWithEmptyNameIfNoFieldInRequest($method)
{
$form = $this->getMockForm('', $method);
$form->expects($this->any())
->method('all')
->will($this->returnValue([
'param1' => $this->getMockForm('param1'),
'param2' => $this->getMockForm('param2'),
]));
$form = $this->createForm('', $method, true);
$form->add($this->createForm('param1'));
$form->add($this->createForm('param2'));
$this->setRequestData($method, [
'paramx' => 'submitted value',
]);
$form->expects($this->never())
->method('submit');
$this->requestHandler->handleRequest($form, $this->request);
$this->assertFalse($form->isSubmitted());
}
/**
@ -199,7 +197,9 @@ abstract class AbstractRequestHandlerTest extends TestCase
*/
public function testMergeParamsAndFiles($method)
{
$form = $this->getMockForm('param1', $method);
$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();
$this->setRequestData($method, [
@ -212,14 +212,11 @@ abstract class AbstractRequestHandlerTest extends TestCase
],
]);
$form->expects($this->once())
->method('submit')
->with([
'field1' => 'DATA',
'field2' => $file,
], 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request);
$this->assertTrue($form->isSubmitted());
$this->assertSame('DATA', $form->get('field1')->getData());
$this->assertSame($file, $form->get('field2')->getData());
}
/**
@ -227,7 +224,7 @@ abstract class AbstractRequestHandlerTest extends TestCase
*/
public function testParamTakesPrecedenceOverFile($method)
{
$form = $this->getMockForm('param1', $method);
$form = $this->createForm('param1', $method);
$file = $this->getMockFile();
$this->setRequestData($method, [
@ -236,11 +233,10 @@ abstract class AbstractRequestHandlerTest extends TestCase
'param1' => $file,
]);
$form->expects($this->once())
->method('submit')
->with('DATA', 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request);
$this->assertTrue($form->isSubmitted());
$this->assertSame('DATA', $form->getData());
}
/**
@ -248,7 +244,9 @@ abstract class AbstractRequestHandlerTest extends TestCase
*/
public function testSubmitFileIfNoParam($method)
{
$form = $this->getMockForm('param1', $method);
$form = $this->createBuilder('param1', false, ['allow_file_upload' => true])
->setMethod($method)
->getForm();
$file = $this->getMockFile();
$this->setRequestData($method, [
@ -257,11 +255,10 @@ abstract class AbstractRequestHandlerTest extends TestCase
'param1' => $file,
]);
$form->expects($this->once())
->method('submit')
->with($file, 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request);
$this->assertTrue($form->isSubmitted());
$this->assertSame($file, $form->getData());
}
/**
@ -269,7 +266,9 @@ abstract class AbstractRequestHandlerTest extends TestCase
*/
public function testSubmitMultipleFiles($method)
{
$form = $this->getMockForm('param1', $method);
$form = $this->createBuilder('param1', false, ['allow_file_upload' => true])
->setMethod($method)
->getForm();
$file = $this->getMockFile();
$this->setRequestData($method, [
@ -280,32 +279,10 @@ abstract class AbstractRequestHandlerTest extends TestCase
'param3' => $this->getMockFile('3'),
]);
$form->expects($this->once())
->method('submit')
->with($file, 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request);
}
/**
* @dataProvider methodExceptGetProvider
*/
public function testSubmitFileWithNamelessForm($method)
{
$form = $this->getMockForm(null, $method);
$file = $this->getMockFile();
$this->setRequestData($method, [
'' => null,
], [
'' => $file,
]);
$form->expects($this->once())
->method('submit')
->with($file, 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request);
$this->assertTrue($form->isSubmitted());
$this->assertSame($file, $form->getData());
}
/**
@ -371,24 +348,26 @@ abstract class AbstractRequestHandlerTest extends TestCase
abstract protected function getInvalidFile();
protected function getMockForm($name, $method = null, $compound = true)
protected function createForm($name, $method = null, $compound = false)
{
$config = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')->getMock();
$config->expects($this->any())
->method('getMethod')
->will($this->returnValue($method));
$config->expects($this->any())
->method('getCompound')
->will($this->returnValue($compound));
$config = $this->createBuilder($name, $compound);
$form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
$form->expects($this->any())
->method('getName')
->will($this->returnValue($name));
$form->expects($this->any())
->method('getConfig')
->will($this->returnValue($config));
if (null !== $method) {
$config->setMethod($method);
}
return $form;
return new Form($config);
}
protected function createBuilder($name, $compound = false, array $options = [])
{
$builder = new FormBuilder($name, null, new EventDispatcher(), $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock(), $options);
$builder->setCompound($compound);
if ($compound) {
$builder->setDataMapper(new PropertyPathMapper());
}
return $builder;
}
}

View File

@ -11,9 +11,12 @@
namespace Symfony\Component\Form\Tests;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\SubmitButtonBuilder;
@ -69,40 +72,35 @@ class CompoundFormTest extends AbstractFormTest
public function testSubmitForwardsNullIfNotClearMissingButValueIsExplicitlyNull()
{
$child = $this->getMockForm('firstName');
$child = $this->createForm('firstName', false);
$this->form->add($child);
$child->expects($this->once())
->method('submit')
->with($this->equalTo(null));
$this->form->submit(['firstName' => null], false);
$this->assertNull($this->form->get('firstName')->getData());
}
public function testSubmitForwardsNullIfValueIsMissing()
{
$child = $this->getMockForm('firstName');
$child = $this->createForm('firstName', false);
$this->form->add($child);
$child->expects($this->once())
->method('submit')
->with($this->equalTo(null));
$this->form->submit([]);
$this->assertNull($this->form->get('firstName')->getData());
}
public function testSubmitDoesNotForwardNullIfNotClearMissing()
{
$child = $this->getMockForm('firstName');
$child = $this->createForm('firstName', false);
$this->form->add($child);
$child->expects($this->never())
->method('submit');
$this->form->submit([], false);
$this->assertFalse($child->isSubmitted());
}
public function testSubmitDoesNotAddExtraFieldForNullValues()
@ -120,15 +118,22 @@ class CompoundFormTest extends AbstractFormTest
public function testClearMissingFlagIsForwarded()
{
$child = $this->getMockForm('firstName');
$personForm = $this->createForm('person');
$this->form->add($child);
$firstNameForm = $this->createForm('firstName', false);
$personForm->add($firstNameForm);
$child->expects($this->once())
->method('submit')
->with($this->equalTo('foo'), false);
$lastNameForm = $this->createForm('lastName', false);
$lastNameForm->setData('last name');
$personForm->add($lastNameForm);
$this->form->submit(['firstName' => 'foo'], false);
$this->form->add($personForm);
$this->form->submit(['person' => ['firstName' => 'foo']], false);
$this->assertTrue($firstNameForm->isSubmitted());
$this->assertSame('foo', $firstNameForm->getData());
$this->assertFalse($lastNameForm->isSubmitted());
$this->assertSame('last name', $lastNameForm->getData());
}
public function testCloneChildren()
@ -145,10 +150,8 @@ class CompoundFormTest extends AbstractFormTest
public function testNotEmptyIfChildNotEmpty()
{
$child = $this->getMockForm();
$child->expects($this->once())
->method('isEmpty')
->will($this->returnValue(false));
$child = $this->createForm('name', false);
$child->setData('foo');
$this->form->setData(null);
$this->form->add($child);
@ -400,29 +403,25 @@ class CompoundFormTest extends AbstractFormTest
->setDataMapper(new PropertyPathMapper())
->getForm();
$child = $this->getMockForm('child');
$childToBeRemoved = $this->getMockForm('removed');
$childToBeAdded = $this->getMockForm('added');
$childToBeRemoved = $this->createForm('removed', false);
$childToBeAdded = $this->createForm('added', false);
$child = $this->getBuilder('child', new EventDispatcher())
->setCompound(true)
->setDataMapper(new PropertyPathMapper())
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($form, $childToBeAdded) {
$form->remove('removed');
$form->add($childToBeAdded);
})
->getForm();
$form->add($child);
$form->add($childToBeRemoved);
$child->expects($this->once())
->method('setData')
->will($this->returnCallback(function () use ($form, $childToBeAdded) {
$form->remove('removed');
$form->add($childToBeAdded);
}));
$childToBeRemoved->expects($this->never())
->method('setData');
// once when it it is created, once when it is added
$childToBeAdded->expects($this->exactly(2))
->method('setData');
// pass NULL to all children
$form->setData([]);
$this->assertFalse($form->has('removed'));
$this->assertTrue($form->has('added'));
}
public function testSetDataMapsViewDataToChildren()
@ -453,30 +452,25 @@ class CompoundFormTest extends AbstractFormTest
public function testSubmitSupportsDynamicAdditionAndRemovalOfChildren()
{
$child = $this->getMockForm('child');
$childToBeRemoved = $this->getMockForm('removed');
$childToBeAdded = $this->getMockForm('added');
$form = $this->form;
$childToBeRemoved = $this->createForm('removed');
$childToBeAdded = $this->createForm('added');
$child = $this->getBuilder('child')
->addEventListener(FormEvents::PRE_SUBMIT, function () use ($form, $childToBeAdded) {
$form->remove('removed');
$form->add($childToBeAdded);
})
->getForm();
$this->form->add($child);
$this->form->add($childToBeRemoved);
$form = $this->form;
$child->expects($this->once())
->method('submit')
->will($this->returnCallback(function () use ($form, $childToBeAdded) {
$form->remove('removed');
$form->add($childToBeAdded);
}));
$childToBeRemoved->expects($this->never())
->method('submit');
$childToBeAdded->expects($this->once())
->method('submit');
// pass NULL to all children
$this->form->submit([]);
$this->assertFalse($childToBeRemoved->isSubmitted());
$this->assertTrue($childToBeAdded->isSubmitted());
}
public function testSubmitMapsSubmittedChildrenOntoExistingViewData()
@ -880,12 +874,24 @@ class CompoundFormTest extends AbstractFormTest
public function testCreateViewWithChildren()
{
$type = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock();
$type1 = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock();
$type2 = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock();
$options = ['a' => 'Foo', 'b' => 'Bar'];
$field1 = $this->getMockForm('foo');
$field2 = $this->getMockForm('bar');
$field1 = $this->getBuilder('foo')
->setType($type1)
->getForm();
$field2 = $this->getBuilder('bar')
->setType($type2)
->getForm();
$view = new FormView();
$field1View = new FormView();
$type1
->method('createView')
->will($this->returnValue($field1View));
$field2View = new FormView();
$type2
->method('createView')
->will($this->returnValue($field2View));
$this->form = $this->getBuilder('form', null, null, $options)
->setCompound(true)
@ -912,24 +918,8 @@ class CompoundFormTest extends AbstractFormTest
->with($view, $this->form, $options)
->will($this->returnCallback($assertChildViewsEqual([])));
// Then add the first child form
$field1->expects($this->once())
->method('createView')
->will($this->returnValue($field1View));
// Then the second child form
$field2->expects($this->once())
->method('createView')
->will($this->returnValue($field2View));
// Again build the view for the form itself. This time the child views
// exist.
$type->expects($this->once())
->method('finishView')
->with($view, $this->form, $options)
->will($this->returnCallback($assertChildViewsEqual(['foo' => $field1View, 'bar' => $field2View])));
$this->assertSame($view, $this->form->createView());
$this->assertSame(['foo' => $field1View, 'bar' => $field2View], $view->children);
}
public function testNoClickedButtonBeforeSubmission()
@ -1022,7 +1012,7 @@ class CompoundFormTest extends AbstractFormTest
->getForm();
$form = $this->createForm()
->add($this->getBuilder('text')->getForm())
->add($this->createForm('text', false))
->add($submit)
;
@ -1067,11 +1057,17 @@ class CompoundFormTest extends AbstractFormTest
$this->assertNull($this->form->get('bar')->getData());
}
protected function createForm()
protected function createForm($name = 'name', $compound = true)
{
return $this->getBuilder()
->setCompound(true)
->setDataMapper($this->getDataMapper())
->getForm();
$builder = $this->getBuilder($name);
if ($compound) {
$builder
->setCompound(true)
->setDataMapper($this->getDataMapper())
;
}
return $builder->getForm();
}
}

View File

@ -13,6 +13,8 @@ namespace Symfony\Component\Form\Tests\Extension\Core\EventListener;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Extension\Core\EventListener\FixUrlProtocolListener;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormConfigInterface;
use Symfony\Component\Form\FormEvent;
class FixUrlProtocolListenerTest extends TestCase
@ -20,7 +22,7 @@ class FixUrlProtocolListenerTest extends TestCase
public function testFixHttpUrl()
{
$data = 'www.symfony.com';
$form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
$form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock());
$event = new FormEvent($form, $data);
$filter = new FixUrlProtocolListener('http');
@ -32,7 +34,7 @@ class FixUrlProtocolListenerTest extends TestCase
public function testSkipKnownUrl()
{
$data = 'http://www.symfony.com';
$form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
$form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock());
$event = new FormEvent($form, $data);
$filter = new FixUrlProtocolListener('http');
@ -57,7 +59,7 @@ class FixUrlProtocolListenerTest extends TestCase
*/
public function testSkipOtherProtocol($url)
{
$form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
$form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock());
$event = new FormEvent($form, $url);
$filter = new FixUrlProtocolListener('http');

View File

@ -44,11 +44,6 @@ abstract class MergeCollectionListenerTest extends TestCase
return $this->getBuilder($name)->setAttribute('property_path', $propertyPath)->getForm();
}
protected function getMockForm()
{
return $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
}
public function getBooleanMatrix1()
{
return [

View File

@ -58,11 +58,6 @@ class ResizeFormListenerTest extends TestCase
return $this->getMockBuilder('Symfony\Component\Form\DataMapperInterface')->getMock();
}
protected function getMockForm()
{
return $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
}
public function testPreSetDataResizesForm()
{
$this->form->add($this->getForm('0'));

View File

@ -13,6 +13,8 @@ namespace Symfony\Component\Form\Tests\Extension\Core\EventListener;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Extension\Core\EventListener\TrimListener;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormConfigInterface;
use Symfony\Component\Form\FormEvent;
class TrimListenerTest extends TestCase
@ -20,7 +22,7 @@ class TrimListenerTest extends TestCase
public function testTrim()
{
$data = ' Foo! ';
$form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
$form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock());
$event = new FormEvent($form, $data);
$filter = new TrimListener();
@ -32,7 +34,7 @@ class TrimListenerTest extends TestCase
public function testTrimSkipNonStrings()
{
$data = 1234;
$form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
$form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock());
$event = new FormEvent($form, $data);
$filter = new TrimListener();

View File

@ -51,11 +51,6 @@ class CsrfValidationListenerTest extends TestCase
return $this->getMockBuilder('Symfony\Component\Form\DataMapperInterface')->getMock();
}
protected function getMockForm()
{
return $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
}
// https://github.com/symfony/symfony/pull/5838
public function testStringFormData()
{

View File

@ -26,7 +26,7 @@ class HttpFoundationRequestHandlerTest extends AbstractRequestHandlerTest
*/
public function testRequestShouldNotBeNull()
{
$this->requestHandler->handleRequest($this->getMockForm('name', 'GET'));
$this->requestHandler->handleRequest($this->createForm('name', 'GET'));
}
/**
@ -34,7 +34,7 @@ class HttpFoundationRequestHandlerTest extends AbstractRequestHandlerTest
*/
public function testRequestShouldBeInstanceOfRequest()
{
$this->requestHandler->handleRequest($this->getMockForm('name', 'GET'), new \stdClass());
$this->requestHandler->handleRequest($this->createForm('name', 'GET'), new \stdClass());
}
protected function setRequestData($method, $data, $files = [])

View File

@ -12,10 +12,14 @@
namespace Symfony\Component\Form\Tests\Extension\Validator\EventListener;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Extension\Validator\Constraints\Form;
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\Form;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
@ -67,7 +71,7 @@ class ValidationListenerTest extends TestCase
private function getConstraintViolation($code = null)
{
return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code, new Form());
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)
@ -86,9 +90,16 @@ class ValidationListenerTest extends TestCase
return $this->getBuilder($name, $propertyPath, $dataClass)->getForm();
}
private function getMockForm()
private function createForm($name = '', $compound = false)
{
return $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
$config = new FormBuilder($name, null, $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), $this->getMockBuilder(FormFactoryInterface::class)->getMock());
$config->setCompound($compound);
if ($compound) {
$config->setDataMapper(new PropertyPathMapper());
}
return new Form($config);
}
// More specific mapping tests can be found in ViolationMapperTest
@ -110,7 +121,7 @@ class ValidationListenerTest extends TestCase
public function testMapViolationAllowsNonSyncIfInvalid()
{
$violation = $this->getConstraintViolation(Form::NOT_SYNCHRONIZED_ERROR);
$violation = $this->getConstraintViolation(FormConstraint::NOT_SYNCHRONIZED_ERROR);
$form = $this->getForm('street');
$this->validator->expects($this->once())
@ -127,10 +138,10 @@ class ValidationListenerTest extends TestCase
public function testValidateIgnoresNonRoot()
{
$form = $this->getMockForm();
$form->expects($this->once())
->method('isRoot')
->will($this->returnValue(false));
$childForm = $this->createForm('child');
$form = $this->createForm('', true);
$form->add($childForm);
$this->validator->expects($this->never())
->method('validate');
@ -138,15 +149,12 @@ class ValidationListenerTest extends TestCase
$this->violationMapper->expects($this->never())
->method('mapViolation');
$this->listener->validateForm(new FormEvent($form, null));
$this->listener->validateForm(new FormEvent($childForm, null));
}
public function testValidateWithEmptyViolationList()
{
$form = $this->getMockForm();
$form->expects($this->once())
->method('isRoot')
->will($this->returnValue(true));
$form = $this->createForm();
$this->validator
->expects($this->once())

View File

@ -53,12 +53,12 @@ class NativeRequestHandlerTest extends AbstractRequestHandlerTest
*/
public function testRequestShouldBeNull()
{
$this->requestHandler->handleRequest($this->getMockForm('name', 'GET'), 'request');
$this->requestHandler->handleRequest($this->createForm('name', 'GET'), 'request');
}
public function testMethodOverrideHeaderTakesPrecedenceIfPost()
{
$form = $this->getMockForm('param1', 'PUT');
$form = $this->createForm('param1', 'PUT');
$this->setRequestData('POST', [
'param1' => 'DATA',
@ -66,16 +66,15 @@ class NativeRequestHandlerTest extends AbstractRequestHandlerTest
$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
$form->expects($this->once())
->method('submit')
->with('DATA');
$this->requestHandler->handleRequest($form, $this->request);
$this->assertTrue($form->isSubmitted());
$this->assertSame('DATA', $form->getData());
}
public function testConvertEmptyUploadedFilesToNull()
{
$form = $this->getMockForm('param1', 'POST', false);
$form = $this->createForm('param1', 'POST', false);
$this->setRequestData('POST', [], ['param1' => [
'name' => '',
@ -85,16 +84,17 @@ class NativeRequestHandlerTest extends AbstractRequestHandlerTest
'size' => 0,
]]);
$form->expects($this->once())
->method('submit')
->with($this->identicalTo(null));
$this->requestHandler->handleRequest($form, $this->request);
$this->assertTrue($form->isSubmitted());
$this->assertNull($form->getData());
}
public function testFixBuggyFilesArray()
{
$form = $this->getMockForm('param1', 'POST', false);
$form = $this->createForm('param1', 'POST', true);
$fieldForm = $this->createBuilder('field', false, ['allow_file_upload' => true])->getForm();
$form->add($fieldForm);
$this->setRequestData('POST', [], ['param1' => [
'name' => [
@ -114,24 +114,25 @@ class NativeRequestHandlerTest extends AbstractRequestHandlerTest
],
]]);
$form->expects($this->once())
->method('submit')
->with([
'field' => [
'name' => 'upload.txt',
'type' => 'text/plain',
'tmp_name' => 'owfdskjasdfsa',
'error' => UPLOAD_ERR_OK,
'size' => 100,
],
]);
$this->requestHandler->handleRequest($form, $this->request);
$this->assertTrue($form->isSubmitted());
$this->assertEquals([
'name' => 'upload.txt',
'type' => 'text/plain',
'tmp_name' => 'owfdskjasdfsa',
'error' => UPLOAD_ERR_OK,
'size' => 100,
], $fieldForm->getData());
}
public function testFixBuggyNestedFilesArray()
{
$form = $this->getMockForm('param1', 'POST');
$form = $this->createForm('param1', 'POST', true);
$fieldForm = $this->createForm('field', null, true);
$form->add($fieldForm);
$subfieldForm = $this->createBuilder('subfield', false, ['allow_file_upload' => true])->getForm();
$fieldForm->add($subfieldForm);
$this->setRequestData('POST', [], ['param1' => [
'name' => [
@ -151,26 +152,21 @@ class NativeRequestHandlerTest extends AbstractRequestHandlerTest
],
]]);
$form->expects($this->once())
->method('submit')
->with([
'field' => [
'subfield' => [
'name' => 'upload.txt',
'type' => 'text/plain',
'tmp_name' => 'owfdskjasdfsa',
'error' => UPLOAD_ERR_OK,
'size' => 100,
],
],
]);
$this->requestHandler->handleRequest($form, $this->request);
$this->assertTrue($subfieldForm->isSubmitted());
$this->assertEquals([
'name' => 'upload.txt',
'type' => 'text/plain',
'tmp_name' => 'owfdskjasdfsa',
'error' => UPLOAD_ERR_OK,
'size' => 100,
], $subfieldForm->getData());
}
public function testMethodOverrideHeaderIgnoredIfNotPost()
{
$form = $this->getMockForm('param1', 'POST');
$form = $this->createForm('param1', 'POST');
$this->setRequestData('GET', [
'param1' => 'DATA',
@ -178,10 +174,9 @@ class NativeRequestHandlerTest extends AbstractRequestHandlerTest
$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
$form->expects($this->never())
->method('submit');
$this->requestHandler->handleRequest($form, $this->request);
$this->assertFalse($form->isSubmitted());
}
protected function setRequestData($method, $data, $files = [])

View File

@ -12,6 +12,8 @@
namespace Symfony\Component\Form\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormConfigInterface;
use Symfony\Component\Form\FormTypeExtensionInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\Form\ResolvedFormType;
@ -220,7 +222,7 @@ class ResolvedFormTypeTest extends TestCase
public function testCreateView()
{
$form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
$form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock());
$view = $this->resolvedType->createView($form);
@ -230,7 +232,7 @@ class ResolvedFormTypeTest extends TestCase
public function testCreateViewWithParent()
{
$form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
$form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock());
$parentView = $this->getMockBuilder('Symfony\Component\Form\FormView')->getMock();
$view = $this->resolvedType->createView($form, $parentView);
@ -242,7 +244,7 @@ class ResolvedFormTypeTest extends TestCase
public function testBuildView()
{
$options = ['a' => '1', 'b' => '2'];
$form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
$form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock());
$view = $this->getMockBuilder('Symfony\Component\Form\FormView')->getMock();
$i = 0;
@ -284,7 +286,7 @@ class ResolvedFormTypeTest extends TestCase
public function testFinishView()
{
$options = ['a' => '1', 'b' => '2'];
$form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
$form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock());
$view = $this->getMockBuilder('Symfony\Component\Form\FormView')->getMock();
$i = 0;

View File

@ -218,14 +218,14 @@ class SimpleFormTest extends AbstractFormTest
public function testGetRootReturnsRootOfParent()
{
$parent = $this->getMockForm();
$parent->expects($this->once())
->method('getRoot')
->will($this->returnValue('ROOT'));
$root = $this->createForm();
$parent = $this->createForm();
$parent->setParent($root);
$this->form->setParent($parent);
$this->assertEquals('ROOT', $this->form->getRoot());
$this->assertSame($root, $this->form->getRoot());
}
public function testGetRootReturnsSelfIfNoParent()
@ -720,12 +720,13 @@ class SimpleFormTest extends AbstractFormTest
{
$type = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock();
$view = $this->getMockBuilder('Symfony\Component\Form\FormView')->getMock();
$parentForm = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
$parentType = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock();
$parentForm = $this->getBuilder()->setType($parentType)->getForm();
$parentView = $this->getMockBuilder('Symfony\Component\Form\FormView')->getMock();
$form = $this->getBuilder()->setType($type)->getForm();
$form->setParent($parentForm);
$parentForm->expects($this->once())
$parentType->expects($this->once())
->method('createView')
->will($this->returnValue($parentView));

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Serializer\Tests\Mapping;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
use Symfony\Component\Serializer\Mapping\ClassMetadata;
/**
@ -29,11 +30,8 @@ class ClassMetadataTest extends TestCase
{
$classMetadata = new ClassMetadata('c');
$a1 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
$a1->method('getName')->willReturn('a1');
$a2 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
$a2->method('getName')->willReturn('a2');
$a1 = new AttributeMetadata('a1');
$a2 = new AttributeMetadata('a2');
$classMetadata->addAttributeMetadata($a1);
$classMetadata->addAttributeMetadata($a2);
@ -46,33 +44,28 @@ class ClassMetadataTest extends TestCase
$classMetadata1 = new ClassMetadata('c1');
$classMetadata2 = new ClassMetadata('c2');
$ac1 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
$ac1->method('getName')->willReturn('a1');
$ac1->method('getGroups')->willReturn(['a', 'b']);
$ac2 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
$ac2->method('getName')->willReturn('a1');
$ac2->method('getGroups')->willReturn(['b', 'c']);
$ac1 = new AttributeMetadata('a1');
$ac1->addGroup('a');
$ac1->addGroup('b');
$ac2 = new AttributeMetadata('a1');
$ac2->addGroup('b');
$ac2->addGroup('c');
$classMetadata1->addAttributeMetadata($ac1);
$classMetadata2->addAttributeMetadata($ac2);
$classMetadata1->merge($classMetadata2);
$ac1->method('getGroups')->willReturn('a', 'b', 'c');
$this->assertEquals(['a1' => $ac1], $classMetadata2->getAttributesMetadata());
$this->assertSame(['a', 'b', 'c'], $ac1->getGroups());
$this->assertEquals(['a1' => $ac1], $classMetadata1->getAttributesMetadata());
}
public function testSerialize()
{
$classMetadata = new ClassMetadata('a');
$a1 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
$a1->method('getName')->willReturn('b1');
$a2 = $this->getMockBuilder('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface')->getMock();
$a2->method('getName')->willReturn('b2');
$a1 = new AttributeMetadata('b1');
$a2 = new AttributeMetadata('b2');
$classMetadata->addAttributeMetadata($a1);
$classMetadata->addAttributeMetadata($a2);

View File

@ -15,7 +15,6 @@ use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\MetadataInterface;
use Symfony\Component\Validator\Validator\ContextualValidatorInterface;
@ -87,13 +86,13 @@ class TraceableValidatorTest extends TestCase
$expects('startContext')->willReturn($expected = $this->createMock(ContextualValidatorInterface::class));
$this->assertSame($expected, $validator->startContext(), 'returns original validator startContext() result');
$expects('validate')->willReturn($expected = $this->createMock(ConstraintViolationListInterface::class));
$expects('validate')->willReturn($expected = new ConstraintViolationList());
$this->assertSame($expected, $validator->validate('value'), 'returns original validator validate() result');
$expects('validateProperty')->willReturn($expected = $this->createMock(ConstraintViolationListInterface::class));
$expects('validateProperty')->willReturn($expected = new ConstraintViolationList());
$this->assertSame($expected, $validator->validateProperty(new \stdClass(), 'property'), 'returns original validator validateProperty() result');
$expects('validatePropertyValue')->willReturn($expected = $this->createMock(ConstraintViolationListInterface::class));
$expects('validatePropertyValue')->willReturn($expected = new ConstraintViolationList());
$this->assertSame($expected, $validator->validatePropertyValue(new \stdClass(), 'property', 'value'), 'returns original validator validatePropertyValue() result');
}