Skip to content

Commit

Permalink
bug #32981 Fix tests/code for php 7.4 (jderusse)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

Fix tests/code for php 7.4

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #32844
| License       | MIT
| Doc PR        | NA

Fix remaining tests and deprecation

Commits
-------

05ec8a0 Fix remaining tests
  • Loading branch information
nicolas-grekas committed Aug 7, 2019
2 parents c88d125 + 05ec8a0 commit b27c999
Show file tree
Hide file tree
Showing 23 changed files with 166 additions and 23 deletions.
Expand Up @@ -264,7 +264,7 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
->canBeEnabled()
->beforeNormalization()
->always(function ($v) {
if (true === $v['enabled']) {
if (\is_array($v) && true === $v['enabled']) {
$workflows = $v;
unset($workflows['enabled']);

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Config/Util/XmlUtils.php
Expand Up @@ -234,7 +234,7 @@ public static function phpize($value)
return true;
case 'false' === $lowercaseValue:
return false;
case isset($value[1]) && '0b' == $value[0].$value[1]:
case isset($value[1]) && '0b' == $value[0].$value[1] && preg_match('/^0b[01]*$/', $value):
return bindec($value);
case is_numeric($value):
return '0x' === $value[0].$value[1] ? hexdec($value) : (float) $value;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Debug/ErrorHandler.php
Expand Up @@ -470,7 +470,7 @@ public function handleError($type, $message, $file, $line)
}

if ($throw) {
if (E_USER_ERROR & $type) {
if (\PHP_VERSION_ID < 70400 && E_USER_ERROR & $type) {
for ($i = 1; isset($backtrace[$i]); ++$i) {
if (isset($backtrace[$i]['function'], $backtrace[$i]['type'], $backtrace[$i - 1]['function'])
&& '__toString' === $backtrace[$i]['function']
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Debug/Tests/ErrorHandlerTest.php
Expand Up @@ -283,6 +283,10 @@ public function testHandleError()

public function testHandleUserError()
{
if (\PHP_VERSION_ID >= 70400) {
$this->markTestSkipped('PHP 7.4 allows __toString to throw exceptions');
}

try {
$handler = ErrorHandler::register();
$handler->throwAt(0, true);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/DomCrawler/FormFieldRegistry.php
Expand Up @@ -57,7 +57,7 @@ public function remove($name)
$target = &$this->fields;
while (\count($segments) > 1) {
$path = array_shift($segments);
if (!\array_key_exists($path, $target)) {
if (!\is_array($target) || !\array_key_exists($path, $target)) {
return;
}
$target = &$target[$path];
Expand All @@ -80,7 +80,7 @@ public function &get($name)
$target = &$this->fields;
while ($segments) {
$path = array_shift($segments);
if (!\array_key_exists($path, $target)) {
if (!\is_array($target) || !\array_key_exists($path, $target)) {
throw new \InvalidArgumentException(sprintf('Unreachable field "%s"', $path));
}
$target = &$target[$path];
Expand Down
Expand Up @@ -33,11 +33,7 @@ public function testAccept($mode, $expected)
if (!\is_callable($mode)) {
switch ($mode) {
case SortableIterator::SORT_BY_ACCESSED_TIME:
if ('\\' === \DIRECTORY_SEPARATOR) {
touch(self::toAbsolute('.git'));
} else {
file_get_contents(self::toAbsolute('.git'));
}
touch(self::toAbsolute('.git'));
sleep(1);
file_get_contents(self::toAbsolute('.bar'));
break;
Expand Down
Expand Up @@ -471,6 +471,7 @@ public function testYearsOption()

public function testMonthsOption()
{
\Locale::setDefault('en');
$form = $this->factory->create(static::TESTED_TYPE, null, [
'months' => [6, 7],
'format' => \IntlDateFormatter::SHORT,
Expand All @@ -479,8 +480,8 @@ public function testMonthsOption()
$view = $form->createView();

$this->assertEquals([
new ChoiceView(6, '6', '06'),
new ChoiceView(7, '7', '07'),
new ChoiceView(6, '6', '6'),
new ChoiceView(7, '7', '7'),
], $view['month']->vars['choices']);
}

Expand Down Expand Up @@ -544,14 +545,15 @@ public function testMonthsOptionLongFormatWithDifferentTimezone()

public function testIsDayWithinRangeReturnsTrueIfWithin()
{
\Locale::setDefault('en');
$view = $this->factory->create(static::TESTED_TYPE, null, [
'days' => [6, 7],
])
->createView();

$this->assertEquals([
new ChoiceView(6, '6', '06'),
new ChoiceView(7, '7', '07'),
new ChoiceView(6, '6', '6'),
new ChoiceView(7, '7', '7'),
], $view['day']->vars['choices']);
}

Expand Down
Expand Up @@ -589,6 +589,7 @@ abstract class AbstractCurrencyDataProviderTest extends AbstractDataProviderTest
* @var CurrencyDataProvider
*/
protected $dataProvider;
private $defaultLocale;

protected function setUp()
{
Expand All @@ -598,6 +599,15 @@ protected function setUp()
$this->getDataDirectory().'/'.Intl::CURRENCY_DIR,
$this->createEntryReader()
);

$this->defaultLocale = \Locale::getDefault();
}

protected function tearDown()
{
parent::tearDown();

\Locale::setDefault($this->defaultLocale);
}

abstract protected function getDataDirectory();
Expand Down
Expand Up @@ -831,6 +831,7 @@ abstract class AbstractLanguageDataProviderTest extends AbstractDataProviderTest
* @var LanguageDataProvider
*/
protected $dataProvider;
private $defaultLocale;

protected function setUp()
{
Expand All @@ -840,6 +841,15 @@ protected function setUp()
$this->getDataDirectory().'/'.Intl::LANGUAGE_DIR,
$this->createEntryReader()
);

$this->defaultLocale = \Locale::getDefault();
}

protected function tearDown()
{
parent::tearDown();

\Locale::setDefault($this->defaultLocale);
}

abstract protected function getDataDirectory();
Expand Down
Expand Up @@ -23,6 +23,7 @@ abstract class AbstractLocaleDataProviderTest extends AbstractDataProviderTest
* @var LocaleDataProvider
*/
protected $dataProvider;
private $defaultLocale;

protected function setUp()
{
Expand All @@ -32,6 +33,13 @@ protected function setUp()
$this->getDataDirectory().'/'.Intl::LOCALE_DIR,
$this->createEntryReader()
);

$this->defaultLocale = \Locale::getDefault();
}

protected function tearDown()
{
\Locale::setDefault($this->defaultLocale);
}

abstract protected function getDataDirectory();
Expand Down
Expand Up @@ -283,6 +283,7 @@ abstract class AbstractRegionDataProviderTest extends AbstractDataProviderTest
* @var RegionDataProvider
*/
protected $dataProvider;
private $defaultLocale;

protected function setUp()
{
Expand All @@ -292,6 +293,15 @@ protected function setUp()
$this->getDataDirectory().'/'.Intl::REGION_DIR,
$this->createEntryReader()
);

$this->defaultLocale = \Locale::getDefault();
}

protected function tearDown()
{
parent::tearDown();

\Locale::setDefault($this->defaultLocale);
}

abstract protected function getDataDirectory();
Expand Down
Expand Up @@ -219,6 +219,7 @@ abstract class AbstractScriptDataProviderTest extends AbstractDataProviderTest
* @var ScriptDataProvider
*/
protected $dataProvider;
private $defaultLocale;

protected function setUp()
{
Expand All @@ -228,6 +229,15 @@ protected function setUp()
$this->getDataDirectory().'/'.Intl::SCRIPT_DIR,
$this->createEntryReader()
);

$this->defaultLocale = \Locale::getDefault();
}

protected function tearDown()
{
parent::tearDown();

\Locale::setDefault($this->defaultLocale);
}

abstract protected function getDataDirectory();
Expand Down
Expand Up @@ -24,11 +24,23 @@
*/
abstract class AbstractIntlDateFormatterTest extends TestCase
{
private $defaultLocale;

protected function setUp()
{
parent::setUp();

$this->defaultLocale = \Locale::getDefault();
\Locale::setDefault('en');
}

protected function tearDown()
{
parent::tearDown();

\Locale::setDefault($this->defaultLocale);
}

/**
* When a time zone is not specified, it uses the system default however it returns null in the getter method.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Intl/Tests/IntlTest.php
Expand Up @@ -16,6 +16,22 @@

class IntlTest extends TestCase
{
private $defaultLocale;

protected function setUp()
{
parent::setUp();

$this->defaultLocale = \Locale::getDefault();
}

protected function tearDown()
{
parent::tearDown();

\Locale::setDefault($this->defaultLocale);
}

/**
* @requires extension intl
*/
Expand Down
Expand Up @@ -36,7 +36,7 @@ public function formatCurrencyWithDecimalStyleProvider()
{
return [
[100, 'ALL', '100'],
[100, 'BRL', '100.00'],
[100, 'BRL', '100'],
[100, 'CRC', '100'],
[100, 'JPY', '100'],
[100, 'CHF', '100'],
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php
Expand Up @@ -87,7 +87,7 @@ public function testStop()
$event = $stopwatch->stop('foo');

$this->assertInstanceOf('Symfony\Component\Stopwatch\StopwatchEvent', $event);
$this->assertEquals(200, $event->getDuration(), '', self::DELTA);
$this->assertEqualsWithDelta(200, $event->getDuration(), self::DELTA);
}

public function testUnknownEvent()
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Translation/Loader/CsvFileLoader.php
Expand Up @@ -41,6 +41,10 @@ protected function loadResource($resource)
$file->setCsvControl($this->delimiter, $this->enclosure, $this->escape);

foreach ($file as $data) {
if (false === $data) {
continue;
}

if ('#' !== substr($data[0], 0, 1) && isset($data[1]) && 2 === \count($data)) {
$messages[$data[0]] = $data[1];
}
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Translation/Tests/IdentityTranslatorTest.php
Expand Up @@ -17,6 +17,22 @@

class IdentityTranslatorTest extends TestCase
{
private $defaultLocale;

protected function setUp()
{
parent::setUp();

$this->defaultLocale = \Locale::getDefault();
}

protected function tearDown()
{
parent::tearDown();

\Locale::setDefault($this->defaultLocale);
}

/**
* @dataProvider getTransTests
*/
Expand Down
Expand Up @@ -18,6 +18,22 @@

class CountryValidatorTest extends ConstraintValidatorTestCase
{
private $defaultLocale;

protected function setUp()
{
parent::setUp();

$this->defaultLocale = \Locale::getDefault();
}

protected function tearDown()
{
parent::tearDown();

\Locale::setDefault($this->defaultLocale);
}

protected function createValidator()
{
return new CountryValidator();
Expand Down
Expand Up @@ -18,6 +18,22 @@

class CurrencyValidatorTest extends ConstraintValidatorTestCase
{
private $defaultLocale;

protected function setUp()
{
parent::setUp();

$this->defaultLocale = \Locale::getDefault();
}

protected function tearDown()
{
parent::tearDown();

\Locale::setDefault($this->defaultLocale);
}

protected function createValidator()
{
return new CurrencyValidator();
Expand Down
Expand Up @@ -18,6 +18,22 @@

class LanguageValidatorTest extends ConstraintValidatorTestCase
{
private $defaultLocale;

protected function setUp()
{
parent::setUp();

$this->defaultLocale = \Locale::getDefault();
}

protected function tearDown()
{
parent::tearDown();

\Locale::setDefault($this->defaultLocale);
}

protected function createValidator()
{
return new LanguageValidator();
Expand Down

0 comments on commit b27c999

Please sign in to comment.