Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 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
	public function testTimeHelperProxyMethodCalls() {
83
		$methods = array(
84
			'convertSpecifiers', 'convert', 'serverOffset', 'fromString',
85
			'nice', 'niceShort', 'daysAsSql', 'dayAsSql',
86
			'isToday', 'isThisMonth', 'isThisYear', 'wasYesterday',
87
			'isTomorrow', 'toQuarter', 'toUnix', 'toAtom', 'toRSS',
88
			'wasWithinLast', 'gmt', 'format', 'i18nFormat',
89
		);
90
		$CakeTime = $this->getMock('CakeTimeMock', $methods);
91
		$Time = new TimeHelperTestObject($this->View, array('engine' => 'CakeTimeMock'));
92
		$Time->attach($CakeTime);
93
		foreach ($methods as $method) {
94
			$CakeTime->expects($this->at(0))->method($method);
95
			$Time->{$method}('who', 'what', 'when', 'where', 'how');
96
		}
97
 
98
		$CakeTime = $this->getMock('CakeTimeMock', array('timeAgoInWords'));
99
		$Time = new TimeHelperTestObject($this->View, array('engine' => 'CakeTimeMock'));
100
		$Time->attach($CakeTime);
101
		$CakeTime->expects($this->at(0))->method('timeAgoInWords');
102
		$Time->timeAgoInWords('who', array('what'), array('when'), array('where'), array('how'));
103
	}
104
 
105
/**
106
 * test engine override
107
 */
108
	public function testEngineOverride() {
109
		App::build(array(
110
			'Utility' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Utility' . DS)
111
		), App::REGISTER);
112
		$Time = new TimeHelperTestObject($this->View, array('engine' => 'TestAppEngine'));
113
		$this->assertInstanceOf('TestAppEngine', $Time->engine());
114
 
115
		App::build(array(
116
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
117
		));
118
		CakePlugin::load('TestPlugin');
119
		$Time = new TimeHelperTestObject($this->View, array('engine' => 'TestPlugin.TestPluginEngine'));
120
		$this->assertInstanceOf('TestPluginEngine', $Time->engine());
121
		CakePlugin::unload('TestPlugin');
122
	}
123
 
124
/**
125
 * Test element wrapping in timeAgoInWords
126
 *
127
 * @return void
128
 */
129
	public function testTimeAgoInWords() {
130
		$Time = new TimeHelper($this->View);
131
		$timestamp = strtotime('+8 years, +4 months +2 weeks +3 days');
132
		$result = $Time->timeAgoInWords($timestamp, array(
133
			'end' => '1 years',
134
			'element' => 'span'
135
		));
136
		$expected = array(
137
			'span' => array(
138
				'title' => $timestamp,
139
				'class' => 'time-ago-in-words'
140
			),
141
			'on ' . date('j/n/y', $timestamp),
142
			'/span'
143
		);
144
		$this->assertTags($result, $expected);
145
 
146
		$result = $Time->timeAgoInWords($timestamp, array(
147
			'end' => '1 years',
148
			'element' => array(
149
				'title' => 'testing',
150
				'rel' => 'test'
151
			)
152
		));
153
		$expected = array(
154
			'span' => array(
155
				'title' => 'testing',
156
				'class' => 'time-ago-in-words',
157
				'rel' => 'test'
158
			),
159
			'on ' . date('j/n/y', $timestamp),
160
			'/span'
161
		);
162
		$this->assertTags($result, $expected);
163
 
164
		$timestamp = strtotime('+2 weeks');
165
		$result = $Time->timeAgoInWords(
166
			$timestamp,
167
			array('end' => '1 years', 'element' => 'div')
168
		);
169
		$expected = array(
170
			'div' => array(
171
				'title' => $timestamp,
172
				'class' => 'time-ago-in-words'
173
			),
174
			'2 weeks',
175
			'/div'
176
		);
177
		$this->assertTags($result, $expected);
178
	}
179
 
180
}