Skip to content

Commit

Permalink
Merge pull request #1568 from omero/drupal-style-namespace-test
Browse files Browse the repository at this point in the history
#1542  Drupal style namespace test
  • Loading branch information
jmolivas committed Dec 23, 2015
2 parents 63043c3 + 851e54f commit fd78608
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 86 deletions.
97 changes: 48 additions & 49 deletions src/Command/Test/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\Table;
use Drupal\Component\Serialization\Yaml;
use Drupal\Console\Command\ContainerAwareCommand;
use Drupal\Console\Style\DrupalStyle;

/**
* Class DebugCommand
* @package Drupal\Console\Command\Test
*/
class DebugCommand extends ContainerAwareCommand
{
/**
Expand Down Expand Up @@ -45,90 +49,82 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
//Registers namespaces for disabled modules.
$this->getTestDiscovery()->registerTestNamespaces();

$test_class = $input->getArgument('test-class');
$testClass = $input->getArgument('test-class');
$group = $input->getOption('group');

$table = new Table($output);
$table->setStyle('compact');

if ($test_class) {
$this->getTestByID($output, $table, $test_class);
if ($testClass) {
$this->testDetail($io, $testClass);
} else {
$this->getAllTests($output, $table, $group);
$this->testList($io, $group);
}
}

/**
* @param $output OutputInterface
* @param $table TableHelper
* @param $config_name String
*/
private function getTestByID($output, $table, $test_class)
private function testDetail(DrupalStyle $io, $test_class)
{
$testing_groups = $this->getTestDiscovery()->getTestClasses(null);
$testingGroups = $this->getTestDiscovery()->getTestClasses(null);

$test_details = null;
foreach ($testing_groups as $testing_group => $tests) {
$testDetails = null;
foreach ($testingGroups as $testing_group => $tests) {
foreach ($tests as $key => $test) {
if ($test['name'] == $test_class) {
$test_details = $test;
$testDetails = $test;
break;
}
}
if ($test_details !== null) {
if ($testDetails !== null) {
break;
}
}

$class = null;
if ($test_details) {
if ($testDetails) {
$class = new \ReflectionClass($test['name']);
if (is_subclass_of($test_details['name'], 'PHPUnit_Framework_TestCase')) {
$test_details['type'] = 'phpunit';
if (is_subclass_of($testDetails['name'], 'PHPUnit_Framework_TestCase')) {
$testDetails['type'] = 'phpunit';
} else {
$test_details = $this->getTestDiscovery()->getTestInfo($test_details['name']);
$test_details['type'] = 'simpletest';
$testDetails = $this->getTestDiscovery()->getTestInfo($testDetails['name']);
$testDetails['type'] = 'simpletest';
}

$configurationEncoded = Yaml::encode($test_details);
$table->addRow([$configurationEncoded]);
$table->render();
$io->comment($testDetails['name']);

$testInfo = [];
foreach ($testDetails as $key => $value) {
$testInfo [] = [$key, $value];
}

$io->table([], $testInfo);

if ($class) {
$methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
$output->writeln('[+] <info>'. $this->trans('commands.test.debug.messages.methods').'</info>');
$io->info($this->trans('commands.test.debug.messages.methods'));
foreach ($methods as $method) {
if ($method->class == $test_details['name'] && strpos($method->name, 'test') === 0) {
$output->writeln('[-] <info>'. $method->name .'</info>');
if ($method->class == $testDetails['name'] && strpos($method->name, 'test') === 0) {
$io->simple($method->name);
}
}
}
} else {
$output->writeln('[+] <error>'. $this->trans('commands.test.debug.messages.not-found').'</error>');
$io->error($this->trans('commands.test.debug.messages.not-found'));
}
}

/**
* @param $output OutputInterface
* @param $table TableHelper
* @param $config_name String
*/
protected function getAllTests($output, $table, $group)
protected function testList(DrupalStyle $io, $group)
{
$testing_groups = $this->getTestDiscovery()->getTestClasses(null);
$testingGroups = $this->getTestDiscovery()->getTestClasses(null);

$table->setHeaders(
[
$this->trans('commands.test.debug.messages.class'),
$this->trans('commands.test.debug.messages.group'),
$this->trans('commands.test.debug.messages.type'),
]
);
$tableHeader = [
$this->trans('commands.test.debug.messages.class'),
$this->trans('commands.test.debug.messages.group'),
$this->trans('commands.test.debug.messages.type')
];

foreach ($testing_groups as $testing_group => $tests) {
$tableRows = [];
foreach ($testingGroups as $testing_group => $tests) {
if (!empty($group) && $group != $testing_group) {
continue;
}
Expand All @@ -139,10 +135,13 @@ protected function getAllTests($output, $table, $group)
} else {
$test['type'] = 'simpletest';
}
$table->addRow(array($test['name'], $test['group'], $test['type']));
$tableRows[] =[
$test['name'],
$test['group'],
$test['type']
];
}
}

$table->render();
$io->table($tableHeader, $tableRows, 'compact');
}
}
74 changes: 37 additions & 37 deletions src/Command/Test/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Component\Utility\Timer;
use Drupal\Console\Command\ContainerAwareCommand;
use Drupal\Console\Style\DrupalStyle;

class RunCommand extends ContainerAwareCommand
{
Expand Down Expand Up @@ -46,8 +47,6 @@ protected function configure()
protected function setEnvironment($url)
{
$base_url;
$host = 'localhost';
$path = '';
$port = '80';

$parsed_url = parse_url($url);
Expand Down Expand Up @@ -92,82 +91,83 @@ protected function setEnvironment($url)
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

//Registers namespaces for disabled modules.
$this->getTestDiscovery()->registerTestNamespaces();

$test_class = $input->getArgument('test-class');
$testClass = $input->getArgument('test-class');

$url = $input->getOption('url');

if (!$url) {
$output->writeln('[+] <error>'. $this->trans('commands.test.run.messages.url-required') .'</error>');
$io->error($this->trans('commands.test.run.messages.url-required'));
return;
}

$this->setEnvironment($url);

// Create simpletest test id
$test_id = db_insert('simpletest_test_id')
$testId = db_insert('simpletest_test_id')
->useDefaults(array('test_id'))
->execute();

if (is_subclass_of($test_class, 'PHPUnit_Framework_TestCase')) {
$output->writeln('[+] <info>'. $this->trans('commands.test.run.messages.phpunit-pending') .'</info>');
if (is_subclass_of($testClass, 'PHPUnit_Framework_TestCase')) {
$io->info($this->trans('commands.test.run.messages.phpunit-pending'));
return;
} else {
$test = new $test_class($test_id);
$output->writeln('[+] <info>'. $this->trans('commands.test.run.messages.starting-test') .'</info>');
$test = new $testClass($testId);
$io->info($this->trans('commands.test.run.messages.starting-test'));
Timer::start('run-tests');

$test->run();

$end = Timer::stop('run-tests');

$output->writeln('[+] <info>'. $this->trans('commands.test.run.messages.test-duration') . ': ' . \Drupal::service('date.formatter')->formatInterval($end['time'] / 1000) . '</info>');
$output->writeln('[+] <info>'. $this->trans('commands.test.run.messages.test-pass') . ': ' . $test->results['#pass'] . '</info>');
$output->writeln('[+] <error>'. $this->trans('commands.test.run.messages.test-fail') . ': ' . $test->results['#fail'] . '</error>');
$output->writeln('[+] <error>'. $this->trans('commands.test.run.messages.test-exception') . ': ' . $test->results['#exception'] . '</error>');
$output->writeln('[+] <info>'. $this->trans('commands.test.run.messages.test-debug') . ': ' . $test->results['#debug'] . '</info>');
$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));

print "\n";
$output->writeln($this->trans('commands.test.run.messages.test-summary'));
print "\n";
$io->newLine();
$io->info($this->trans('commands.test.run.messages.test-summary'));
$io->newLine();

$current_class = null;
$current_group = null;
$current_status = null;
$currentClass = null;
$currentGroup = null;
$currentStatus = null;

$messages = $this->simpletestScriptLoadMessagesByTestIds(array($test_id));
$messages = $this->simpletestScriptLoadMessagesByTestIds(array($testId));

foreach ($messages as $message) {
if ($current_class === null || $current_class != $message->test_class) {
$current_class = $message->test_class;
$output->writeln('[+] <info>' . $message->test_class . '</info>');
if ($currentClass === null || $currentClass != $message->test_class) {
$currentClass = $message->test_class;
$io->comment($message->test_class);
}

if ($current_group === null || $current_group != $message->message_group) {
$current_group = $message->message_group;
if ($currentGroup === null || $currentGroup != $message->message_group) {
$currentGroup = $message->message_group;
}

if ($current_status === null || $current_status != $message->status) {
$current_status = $message->status;
if ($currentStatus === null || $currentStatus != $message->status) {
$currentStatus = $message->status;
if ($message->status == 'fail') {
$output->writeln('[+] <error>' . $this->trans('commands.test.run.messages.group') . ':' . $message->message_group . ' ' . $this->trans('commands.test.run.messages.status') . ':' . $message->status . '</error>');
print "\n";
$io->error($this->trans('commands.test.run.messages.group') . ':' . $message->message_group . ' ' . $this->trans('commands.test.run.messages.status') . ':' . $message->status);
$io->newLine();
} else {
$output->writeln('[+] <info>' . $this->trans('commands.test.run.messages.group') . ':' . $message->message_group . ' ' . $this->trans('commands.test.run.messages.status') . ':' . $message->status . '</info>');
print "\n";
$io->info($this->trans('commands.test.run.messages.group') . ':' . $message->message_group . ' ' . $this->trans('commands.test.run.messages.status') . ':' . $message->status);
$io->newLine();
}
}

$output->writeln('[-] <info>' . $this->trans('commands.test.run.messages.file') . ': ' . str_replace($this->getDrupalHelper()->getRoot(), '', $message->file) . '</info>');
$output->writeln('[-] <info>' . $this->trans('commands.test.run.messages.method') . ': ' . $message->function . '</info>');
$output->writeln('[-] <info>' . $this->trans('commands.test.run.messages.line') . ': ' . $message->line . '</info>');
$output->writeln('[-] <info>' . $this->trans('commands.test.run.messages.message') . ': ' . $message->message . '</info>');
print "\n";
$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;
}
Expand Down
13 changes: 13 additions & 0 deletions src/Style/DrupalStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,17 @@ function ($row) {
$table->render();
$this->newLine();
}

/**
* {@inheritdoc}
*/
public function simple($message, $newLine = true)
{
$message = sprintf(' %s', $message);
if ($newLine) {
$this->writeln($message);
} else {
$this->write($message);
}
}
}

0 comments on commit fd78608

Please sign in to comment.