TimeagoTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Testing timeago dates
  4. * @author jimmiw
  5. * @since 2016-03-11
  6. */
  7. class TimeagoTest extends PHPUnit_Framework_TestCase
  8. {
  9. public function testIsAlive()
  10. {
  11. $this->assertTrue(true);
  12. }
  13. public function testInit()
  14. {
  15. $timeAgo = new TimeAgo();
  16. $this->assertNotNull($timeAgo);
  17. }
  18. /**
  19. * Tests the old core functionality, by being in the same timezone and not changing language.
  20. */
  21. public function testTimeAgoInWords()
  22. {
  23. $timeAgo = new TimeAgo();
  24. // testing "less than a minute"
  25. $this->assertEquals('less than a minute ago', $timeAgo->inWords("now"));
  26. $this->assertEquals('less than a minute ago', $timeAgo->inWords("-1 second"));
  27. $this->assertEquals('less than a minute ago', $timeAgo->inWords("-29 second"));
  28. $this->assertNotEquals('less than a minute ago', $timeAgo->inWords("-30 second"));
  29. // testing "1 minute"
  30. $this->assertEquals('1 minute ago', $timeAgo->inWords("-30 second"));
  31. $this->assertEquals('1 minute ago', $timeAgo->inWords("-60 second"));
  32. $this->assertEquals('1 minute ago', $timeAgo->inWords("-89 second"));
  33. $this->assertNotEquals('1 minute ago', $timeAgo->inWords("-90 second"));
  34. // testing 2..44 minutes
  35. $this->assertContains('minutes ago', $timeAgo->inWords("-2 minute"));
  36. $this->assertContains('minutes ago', $timeAgo->inWords("-44 minute"));
  37. $this->assertContains('minutes ago', $timeAgo->inWords("-44 minute -29 second"));
  38. $this->assertNotContains('minutes ago', $timeAgo->inWords("-44 minute -30 second"));
  39. // testing about 1 hour
  40. $this->assertEquals('about 1 hour ago', $timeAgo->inWords("-44 minute -30 second"));
  41. $this->assertEquals('about 1 hour ago', $timeAgo->inWords("-89 minute -29 second"));
  42. $this->assertNotEquals('about 1 hour ago', $timeAgo->inWords("-90 minute"));
  43. // testing about 2..24 hours
  44. $this->assertContains('hours ago', $timeAgo->inWords("-90 minute"));
  45. $this->assertContains('hours ago', $timeAgo->inWords("-23 hour -59 minute -29 second"));
  46. $this->assertNotContains('hours ago', $timeAgo->inWords("-23 hour -59 minute -30 second"));
  47. $this->assertNotContains('hours ago', $timeAgo->inWords("-24 hour"));
  48. // testing 1 day
  49. $this->assertEquals('1 day ago', $timeAgo->inWords("-23 hour -59 minute -30 second"));
  50. $this->assertEquals('1 day ago', $timeAgo->inWords("-47 hour -59 minute -29 second"));
  51. $this->assertNotEquals('1 day ago', $timeAgo->inWords("-47 hour -59 minute -30 second"));
  52. // testing 2..24 days
  53. $this->assertContains('days ago', $timeAgo->inWords("-47 hour -59 minute -30 second"));
  54. $this->assertContains('days ago', $timeAgo->inWords("-29 day -23 hour -59 minute -29 second"));
  55. $this->assertNotContains('days ago', $timeAgo->inWords("-29 day -23 hour -59 minute -30 second"));
  56. // testing 1 month
  57. $this->assertEquals('about 1 month ago', $timeAgo->inWords("-29 day -23 hour -59 minute -30 second"));
  58. $this->assertEquals('about 1 month ago', $timeAgo->inWords("-59 day -23 hour -59 minute -29 second"));
  59. $this->assertNotEquals('about 1 month ago', $timeAgo->inWords("-59 day -23 hour -59 minute -30 second"));
  60. // testing 2..12 months
  61. $this->assertContains('months ago', $timeAgo->inWords("-59 day -23 hour -59 minute -30 second"));
  62. // seemed to be the easiest way to get 1 year - 1 second, which should be the day before 1 year ago :)
  63. $oneYearAgo = strtotime("-1 year");
  64. // NOTE: this fails around leap years... so... -2 days must be accurate enough
  65. $twoDays = (2*86400); // 2 days in seconds
  66. $this->assertContains('months ago', $timeAgo->inWords(date('c', $oneYearAgo + $twoDays)));
  67. $this->assertNotContains('months ago', $timeAgo->inWords($oneYearAgo));
  68. // testing 1 year
  69. $this->assertContains('1 year ago', $timeAgo->inWords(date('c', $oneYearAgo - $twoDays)));
  70. $twoYearsAgo = strtotime("-2 year");
  71. $this->assertContains('1 year ago', $timeAgo->inWords(date('c', $twoYearsAgo + $twoDays)));
  72. $this->assertNotContains('1 year ago', $timeAgo->inWords($twoYearsAgo));
  73. // testing 2 years or more
  74. $this->assertEquals('over 2 years ago', $timeAgo->inWords("-2 year"));
  75. $this->assertEquals('over 2 years ago', $timeAgo->inWords("-2 year - 59 day"));
  76. $this->assertEquals('over 3 years ago', $timeAgo->inWords("-3 year"));
  77. $this->assertEquals('over 4 years ago', $timeAgo->inWords("-4 year"));
  78. $this->assertEquals('over 5 years ago', $timeAgo->inWords("-5 year"));
  79. $this->assertEquals('over 6 years ago', $timeAgo->inWords("-6 year"));
  80. $this->assertEquals('over 7 years ago', $timeAgo->inWords("-7 year"));
  81. $this->assertEquals('over 8 years ago', $timeAgo->inWords("-8 year"));
  82. $this->assertEquals('over 9 years ago', $timeAgo->inWords("-9 year"));
  83. $this->assertEquals('over 10 years ago', $timeAgo->inWords("-10 year"));
  84. // you get the point right?...
  85. }
  86. }