File: testunit_date_span.php

package info (click to toggle)
php-date 1.4.7-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze, wheezy
  • size: 508 kB
  • ctags: 697
  • sloc: php: 7,202; xml: 320; makefile: 50
file content (179 lines) | stat: -rw-r--r-- 5,808 bytes parent folder | download | duplicates (4)
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
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 Leandro Lucarella                            |
// +----------------------------------------------------------------------+
// | This source file is subject to the New BSD license, That is bundled  |
// | with this package in the file LICENSE, and is available through      |
// | the world-wide-web at                                                |
// | http://www.opensource.org/licenses/bsd-license.php                   |
// | If you did not receive a copy of the new BSDlicense and are unable   |
// | to obtain it through the world-wide-web, please send a note to       |
// | pear-dev@lists.php.net so we can mail you a copy immediately.        |
// +----------------------------------------------------------------------+
// | Author: Leandro Lucarella <llucax@php.net>                           |
// +----------------------------------------------------------------------+
//
// $Id: testunit_date_span.php,v 1.4 2005/11/15 00:16:40 pajoye Exp $
//

require_once 'Date.php';
require_once 'Date/Span.php';
require_once 'PHPUnit.php';

/**
 * Test case for Date_Span
 *
 * @package Date
 * @author Leandro Lucarella <llucax@php.net>
 */
class Date_SpanTest extends PHPUnit_TestCase {

    var $time;

    function Date_SpanTest($name) {
        $this->PHPUnit_TestCase($name);
    }

    function setUp() {
        $this->time = new Date_Span(97531);
    }

    function tearDown() {
        unset($this->time);
    }

    function testSetFromArray() {
        $this->time->setFromArray(array(5, 48.5, 28.5, 31));
        $this->assertEquals(
            '7:0:59:1',
            sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
                $this->time->minute, $this->time->second)
        );
    }

    function testSetFromString() {
        $this->time->setFromString('5:00:59:31');
        $this->assertEquals(
            '5:0:59:31',
            sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
                $this->time->minute, $this->time->second)
        );
    }

    function testSetFromSeconds() {
        $this->time->setFromSeconds(434344);
        $this->assertEquals(
            '5:0:39:4',
            sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
                $this->time->minute, $this->time->second)
        );
    }

    function testSetFromMinutes() {
        $this->time->setFromMinutes(7860.0166666666);
        $this->assertEquals(
            '5:11:0:1',
            sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
                $this->time->minute, $this->time->second)
        );
    }

    function testSetFromHours() {
        $this->time->setFromHours(50.12345);
        $this->assertEquals(
            '2:2:7:24',
            sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
                $this->time->minute, $this->time->second)
        );
    }

    function testSetFromDays() {
        $this->time->setFromDays(pi());
        $this->assertEquals(
            '3:3:23:54',
            sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
                $this->time->minute, $this->time->second)
        );
    }

    function testSetFromDateDiff() {
        $this->time->setFromDateDiff(
            new Date('2004-03-10 01:15:59'),
            new Date('2003-03-10 00:10:50')
        );
        $this->assertEquals(
            '366:1:5:9',
            sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
                $this->time->minute, $this->time->second)
        );
    }

    function testCopy() {
        $time = new Date_Span();
        $time->copy($this->time);
        $this->assertEquals(
            sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
                $this->time->minute, $this->time->second),
            sprintf('%d:%d:%d:%d', $time->day, $time->hour,
                $time->minute, $time->second)
        );
    }

    function testFormat() {
        $codes = array(
            'C' => '1, 03:05:31',
            'd' => '1.1288310185185',
            'D' => '1',
            'e' => '27.091944444444',
            'f' => '1625.5166666667',
            'g' => '97531',
            'h' => '3',
            'H' => '03',
            'i' => '3',
            'I' => '03',
            'm' => '5',
            'M' => '05',
            'n' => "\n",
            'p' => 'am',
            'P' => 'AM',
            'r' => '03:05:31 am',
            'R' => '03:05',
            's' => '31',
            'S' => '31',
            't' => "\t",
            'T' => '03:05:31',
            '%' => '%',
        );
        foreach ($codes as $code => $expected) {
            $this->assertEquals(
                "$code: $expected", $this->time->format("$code: %$code")
            );
        }
    }

    function testAdd() {
        $this->time->add(new Date_Span(6000));
        $result = $this->time->toSeconds();
        $expected = 103531;
        $this->assertEquals($expected, $result);
    }

    function testSubtract() {
        $this->time->subtract(new Date_Span(6000));
        $result = $this->time->toSeconds();
        $expected = 91531;
        $this->assertEquals($expected, $result);
    }

}

// runs the tests
$suite = new PHPUnit_TestSuite("Date_SpanTest");
$result = PHPUnit::run($suite);
// prints the tests
echo $result->toString();

?>