-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNetCommonsTimeTest.php
More file actions
255 lines (217 loc) · 7.37 KB
/
NetCommonsTimeTest.php
File metadata and controls
255 lines (217 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* NetCommonsTimeTest
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('NetCommonsTime', 'NetCommons.Utility');
App::uses('CakeTime', 'Utility');
App::uses('Current', 'NetCommons.Utility');
App::uses('NetCommonsCakeTestCase', 'NetCommons.TestSuite');
/**
* Class NetCommonsTimeTest
*/
class NetCommonsTimeTest extends NetCommonsCakeTestCase {
/**
* Fixtures
*
* @var array
*/
public $fixtures = array(
'plugin.site_manager.site_setting',
'plugin.users.user',
);
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->Time = new CakeTime();
$this->_systemTimezoneIdentifier = date_default_timezone_get();
Configure::write('Config.language', 'ja');
Current::$current['Language']['id'] = 2; // ja
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
unset($this->Time);
$this->_restoreSystemTimezone();
}
/**
* Restored the original system timezone
*
* @return void
*/
protected function _restoreSystemTimezone() {
date_default_timezone_set($this->_systemTimezoneIdentifier);
}
/**
* DateTimeクラスがUnixtime範囲外の日時でもタイムゾーン変換できることを確認
*
* @return void
*/
public function testOutRangeUnixtimeConvert() {
date_default_timezone_set('UTC');
$outRange = new DateTime('1888-01-01 00:00:00', new DateTimeZone('UTC'));
$outRange->setTimezone(new DateTimeZone('Asia/Tokyo'));
$this->assertEquals('1888-01-01 09:00:00', $outRange->format('Y-m-d H:i:s'));
$outRange = new DateTime('9999-01-01 00:00:00', new DateTimeZone('UTC'));
$outRange->setTimezone(new DateTimeZone('Asia/Tokyo'));
$this->assertEquals('9999-01-01 09:00:00', $outRange->format('Y-m-d H:i:s'));
}
/**
* 同一セッション中は別インスタンスでも同じ日時を返すことを確認
*
* @return void
*/
public function testNowStatic() {
// Static call
$start = NetCommonsTime::getNowDatetime();
sleep(1);
// 1秒後のStatic call
$after1secNow = NetCommonsTime::getNowDatetime();
$this->assertEquals($start, $after1secNow);
// newしても同じ日時
$netCommonsTime1 = new NetCommonsTime();
$firstInstanceNow = $netCommonsTime1->getNowDatetime();
$this->assertEquals($start, $firstInstanceNow);
$netCommonsTime2 = new NetCommonsTime();
$secondInstanceNow = $netCommonsTime2->getNowDatetime();
$this->assertEquals($start, $secondInstanceNow);
}
/**
* テストのために現在日時を差し替えられることを確認
*
* @return void
*/
public function testNowInjection() {
$netCommonsTime = new NetCommonsTime();
$now = $netCommonsTime->getNowDatetime();
$this->assertNotEquals('2000-01-01 00:00:00', $now);
// NetCommonsTimeの現在時刻を差し替える
$nowProperty = new ReflectionProperty('NetCommonsTime', '_now');
$nowProperty->setAccessible(true);
$nowProperty->setValue(strtotime('2000-01-01 00:00:00'));
// new NetCommonsTime()->getNowDatetime() が差し替えた時刻になることを確認
$time = new NetCommonsTime();
$sasikaeNowDatetime = $time->getNowDatetime();
$this->assertEquals('2000-01-01 00:00:00', $sasikaeNowDatetime);
//Static callでも差し替えた日時になる
$this->assertEquals('2000-01-01 00:00:00', NetCommonsTime::getNowDatetime());
// メソッドチェーンもOK
$this->assertEquals('2000-01-01 00:00:00', (new NetCommonsTime())->getNowDatetime());
$nowProperty->setValue(null);
}
/**
* 配列中の指定されたキーの値をユーザタイムゾーンに変換するテスト
*
* @return void
*/
public function testToUserDatetimeArray() {
$netCommonsTime = new NetCommonsTime();
$data = [
'BlogEntry' => [
'title' => 'Title',
'body1' => 'Body',
'published_datetime' => '2000-01-01 00:00:00',
]
];
Current::$current['User']['timezone'] = 'Asia/Tokyo';
$userData = $netCommonsTime->toUserDatetimeArray($data, array('BlogEntry.published_datetime'));
$this->assertEquals('2000-01-01 09:00:00', $userData['BlogEntry']['published_datetime']);
}
/**
* 配列中の指定されたキーの値をサーバタイムゾーンに変換するテスト
*
* @return void
*/
public function testToServerDatetimeArray() {
$netCommonsTime = new NetCommonsTime();
$data = [
'BlogEntry' => [
'title' => 'Title',
'body1' => 'Body',
'published_datetime' => '2000-01-01 09:00:00',
]
];
Current::$current['User']['timezone'] = 'Asia/Tokyo';
$userData = $netCommonsTime->toServerDatetimeArray($data, array('BlogEntry.published_datetime'));
$this->assertEquals('2000-01-01 00:00:00', $userData['BlogEntry']['published_datetime']);
}
/**
* SiteTimezoneの取得テスト
*
* @return void
*/
public function testGetSiteTimezone() {
$siteTimezoneProperty = new ReflectionProperty('NetCommonsTime', 'siteTimezone');
$siteTimezoneProperty->setAccessible(true);
$siteTimezoneProperty->setValue(null);
// NetCommonsTimeの現在時刻を差し替える
$netCommonsTime = new NetCommonsTime();
$method = new ReflectionMethod($netCommonsTime, 'getSiteTimezone');
$method->setAccessible(true);
$this->getMockForModel('SiteManager.SiteSetting', ['getSiteTimezone'])
->expects($this->once())
->method('getSiteTimezone')
->will($this->returnValue('Asia/Tokyo'));
$siteTimezone = $method->invoke($netCommonsTime);
$this->assertEquals('Asia/Tokyo', $siteTimezone);
$method->invoke($netCommonsTime); // 2回目はSiteSettingを呼び出さない
$siteTimezoneProperty = new ReflectionProperty('NetCommonsTime', 'siteTimezone');
$siteTimezoneProperty->setAccessible(true);
$siteTimezoneProperty->setValue(null);
}
/**
* ゲストでUser.timezoneが取得できないときは、NetCommonsTime::_getSiteTimezone()でサイトタイムゾーンを得るのを確認
*
* @return void
*/
public function testGuestConvertCallGetSiteTimezone() {
Current::$current['User']['timezone'] = null;
// NetCommonsTime::getSiteTimezone()が2回呼ばれるはず
$mock = $this->getMock('NetCommonsTime', ['getSiteTimezone']);
$mock->expects($this->exactly(2))
->method('getSiteTimezone')
->will($this->returnValue('Asia/Tokyo'));
$mock->toServerDatetime('2000-01-01 00:00:00');
$mock->toUserDatetime('2000-01-01 00:00:00');
}
/**
* コンストラクタからチェーンメソッドでも呼べるのを確認した
*
* @return void
*/
public function testMethodChain() {
$siteTimezone = (new NetCommonsTime())->getSiteTimezone();
$this->assertEquals('Asia/Tokyo', $siteTimezone);
}
/**
* メソッドチェーンとインスタンス生成でのメモリ消費ぐあいの確認
*
* @return void
*/
public function testMethodChainUseMemory() {
// コンストラクタから直接メソッドチェーンするとメモリは余計には使われない
echo memory_get_usage() . '/';
$timezone = (new NetCommonsTime())->getSiteTimezone();
echo memory_get_usage() . '/';
$timezone = (new NetCommonsTime())->getSiteTimezone();
echo memory_get_usage() . '/';
// インスタンスをそれぞれ作ると各インスタンス毎にメモリを消費する
echo memory_get_usage() . '/';
$instance1 = new NetCommonsTime();
echo memory_get_usage() . '/';
$instance2 = new NetCommonsTime();
echo memory_get_usage() . '/';
unset($instance1, $instance2);
}
}