TimeHelperTest.php 4.5 KB

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