Skip to content

Commit 3111fb5

Browse files
authored
Fix #17 - Error suppression operator is not fully honoured (#18)
* Fixed incomplete command example in README. * Fixed suppressed warnings causing test to be reported as Warning. * Fixed suppressed notices and deprecations changing test status. * Omitted PHPUnit cache files from version control.
1 parent e71b10a commit 3111fb5

7 files changed

Lines changed: 70 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
!/.github/
33
/vendor/
44
/composer.lock
5+
*.cache

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ The printer's capabilities are exploited via `CapabilitiesTest`. However, this t
7676
7777
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.
7878
79-
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`.
79+
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`.
8080
8181
### Writing a functional test
8282

src/Printer.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,23 @@ public function trace(Event $event): void
8080
$this->trace = new Trace($event->message(), $event->test()->file(), $event->test()->line());
8181
}
8282
if ($event instanceof PhpNoticeTriggered) {
83-
$this->status ??= TestStatus::Notice;
83+
if (!$event->wasSuppressed()) {
84+
$this->status ??= TestStatus::Notice;
85+
}
8486

8587
$this->trace = Trace::fromEvent($event);
8688
}
8789
if ($event instanceof PhpWarningTriggered) {
88-
$this->status ??= TestStatus::Warning;
90+
if (!$event->wasSuppressed()) {
91+
$this->status ??= TestStatus::Warning;
92+
}
8993

9094
$this->trace = Trace::fromEvent($event);
9195
}
9296
if ($event instanceof PhpDeprecationTriggered) {
93-
$this->status ??= TestStatus::Deprecated;
97+
if (!$event->wasSuppressed()) {
98+
$this->status ??= TestStatus::Deprecated;
99+
}
94100

95101
$this->trace = Trace::fromEvent($event);
96102
}

test/CapabilitiesTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public function testNotice(): void
5656
self::assertTrue(true);
5757
}
5858

59+
public function testSilencedNotice(): void
60+
{
61+
// Only variables should be assigned by reference.
62+
@$foo = &self::provideData();
63+
64+
self::assertTrue(true);
65+
}
66+
5967
public function testWarning(): void
6068
{
6169
// foreach() argument must be of type array|object.
@@ -82,6 +90,14 @@ function unserialize(string $data) {}
8290
self::assertTrue(true);
8391
}
8492

93+
public function testSilencedDeprecation(): void
94+
{
95+
// Passing null to parameter #1 ($string) of type string is deprecated.
96+
@trim(null);
97+
98+
self::assertTrue(true);
99+
}
100+
85101
#[DataProvider('provideData')]
86102

87103
public function testDataProvider(): void
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Tests that when a test generates a deprecation that is suppressed, the deprecation is not printed.
3+
4+
--ARGS--
5+
-c test --colors=always test/CapabilitiesTest.php --filter ::testSilencedDeprecation$
6+
7+
--FILE_EXTERNAL--
8+
../PHPUnit runner.php
9+
10+
--EXPECTF--
11+
PHPUnit %s
12+
13+
Runtime: %s
14+
Configuration: %s
15+
16+
100% . ScriptFUSIONTest\Pip\CapabilitiesTest::testSilencedDeprecation (%d ms)
17+
18+
19+
Time: %s
20+
%A
21+
OK (1 test, 1 assertion)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Tests that when a test generates a notice that is suppressed, the notice is not printed.
3+
4+
--ARGS--
5+
-c test --colors=always test/CapabilitiesTest.php --filter ::testSilencedNotice$
6+
7+
--FILE_EXTERNAL--
8+
../PHPUnit runner.php
9+
10+
--EXPECTF--
11+
PHPUnit %s
12+
13+
Runtime: %s
14+
Configuration: %s
15+
16+
100% . ScriptFUSIONTest\Pip\CapabilitiesTest::testSilencedNotice (%d ms)
17+
18+
19+
Time: %s
20+
%A
21+
OK (1 test, 1 assertion)

test/functional/warning silenced.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ PHPUnit %s
1313
Runtime: %s
1414
Configuration: %s
1515

16-
100% [33;1mW[0m [33;1mScriptFUSIONTest\Pip\CapabilitiesTest::testSilencedWarning[0m [32m(%d ms)[0m
16+
100% . [32;1mScriptFUSIONTest\Pip\CapabilitiesTest::testSilencedWarning[0m [32m(%d ms)[0m
1717

1818

1919
Time: %s

0 commit comments

Comments
 (0)