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