Skip to content

Commit 5e1e3a6

Browse files
authored
Merge pull request #695 from PHPCSStandards/feature/generator-html-markdown-add-error-silencing-test
Generators HTML/Markdown: add extra test around error silencing
2 parents 06ebc1c + dfce5e2 commit 5e1e3a6

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

tests/Core/Generators/HTMLTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,47 @@ public function testFooterResetsErrorReportingToOriginalSetting()
129129
}//end testFooterResetsErrorReportingToOriginalSetting()
130130

131131

132+
/**
133+
* Safeguard that users won't see a PHP warning about the timezone not being set when calling date().
134+
*
135+
* The warning we don't want to see is:
136+
* "date(): It is not safe to rely on the system's timezone settings. You are *required* to use
137+
* the date.timezone setting or the date_default_timezone_set() function. In case you used any of
138+
* those methods and you are still getting this warning, you most likely misspelled the timezone
139+
* identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select
140+
* your timezone."
141+
*
142+
* JRF: Based on my tests, the warning only occurs on PHP < 7.0, but never a bad thing to safeguard this
143+
* on a wider range of PHP versions.
144+
*
145+
* Note: as of PHP 8.2, PHP no longer accepts an empty string as timezone and will use `UTC` instead,
146+
* so the warning on calling date() in the code itself would not display anyway.
147+
*
148+
* @requires PHP < 8.2
149+
*
150+
* @doesNotPerformAssertions
151+
*
152+
* @return void
153+
*/
154+
public function testFooterDoesntThrowWarningOnMissingTimezone()
155+
{
156+
$originalIni = @ini_set('date.timezone', '');
157+
158+
// Set up the ruleset.
159+
$standard = __DIR__.'/OneDocTest.xml';
160+
$config = new ConfigDouble(["--standard=$standard"]);
161+
$ruleset = new Ruleset($config);
162+
163+
// We know there will be output, but we're not interested in the output for this test.
164+
ob_start();
165+
$generator = new HTMLDouble($ruleset);
166+
$generator->printRealFooter();
167+
ob_end_clean();
168+
169+
// Reset the timezone to its original state.
170+
ini_set('date.timezone', $originalIni);
171+
172+
}//end testFooterDoesntThrowWarningOnMissingTimezone()
173+
174+
132175
}//end class

tests/Core/Generators/MarkdownTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,47 @@ public function testFooterResetsErrorReportingToOriginalSetting()
127127
}//end testFooterResetsErrorReportingToOriginalSetting()
128128

129129

130+
/**
131+
* Safeguard that users won't see a PHP warning about the timezone not being set when calling date().
132+
*
133+
* The warning we don't want to see is:
134+
* "date(): It is not safe to rely on the system's timezone settings. You are *required* to use
135+
* the date.timezone setting or the date_default_timezone_set() function. In case you used any of
136+
* those methods and you are still getting this warning, you most likely misspelled the timezone
137+
* identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select
138+
* your timezone."
139+
*
140+
* JRF: Based on my tests, the warning only occurs on PHP < 7.0, but never a bad thing to safeguard this
141+
* on a wider range of PHP versions.
142+
*
143+
* Note: as of PHP 8.2, PHP no longer accepts an empty string as timezone and will use `UTC` instead,
144+
* so the warning on calling date() in the code itself would not display anyway.
145+
*
146+
* @requires PHP < 8.2
147+
*
148+
* @doesNotPerformAssertions
149+
*
150+
* @return void
151+
*/
152+
public function testFooterDoesntThrowWarningOnMissingTimezone()
153+
{
154+
$originalIni = @ini_set('date.timezone', '');
155+
156+
// Set up the ruleset.
157+
$standard = __DIR__.'/OneDocTest.xml';
158+
$config = new ConfigDouble(["--standard=$standard"]);
159+
$ruleset = new Ruleset($config);
160+
161+
// We know there will be output, but we're not interested in the output for this test.
162+
ob_start();
163+
$generator = new MarkdownDouble($ruleset);
164+
$generator->printRealFooter();
165+
ob_end_clean();
166+
167+
// Reset the timezone to its original state.
168+
ini_set('date.timezone', $originalIni);
169+
170+
}//end testFooterDoesntThrowWarningOnMissingTimezone()
171+
172+
130173
}//end class

0 commit comments

Comments
 (0)