TimeHelperTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 CakePHP(tm) v 1.2.0.4206
  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(null);
  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. public function testTimeHelperProxyMethodCalls() {
  74. $methods = array(
  75. 'convertSpecifiers', 'convert', 'serverOffset', 'fromString',
  76. 'nice', 'niceShort', 'daysAsSql', 'dayAsSql',
  77. 'isToday', 'isThisMonth', 'isThisYear', 'wasYesterday',
  78. 'isTomorrow', 'toQuarter', 'toUnix', 'toAtom', 'toRSS',
  79. 'wasWithinLast', 'gmt', 'format', 'i18nFormat',
  80. );
  81. $CakeTime = $this->getMock(__NAMESPACE__ . '\TimeMock', $methods);
  82. $Time = new TimeHelperTestObject($this->View, array('engine' => __NAMESPACE__ . '\TimeMock'));
  83. $Time->attach($CakeTime);
  84. foreach ($methods as $method) {
  85. $CakeTime->expects($this->at(0))->method($method);
  86. $Time->{$method}('who', 'what', 'when', 'where', 'how');
  87. }
  88. $CakeTime = $this->getMock(__NAMESPACE__ . '\TimeMock', array('timeAgoInWords'));
  89. $Time = new TimeHelperTestObject($this->View, array('engine' => __NAMESPACE__ . '\TimeMock'));
  90. $Time->attach($CakeTime);
  91. $CakeTime->expects($this->at(0))->method('timeAgoInWords');
  92. $Time->timeAgoInWords('who', array('what'), array('when'), array('where'), array('how'));
  93. }
  94. /**
  95. * test engine override
  96. */
  97. public function testEngineOverride() {
  98. $Time = new TimeHelperTestObject($this->View, array('engine' => 'TestAppEngine'));
  99. $this->assertInstanceOf('TestApp\Utility\TestAppEngine', $Time->engine());
  100. Plugin::load('TestPlugin');
  101. $Time = new TimeHelperTestObject($this->View, array('engine' => 'TestPlugin.TestPluginEngine'));
  102. $this->assertInstanceOf('TestPlugin\Utility\TestPluginEngine', $Time->engine());
  103. Plugin::unload('TestPlugin');
  104. }
  105. /**
  106. * Test element wrapping in timeAgoInWords
  107. *
  108. * @return void
  109. */
  110. public function testTimeAgoInWords() {
  111. $Time = new TimeHelper($this->View);
  112. $timestamp = strtotime('+8 years, +4 months +2 weeks +3 days');
  113. $result = $Time->timeAgoInWords($timestamp, array(
  114. 'end' => '1 years',
  115. 'element' => 'span'
  116. ));
  117. $expected = array(
  118. 'span' => array(
  119. 'title' => $timestamp,
  120. 'class' => 'time-ago-in-words'
  121. ),
  122. 'on ' . date('j/n/y', $timestamp),
  123. '/span'
  124. );
  125. $this->assertTags($result, $expected);
  126. $result = $Time->timeAgoInWords($timestamp, array(
  127. 'end' => '1 years',
  128. 'element' => array(
  129. 'title' => 'testing',
  130. 'rel' => 'test'
  131. )
  132. ));
  133. $expected = array(
  134. 'span' => array(
  135. 'title' => 'testing',
  136. 'class' => 'time-ago-in-words',
  137. 'rel' => 'test'
  138. ),
  139. 'on ' . date('j/n/y', $timestamp),
  140. '/span'
  141. );
  142. $this->assertTags($result, $expected);
  143. $timestamp = strtotime('+2 weeks');
  144. $result = $Time->timeAgoInWords(
  145. $timestamp,
  146. array('end' => '1 years', 'element' => 'div')
  147. );
  148. $expected = array(
  149. 'div' => array(
  150. 'title' => $timestamp,
  151. 'class' => 'time-ago-in-words'
  152. ),
  153. '2 weeks',
  154. '/div'
  155. );
  156. $this->assertTags($result, $expected);
  157. }
  158. }