123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- <?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\TimeField;
- class TimeFieldTest extends DateTimeTestCase
- {
- public function testSubmit_dateTime()
- {
- $field = new TimeField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'type' => TimeField::DATETIME,
- ));
- $input = array(
- 'hour' => '3',
- 'minute' => '4',
- );
- $field->submit($input);
- $dateTime = new \DateTime('1970-01-01 03:04:00 UTC');
- $this->assertEquals($dateTime, $field->getData());
- $this->assertEquals($input, $field->getDisplayedData());
- }
- public function testSubmit_string()
- {
- $field = new TimeField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'type' => TimeField::STRING,
- ));
- $input = array(
- 'hour' => '3',
- 'minute' => '4',
- );
- $field->submit($input);
- $this->assertEquals('03:04:00', $field->getData());
- $this->assertEquals($input, $field->getDisplayedData());
- }
- public function testSubmit_timestamp()
- {
- $field = new TimeField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'type' => TimeField::TIMESTAMP,
- ));
- $input = array(
- 'hour' => '3',
- 'minute' => '4',
- );
- $field->submit($input);
- $dateTime = new \DateTime('1970-01-01 03:04:00 UTC');
- $this->assertEquals($dateTime->format('U'), $field->getData());
- $this->assertEquals($input, $field->getDisplayedData());
- }
- public function testSubmit_raw()
- {
- $field = new TimeField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'type' => TimeField::RAW,
- ));
- $input = array(
- 'hour' => '3',
- 'minute' => '4',
- );
- $data = array(
- 'hour' => '3',
- 'minute' => '4',
- );
- $field->submit($input);
- $this->assertEquals($data, $field->getData());
- $this->assertEquals($input, $field->getDisplayedData());
- }
- public function testSetData_withSeconds()
- {
- $field = new TimeField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'type' => TimeField::DATETIME,
- 'with_seconds' => true,
- ));
- $field->setData(new \DateTime('03:04:05 UTC'));
- $this->assertEquals(array('hour' => 3, 'minute' => 4, 'second' => 5), $field->getDisplayedData());
- }
- public function testSetData_differentTimezones()
- {
- $field = new TimeField('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' => TimeField::STRING,
- 'with_seconds' => true,
- ));
- $dateTime = new \DateTime('03:04:05 America/New_York');
- $field->setData($dateTime->format('H:i:s'));
- $dateTime = clone $dateTime;
- $dateTime->setTimezone(new \DateTimeZone('Pacific/Tahiti'));
- $displayedData = array(
- 'hour' => (int)$dateTime->format('H'),
- 'minute' => (int)$dateTime->format('i'),
- 'second' => (int)$dateTime->format('s')
- );
- $this->assertEquals($displayedData, $field->getDisplayedData());
- }
- public function testIsHourWithinRange_returnsTrueIfWithin()
- {
- $field = new TimeField('name', array(
- 'hours' => array(6, 7),
- ));
- $field->submit(array('hour' => '06', 'minute' => '12'));
- $this->assertTrue($field->isHourWithinRange());
- }
- public function testIsHourWithinRange_returnsTrueIfEmpty()
- {
- $field = new TimeField('name', array(
- 'hours' => array(6, 7),
- ));
- $field->submit(array('hour' => '', 'minute' => '06'));
- $this->assertTrue($field->isHourWithinRange());
- }
- public function testIsHourWithinRange_returnsFalseIfNotContained()
- {
- $field = new TimeField('name', array(
- 'hours' => array(6, 7),
- ));
- $field->submit(array('hour' => '08', 'minute' => '12'));
- $this->assertFalse($field->isHourWithinRange());
- }
- public function testIsMinuteWithinRange_returnsTrueIfWithin()
- {
- $field = new TimeField('name', array(
- 'minutes' => array(6, 7),
- ));
- $field->submit(array('hour' => '06', 'minute' => '06'));
- $this->assertTrue($field->isMinuteWithinRange());
- }
- public function testIsMinuteWithinRange_returnsTrueIfEmpty()
- {
- $field = new TimeField('name', array(
- 'minutes' => array(6, 7),
- ));
- $field->submit(array('hour' => '06', 'minute' => ''));
- $this->assertTrue($field->isMinuteWithinRange());
- }
- public function testIsMinuteWithinRange_returnsFalseIfNotContained()
- {
- $field = new TimeField('name', array(
- 'minutes' => array(6, 7),
- ));
- $field->submit(array('hour' => '06', 'minute' => '08'));
- $this->assertFalse($field->isMinuteWithinRange());
- }
- public function testIsSecondWithinRange_returnsTrueIfWithin()
- {
- $field = new TimeField('name', array(
- 'seconds' => array(6, 7),
- 'with_seconds' => true,
- ));
- $field->submit(array('hour' => '04', 'minute' => '05', 'second' => '06'));
- $this->assertTrue($field->isSecondWithinRange());
- }
- public function testIsSecondWithinRange_returnsTrueIfEmpty()
- {
- $field = new TimeField('name', array(
- 'seconds' => array(6, 7),
- 'with_seconds' => true,
- ));
- $field->submit(array('hour' => '06', 'minute' => '06', 'second' => ''));
- $this->assertTrue($field->isSecondWithinRange());
- }
- public function testIsSecondWithinRange_returnsTrueIfNotWithSeconds()
- {
- $field = new TimeField('name', array(
- 'seconds' => array(6, 7),
- ));
- $field->submit(array('hour' => '06', 'minute' => '06'));
- $this->assertTrue($field->isSecondWithinRange());
- }
- public function testIsSecondWithinRange_returnsFalseIfNotContained()
- {
- $field = new TimeField('name', array(
- 'seconds' => array(6, 7),
- 'with_seconds' => true,
- ));
- $field->submit(array('hour' => '04', 'minute' => '05', 'second' => '08'));
- $this->assertFalse($field->isSecondWithinRange());
- }
- public function testIsPartiallyFilled_returnsFalseIfCompletelyEmpty()
- {
- $field = new TimeField('name', array(
- 'widget' => 'choice',
- ));
- $field->submit(array(
- 'hour' => '',
- 'minute' => '',
- ));
- $this->assertFalse($field->isPartiallyFilled());
- }
- public function testIsPartiallyFilled_returnsFalseIfCompletelyEmpty_withSeconds()
- {
- $field = new TimeField('name', array(
- 'widget' => 'choice',
- 'with_seconds' => true,
- ));
- $field->submit(array(
- 'hour' => '',
- 'minute' => '',
- 'second' => '',
- ));
- $this->assertFalse($field->isPartiallyFilled());
- }
- public function testIsPartiallyFilled_returnsFalseIfCompletelyFilled()
- {
- $field = new TimeField('name', array(
- 'widget' => 'choice',
- ));
- $field->submit(array(
- 'hour' => '0',
- 'minute' => '0',
- ));
- $this->assertFalse($field->isPartiallyFilled());
- }
- public function testIsPartiallyFilled_returnsFalseIfCompletelyFilled_withSeconds()
- {
- $field = new TimeField('name', array(
- 'widget' => 'choice',
- 'with_seconds' => true,
- ));
- $field->submit(array(
- 'hour' => '0',
- 'minute' => '0',
- 'second' => '0',
- ));
- $this->assertFalse($field->isPartiallyFilled());
- }
- public function testIsPartiallyFilled_returnsTrueIfChoiceAndHourEmpty()
- {
- $field = new TimeField('name', array(
- 'widget' => 'choice',
- 'with_seconds' => true,
- ));
- $field->submit(array(
- 'hour' => '',
- 'minute' => '0',
- 'second' => '0',
- ));
- $this->assertTrue($field->isPartiallyFilled());
- }
- public function testIsPartiallyFilled_returnsTrueIfChoiceAndMinuteEmpty()
- {
- $field = new TimeField('name', array(
- 'widget' => 'choice',
- 'with_seconds' => true,
- ));
- $field->submit(array(
- 'hour' => '0',
- 'minute' => '',
- 'second' => '0',
- ));
- $this->assertTrue($field->isPartiallyFilled());
- }
- public function testIsPartiallyFilled_returnsTrueIfChoiceAndSecondsEmpty()
- {
- $field = new TimeField('name', array(
- 'widget' => 'choice',
- 'with_seconds' => true,
- ));
- $field->submit(array(
- 'hour' => '0',
- 'minute' => '0',
- 'second' => '',
- ));
- $this->assertTrue($field->isPartiallyFilled());
- }
- }
|