home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / testunit_date_span.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  5.5 KB  |  180 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Leandro Lucarella <llucax@php.net>                           |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: testunit_date_span.php,v 1.3 2003/10/24 19:41:06 mroch Exp $
  20. //
  21.  
  22. require_once 'Date.php';
  23. require_once 'Date/Span.php';
  24. require_once 'PHPUnit.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_TestCase {
  33.  
  34.     var $time;
  35.  
  36.     function Date_SpanTest($name) {
  37.         $this->PHPUnit_TestCase($name);
  38.     }
  39.  
  40.     function setUp() {
  41.         $this->time = new Date_Span(97531);
  42.     }
  43.  
  44.     function tearDown() {
  45.         unset($this->time);
  46.     }
  47.  
  48.     function testSetFromArray() {
  49.         $this->time->setFromArray(array(5, 48.5, 28.5, 31));
  50.         $this->assertEquals(
  51.             '7:0:59:1',
  52.             sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
  53.                 $this->time->minute, $this->time->second)
  54.         );
  55.     }
  56.  
  57.     function testSetFromString() {
  58.         $this->time->setFromString('5:00:59:31');
  59.         $this->assertEquals(
  60.             '5:0:59:31',
  61.             sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
  62.                 $this->time->minute, $this->time->second)
  63.         );
  64.     }
  65.  
  66.     function testSetFromSeconds() {
  67.         $this->time->setFromSeconds(434344);
  68.         $this->assertEquals(
  69.             '5:0:39:4',
  70.             sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
  71.                 $this->time->minute, $this->time->second)
  72.         );
  73.     }
  74.  
  75.     function testSetFromMinutes() {
  76.         $this->time->setFromMinutes(7860.0166666666);
  77.         $this->assertEquals(
  78.             '5:11:0:1',
  79.             sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
  80.                 $this->time->minute, $this->time->second)
  81.         );
  82.     }
  83.  
  84.     function testSetFromHours() {
  85.         $this->time->setFromHours(50.12345);
  86.         $this->assertEquals(
  87.             '2:2:7:24',
  88.             sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
  89.                 $this->time->minute, $this->time->second)
  90.         );
  91.     }
  92.  
  93.     function testSetFromDays() {
  94.         $this->time->setFromDays(pi());
  95.         $this->assertEquals(
  96.             '3:3:23:54',
  97.             sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
  98.                 $this->time->minute, $this->time->second)
  99.         );
  100.     }
  101.  
  102.     function testSetFromDateDiff() {
  103.         $this->time->setFromDateDiff(
  104.             new Date('2004-03-10 01:15:59'),
  105.             new Date('2003-03-10 00:10:50')
  106.         );
  107.         $this->assertEquals(
  108.             '366:1:5:9',
  109.             sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
  110.                 $this->time->minute, $this->time->second)
  111.         );
  112.     }
  113.  
  114.     function testCopy() {
  115.         $time = new Date_Span();
  116.         $time->copy($this->time);
  117.         $this->assertEquals(
  118.             sprintf('%d:%d:%d:%d', $this->time->day, $this->time->hour,
  119.                 $this->time->minute, $this->time->second),
  120.             sprintf('%d:%d:%d:%d', $time->day, $time->hour,
  121.                 $time->minute, $time->second)
  122.         );
  123.     }
  124.  
  125.     function testFormat() {
  126.         $codes = array(
  127.             'C' => '1, 03:05:31',
  128.             'd' => '1.1288310185185',
  129.             'D' => '1',
  130.             'e' => '27.091944444444',
  131.             'f' => '1625.5166666667',
  132.             'g' => '97531',
  133.             'h' => '3',
  134.             'H' => '03',
  135.             'i' => '3',
  136.             'I' => '03',
  137.             'm' => '5',
  138.             'M' => '05',
  139.             'n' => "\n",
  140.             'p' => 'am',
  141.             'P' => 'AM',
  142.             'r' => '03:05:31 am',
  143.             'R' => '03:05',
  144.             's' => '31',
  145.             'S' => '31',
  146.             't' => "\t",
  147.             'T' => '03:05:31',
  148.             '%' => '%',
  149.         );
  150.         foreach ($codes as $code => $expected) {
  151.             $this->assertEquals(
  152.                 "$code: $expected", $this->time->format("$code: %$code")
  153.             );
  154.         }
  155.     }
  156.  
  157.     function testAdd() {
  158.         $this->time->add(new Date_Span(6000));
  159.         $result = $this->time->toSeconds();
  160.         $expected = 103531;
  161.         $this->assertEquals($expected, $result); 
  162.     }
  163.  
  164.     function testSubtract() {
  165.         $this->time->subtract(new Date_Span(6000));
  166.         $result = $this->time->toSeconds();
  167.         $expected = 91531;
  168.         $this->assertEquals($expected, $result); 
  169.     }
  170.  
  171. }
  172.  
  173. // runs the tests
  174. $suite = new PHPUnit_TestSuite("Date_SpanTest");
  175. $result = PHPUnit::run($suite);
  176. // prints the tests
  177. echo $result->toString();
  178.  
  179. ?>
  180.