Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
// vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
3
// +----------------------------------------------------------------------+
4
// | PHP Version 4                                                        |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2003 Marshall Roch                                |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to the New BSD license, That is bundled  |
9
// | with this package in the file LICENSE, and is available through      |
10
// | the world-wide-web at                                                |
11
// | http://www.opensource.org/licenses/bsd-license.php                   |
12
// | If you did not receive a copy of the new BSDlicense and are unable   |
13
// | to obtain it through the world-wide-web, please send a note to       |
14
// | pear-dev@lists.php.net so we can mail you a copy immediately.        |
15
// +----------------------------------------------------------------------+
16
// | Author: Marshall Roch <mroch@php.net>                                |
17
// +----------------------------------------------------------------------+
18
//
19
// $Id: testunit_date.php,v 1.3 2005/11/15 00:16:40 pajoye Exp $
20
//
21
 
22
require_once 'Date.php';
23
require_once 'PHPUnit.php';
24
 
25
class myDate extends Date {
26
    function myDate($date)
27
    {
28
        $this->Date($date);
29
    }
30
}
31
 
32
/**
33
 * Test case for Date
34
 *
35
 * @package Date
36
 * @author Marshall Roch <mroch@php.net>
37
 */
38
class Date_Test extends PHPUnit_TestCase {
39
 
40
    var $time;
41
 
42
    function Date_Test($name)
43
    {
44
        $this->PHPUnit_TestCase($name);
45
    }
46
 
47
    function setUp()
48
    {
49
        $this->time = new Date("2003-10-04 14:03:24");
50
    }
51
 
52
    function tearDown()
53
    {
54
        unset($this->time);
55
    }
56
 
57
    function testDateNull()
58
    {
59
        $time = new Date();
60
        $this->assertEquals(
61
            date('Y-m-d H:i:s'),
62
            sprintf('%04d-%02d-%02d %02d:%02d:%02d',
63
                $time->year, $time->month, $time->day,
64
                $time->hour, $time->minute, $time->second)
65
        );
66
    }
67
 
68
    function testAbstraction()
69
    {
70
        $d = new Date();
71
        $my = new myDate($d);
72
        $this->assertEquals($d->getDate(),$my->getDate());
73
    }
74
 
75
    function testDateCopy()
76
    {
77
        $temp = new Date($this->time);
78
        $this->assertEquals($temp, $this->time);
79
    }
80
 
81
    function testDateISO()
82
    {
83
        $temp = new Date("2003-10-04 14:03:24");
84
        $this->assertEquals(
85
            '2003-10-04 14:03:24',
86
            sprintf('%04d-%02d-%02d %02d:%02d:%02d',
87
                $temp->year, $temp->month, $temp->day,
88
                $temp->hour, $temp->minute, $temp->second)
89
        );
90
    }
91
 
92
    function testDateISOBasic()
93
    {
94
        $temp = new Date("20031004T140324");
95
        $this->assertEquals(
96
            '2003-10-04 14:03:24',
97
            sprintf('%04d-%02d-%02d %02d:%02d:%02d',
98
                $temp->year, $temp->month, $temp->day,
99
                $temp->hour, $temp->minute, $temp->second)
100
        );
101
    }
102
 
103
    function testDateISOExtended()
104
    {
105
        $temp = new Date("2003-10-04T14:03:24");
106
        $this->assertEquals(
107
            '2003-10-04 14:03:24',
108
            sprintf('%04d-%02d-%02d %02d:%02d:%02d',
109
                $temp->year, $temp->month, $temp->day,
110
                $temp->hour, $temp->minute, $temp->second)
111
        );
112
    }
113
 
114
    function testDateISOTimestamp()
115
    {
116
        $temp = new Date("20031004140324");
117
        $this->assertEquals(
118
            '2003-10-04 14:03:24',
119
            sprintf('%04d-%02d-%02d %02d:%02d:%02d',
120
                $temp->year, $temp->month, $temp->day,
121
                $temp->hour, $temp->minute, $temp->second)
122
        );
123
    }
124
 
125
    function testDateUnixtime()
126
    {
127
        $temp = new Date(strtotime("2003-10-04 14:03:24"));
128
        $this->assertEquals(
129
            '2003-10-04 14:03:24',
130
            sprintf('%04d-%02d-%02d %02d:%02d:%02d',
131
                $temp->year, $temp->month, $temp->day,
132
                $temp->hour, $temp->minute, $temp->second)
133
        );
134
    }
135
 
136
    function testSetDateISO()
137
    {
138
        $this->time->setDate("2003-10-04 14:03:24");
139
        $this->assertEquals(
140
            '2003-10-04 14:03:24',
141
            sprintf('%04d-%02d-%02d %02d:%02d:%02d',
142
                $this->time->year, $this->time->month, $this->time->day,
143
                $this->time->hour, $this->time->minute, $this->time->second)
144
        );
145
    }
146
 
147
    function testSetDateISOBasic()
148
    {
149
        $this->time->setDate("20031004T140324");
150
        $this->assertEquals(
151
            '2003-10-04 14:03:24',
152
            sprintf('%04d-%02d-%02d %02d:%02d:%02d',
153
                $this->time->year, $this->time->month, $this->time->day,
154
                $this->time->hour, $this->time->minute, $this->time->second)
155
        );
156
    }
157
 
158
    function testSetDateISOExtended()
159
    {
160
        $this->time->setDate("2003-10-04T14:03:24");
161
        $this->assertEquals(
162
            '2003-10-04 14:03:24',
163
            sprintf('%04d-%02d-%02d %02d:%02d:%02d',
164
                $this->time->year, $this->time->month, $this->time->day,
165
                $this->time->hour, $this->time->minute, $this->time->second)
166
        );
167
    }
168
 
169
    function testSetDateTimestamp()
170
    {
171
        $this->time->setDate("20031004140324");
172
        $this->assertEquals(
173
            '2003-10-04 14:03:24',
174
            sprintf('%04d-%02d-%02d %02d:%02d:%02d',
175
                $this->time->year, $this->time->month, $this->time->day,
176
                $this->time->hour, $this->time->minute, $this->time->second)
177
        );
178
    }
179
 
180
    function testSetDateUnixtime()
181
    {
182
        $this->time->setDate(strtotime("2003-10-04 14:03:24"));
183
        $this->assertEquals(
184
            '2003-10-04 14:03:24',
185
            sprintf('%04d-%02d-%02d %02d:%02d:%02d',
186
                $this->time->year, $this->time->month, $this->time->day,
187
                $this->time->hour, $this->time->minute, $this->time->second)
188
        );
189
    }
190
 
191
    function testGetDateISO()
192
    {
193
        $date = $this->time->getDate(DATE_FORMAT_ISO);
194
        $this->assertEquals('2003-10-04 14:03:24', $date);
195
    }
196
 
197
    function testGetDateISOBasic()
198
    {
199
        $date = $this->time->getDate(DATE_FORMAT_ISO_BASIC);
200
        $this->assertEquals('20031004T140324Z', $date);
201
    }
202
 
203
    function testGetDateISOExtended()
204
    {
205
        $date = $this->time->getDate(DATE_FORMAT_ISO_EXTENDED);
206
        $this->assertEquals('2003-10-04T14:03:24Z', $date);
207
    }
208
 
209
    function testGetDateTimestamp()
210
    {
211
        $date = $this->time->getDate(DATE_FORMAT_TIMESTAMP);
212
        $this->assertEquals('20031004140324', $date);
213
    }
214
 
215
    function testGetDateUnixtime()
216
    {
217
        $date = $this->time->getDate(DATE_FORMAT_UNIXTIME);
218
        $this->assertEquals(strtotime('2003-10-04 14:03:24'), $date);
219
    }
220
 
221
    function testFormat()
222
    {
223
        $codes = array(
224
            'a' => 'Sat',
225
            'A' => 'Saturday',
226
            'b' => 'Oct',
227
            'B' => 'October',
228
            'C' => '20',
229
            'd' => '04',
230
            'D' => '10/04/2003',
231
            'e' => '4',
232
            'H' => '14',
233
            'I' => '02',
234
            'j' => '277',
235
            'm' => '10',
236
            'M' => '03',
237
            'n' => "\n",
238
            'O' => '+00:00',
239
            'o' => '+00:00',
240
            'p' => 'pm',
241
            'P' => 'PM',
242
            'r' => '02:03:24 PM',
243
            'R' => '14:03',
244
            'S' => '24',
245
            't' => "\t",
246
            'T' => '14:03:24',
247
            'w' => '6',
248
            'U' => '40',
249
            'y' => '03',
250
            'Y' => '2003',
251
            '%' => '%'
252
        );
253
 
254
        foreach ($codes as $code => $expected) {
255
            $this->assertEquals(
256
                "$code: $expected", $this->time->format("$code: %$code")
257
            );
258
        }
259
    }
260
 
261
    function testToUTCbyOffset()
262
    {
263
        $this->time->setTZbyID('EST');
264
        $this->time->toUTC();
265
        $temp = new Date("2003-10-04 14:03:24");
266
        $temp->toUTCbyOffset("-05:00");
267
 
268
        $this->assertEquals($temp, $this->time);
269
    }
270
 
271
}
272
 
273
// runs the tests
274
$suite = new PHPUnit_TestSuite("Date_Test");
275
$result = PHPUnit::run($suite);
276
// prints the tests
277
echo $result->toString();
278
 
279
?>