public function testDatetimeField() { $field = new field\Datetime(); $wrong_values = array('foobar', '-123/-23/-23'); $wrong_values = array(); foreach ($wrong_values as $v) { try { $nv = $field->clean($v); } catch (Invalid $e) { $this->assertEquals(0, $e->getCode()); continue; } $this->fail(sprintf('This value should be wrong: %s', $v)); } $goods = array(array('1999-12-24 12:23:34', '1999-12-24 12:23:34'), array('12/25/99 12:23', '1999-12-25 12:23:00'), array('12/26/1999 12:24', '1999-12-26 12:24:00')); foreach ($goods as $good) { $date = $field->clean($good[0]); $this->assertEquals($good[1], $date->format('Y-m-d H:i:s')); } $this->assertEquals('', $field->clean('')); $date = new \photon\datetime\DateTime(); $this->assertSame($date, $field->clean($date)); $fr = array('input_formats' => fr_formats\DATETIME_INPUT_FORMATS); $field = new field\DateTime($fr); $wrong_values = array('12/21/99 12:00', '12/21/1999 34:12'); foreach ($wrong_values as $v) { try { $nv = $field->clean($v); } catch (Invalid $e) { $this->assertEquals(0, $e->getCode()); continue; } $this->fail(sprintf('This value should be wrong: %s = %s', $v, $nv->format('Y-m-d'))); } $goods = array(array('1979-12-24 23:00', '1979-12-24 23:00:00'), array('25/12/1932', '1932-12-25 00:00:00'), array('26/12/1987', '1987-12-26 00:00:00')); foreach ($goods as $good) { try { $date = $field->clean($good[0]); } catch (Invalid $e) { $this->fail(sprintf('This value should be good: %s.', $good[0])); } $this->assertEquals($good[1], $date->format('Y-m-d H:i:s')); } }