Skip to content

Instantly share code, notes, and snippets.

@jeroenvdgulik
Last active February 18, 2017 12:10
Show Gist options
  • Save jeroenvdgulik/c693d3f37b76efb66a55a2b50944c499 to your computer and use it in GitHub Desktop.
Save jeroenvdgulik/c693d3f37b76efb66a55a2b50944c499 to your computer and use it in GitHub Desktop.
"Solving" the issue of Symfony 3.x and PHPUnit 6.x (obviously this is a hack until they fix it properly)
<?php
// create a file in /tests/bootstrap.php at the root of your symfony project
$loader = require(__DIR__ . '/../app/autoload.php'); // or whatever path your autoload.php is in
if (!class_exists('\PHPUnit_Framework_TestCase') && class_exists('\PHPUnit\Framework\TestCase')) {
class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
}
return $loader;
// In your phpunit.xml or phpunit.xml.dist change:
bootstrap="app/autoload.php"
// to
bootstrap="tests/bootstrap.php"
<?php
// Alternatively if you use your own bootstrap file, just add this line:
if (!class_exists('\PHPUnit_Framework_TestCase') && class_exists('\PHPUnit\Framework\TestCase')) {
class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
}
<?php
// Create a file in /tests/alias.php at the root of your symfony project containing:
if (!class_exists('\PHPUnit_Framework_TestCase') && class_exists('\PHPUnit\Framework\TestCase')) {
class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
}
// In your composer.json We can misuse composer autoloading.
// much thanks to @WyriHaximus who pointed it out.
// Add the following:
"autoload-dev": {
"files": [
"tests/alias.php"
]
},
@ruudk
Copy link

ruudk commented Feb 18, 2017

Thanks for this. Works really well!

I updated it to support more variants in my Symfony3 project.

$convert = [
    '\PHPUnit_Framework_TestCase' => '\PHPUnit\Framework\TestCase',
    '\PHPUnit_Util_Test' => '\PHPUnit\Util\Test',
    '\PHPUnit_Framework_Constraint' => '\PHPUnit\Framework\Constraint\Constraint',
    '\PHPUnit_Framework_Assert' => '\PHPUnit\Framework\Assert',
    '\PHPUnit_Framework_Constraint_IsEqual' => '\PHPUnit\Framework\Constraint\IsEqual',
    '\PHPUnit_Framework_Constraint_ExceptionMessage' => '\PHPUnit\Framework\Constraint\ExceptionMessage',
];

foreach ($convert as $oldClass => $newClass) {
    if (!class_exists($oldClass) && class_exists($newClass)) {
        class_alias($newClass, $oldClass);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment