2012-12-30 15:38:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Form\Tests;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
|
|
*/
|
2013-04-20 14:56:42 +01:00
|
|
|
abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
/**
|
2013-04-20 14:56:42 +01:00
|
|
|
* @var \Symfony\Component\Form\RequestHandlerInterface
|
2012-12-30 15:38:36 +00:00
|
|
|
*/
|
2013-04-20 14:56:42 +01:00
|
|
|
protected $requestHandler;
|
2012-12-30 15:38:36 +00:00
|
|
|
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler = $this->getRequestHandler();
|
2012-12-30 15:38:36 +00:00
|
|
|
$this->request = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function methodExceptGetProvider()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('POST'),
|
|
|
|
array('PUT'),
|
|
|
|
array('DELETE'),
|
|
|
|
array('PATCH'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function methodProvider()
|
|
|
|
{
|
|
|
|
return array_merge(array(
|
|
|
|
array('GET'),
|
|
|
|
), $this->methodExceptGetProvider());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodProvider
|
|
|
|
*/
|
|
|
|
public function testBindIfNameInRequest($method)
|
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', $method);
|
|
|
|
|
|
|
|
$this->setRequestData($method, array(
|
|
|
|
'param1' => 'DATA',
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->once())
|
|
|
|
->method('bind')
|
|
|
|
->with('DATA');
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodProvider
|
|
|
|
*/
|
|
|
|
public function testDoNotBindIfWrongRequestMethod($method)
|
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', $method);
|
|
|
|
|
|
|
|
$otherMethod = 'POST' === $method ? 'PUT' : 'POST';
|
|
|
|
|
|
|
|
$this->setRequestData($otherMethod, array(
|
|
|
|
'param1' => 'DATA',
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->never())
|
|
|
|
->method('bind');
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodExceptGetProvider
|
|
|
|
*/
|
|
|
|
public function testBindSimpleFormWithNullIfNameNotInRequestAndNotGetRequest($method)
|
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', $method, false);
|
|
|
|
|
|
|
|
$this->setRequestData($method, array(
|
|
|
|
'paramx' => array(),
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->once())
|
|
|
|
->method('bind')
|
|
|
|
->with($this->identicalTo(null));
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodExceptGetProvider
|
|
|
|
*/
|
|
|
|
public function testBindCompoundFormWithArrayIfNameNotInRequestAndNotGetRequest($method)
|
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', $method, true);
|
|
|
|
|
|
|
|
$this->setRequestData($method, array(
|
|
|
|
'paramx' => array(),
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->once())
|
|
|
|
->method('bind')
|
|
|
|
->with($this->identicalTo(array()));
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoNotBindIfNameNotInRequestAndGetRequest()
|
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', 'GET');
|
|
|
|
|
|
|
|
$this->setRequestData('GET', array(
|
|
|
|
'paramx' => array(),
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->never())
|
|
|
|
->method('bind');
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodProvider
|
|
|
|
*/
|
|
|
|
public function testBindFormWithEmptyNameIfAtLeastOneFieldInRequest($method)
|
|
|
|
{
|
|
|
|
$form = $this->getMockForm('', $method);
|
|
|
|
$form->expects($this->any())
|
|
|
|
->method('all')
|
|
|
|
->will($this->returnValue(array(
|
|
|
|
'param1' => $this->getMockForm('param1'),
|
|
|
|
'param2' => $this->getMockForm('param2'),
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->setRequestData($method, $requestData = array(
|
|
|
|
'param1' => 'submitted value',
|
|
|
|
'paramx' => 'submitted value',
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->once())
|
|
|
|
->method('bind')
|
|
|
|
->with($requestData);
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodProvider
|
|
|
|
*/
|
|
|
|
public function testDoNotBindFormWithEmptyNameIfNoFieldInRequest($method)
|
|
|
|
{
|
|
|
|
$form = $this->getMockForm('', $method);
|
|
|
|
$form->expects($this->any())
|
|
|
|
->method('all')
|
|
|
|
->will($this->returnValue(array(
|
|
|
|
'param1' => $this->getMockForm('param1'),
|
|
|
|
'param2' => $this->getMockForm('param2'),
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->setRequestData($method, array(
|
|
|
|
'paramx' => 'submitted value',
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->never())
|
|
|
|
->method('bind');
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodExceptGetProvider
|
|
|
|
*/
|
|
|
|
public function testMergeParamsAndFiles($method)
|
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', $method);
|
|
|
|
$file = $this->getMockFile();
|
|
|
|
|
|
|
|
$this->setRequestData($method, array(
|
|
|
|
'param1' => array(
|
|
|
|
'field1' => 'DATA',
|
|
|
|
),
|
|
|
|
), array(
|
|
|
|
'param1' => array(
|
|
|
|
'field2' => $file,
|
|
|
|
),
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->once())
|
|
|
|
->method('bind')
|
|
|
|
->with(array(
|
|
|
|
'field1' => 'DATA',
|
|
|
|
'field2' => $file,
|
|
|
|
));
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodExceptGetProvider
|
|
|
|
*/
|
|
|
|
public function testParamTakesPrecedenceOverFile($method)
|
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', $method);
|
|
|
|
$file = $this->getMockFile();
|
|
|
|
|
|
|
|
$this->setRequestData($method, array(
|
|
|
|
'param1' => 'DATA',
|
|
|
|
), array(
|
|
|
|
'param1' => $file,
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->once())
|
|
|
|
->method('bind')
|
|
|
|
->with('DATA');
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodExceptGetProvider
|
|
|
|
*/
|
|
|
|
public function testBindFileIfNoParam($method)
|
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', $method);
|
|
|
|
$file = $this->getMockFile();
|
|
|
|
|
|
|
|
$this->setRequestData($method, array(
|
|
|
|
'param1' => null,
|
|
|
|
), array(
|
|
|
|
'param1' => $file,
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->once())
|
|
|
|
->method('bind')
|
|
|
|
->with($file);
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract protected function setRequestData($method, $data, $files = array());
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
abstract protected function getRequestHandler();
|
2012-12-30 15:38:36 +00:00
|
|
|
|
|
|
|
abstract protected function getMockFile();
|
|
|
|
|
|
|
|
protected function getMockForm($name, $method = null, $compound = true)
|
|
|
|
{
|
|
|
|
$config = $this->getMock('Symfony\Component\Form\FormConfigInterface');
|
|
|
|
$config->expects($this->any())
|
|
|
|
->method('getMethod')
|
|
|
|
->will($this->returnValue($method));
|
|
|
|
$config->expects($this->any())
|
|
|
|
->method('getCompound')
|
|
|
|
->will($this->returnValue($compound));
|
|
|
|
|
|
|
|
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
|
|
|
|
$form->expects($this->any())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue($name));
|
|
|
|
$form->expects($this->any())
|
|
|
|
->method('getConfig')
|
|
|
|
->will($this->returnValue($config));
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
}
|