From dc942148f4176e3be6623f993f05b2e41c62e949 Mon Sep 17 00:00:00 2001 From: Bilge Date: Wed, 6 Nov 2024 14:06:50 +0000 Subject: [PATCH] Changed unique traces to exclude suppressed traces so they don't have to be filtered later. Changed ClassicTheme to emit line break before trace colour block instead of inside. Renamed CapabilitiesTest::testGigaSlow -> testVerySlow. Updated Readme to include performance threshold customization. --- README.md | 24 ++++++++++++------- src/PipExtension.php | 2 +- src/Printer.php | 10 ++++---- src/Theme/ClassicTheme.php | 24 ++++++++----------- src/Trace.php | 11 +++------ test/CapabilitiesTest.php | 4 ++-- .../errors & exceptions/E_DEPRECATED.phpt | 4 ++-- .../errors & exceptions/E_NOTICE.phpt | 4 ++-- .../E_WARNING duplicates.phpt | 4 ++-- .../errors & exceptions/E_WARNING.phpt | 4 ++-- .../errors & exceptions/mixed severities.phpt | 4 ++-- test/functional/risky.phpt | 4 ++-- test/functional/speed vslow.phpt | 4 ++-- 13 files changed, 51 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index d133732..22e5513 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Pip is a [PHPUnit][] extension that immediately prints exceptions and assertion ## Benefits * Display the name of each test case as it is executed. -* Display the execution time of each test in tiered colour bands. +* Display the execution time of each test in configurable, tiered colour bands. * Immediately print exceptions, assertion failures, warnings, notice and deprecation messages as they occur. * Flawless test suite indicator: success dot turns to red exclamation mark if any prior tests failed. Useful for CI consoles without a scrollback buffer. @@ -57,10 +57,10 @@ Pip's behaviour can be customized by adding `` nodes as children of t | Parameter name | Default value | Description | |-----------------|---------------|------------------------------------------------------------------------| -| perf.slow | 200 (ms) | Sets the performance threshold for _slow_ (yellow) tests | -| perf.vslow | 1000 (ms) | Sets the performance threshold for _very slow_ (red) tests | +| perf.slow | 200 (ms) | Sets the performance threshold for _slow_ (yellow) tests. | +| perf.vslow | 1000 (ms) | Sets the performance threshold for _very slow_ (red). tests | | test.dp.args | true | True to show the arguments passed by the data provider, false to hide. | -| test.name.strip | '' | Strips the specified matching portion of the test name | +| test.name.strip | '' | Strips the specified matching portion of the test name. | ## Requirements @@ -72,13 +72,21 @@ Pip's behaviour can be customized by adding `` nodes as children of t ## Testing Pip -The printer's capabilities are exploited via `CapabilitiesTest`. However, this test file isn't run directly because many of these tests are designed to fail. Instead, we write tests that run PHPUnit internally, each of which invokes one of the capability test cases and verifies its output. +To run the full test suite, use the following command. -The real tests, also known as *functional tests*, are located in `test/functional`, written in PHPT format. PHPT is a [scarcely documented format](http://qa.php.net/phpt_details.php) designed to support [testing PHP itself](https://qa.php.net/write-test.php). An undocumented feature of PHPUnit is its limited support for a subset of the PHPT test specification, which we exploit to test PHPUnit itself with our printer implementation loaded. +```sh +composer test +``` + +Pip's capabilities are exploited via `CapabilitiesTest`. However, this test file isn't run directly because many of these tests are designed to fail. Instead, we write tests that run PHPUnit internally, each of which invokes one of the capability test cases and verifies its output. To run `CapabilitiesTest`, specify the following command -To run the tests, simply specify `vendor/bin/phpunit -c test` on the command line from the project directory. By default, we run all the functional PHPT tests. To run `CapabilitiesTest` instead, specify `vendor/bin/phpunit -c test test/CapabilitiesTest.php`. +```sh +composer test test/CapabilitiesTest.php +``` + +The real tests, also known as *functional tests*, are located in `test/functional`, written in PHPT format. PHPT is a [scarcely documented format](http://qa.php.net/phpt_details.php) designed to support [testing PHP itself](https://qa.php.net/write-test.php). An undocumented feature of PHPUnit is its limited support for a subset of the PHPT test specification, which we exploit to test PHPUnit itself with our printer implementation loaded. -### Writing a functional test +### Writing functional tests To test the output of a particular capability we run `CapabilitiesTest` with the `--filter` option to target a specific test case. Each functional test contains the arguments passed to PHPUnit in the `--ARGS--` section of the file. These arguments can be pasted directly after the PHPUnit command to see the resulting output from that test case. We verify the output in the `--EXPECTF--` section of the file. diff --git a/src/PipExtension.php b/src/PipExtension.php index cc7dfdf..b691ba1 100644 --- a/src/PipExtension.php +++ b/src/PipExtension.php @@ -1,5 +1,5 @@ uniqueTraces[$trace->getIssueId()] = $trace; - } - public function trace(Event $event): void { if ($event instanceof ExecutionStarted) { @@ -155,4 +150,9 @@ public function trace(Event $event): void echo PHP_EOL, PHP_EOL; } } + + private function addUniqueTrace(Trace $trace): void + { + $trace->suppressed || $this->uniqueTraces[$trace->getId()] ??= $trace; + } } diff --git a/src/Theme/ClassicTheme.php b/src/Theme/ClassicTheme.php index e02bc8e..f74fea8 100644 --- a/src/Theme/ClassicTheme.php +++ b/src/Theme/ClassicTheme.php @@ -47,21 +47,17 @@ public function onTestFinished(TestResult $result): void $throwable = $throwable->previous(); } - $firstIssue = true; + $result->uniqueTraces && print PHP_EOL; foreach ($result->uniqueTraces as $trace) { - if (!$trace->suppressed) { - $issueStatusColour = self::getColour($trace->issueStatus); - printf( - Color::colorize("fg-$issueStatusColour", '%s%s: %s in %s on line %d%s'), - $firstIssue ? PHP_EOL : '', - $trace->issueStatus->name, - $trace->message, - $trace->file, - $trace->line, - PHP_EOL . PHP_EOL, - ); - $firstIssue = false; - } + $issueStatusColour = self::getColour($trace->issueStatus); + printf( + Color::colorize("fg-$issueStatusColour", '%s: %s in %s on line %s%s%5$s'), + $trace->issueStatus->name, + $trace->message, + $trace->file, + $trace->line, + PHP_EOL, + ); } } diff --git a/src/Trace.php b/src/Trace.php index a5a0a97..0e84385 100644 --- a/src/Trace.php +++ b/src/Trace.php @@ -33,17 +33,12 @@ public static function fromEvent(PhpWarningTriggered|PhpNoticeTriggered|PhpDepre } /** - * Key to identify identical issues, using the same rules as PHPUnit. + * Gets a unique identifier for the source of this trace, using the same rules as PHPUnit. * * @see \PHPUnit\TestRunner\TestResult\Collector::issueId */ - public function getIssueId(): string + public function getId(): string { - return sprintf( - '%s:%s:%s', - $this->file, - $this->line, - $this->message, - ); + return "$this->file:$this->line:$this->message"; } } diff --git a/test/CapabilitiesTest.php b/test/CapabilitiesTest.php index 00ddaa5..56a0ee8 100644 --- a/test/CapabilitiesTest.php +++ b/test/CapabilitiesTest.php @@ -5,7 +5,7 @@ use PHPUnit\Framework\TestCase; /** - * Tests the capabilities of the PIP printer. + * Tests the capabilities of the Pip printer. */ final class CapabilitiesTest extends TestCase { @@ -155,7 +155,7 @@ public function testSlow(): void self::assertTrue(true); } - public function testGigaSlow(): void + public function testVerySlow(): void { sleep(1); diff --git a/test/functional/errors & exceptions/E_DEPRECATED.phpt b/test/functional/errors & exceptions/E_DEPRECATED.phpt index 87962ad..c2d5207 100644 --- a/test/functional/errors & exceptions/E_DEPRECATED.phpt +++ b/test/functional/errors & exceptions/E_DEPRECATED.phpt @@ -14,8 +14,8 @@ Runtime: %s Configuration: %s 100% D ScriptFUSIONTest\Pip\CapabilitiesTest::testDeprecation (%d ms) - -Deprecated: Serializable@anonymous implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s%eCapabilitiesTest.php on line %d + +Deprecated: Serializable@anonymous implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s%eCapabilitiesTest.php on line %d  diff --git a/test/functional/errors & exceptions/E_NOTICE.phpt b/test/functional/errors & exceptions/E_NOTICE.phpt index 6b348aa..3de69eb 100644 --- a/test/functional/errors & exceptions/E_NOTICE.phpt +++ b/test/functional/errors & exceptions/E_NOTICE.phpt @@ -14,8 +14,8 @@ Runtime: %s Configuration: %s 100% N ScriptFUSIONTest\Pip\CapabilitiesTest::testNotice (%d ms) - -Notice: Only variables should be assigned by reference in %s%eCapabilitiesTest.php on line %d + +Notice: Only variables should be assigned by reference in %s%eCapabilitiesTest.php on line %d  diff --git a/test/functional/errors & exceptions/E_WARNING duplicates.phpt b/test/functional/errors & exceptions/E_WARNING duplicates.phpt index 158227f..cb28d7a 100644 --- a/test/functional/errors & exceptions/E_WARNING duplicates.phpt +++ b/test/functional/errors & exceptions/E_WARNING duplicates.phpt @@ -14,8 +14,8 @@ Runtime: %s Configuration: %s 100% W ScriptFUSIONTest\Pip\CapabilitiesTest::testWarningDuplicates (%d ms) - -Warning: foreach() argument must be of type array|object, int given in %s%eCapabilitiesTest.php on line %d + +Warning: foreach() argument must be of type array|object, int given in %s%eCapabilitiesTest.php on line %d Warning: foreach() argument must be of type array|object, int given in %s%eCapabilitiesTest.php on line %d diff --git a/test/functional/errors & exceptions/E_WARNING.phpt b/test/functional/errors & exceptions/E_WARNING.phpt index 37da75b..35409ba 100644 --- a/test/functional/errors & exceptions/E_WARNING.phpt +++ b/test/functional/errors & exceptions/E_WARNING.phpt @@ -14,8 +14,8 @@ Runtime: %s Configuration: %s 100% W ScriptFUSIONTest\Pip\CapabilitiesTest::testWarning (%d ms) - -Warning: foreach() argument must be of type array|object, int given in %s%eCapabilitiesTest.php on line %d + +Warning: foreach() argument must be of type array|object, int given in %s%eCapabilitiesTest.php on line %d  diff --git a/test/functional/errors & exceptions/mixed severities.phpt b/test/functional/errors & exceptions/mixed severities.phpt index 84f990f..bc6b1f1 100644 --- a/test/functional/errors & exceptions/mixed severities.phpt +++ b/test/functional/errors & exceptions/mixed severities.phpt @@ -15,8 +15,8 @@ Runtime: %s Configuration: %s 100% N ScriptFUSIONTest\Pip\CapabilitiesTest::testMixedSeverities (%d ms) - -Notice: Only variables should be assigned by reference in %s%eCapabilitiesTest.php on line %d + +Notice: Only variables should be assigned by reference in %s%eCapabilitiesTest.php on line %d Warning: foreach() argument must be of type array|object, int given in %s%eCapabilitiesTest.php on line %d diff --git a/test/functional/risky.phpt b/test/functional/risky.phpt index a63874a..708a91d 100644 --- a/test/functional/risky.phpt +++ b/test/functional/risky.phpt @@ -14,8 +14,8 @@ Runtime: %s Configuration: %s 100% R ScriptFUSIONTest\Pip\CapabilitiesTest::testRisky (%d ms) - -Risky: This test did not perform any assertions in %s%eCapabilitiesTest.php on line %d + +Risky: This test did not perform any assertions in %s%eCapabilitiesTest.php on line %d  diff --git a/test/functional/speed vslow.phpt b/test/functional/speed vslow.phpt index f925794..e696832 100644 --- a/test/functional/speed vslow.phpt +++ b/test/functional/speed vslow.phpt @@ -2,7 +2,7 @@ Tests that when a test takes more than 1s to complete, its timer output is shown in red. --ARGS-- --c test --colors=always test/CapabilitiesTest.php --filter ::testGigaSlow$ +-c test --colors=always test/CapabilitiesTest.php --filter ::testVerySlow$ --FILE_EXTERNAL-- ../PHPUnit runner.php @@ -13,7 +13,7 @@ PHPUnit %s Runtime: %s Configuration: %s -100% . ScriptFUSIONTest\Pip\CapabilitiesTest::testGigaSlow (%d ms) +100% . ScriptFUSIONTest\Pip\CapabilitiesTest::testVerySlow (%d ms) Time: %s