This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/Form/DateFieldTest.php
Bernhard Schussek fb1f99137d [Form] Changed semantics of a "bound" form
A form now always has to be bound, independent of whether the request is a POST request or not. The bind() method detects itself whether the request was a post request or not and reads its data accordingly. The "old" bind()/isBound() methods were renamed to submit()/isSubmitted().

	$form = new Form('author');
	$form->bind($request, $author);

	if ($form->isValid()) {
		// isValid() implies isSubmitted(), non-submitted forms can
		// never be valid
		// do something with author now
	}

Alternatively, you can only bind global variables, if you don't have a request object.

	$form->bindGlobals($author);

Note that the $author object is in both cases optional. You can also pass no object at all and read the data using $form->getData(), but then no validation will occur. You can also prefill the form with an object during instantiation.

	$form = new Form('author', array('data' => $author));
	$form->bind($request);

	// etc.
2011-02-01 15:27:12 +01:00

235 lines
6.1 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Form;
require_once __DIR__ . '/DateTimeTestCase.php';
use Symfony\Component\Form\DateField;
use Symfony\Component\Form\FormContext;
class DateFieldTest extends DateTimeTestCase
{
protected function setUp()
{
\Locale::setDefault('de_AT');
}
public function testSubmit_fromInput_dateTime()
{
$field = new DateField('name', array('widget' => 'input', 'type' => DateField::DATETIME));
$field->submit('2.6.2010');
$this->assertDateTimeEquals(new \DateTime('2010-06-02 UTC'), $field->getData());
$this->assertEquals('02.06.2010', $field->getDisplayedData());
}
public function testSubmit_fromInput_string()
{
$field = new DateField('name', array('widget' => 'input', 'type' => DateField::STRING));
$field->submit('2.6.2010');
$this->assertEquals('2010-06-02', $field->getData());
$this->assertEquals('02.06.2010', $field->getDisplayedData());
}
public function testSubmit_fromInput_timestamp()
{
$field = new DateField('name', array('widget' => 'input', 'type' => DateField::TIMESTAMP));
$field->submit('2.6.2010');
$dateTime = new \DateTime('2010-06-02 UTC');
$this->assertEquals($dateTime->format('U'), $field->getData());
$this->assertEquals('02.06.2010', $field->getDisplayedData());
}
public function testSubmit_fromInput_raw()
{
$field = new DateField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'widget' => 'input',
'type' => DateField::RAW,
));
$field->submit('2.6.2010');
$output = array(
'day' => '2',
'month' => '6',
'year' => '2010',
);
$this->assertEquals($output, $field->getData());
$this->assertEquals('02.06.2010', $field->getDisplayedData());
}
public function testSubmit_fromChoice()
{
$field = new DateField('name', array('widget' => DateField::CHOICE));
$input = array(
'day' => '2',
'month' => '6',
'year' => '2010',
);
$field->submit($input);
$dateTime = new \DateTime('2010-06-02 UTC');
$this->assertDateTimeEquals($dateTime, $field->getData());
$this->assertEquals($input, $field->getDisplayedData());
}
public function testSubmit_fromChoice_empty()
{
$field = new DateField('name', array('widget' => DateField::CHOICE, 'required' => false));
$input = array(
'day' => '',
'month' => '',
'year' => '',
);
$field->submit($input);
$this->assertSame(null, $field->getData());
$this->assertEquals($input, $field->getDisplayedData());
}
public function testSetData_differentTimezones()
{
$field = new DateField('name', array(
'data_timezone' => 'America/New_York',
'user_timezone' => 'Pacific/Tahiti',
// don't do this test with DateTime, because it leads to wrong results!
'type' => DateField::STRING,
'widget' => 'input',
));
$field->setData('2010-06-02');
$this->assertEquals('01.06.2010', $field->getDisplayedData());
}
public function testIsYearWithinRange_returnsTrueIfWithin()
{
$field = new DateField('name', array(
'widget' => 'input',
'years' => array(2010, 2011),
));
$field->submit('2.6.2010');
$this->assertTrue($field->isYearWithinRange());
}
public function testIsYearWithinRange_returnsTrueIfEmpty()
{
$field = new DateField('name', array(
'widget' => 'input',
'years' => array(2010, 2011),
));
$field->submit('');
$this->assertTrue($field->isYearWithinRange());
}
public function testIsYearWithinRange_returnsFalseIfNotContained()
{
$field = new DateField('name', array(
'widget' => 'input',
'years' => array(2010, 2012),
));
$field->submit('2.6.2011');
$this->assertFalse($field->isYearWithinRange());
}
public function testIsMonthWithinRange_returnsTrueIfWithin()
{
$field = new DateField('name', array(
'widget' => 'input',
'months' => array(6, 7),
));
$field->submit('2.6.2010');
$this->assertTrue($field->isMonthWithinRange());
}
public function testIsMonthWithinRange_returnsTrueIfEmpty()
{
$field = new DateField('name', array(
'widget' => 'input',
'months' => array(6, 7),
));
$field->submit('');
$this->assertTrue($field->isMonthWithinRange());
}
public function testIsMonthWithinRange_returnsFalseIfNotContained()
{
$field = new DateField('name', array(
'widget' => 'input',
'months' => array(6, 8),
));
$field->submit('2.7.2010');
$this->assertFalse($field->isMonthWithinRange());
}
public function testIsDayWithinRange_returnsTrueIfWithin()
{
$field = new DateField('name', array(
'widget' => 'input',
'days' => array(6, 7),
));
$field->submit('6.6.2010');
$this->assertTrue($field->isDayWithinRange());
}
public function testIsDayWithinRange_returnsTrueIfEmpty()
{
$field = new DateField('name', array(
'widget' => 'input',
'days' => array(6, 7),
));
$field->submit('');
$this->assertTrue($field->isDayWithinRange());
}
public function testIsDayWithinRange_returnsFalseIfNotContained()
{
$field = new DateField('name', array(
'widget' => 'input',
'days' => array(6, 8),
));
$field->submit('7.6.2010');
$this->assertFalse($field->isDayWithinRange());
}
}