DateTimeHelperTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace fphammerle\helpers\tests;
  3. use fphammerle\helpers\DateTimeHelper;
  4. class DateTimeHelperTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function testTimestampToDateTimeZero()
  7. {
  8. $this->assertEquals(
  9. 0,
  10. DateTimeHelper::timestampToDateTime(0)->getTimestamp()
  11. );
  12. }
  13. public function testTimestampToDateTimeCompareUTC()
  14. {
  15. $created = DateTimeHelper::timestampToDateTime(1234567890);
  16. $expected = new \DateTime('2009-02-13 23:31:30', new \DateTimeZone('UTC'));
  17. $this->assertEquals($created->getTimestamp(), $expected->getTimestamp());
  18. }
  19. public function testTimestampToDateTimeCompareLocal()
  20. {
  21. $created = DateTimeHelper::timestampToDateTime(1234567890);
  22. $expected = new \DateTime('2009-02-14 00:31:30', new \DateTimeZone('Europe/Vienna'));
  23. $this->assertEquals($created->getTimestamp(), $expected->getTimestamp());
  24. }
  25. public function testTimestampToDateTimeSetLocal()
  26. {
  27. date_default_timezone_set('Europe/Vienna');
  28. $this->assertEquals(
  29. DateTimeHelper::timestampToDateTime(123456)->getTimestamp(),
  30. 123456
  31. );
  32. }
  33. public function testTimestampToDateTimeSetUTC()
  34. {
  35. date_default_timezone_set('UTC');
  36. $this->assertEquals(
  37. DateTimeHelper::timestampToDateTime(123456)->getTimestamp(),
  38. 123456
  39. );
  40. }
  41. public function testTimestampToDateTimeNull()
  42. {
  43. $this->assertEquals(
  44. DateTimeHelper::timestampToDateTime(null),
  45. null
  46. );
  47. }
  48. /**
  49. * @expectedException InvalidArgumentException
  50. */
  51. public function testTimestampToDateInvalidArgumentFloat()
  52. {
  53. DateTimeHelper::timestampToDateTime(1.23);
  54. }
  55. /**
  56. * @expectedException InvalidArgumentException
  57. */
  58. public function testTimestampToDateInvalidArgumentString()
  59. {
  60. DateTimeHelper::timestampToDateTime('');
  61. }
  62. }