TimeHelperTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * TimeHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing 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->_CakeTime = $cakeTime;
  28. }
  29. public function engine() {
  30. return $this->_CakeTime;
  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. }