|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the \PHP_CodeSniffer\Util\Timing class. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl> |
| 6 | + * @copyright 2024 PHPCSStandards and contributors |
| 7 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Util\Timing; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Util\Timing; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +/** |
| 16 | + * Tests for the \PHP_CodeSniffer\Util\Timing class. |
| 17 | + * |
| 18 | + * {@internal These tests need to run in separate processes as the Timing class uses static properties |
| 19 | + * to keep track of the start time and whether or not the runtime has been printed and these |
| 20 | + * can't be unset/reset once set.} |
| 21 | + * |
| 22 | + * @covers \PHP_CodeSniffer\Util\Timing |
| 23 | + * |
| 24 | + * @runTestsInSeparateProcesses |
| 25 | + * @preserveGlobalState disabled |
| 26 | + */ |
| 27 | +final class TimingTest extends TestCase |
| 28 | +{ |
| 29 | + |
| 30 | + |
| 31 | + /** |
| 32 | + * Verify that getDuration() returns 0 when the timer wasn't started. |
| 33 | + * |
| 34 | + * @return void |
| 35 | + */ |
| 36 | + public function testGetDurationWithoutStartReturnsZero() |
| 37 | + { |
| 38 | + $this->assertSame(0, Timing::getDuration()); |
| 39 | + |
| 40 | + }//end testGetDurationWithoutStartReturnsZero() |
| 41 | + |
| 42 | + |
| 43 | + /** |
| 44 | + * Verify that getDuration() returns 0 when the timer wasn't started. |
| 45 | + * |
| 46 | + * @return void |
| 47 | + */ |
| 48 | + public function testGetDurationWithStartReturnsMilliseconds() |
| 49 | + { |
| 50 | + Timing::startTiming(); |
| 51 | + usleep(1500); |
| 52 | + $duration = Timing::getDuration(); |
| 53 | + |
| 54 | + $this->assertTrue(is_float($duration)); |
| 55 | + $this->assertGreaterThan(1, $duration); |
| 56 | + $this->assertLessThan(15, $duration); |
| 57 | + |
| 58 | + }//end testGetDurationWithStartReturnsMilliseconds() |
| 59 | + |
| 60 | + |
| 61 | + /** |
| 62 | + * Verify that printRunTime() doesn't print anything if the timer wasn't started. |
| 63 | + * |
| 64 | + * @return void |
| 65 | + */ |
| 66 | + public function testTimeIsNotPrintedIfTimerWasNeverStarted() |
| 67 | + { |
| 68 | + $this->expectOutputString(''); |
| 69 | + Timing::printRunTime(); |
| 70 | + |
| 71 | + }//end testTimeIsNotPrintedIfTimerWasNeverStarted() |
| 72 | + |
| 73 | + |
| 74 | + /** |
| 75 | + * Verify that printRunTime() doesn't print anything if the timer wasn't started. |
| 76 | + * |
| 77 | + * @return void |
| 78 | + */ |
| 79 | + public function testTimeIsNotPrintedIfTimerWasNeverStartedEvenWhenForced() |
| 80 | + { |
| 81 | + $this->expectOutputString(''); |
| 82 | + Timing::printRunTime(true); |
| 83 | + |
| 84 | + }//end testTimeIsNotPrintedIfTimerWasNeverStartedEvenWhenForced() |
| 85 | + |
| 86 | + |
| 87 | + /** |
| 88 | + * Verify that printRunTime() when called multiple times only prints the runtime information once. |
| 89 | + * |
| 90 | + * @return void |
| 91 | + */ |
| 92 | + public function testTimeIsPrintedOnlyOnce() |
| 93 | + { |
| 94 | + $this->expectOutputRegex('`^Time: [0-9]+ms; Memory: [0-9\.]+MB'.PHP_EOL.PHP_EOL.'$`'); |
| 95 | + |
| 96 | + Timing::startTiming(); |
| 97 | + usleep(2000); |
| 98 | + Timing::printRunTime(); |
| 99 | + Timing::printRunTime(); |
| 100 | + Timing::printRunTime(); |
| 101 | + |
| 102 | + }//end testTimeIsPrintedOnlyOnce() |
| 103 | + |
| 104 | + |
| 105 | + /** |
| 106 | + * Verify that printRunTime() when called multiple times prints the runtime information multiple times if forced. |
| 107 | + * |
| 108 | + * @return void |
| 109 | + */ |
| 110 | + public function testTimeIsPrintedMultipleTimesOnlyIfForced() |
| 111 | + { |
| 112 | + $this->expectOutputRegex('`^(Time: [0-9]+ms; Memory: [0-9\.]+MB'.PHP_EOL.PHP_EOL.'){3}$`'); |
| 113 | + |
| 114 | + Timing::startTiming(); |
| 115 | + usleep(2000); |
| 116 | + Timing::printRunTime(true); |
| 117 | + Timing::printRunTime(true); |
| 118 | + Timing::printRunTime(true); |
| 119 | + |
| 120 | + }//end testTimeIsPrintedMultipleTimesOnlyIfForced() |
| 121 | + |
| 122 | + |
| 123 | +}//end class |
0 commit comments