TimeHelperTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * TimeHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('TimeHelper', 'View/Helper');
  20. App::uses('View', 'View');
  21. App::uses('CakeTime', 'Utility');
  22. /**
  23. * TimeHelperTestObject class
  24. */
  25. class TimeHelperTestObject extends TimeHelper {
  26. public function attach(CakeTimeMock $cakeTime) {
  27. $this->_engine = $cakeTime;
  28. }
  29. public function engine() {
  30. return $this->_engine;
  31. }
  32. }
  33. /**
  34. * CakeTimeMock class
  35. */
  36. class CakeTimeMock {
  37. }
  38. /**
  39. * TimeHelperTest class
  40. *
  41. * @package Cake.Test.Case.View.Helper
  42. */
  43. class TimeHelperTest extends CakeTestCase {
  44. public $Time = null;
  45. public $CakeTime = null;
  46. /**
  47. * setUp method
  48. *
  49. * @return void
  50. */
  51. public function setUp() {
  52. parent::setUp();
  53. $this->View = new View(null);
  54. }
  55. /**
  56. * tearDown method
  57. *
  58. * @return void
  59. */
  60. public function tearDown() {
  61. unset($this->View);
  62. parent::tearDown();
  63. }
  64. /**
  65. * test CakeTime class methods are called correctly
  66. */
  67. public function testTimeHelperProxyMethodCalls() {
  68. $methods = array(
  69. 'convertSpecifiers', 'convert', 'serverOffset', 'fromString',
  70. 'nice', 'niceShort', 'daysAsSql', 'dayAsSql',
  71. 'isToday', 'isThisMonth', 'isThisYear', 'wasYesterday',
  72. 'isTomorrow', 'toQuarter', 'toUnix', 'toAtom', 'toRSS',
  73. 'timeAgoInWords', 'wasWithinLast', 'gmt', 'format', 'i18nFormat',
  74. );
  75. $CakeTime = $this->getMock('CakeTimeMock', $methods);
  76. $Time = new TimeHelperTestObject($this->View, array('engine' => 'CakeTimeMock'));
  77. $Time->attach($CakeTime);
  78. foreach ($methods as $method) {
  79. $CakeTime->expects($this->at(0))->method($method);
  80. $Time->{$method}('who', 'what', 'when', 'where', 'how');
  81. }
  82. }
  83. /**
  84. * test engine override
  85. */
  86. public function testEngineOverride() {
  87. App::build(array(
  88. 'Utility' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Utility' . DS)
  89. ), App::REGISTER);
  90. $Time = new TimeHelperTestObject($this->View, array('engine' => 'TestAppEngine'));
  91. $this->assertInstanceOf('TestAppEngine', $Time->engine());
  92. App::build(array(
  93. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  94. ));
  95. CakePlugin::load('TestPlugin');
  96. $Time = new TimeHelperTestObject($this->View, array('engine' => 'TestPlugin.TestPluginEngine'));
  97. $this->assertInstanceOf('TestPluginEngine', $Time->engine());
  98. CakePlugin::unload('TestPlugin');
  99. }
  100. /**
  101. * Test element wrapping in timeAgoInWords
  102. *
  103. * @return void
  104. */
  105. public function testTimeAgoInWords() {
  106. $Time = new TimeHelper($this->View);
  107. $timestamp = strtotime('+8 years, +4 months +2 weeks +3 days');
  108. $result = $Time->timeAgoInWords($timestamp, array(
  109. 'end' => '1 years',
  110. 'element' => 'span'
  111. ));
  112. $expected = array(
  113. 'span' => array(
  114. 'title' => $timestamp,
  115. 'class' => 'time-ago-in-words'
  116. ),
  117. 'on ' . date('j/n/y', $timestamp),
  118. '/span'
  119. );
  120. $this->assertTags($result, $expected);
  121. $result = $Time->timeAgoInWords($timestamp, array(
  122. 'end' => '1 years',
  123. 'element' => array(
  124. 'title' => 'testing',
  125. 'rel' => 'test'
  126. )
  127. ));
  128. $expected = array(
  129. 'span' => array(
  130. 'title' => 'testing',
  131. 'class' => 'time-ago-in-words',
  132. 'rel' => 'test'
  133. ),
  134. 'on ' . date('j/n/y', $timestamp),
  135. '/span'
  136. );
  137. $this->assertTags($result, $expected);
  138. $timestamp = strtotime('+2 weeks');
  139. $result = $Time->timeAgoInWords(
  140. $timestamp,
  141. array('end' => '1 years', 'element' => 'div')
  142. );
  143. $expected = array(
  144. 'div' => array(
  145. 'title' => $timestamp,
  146. 'class' => 'time-ago-in-words'
  147. ),
  148. '2 weeks',
  149. '/div'
  150. );
  151. $this->assertTags($result, $expected);
  152. }
  153. }