TimeHelperTest.php 4.5 KB

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