TimeHelperTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. namespace Cake\Test\TestCase\View\Helper;
  20. use Cake\Core\App;
  21. use Cake\Core\Configure;
  22. use Cake\Core\Plugin;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\View\Helper\TimeHelper;
  25. use Cake\View\View;
  26. /**
  27. * TimeHelperTestObject class
  28. *
  29. */
  30. class TimeHelperTestObject extends TimeHelper {
  31. public function attach(TimeMock $cakeTime) {
  32. $this->_engine = $cakeTime;
  33. }
  34. public function engine() {
  35. return $this->_engine;
  36. }
  37. }
  38. /**
  39. * TimeMock class
  40. *
  41. */
  42. class TimeMock {
  43. }
  44. /**
  45. * TimeHelperTest class
  46. *
  47. */
  48. class TimeHelperTest extends TestCase {
  49. public $Time = null;
  50. public $CakeTime = null;
  51. /**
  52. * setUp method
  53. *
  54. * @return void
  55. */
  56. public function setUp() {
  57. parent::setUp();
  58. $this->View = new View(null);
  59. $this->_appNamespace = Configure::read('App.namespace');
  60. Configure::write('App.namespace', 'TestApp');
  61. }
  62. /**
  63. * tearDown method
  64. *
  65. * @return void
  66. */
  67. public function tearDown() {
  68. unset($this->View);
  69. Configure::write('App.namespace', $this->_appNamespace);
  70. parent::tearDown();
  71. }
  72. /**
  73. * test Cake\Utility\Time class methods are called correctly
  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. public function testEngineOverride() {
  100. $Time = new TimeHelperTestObject($this->View, array('engine' => 'TestAppEngine'));
  101. $this->assertInstanceOf('TestApp\Utility\TestAppEngine', $Time->engine());
  102. Plugin::load('TestPlugin');
  103. $Time = new TimeHelperTestObject($this->View, array('engine' => 'TestPlugin.TestPluginEngine'));
  104. $this->assertInstanceOf('TestPlugin\Utility\TestPluginEngine', $Time->engine());
  105. Plugin::unload('TestPlugin');
  106. }
  107. /**
  108. * Test element wrapping in timeAgoInWords
  109. *
  110. * @return void
  111. */
  112. public function testTimeAgoInWords() {
  113. $Time = new TimeHelper($this->View);
  114. $timestamp = strtotime('+8 years, +4 months +2 weeks +3 days');
  115. $result = $Time->timeAgoInWords($timestamp, array(
  116. 'end' => '1 years',
  117. 'element' => 'span'
  118. ));
  119. $expected = array(
  120. 'span' => array(
  121. 'title' => $timestamp,
  122. 'class' => 'time-ago-in-words'
  123. ),
  124. 'on ' . date('j/n/y', $timestamp),
  125. '/span'
  126. );
  127. $this->assertTags($result, $expected);
  128. $result = $Time->timeAgoInWords($timestamp, array(
  129. 'end' => '1 years',
  130. 'element' => array(
  131. 'title' => 'testing',
  132. 'rel' => 'test'
  133. )
  134. ));
  135. $expected = array(
  136. 'span' => array(
  137. 'title' => 'testing',
  138. 'class' => 'time-ago-in-words',
  139. 'rel' => 'test'
  140. ),
  141. 'on ' . date('j/n/y', $timestamp),
  142. '/span'
  143. );
  144. $this->assertTags($result, $expected);
  145. $timestamp = strtotime('+2 weeks');
  146. $result = $Time->timeAgoInWords(
  147. $timestamp,
  148. array('end' => '1 years', 'element' => 'div')
  149. );
  150. $expected = array(
  151. 'div' => array(
  152. 'title' => $timestamp,
  153. 'class' => 'time-ago-in-words'
  154. ),
  155. '2 weeks',
  156. '/div'
  157. );
  158. $this->assertTags($result, $expected);
  159. }
  160. }