]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/Date/tests/DateSpanTest.php
[PEAR] Modernize Validate code
[quix0rs-gnu-social.git] / extlib / Date / tests / DateSpanTest.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4                                                        |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2003 Leandro Lucarella                            |
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: Leandro Lucarella <llucax@php.net>                           |
17 // +----------------------------------------------------------------------+
18 //
19 // $Id$
20 //
21
22 require_once 'Date.php';
23 require_once 'Date/Span.php';
24 require_once 'PHPUnit/Autoload.php';
25
26 /**
27  * Test case for Date_Span
28  *
29  * @package Date
30  * @author Leandro Lucarella <llucax@php.net>
31  */
32 class Date_SpanTest extends PHPUnit_Framework_TestCase
33 {
34     public $time;
35
36     public function setUp()
37     {
38         $this->time = new Date_Span(97531);
39     }
40
41     public function tearDown()
42     {
43         unset($this->time);
44     }
45
46     public function testSetFromArray()
47     {
48         $this->time->setFromArray(array(5, 48.5, 28.5, 31));
49         $this->assertEquals(
50             '7:0:59:1',
51             sprintf(
52                 '%d:%d:%d:%d',
53                 $this->time->day,
54                 $this->time->hour,
55                 $this->time->minute,
56                 $this->time->second
57             )
58         );
59     }
60
61     public function testSetFromString()
62     {
63         $this->time->setFromString('5:00:59:31');
64         $this->assertEquals(
65             '5:0:59:31',
66             sprintf(
67                 '%d:%d:%d:%d',
68                 $this->time->day,
69                 $this->time->hour,
70                 $this->time->minute,
71                 $this->time->second
72             )
73         );
74     }
75
76     public function testSetFromSeconds()
77     {
78         $this->time->setFromSeconds(434344);
79         $this->assertEquals(
80             '5:0:39:4',
81             sprintf(
82                 '%d:%d:%d:%d',
83                 $this->time->day,
84                 $this->time->hour,
85                 $this->time->minute,
86                 $this->time->second
87             )
88         );
89     }
90
91     public function testSetFromMinutes()
92     {
93         $this->time->setFromMinutes(7860.0166666666);
94         $this->assertEquals(
95             '5:11:0:1',
96             sprintf(
97                 '%d:%d:%d:%d',
98                 $this->time->day,
99                 $this->time->hour,
100                 $this->time->minute,
101                 $this->time->second
102             )
103         );
104     }
105
106     public function testSetFromHours()
107     {
108         $this->time->setFromHours(50.12345);
109         $this->assertEquals(
110             '2:2:7:24',
111             sprintf(
112                 '%d:%d:%d:%d',
113                 $this->time->day,
114                 $this->time->hour,
115                 $this->time->minute,
116                 $this->time->second
117             )
118         );
119     }
120
121     public function testSetFromDays()
122     {
123         $this->time->setFromDays(pi());
124         $this->assertEquals(
125             '3:3:23:54',
126             sprintf(
127                 '%d:%d:%d:%d',
128                 $this->time->day,
129                 $this->time->hour,
130                 $this->time->minute,
131                 $this->time->second
132             )
133         );
134     }
135
136     public function testSetFromDateDiff()
137     {
138         $this->time->setFromDateDiff(
139             new Date('2004-03-10 01:15:59'),
140             new Date('2003-03-10 00:10:50')
141         );
142         $this->assertEquals(
143             '366:1:5:9',
144             sprintf(
145                 '%d:%d:%d:%d',
146                 $this->time->day,
147                 $this->time->hour,
148                 $this->time->minute,
149                 $this->time->second
150             )
151         );
152     }
153
154     public function testCopy()
155     {
156         $time = new Date_Span();
157         $time->copy($this->time);
158         $this->assertEquals(
159             sprintf(
160                 '%d:%d:%d:%d',
161                 $this->time->day,
162                 $this->time->hour,
163                 $this->time->minute,
164                 $this->time->second
165             ),
166             sprintf(
167                 '%d:%d:%d:%d',
168                 $time->day,
169                 $time->hour,
170                 $time->minute,
171                 $time->second
172             )
173         );
174     }
175
176     public function testFormat()
177     {
178         $codes = array(
179             'C' => '1, 03:05:31',
180             'd' => '1.1288310185185',
181             'D' => '1',
182             'e' => '27.091944444444',
183             'f' => '1625.5166666667',
184             'g' => '97531',
185             'h' => '3',
186             'H' => '03',
187             'i' => '3',
188             'I' => '03',
189             'm' => '5',
190             'M' => '05',
191             'n' => "\n",
192             'p' => 'am',
193             'P' => 'AM',
194             'r' => '03:05:31 am',
195             'R' => '03:05',
196             's' => '31',
197             'S' => '31',
198             't' => "\t",
199             'T' => '03:05:31',
200             '%' => '%',
201         );
202         foreach ($codes as $code => $expected) {
203             $this->assertEquals(
204                 "$code: $expected",
205                 $this->time->format("$code: %$code")
206             );
207         }
208     }
209
210     public function testAdd()
211     {
212         $this->time->add(new Date_Span(6000));
213         $result = $this->time->toSeconds();
214         $expected = 103531;
215         $this->assertEquals($expected, $result);
216     }
217
218     public function testSubtract()
219     {
220         $this->time->subtract(new Date_Span(6000));
221         $result = $this->time->toSeconds();
222         $expected = 91531;
223         $this->assertEquals($expected, $result);
224     }
225 }