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/DateTimeFieldTest.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

163 lines
4.7 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\DateTimeField;
use Symfony\Component\Form\DateField;
use Symfony\Component\Form\TimeField;
class DateTimeFieldTest extends DateTimeTestCase
{
public function testSubmit_dateTime()
{
$field = new DateTimeField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'date_widget' => DateField::CHOICE,
'time_widget' => TimeField::CHOICE,
'type' => DateTimeField::DATETIME,
));
$field->submit(array(
'date' => array(
'day' => '2',
'month' => '6',
'year' => '2010',
),
'time' => array(
'hour' => '3',
'minute' => '4',
),
));
$dateTime = new \DateTime('2010-06-02 03:04:00 UTC');
$this->assertDateTimeEquals($dateTime, $field->getData());
}
public function testSubmit_string()
{
$field = new DateTimeField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'type' => DateTimeField::STRING,
'date_widget' => DateField::CHOICE,
'time_widget' => TimeField::CHOICE,
));
$field->submit(array(
'date' => array(
'day' => '2',
'month' => '6',
'year' => '2010',
),
'time' => array(
'hour' => '3',
'minute' => '4',
),
));
$this->assertEquals('2010-06-02 03:04:00', $field->getData());
}
public function testSubmit_timestamp()
{
$field = new DateTimeField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'type' => DateTimeField::TIMESTAMP,
'date_widget' => DateField::CHOICE,
'time_widget' => TimeField::CHOICE,
));
$field->submit(array(
'date' => array(
'day' => '2',
'month' => '6',
'year' => '2010',
),
'time' => array(
'hour' => '3',
'minute' => '4',
),
));
$dateTime = new \DateTime('2010-06-02 03:04:00 UTC');
$this->assertEquals($dateTime->format('U'), $field->getData());
}
public function testSubmit_withSeconds()
{
$field = new DateTimeField('name', array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'date_widget' => DateField::CHOICE,
'time_widget' => TimeField::CHOICE,
'type' => DateTimeField::DATETIME,
'with_seconds' => true,
));
$field->setData(new \DateTime('2010-06-02 03:04:05 UTC'));
$input = array(
'date' => array(
'day' => '2',
'month' => '6',
'year' => '2010',
),
'time' => array(
'hour' => '3',
'minute' => '4',
'second' => '5',
),
);
$field->submit($input);
$this->assertDateTimeEquals(new \DateTime('2010-06-02 03:04:05 UTC'), $field->getData());
}
public function testSubmit_differentTimezones()
{
$field = new DateTimeField('name', array(
'data_timezone' => 'America/New_York',
'user_timezone' => 'Pacific/Tahiti',
'date_widget' => DateField::CHOICE,
'time_widget' => TimeField::CHOICE,
// don't do this test with DateTime, because it leads to wrong results!
'type' => DateTimeField::STRING,
'with_seconds' => true,
));
$dateTime = new \DateTime('2010-06-02 03:04:05 Pacific/Tahiti');
$field->submit(array(
'date' => array(
'day' => (int)$dateTime->format('d'),
'month' => (int)$dateTime->format('m'),
'year' => (int)$dateTime->format('Y'),
),
'time' => array(
'hour' => (int)$dateTime->format('H'),
'minute' => (int)$dateTime->format('i'),
'second' => (int)$dateTime->format('s'),
),
));
$dateTime->setTimezone(new \DateTimeZone('America/New_York'));
$this->assertEquals($dateTime->format('Y-m-d H:i:s'), $field->getData());
}
}