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
|
<?php
/**
* Tests for the \PHP_CodeSniffer\Util\Timing class.
*
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
* @copyright 2024 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Tests\Core\Util\Timing;
use PHP_CodeSniffer\Util\Timing;
use PHPUnit\Framework\TestCase;
/**
* Tests for the \PHP_CodeSniffer\Util\Timing class.
*
* {@internal These tests need to run in separate processes as the Timing class uses static properties
* to keep track of the start time and whether or not the runtime has been printed and these
* can't be unset/reset once set.}
*
* @covers \PHP_CodeSniffer\Util\Timing
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
final class TimingTest extends TestCase
{
/**
* Verify that getDuration() returns 0 when the timer wasn't started.
*
* @return void
*/
public function testGetDurationWithoutStartReturnsZero()
{
$this->assertSame(0, Timing::getDuration());
}//end testGetDurationWithoutStartReturnsZero()
/**
* Verify that getDuration() returns 0 when the timer wasn't started.
*
* @return void
*/
public function testGetDurationWithStartReturnsMilliseconds()
{
Timing::startTiming();
usleep(1500);
$duration = Timing::getDuration();
$this->assertTrue(is_float($duration));
$this->assertGreaterThan(1, $duration);
$this->assertLessThan(15, $duration);
}//end testGetDurationWithStartReturnsMilliseconds()
/**
* Verify that printRunTime() doesn't print anything if the timer wasn't started.
*
* @return void
*/
public function testTimeIsNotPrintedIfTimerWasNeverStarted()
{
$this->expectOutputString('');
Timing::printRunTime();
}//end testTimeIsNotPrintedIfTimerWasNeverStarted()
/**
* Verify that printRunTime() doesn't print anything if the timer wasn't started.
*
* @return void
*/
public function testTimeIsNotPrintedIfTimerWasNeverStartedEvenWhenForced()
{
$this->expectOutputString('');
Timing::printRunTime(true);
}//end testTimeIsNotPrintedIfTimerWasNeverStartedEvenWhenForced()
/**
* Verify that printRunTime() when called multiple times only prints the runtime information once.
*
* @return void
*/
public function testTimeIsPrintedOnlyOnce()
{
$this->expectOutputRegex('`^Time: [0-9]+ms; Memory: [0-9\.]+MB'.PHP_EOL.PHP_EOL.'$`');
Timing::startTiming();
usleep(2000);
Timing::printRunTime();
Timing::printRunTime();
Timing::printRunTime();
}//end testTimeIsPrintedOnlyOnce()
/**
* Verify that printRunTime() when called multiple times prints the runtime information multiple times if forced.
*
* @return void
*/
public function testTimeIsPrintedMultipleTimesOnlyIfForced()
{
$this->expectOutputRegex('`^(Time: [0-9]+ms; Memory: [0-9\.]+MB'.PHP_EOL.PHP_EOL.'){3}$`');
Timing::startTiming();
usleep(2000);
Timing::printRunTime(true);
Timing::printRunTime(true);
Timing::printRunTime(true);
}//end testTimeIsPrintedMultipleTimesOnlyIfForced()
}//end class
|