From 8dddfbcee03bfe7b0748c1c46b08cbcdbe32af5b Mon Sep 17 00:00:00 2001 From: Carlos Jimenez Date: Thu, 14 Jul 2016 07:55:47 +0100 Subject: [PATCH] [Test] as a service (#2419) * Add test service * Update use keywords for Test * Fix last line * Use the new service * Improve readability. * Improve readability. --- config/services/test.yml | 9 ++ src/Command/Test/DebugCommand.php | 102 ++++++++++-------- src/Command/Test/RunCommand.php | 172 +++++++++++++++++------------- 3 files changed, 167 insertions(+), 116 deletions(-) create mode 100644 config/services/test.yml diff --git a/config/services/test.yml b/config/services/test.yml new file mode 100644 index 000000000..c1f062afc --- /dev/null +++ b/config/services/test.yml @@ -0,0 +1,9 @@ +services: + test_debug: + class: Drupal\Console\Command\Test\DebugCommand + tags: + - { name: console.command } + test_run: + class: Drupal\Console\Command\Test\RunCommand + tags: + - { name: console.command } diff --git a/src/Command/Test/DebugCommand.php b/src/Command/Test/DebugCommand.php index 768b64ff0..81e9b76d8 100644 --- a/src/Command/Test/DebugCommand.php +++ b/src/Command/Test/DebugCommand.php @@ -12,37 +12,43 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Drupal\Component\Serialization\Yaml; -use Drupal\Console\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; +use Drupal\Console\Command\Shared\ContainerAwareCommandTrait; +use Drupal\Console\Annotation\DrupalCommand; use Drupal\Console\Style\DrupalStyle; /** * Class DebugCommand * @package Drupal\Console\Command\Test */ -class DebugCommand extends ContainerAwareCommand +class DebugCommand extends Command { + use ContainerAwareCommandTrait; + /** - * {@inheritdoc} + * @DrupalCommand( + * dependencies = { + * “simpletest" + * } + * ) */ protected function configure() { $this - ->setName('test:debug') - ->setDescription($this->trans('commands.test.debug.description')) - ->addArgument( - 'group', - InputArgument::OPTIONAL, - $this->trans('commands.test.debug.options.group'), - null - ) - ->addOption( - 'test-class', - '', - InputOption::VALUE_OPTIONAL, - $this->trans('commands.test.debug.arguments.test-class') - ); - - $this->addDependency('simpletest'); + ->setName('test:debug') + ->setDescription($this->trans('commands.test.debug.description')) + ->addArgument( + 'group', + InputArgument::OPTIONAL, + $this->trans('commands.test.debug.options.group'), + NULL + ) + ->addOption( + 'test-class', + '', + InputOption::VALUE_OPTIONAL, + $this->trans('commands.test.debug.arguments.test-class') + ); } /** @@ -52,23 +58,24 @@ protected function execute(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); //Registers namespaces for disabled modules. - $this->getTestDiscovery()->registerTestNamespaces(); + $this->getDrupalService('test_discovery')->registerTestNamespaces(); $testClass = $input->getOption('test-class'); $group = $input->getArgument('group'); if ($testClass) { $this->testDetail($io, $testClass); - } else { + } + else { $this->testList($io, $group); } } private function testDetail(DrupalStyle $io, $test_class) { - $testingGroups = $this->getTestDiscovery()->getTestClasses(null); + $testingGroups = $this->getDrupalService('test_discovery')->getTestClasses(NULL); - $testDetails = null; + $testDetails = NULL; foreach ($testingGroups as $testing_group => $tests) { foreach ($tests as $key => $test) { if ($test['name'] == $test_class) { @@ -76,18 +83,20 @@ private function testDetail(DrupalStyle $io, $test_class) break; } } - if ($testDetails !== null) { + if ($testDetails !== NULL) { break; } } - $class = null; + $class = NULL; if ($testDetails) { $class = new \ReflectionClass($test['name']); if (is_subclass_of($testDetails['name'], 'PHPUnit_Framework_TestCase')) { $testDetails['type'] = 'phpunit'; - } else { - $testDetails = $this->getTestDiscovery()->getTestInfo($testDetails['name']); + } + else { + $testDetails = $this->getDrupalService('test_discovery') + ->getTestInfo($testDetails['name']); $testDetails['type'] = 'simpletest'; } @@ -109,29 +118,32 @@ private function testDetail(DrupalStyle $io, $test_class) } } } - } else { + } + else { $io->error($this->trans('commands.test.debug.messages.not-found')); } } protected function testList(DrupalStyle $io, $group) { - $testingGroups = $this->getTestDiscovery()->getTestClasses(null); + $testingGroups = $this->getDrupalService('test_discovery') + ->getTestClasses(NULL); if (empty($group)) { $tableHeader = [$this->trans('commands.test.debug.messages.group')]; - } else { + } + else { $tableHeader = [ - $this->trans('commands.test.debug.messages.class'), - $this->trans('commands.test.debug.messages.type') + $this->trans('commands.test.debug.messages.class'), + $this->trans('commands.test.debug.messages.type') ]; $io->writeln( - sprintf( - '%s: %s', - $this->trans('commands.test.debug.messages.group'), - $group - ) + sprintf( + '%s: %s', + $this->trans('commands.test.debug.messages.group'), + $group + ) ); } @@ -149,7 +161,8 @@ protected function testList(DrupalStyle $io, $group) foreach ($tests as $test) { if (is_subclass_of($test['name'], 'PHPUnit_Framework_TestCase')) { $test['type'] = 'phpunit'; - } else { + } + else { $test['type'] = 'simpletest'; } $tableRows[] =[ @@ -162,14 +175,15 @@ protected function testList(DrupalStyle $io, $group) if ($group) { $io->success( - sprintf( - $this->trans('commands.test.debug.messages.success-group'), - $group - ) + sprintf( + $this->trans('commands.test.debug.messages.success-group'), + $group + ) ); - } else { + } + else { $io->success( - $this->trans('commands.test.debug.messages.success-groups') + $this->trans('commands.test.debug.messages.success-groups') ); } } diff --git a/src/Command/Test/RunCommand.php b/src/Command/Test/RunCommand.php index 69b4f1f67..d633ec494 100644 --- a/src/Command/Test/RunCommand.php +++ b/src/Command/Test/RunCommand.php @@ -12,13 +12,21 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Drupal\Component\Utility\Timer; -use Drupal\Console\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; +use Drupal\Console\Command\Shared\ContainerAwareCommandTrait; +use Drupal\Console\Annotation\DrupalCommand; use Drupal\Console\Style\DrupalStyle; -class RunCommand extends ContainerAwareCommand +class RunCommand extends Command { + use ContainerAwareCommandTrait; + /** - * {@inheritdoc} + * @DrupalCommand( + * dependencies = { + * “simpletest" + * } + * ) */ protected function configure() { @@ -41,56 +49,12 @@ protected function configure() InputOption::VALUE_REQUIRED, $this->trans('commands.test.run.arguments.url') ); - - - $this->addDependency('simpletest'); } /* * Set Server variable to be used in test cases. */ - protected function setEnvironment($url) - { - $base_url = ''; - $port = '80'; - $parsed_url = parse_url($url); - $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''); - $path = isset($parsed_url['path']) ? rtrim(rtrim($parsed_url['path']), '/') : ''; - $port = (isset($parsed_url['port']) ? $parsed_url['port'] : $port); - if ($path == '/') { - $path = ''; - } - // If the passed URL schema is 'https' then setup the $_SERVER variables - // properly so that testing will run under HTTPS. - if ($parsed_url['scheme'] == 'https') { - $_SERVER['HTTPS'] = 'on'; - } - - - if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') { - $base_url = 'https://'; - } else { - $base_url = 'http://'; - } - $base_url .= $host; - if ($path !== '') { - $base_url .= $path; - } - putenv('SIMPLETEST_BASE_URL=' . $base_url); - $_SERVER['HTTP_HOST'] = $host; - $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; - $_SERVER['SERVER_ADDR'] = '127.0.0.1'; - $_SERVER['SERVER_PORT'] = $port; - $_SERVER['SERVER_SOFTWARE'] = null; - $_SERVER['SERVER_NAME'] = 'localhost'; - $_SERVER['REQUEST_URI'] = $path .'/'; - $_SERVER['REQUEST_METHOD'] = 'GET'; - $_SERVER['SCRIPT_NAME'] = $path .'/index.php'; - $_SERVER['SCRIPT_FILENAME'] = $path .'/index.php'; - $_SERVER['PHP_SELF'] = $path .'/index.php'; - $_SERVER['HTTP_USER_AGENT'] = 'Drupal Console'; - } /** * {@inheritdoc} */ @@ -99,7 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $io = new DrupalStyle($input, $output); //Registers namespaces for disabled modules. - $this->getTestDiscovery()->registerTestNamespaces(); + $this->getDrupalService('test_discovery')->registerTestNamespaces(); $testClass = $input->getArgument('test-class'); $testMethods = $input->getArgument('test-methods'); @@ -108,20 +72,21 @@ protected function execute(InputInterface $input, OutputInterface $output) if (!$url) { $io->error($this->trans('commands.test.run.messages.url-required')); - return; + return NULL; } $this->setEnvironment($url); // Create simpletest test id $testId = db_insert('simpletest_test_id') - ->useDefaults(array('test_id')) + ->useDefaults(['test_id']) ->execute(); if (is_subclass_of($testClass, 'PHPUnit_Framework_TestCase')) { $io->info($this->trans('commands.test.run.messages.phpunit-pending')); - return; - } else { + return NULL; + } + else { if (!class_exists($testClass)) { $io->error( sprintf( @@ -141,61 +106,124 @@ protected function execute(InputInterface $input, OutputInterface $output) $end = Timer::stop('run-tests'); - $io->simple($this->trans('commands.test.run.messages.test-duration') . ': ' . \Drupal::service('date.formatter')->formatInterval($end['time'] / 1000)); - $io->simple($this->trans('commands.test.run.messages.test-pass') . ': ' . $test->results['#pass']); - $io->commentBlock($this->trans('commands.test.run.messages.test-fail') . ': ' . $test->results['#fail']); - $io->commentBlock($this->trans('commands.test.run.messages.test-exception') . ': ' . $test->results['#exception']); - $io->simple($this->trans('commands.test.run.messages.test-debug') . ': ' . $test->results['#debug']); + $io->simple( + $this->trans('commands.test.run.messages.test-duration') . ': ' . \Drupal::service('date.formatter')->formatInterval($end['time'] / 1000) + ); + $io->simple( + $this->trans('commands.test.run.messages.test-pass') . ': ' . $test->results['#pass'] + ); + $io->commentBlock( + $this->trans('commands.test.run.messages.test-fail') . ': ' . $test->results['#fail'] + ); + $io->commentBlock( + $this->trans('commands.test.run.messages.test-exception') . ': ' . $test->results['#exception'] + ); + $io->simple( + $this->trans('commands.test.run.messages.test-debug') . ': ' . $test->results['#debug'] + ); - $this->getModuleHandler()->invokeAll('test_finished', array($test->results)); + $this->getModuleHandler()->invokeAll( + 'test_finished', + [$test->results] + ); $io->newLine(); $io->info($this->trans('commands.test.run.messages.test-summary')); $io->newLine(); - $currentClass = null; - $currentGroup = null; - $currentStatus = null; + $currentClass = NULL; + $currentGroup = NULL; + $currentStatus = NULL; - $messages = $this->simpletestScriptLoadMessagesByTestIds(array($testId)); + $messages = $this->simpletestScriptLoadMessagesByTestIds([$testId]); foreach ($messages as $message) { - if ($currentClass === null || $currentClass != $message->test_class) { + if ($currentClass === NULL || $currentClass != $message->test_class) { $currentClass = $message->test_class; $io->comment($message->test_class); } - if ($currentGroup === null || $currentGroup != $message->message_group) { + if ($currentGroup === NULL || $currentGroup != $message->message_group) { $currentGroup = $message->message_group; } - if ($currentStatus === null || $currentStatus != $message->status) { + if ($currentStatus === NULL || $currentStatus != $message->status) { $currentStatus = $message->status; if ($message->status == 'fail') { $io->error($this->trans('commands.test.run.messages.group') . ':' . $message->message_group . ' ' . $this->trans('commands.test.run.messages.status') . ':' . $message->status); $io->newLine(); - } else { + } + else { $io->info($this->trans('commands.test.run.messages.group') . ':' . $message->message_group . ' ' . $this->trans('commands.test.run.messages.status') . ':' . $message->status); $io->newLine(); } } - $io->simple($this->trans('commands.test.run.messages.file') . ': ' . str_replace($this->getDrupalHelper()->getRoot(), '', $message->file)); - $io->simple($this->trans('commands.test.run.messages.method') . ': ' . $message->function); - $io->simple($this->trans('commands.test.run.messages.line') . ': ' . $message->line); - $io->simple($this->trans('commands.test.run.messages.message') . ': ' . $message->message); + $io->simple( + $this->trans('commands.test.run.messages.file') . ': ' . str_replace($this->getDrupalHelper()->getRoot(), '', $message->file) + ); + $io->simple( + $this->trans('commands.test.run.messages.method') . ': ' . $message->function + ); + $io->simple( + $this->trans('commands.test.run.messages.line') . ': ' . $message->line + ); + $io->simple( + $this->trans('commands.test.run.messages.message') . ': ' . $message->message + ); $io->newLine(); } - return; + return NULL; } } + protected function setEnvironment($url) { + $base_url = 'http://'; + $port = '80'; + + $parsed_url = parse_url($url); + $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''); + $path = isset($parsed_url['path']) ? rtrim(rtrim($parsed_url['path']), '/') : ''; + $port = (isset($parsed_url['port']) ? $parsed_url['port'] : $port); + if ($path == '/') { + $path = ''; + } + // If the passed URL schema is 'https' then setup the $_SERVER variables + // properly so that testing will run under HTTPS. + if ($parsed_url['scheme'] == 'https') { + $_SERVER['HTTPS'] = 'on'; + } + + + if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') { + $base_url = 'https://'; + } + $base_url .= $host; + if ($path !== '') { + $base_url .= $path; + } + putenv('SIMPLETEST_BASE_URL=' . $base_url); + $_SERVER['HTTP_HOST'] = $host; + $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; + $_SERVER['SERVER_ADDR'] = '127.0.0.1'; + $_SERVER['SERVER_PORT'] = $port; + $_SERVER['SERVER_SOFTWARE'] = NULL; + $_SERVER['SERVER_NAME'] = 'localhost'; + $_SERVER['REQUEST_URI'] = $path . '/'; + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['SCRIPT_NAME'] = $path . '/index.php'; + $_SERVER['SCRIPT_FILENAME'] = $path . '/index.php'; + $_SERVER['PHP_SELF'] = $path . '/index.php'; + $_SERVER['HTTP_USER_AGENT'] = 'Drupal Console'; + } + /* * Get Simletests log after execution */ + protected function simpletestScriptLoadMessagesByTestIds($test_ids) { - $results = array(); + $results = []; foreach ($test_ids as $test_id) { $result = \Drupal::database()->query(