DateTimeValueConverterTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Ddeboer\DataImport\Tests\ValueConverter;
  3. use Ddeboer\DataImport\ValueConverter\DateTimeValueConverter;
  4. class DateTimeValueConverterTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function testConvertWithoutInputOrOutputFormatReturnsDateTimeInstance()
  7. {
  8. $value = '2011-10-20 13:05';
  9. $converter = new DateTimeValueConverter;
  10. $output = call_user_func($converter, $value);
  11. $this->assertInstanceOf('\DateTime', $output);
  12. $this->assertEquals('13', $output->format('H'));
  13. }
  14. public function testConvertWithFormatReturnsDateTimeInstance()
  15. {
  16. $value = '14/10/2008 09:40:20';
  17. $converter = new DateTimeValueConverter('d/m/Y H:i:s');
  18. $output = call_user_func($converter, $value);
  19. $this->assertInstanceOf('\DateTime', $output);
  20. $this->assertEquals('20', $output->format('s'));
  21. }
  22. public function testConvertWithInputAndOutputFormatReturnsString()
  23. {
  24. $value = '14/10/2008 09:40:20';
  25. $converter = new DateTimeValueConverter('d/m/Y H:i:s', 'd-M-Y');
  26. $output = call_user_func($converter, $value);
  27. $this->assertEquals('14-Oct-2008', $output);
  28. }
  29. public function testConvertWithNoInputStringWithOutputFormatReturnsString()
  30. {
  31. $value = '2011-10-20 13:05';
  32. $converter = new DateTimeValueConverter(null, 'd-M-Y');
  33. $output = call_user_func($converter, $value);
  34. $this->assertEquals('20-Oct-2011', $output);
  35. }
  36. public function testInvalidInputFormatThrowsException()
  37. {
  38. $value = '14/10/2008 09:40:20';
  39. $converter = new DateTimeValueConverter('d-m-y', 'd-M-Y');
  40. $this->setExpectedException("UnexpectedValueException", "14/10/2008 09:40:20 is not a valid date/time according to format d-m-y");
  41. call_user_func($converter, $value);
  42. }
  43. public function testNullIsReturnedIfNullPassed()
  44. {
  45. $converter = new DateTimeValueConverter('d-m-y', 'd-M-Y');
  46. $this->assertNull(call_user_func($converter, null));
  47. }
  48. }