Skip to content

Commit

Permalink
[Form] ensure compatibility with older PHPUnit mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh authored and nicolas-grekas committed Jan 25, 2019
1 parent b24a67f commit 872488d
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 298 deletions.
8 changes: 5 additions & 3 deletions Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,11 @@ public function submit($submittedData, $clearMissing = true)
$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.');
Expand Down
20 changes: 0 additions & 20 deletions Tests/AbstractFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,6 @@ protected function getBuilder($name = 'name', EventDispatcherInterface $dispatch
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
*/
Expand Down
185 changes: 82 additions & 103 deletions Tests/AbstractRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,140 +70,136 @@ public function methodProvider()
*/
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());
}

/**
* @dataProvider methodProvider
*/
public function testDoNotSubmitIfWrongRequestMethod($method)
{
$form = $this->getMockForm('param1', $method);
$form = $this->createForm('param1', $method);

$otherMethod = 'POST' === $method ? 'PUT' : 'POST';

$this->setRequestData($otherMethod, [
'param1' => 'DATA',
]);

$form->expects($this->never())
->method('submit');

$this->requestHandler->handleRequest($form, $this->request);

$this->assertFalse($form->isSubmitted());
}

/**
* @dataProvider methodExceptGetProvider
*/
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());
}

/**
* @dataProvider methodExceptGetProvider
*/
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());
}

/**
* @dataProvider methodProvider
*/
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());
}

/**
* @dataProvider methodProvider
*/
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());
}

/**
* @dataProvider methodExceptGetProvider
*/
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, [
Expand All @@ -212,22 +212,19 @@ public function testMergeParamsAndFiles($method)
],
]);

$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());
}

/**
* @dataProvider methodExceptGetProvider
*/
public function testParamTakesPrecedenceOverFile($method)
{
$form = $this->getMockForm('param1', $method);
$form = $this->createForm('param1', $method);
$file = $this->getMockFile();

$this->setRequestData($method, [
Expand All @@ -236,19 +233,20 @@ public function testParamTakesPrecedenceOverFile($method)
'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());
}

/**
* @dataProvider methodExceptGetProvider
*/
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, [
Expand All @@ -257,19 +255,20 @@ public function testSubmitFileIfNoParam($method)
'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());
}

/**
* @dataProvider methodExceptGetProvider
*/
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, [
Expand All @@ -280,32 +279,10 @@ public function testSubmitMultipleFiles($method)
'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());
}

/**
Expand Down Expand Up @@ -371,24 +348,26 @@ abstract protected function getMockFile($suffix = '');

abstract protected function getInvalidFile();

protected function getMockForm($name, $method = null, $compound = true)
protected function createForm($name, $method = null, $compound = false)
{
$config = $this->createBuilder($name, $compound);

if (null !== $method) {
$config->setMethod($method);
}

return new Form($config);
}

protected function createBuilder($name, $compound = false, array $options = [])
{
$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));

$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));

return $form;
$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;
}
}

0 comments on commit 872488d

Please sign in to comment.