|
| 1 | +<?php |
| 2 | +namespace phpbu\App\Util; |
| 3 | + |
| 4 | +/** |
| 5 | + * Time utility test |
| 6 | + * |
| 7 | + * @package phpbu |
| 8 | + * @subpackage tests |
| 9 | + * @author Sebastian Feldmann <[email protected]> |
| 10 | + * @copyright Sebastian Feldmann <[email protected]> |
| 11 | + * @license https://opensource.org/licenses/MIT The MIT License (MIT) |
| 12 | + * @link http://www.phpbu.de/ |
| 13 | + * @since Class available since Release 5.1.2 |
| 14 | + */ |
| 15 | +class TimeTest extends \PHPUnit\Framework\TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Tests Time::timeSinceExecutionStart |
| 19 | + * |
| 20 | + * @expectedException \RuntimeException |
| 21 | + */ |
| 22 | + public function testTimeSinceExecutionStartFail() |
| 23 | + { |
| 24 | + $SERVER = $_SERVER; |
| 25 | + unset($_SERVER['REQUEST_TIME_FLOAT']); |
| 26 | + unset($_SERVER['REQUEST_TIME']); |
| 27 | + |
| 28 | + try { |
| 29 | + $time = Time::timeSinceExecutionStart(); |
| 30 | + } catch (\Exception $e) { |
| 31 | + $_SERVER = $SERVER; |
| 32 | + throw $e; |
| 33 | + } |
| 34 | + |
| 35 | + $this->assertFalse($time); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Tests Time::timeSinceExecutionStart |
| 40 | + */ |
| 41 | + public function testTimeSinceExecutionStartFloat() |
| 42 | + { |
| 43 | + $SERVER = $_SERVER; |
| 44 | + $_SERVER['REQUEST_TIME_FLOAT'] = microtime(true) - 60; |
| 45 | + $time = Time::timeSinceExecutionStart(); |
| 46 | + $this->assertEquals(60, floor($time)); |
| 47 | + $_SERVER = $SERVER; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Tests Time::timeSinceExecutionStart |
| 52 | + */ |
| 53 | + public function testTimeSinceExecutionStart() |
| 54 | + { |
| 55 | + $SERVER = $_SERVER; |
| 56 | + unset($_SERVER['REQUEST_TIME_FLOAT']); |
| 57 | + $_SERVER['REQUEST_TIME'] = time() - 60; |
| 58 | + $time = Time::timeSinceExecutionStart(); |
| 59 | + $this->assertEquals(60, floor($time)); |
| 60 | + $_SERVER = $SERVER; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Tests Time::formatTime |
| 65 | + */ |
| 66 | + public function testFormatTime() |
| 67 | + { |
| 68 | + $this->assertEquals('37 seconds', Time::formatTime(37)); |
| 69 | + $this->assertEquals('1 hour 1 second', Time::formatTime(3601)); |
| 70 | + $this->assertEquals('1 hour 1 minute 1 second', Time::formatTime(3661)); |
| 71 | + $this->assertEquals('2 hours 2 minutes 2 seconds', Time::formatTime(7322)); |
| 72 | + $this->assertEquals('1 hour', Time::formatTime(3600)); |
| 73 | + $this->assertEquals('1 minute', Time::formatTime(60)); |
| 74 | + $this->assertEquals('1 second', Time::formatTime(1)); |
| 75 | + } |
| 76 | +} |
0 commit comments