diff --git a/core/lib/Drupal/Core/Test/PhpUnitTestRunner.php b/core/lib/Drupal/Core/Test/PhpUnitTestRunner.php index 18417d1..f473ed6 100644 --- a/core/lib/Drupal/Core/Test/PhpUnitTestRunner.php +++ b/core/lib/Drupal/Core/Test/PhpUnitTestRunner.php @@ -8,7 +8,6 @@ namespace Drupal\Core\Test; use Drupal\Core\Database\Database; -use Drupal\Core\File\FileSystem; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Process\PhpExecutableFinder; @@ -27,11 +26,13 @@ class PhpUnitTestRunner { /** - * File system service. + * Path to the working directory. * - * @var \Drupal\Core\File\FileSystem + * JUnit log files will be stored in this directory. + * + * @var string */ - protected $fileSystem; + protected $workingDirectory; /** * Path to the application root. @@ -52,7 +53,7 @@ class PhpUnitTestRunner { public static function create(ContainerInterface $c) { return new static( (string) $c->get('app.root'), - $c->get('file_system') + (string) $c->get('file_system')->realpath('public://simpletest') ); } @@ -61,12 +62,13 @@ public static function create(ContainerInterface $c) { * * @param string $app_root * Path to the application root. - * @param Drupal\Core\File\FileSystem $file_system - * File system service. + * @param string $working_directory + * Path to the working directory. JUnit log files will be stored in this + * directory. */ - public function __construct($app_root, FileSystem $file_system) { + public function __construct($app_root, $working_directory) { $this->appRoot = $app_root; - $this->fileSystem = $file_system; + $this->workingDirectory = $working_directory; } /** @@ -79,7 +81,7 @@ public function __construct($app_root, FileSystem $file_system) { * Path to the PHPUnit XML file to use for the current $test_id. */ public function xmlLogFilepath($test_id) { - return $this->fileSystem->realpath('public://simpletest') . '/phpunit-' . $test_id . '.xml'; + return $this->workingDirectory . '/phpunit-' . $test_id . '.xml'; } /** diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module index b0e25bc..d1b381c 100644 --- a/core/modules/simpletest/simpletest.module +++ b/core/modules/simpletest/simpletest.module @@ -181,6 +181,9 @@ function simpletest_run_tests($test_list) { * @return array * The parsed results of PHPUnit's JUnit XML output, in the format of * {simpletest}'s schema. + * + * @deprecated in Drupal 8.1.x, will be removed before 9.0.0. Use + * Drupal\Core\Test\PhpUnitTestRunner::runTests(). */ function simpletest_run_phpunit_tests($test_id, array $unescaped_test_classnames, &$status = NULL) { $runner = PhpUnitTestRunner::create(\Drupal::getContainer()); @@ -215,6 +218,9 @@ function simpletest_process_phpunit_results($phpunit_results) { * * @return string * Path to the PHPUnit XML file to use for the current $test_id. + * + * @deprecated in Drupal 8.1.x, will be removed before 9.0.0. Use + * Drupal\Core\Test\PhpUnitTestRunner::xmlLogFilepath(). */ function simpletest_phpunit_xml_filepath($test_id) { $runner = PhpUnitTestRunner::create(\Drupal::getContainer()); @@ -226,6 +232,9 @@ function simpletest_phpunit_xml_filepath($test_id) { * * @return string * The path to core's phpunit.xml.dist configuration file. + * + * @deprecated in Drupal 8.1.x, will be removed before 9.0.0. Use + * Drupal\Core\Test\PhpUnitTestRunner::phpUnitConfigurationFilepath(). */ function simpletest_phpunit_configuration_filepath() { $runner = PhpUnitTestRunner::create(\Drupal::getContainer()); @@ -246,6 +255,9 @@ function simpletest_phpunit_configuration_filepath() { * * @return string * The results as returned by exec(). + * + * @deprecated in Drupal 8.1.x, will be removed before 9.0.0. Use + * Drupal\Core\Test\PhpUnitTestRunner::runCommand(). */ function simpletest_phpunit_run_command(array $unescaped_test_classnames, $phpunit_file, &$status = NULL) { $runner = PhpUnitTestRunner::create(\Drupal::getContainer()); @@ -257,6 +269,9 @@ function simpletest_phpunit_run_command(array $unescaped_test_classnames, $phpun * * @return string * The command that can be run through exec(). + * + * @deprecated in Drupal 8.1.x, will be removed before 9.0.0. Use + * Drupal\Core\Test\PhpUnitTestRunner::phpUnitCommand(). */ function simpletest_phpunit_command() { $runner = PhpUnitTestRunner::create(\Drupal::getContainer()); @@ -623,6 +638,9 @@ function simpletest_mail_alter(&$message) { * @return array[] * The results as array of rows in a format that can be inserted into * {simpletest}. + * + * @deprecated in Drupal 8.1.x, will be removed before 9.0.0. Use + * Drupal\Core\Test\JUnitConverter::xmlToRows(). */ function simpletest_phpunit_xml_to_rows($test_id, $phpunit_xml_file) { return JUnitConverter::xmlToRows($test_id, $phpunit_xml_file); @@ -638,6 +656,9 @@ function simpletest_phpunit_xml_to_rows($test_id, $phpunit_xml_file) { * * @return array * A list of all test cases. + * + * @deprecated in Drupal 8.1.x, will be removed before 9.0.0. Use + * Drupal\Core\Test\JUnitConverter::findTestCases(). */ function simpletest_phpunit_find_testcases(\SimpleXMLElement $element, \SimpleXMLElement $parent = NULL) { return JUnitConverter::findTestcases($element, $parent); @@ -653,6 +674,9 @@ function simpletest_phpunit_find_testcases(\SimpleXMLElement $element, \SimpleXM * * @return array * An array containing the {simpletest} result row. + * + * @deprecated in Drupal 8.1.x, will be removed before 9.0.0. Use + * Drupal\Core\Test\JUnitConverter::testcaseToSimpletestRow(). */ function simpletest_phpunit_testcase_to_row($test_id, \SimpleXMLElement $testcase) { return JUnitConverter::testcaseToSimpletestRow($test_id, $testcase); diff --git a/core/modules/simpletest/tests/src/Unit/SimpletestPhpunitRunCommandTest.php b/core/modules/simpletest/tests/src/Unit/SimpletestPhpunitRunCommandTest.php index 32aa058..83a6614 100644 --- a/core/modules/simpletest/tests/src/Unit/SimpletestPhpunitRunCommandTest.php +++ b/core/modules/simpletest/tests/src/Unit/SimpletestPhpunitRunCommandTest.php @@ -8,35 +8,79 @@ namespace Drupal\Tests\simpletest\Unit; use Drupal\Core\DependencyInjection\ContainerBuilder; -use Drupal\Tests\UnitTestCase; +use Drupal\Core\Test\PhpUnitTestRunner; /** * Tests simpletest_run_phpunit_tests() handles PHPunit fatals correctly. * + * The file simpletest_phpunit_run_command_test.php contains the test class + * Drupal\Tests\simpletest\Unit\SimpletestPhpunitRunCommandTestWillDie which can + * be made to exit with result code 2. It lives in a file which won't be + * autoloaded, so that it won't fail test runs. + * + * Here, we run SimpletestPhpunitRunCommandTestWillDie, make it die, and see + * what happens. + * * @group simpletest * * @runTestsInSeparateProcesses * @preserveGlobalState disabled */ -class SimpletestPhpunitRunCommandTest extends UnitTestCase { +class SimpletestPhpunitRunCommandTest extends \PHPUnit_Framework_TestCase { + + /** + * Container to use in the tests. + * + * @var Drupal\Core\DependencyInjection\ContainerBuilder + */ + protected $fixture_container; - function testSimpletestPhpUnitRunCommand() { + public function setUp() { include_once __DIR__ .'/../../fixtures/simpletest_phpunit_run_command_test.php'; $app_root = __DIR__ . '/../../../../../..'; include_once "$app_root/core/modules/simpletest/simpletest.module"; - $container = new ContainerBuilder; - $container->set('app.root', $app_root); + $this->fixture_container = new ContainerBuilder; + $this->fixture_container->set('app.root', $app_root); $file_system = $this->prophesize('Drupal\Core\File\FileSystemInterface'); $file_system->realpath('public://simpletest')->willReturn(sys_get_temp_dir()); - $container->set('file_system', $file_system->reveal()); - \Drupal::setContainer($container); - $test_id = basename(tempnam(sys_get_temp_dir(), 'xxx')); + $this->fixture_container->set('file_system', $file_system->reveal()); + } + + /** + * Round-trip test of running a PHPUnit-based test, including exit(). + * + * Uses deprecated simpletest_*() functions. + * + * @coversNothing + */ + public function testSimpletestPhpUnitRunCommandBackwardsCompatibility() { + $test_id = 77; + \Drupal::setContainer($this->fixture_container); foreach (['pass', 'fail'] as $status) { putenv('SimpletestPhpunitRunCommandTestWillDie=' . $status); - $ret = simpletest_run_phpunit_tests($test_id, ['Drupal\Tests\simpletest\Unit\SimpletestPhpunitRunCommandTestWillDie']); + $ret = simpletest_run_phpunit_tests($test_id++, ['Drupal\Tests\simpletest\Unit\SimpletestPhpunitRunCommandTestWillDie']); $this->assertSame($ret[0]['status'], $status); } - unlink(simpletest_phpunit_xml_filepath($test_id)); + unlink(simpletest_phpunit_xml_filepath(--$test_id)); + unlink(simpletest_phpunit_xml_filepath(--$test_id)); } + /** + * Round-trip test of running a PHPUnit-based test, including exit(). + * + * Uses the PhpUnitTestRunner class. + * + * @coversNothing + */ + public function testSimpletestPhpUnitRunCommand() { + $test_id = 23; + $runner = PhpUnitTestRunner::create($this->fixture_container); + foreach (['pass', 'fail'] as $status) { + putenv('SimpletestPhpunitRunCommandTestWillDie=' . $status); + $ret = $runner->runTests($test_id++, ['Drupal\Tests\simpletest\Unit\SimpletestPhpunitRunCommandTestWillDie']); + $this->assertSame($ret[0]['status'], $status); + } + unlink($runner->xmlLogFilepath(--$test_id)); + unlink($runner->xmlLogFilepath(--$test_id)); + } } diff --git a/core/tests/Drupal/Tests/Core/Test/PhpUnitTestRunnerTest.php b/core/tests/Drupal/Tests/Core/Test/PhpUnitTestRunnerTest.php new file mode 100644 index 0000000..9677dca --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Test/PhpUnitTestRunnerTest.php @@ -0,0 +1,71 @@ +getMockBuilder(PhpUnitTestRunner::class) + ->disableOriginalConstructor() + ->setMethods(['xmlLogFilepath', 'runCommand']) + ->getMock(); + + // Set some expectations for xmlLogFilepath(). + $runner->expects($this->once()) + ->method('xmlLogFilepath') + ->willReturn($log_path); + + // We mark a failure by having runCommand() deliver a serious status code. + $runner->expects($this->once()) + ->method('runCommand') + ->willReturnCallback( + function ($unescaped_test_classnames, $phpunit_file, &$status) { + $status = 2; + } + ); + + // The runTests() method expects $status by reference, so we initialize it + // to some value we don't expect back. + $status = -1; + $results = $runner->runTests(23, ['SomeTest'], $status); + + // Make sure our status code made the round trip. + $this->assertEquals(2, $status); + + // A serious error in runCommand() should give us a fixed set of results. + $row = reset($results); + $fail_row = [ + 'test_id' => '1', + 'test_class' => 'SomeTest', + 'status' => 'fail', + 'message' => 'PHPunit Test failed to complete', + 'message_group' => 'Other', + 'function' => 'SomeTest', + 'line' => '0', + 'file' => $log_path, + ]; + $this->assertEquals($fail_row, $row); + } + +}