Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Test / DebugCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Test\DebugCommand.
6  */
7
8 namespace Drupal\Console\Command\Test;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Component\Serialization\Yaml;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Console\Annotations\DrupalCommand;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\simpletest\TestDiscovery;
20
21 /**
22  * @DrupalCommand(
23  *     extension = "simpletest",
24  *     extensionType = "module",
25  * )
26  */
27 class DebugCommand extends Command
28 {
29     use CommandTrait;
30
31     /**
32       * @var TestDiscovery
33       */
34     protected $test_discovery;
35
36     /**
37      * DebugCommand constructor.
38      *
39      * @param TestDiscovery $test_discovery
40      */
41     public function __construct(
42         TestDiscovery $test_discovery
43     ) {
44         $this->test_discovery = $test_discovery;
45         parent::__construct();
46     }
47
48
49     protected function configure()
50     {
51         $this
52             ->setName('test:debug')
53             ->setDescription($this->trans('commands.test.debug.description'))
54             ->addArgument(
55                 'group',
56                 InputArgument::OPTIONAL,
57                 $this->trans('commands.test.debug.options.group'),
58                 null
59             )
60             ->addOption(
61                 'test-class',
62                 null,
63                 InputOption::VALUE_OPTIONAL,
64                 $this->trans('commands.test.debug.arguments.test-class')
65             );
66     }
67
68     /**
69      * {@inheritdoc}
70      */
71     protected function execute(InputInterface $input, OutputInterface $output)
72     {
73         $io = new DrupalStyle($input, $output);
74         //Registers namespaces for disabled modules.
75         $this->test_discovery->registerTestNamespaces();
76
77         $testClass = $input->getOption('test-class');
78         $group = $input->getArgument('group');
79
80         if ($testClass) {
81             $this->testDetail($io, $testClass);
82         } else {
83             $this->testList($io, $group);
84         }
85     }
86
87     private function testDetail(DrupalStyle $io, $test_class)
88     {
89         $testingGroups = $this->test_discovery->getTestClasses(null);
90
91         $testDetails = null;
92         foreach ($testingGroups as $testing_group => $tests) {
93             foreach ($tests as $key => $test) {
94                 if ($test['name'] == $test_class) {
95                     $testDetails = $test;
96                     break;
97                 }
98             }
99             if ($testDetails !== null) {
100                 break;
101             }
102         }
103
104         $class = null;
105         if ($testDetails) {
106             $class = new \ReflectionClass($test['name']);
107             if (is_subclass_of($testDetails['name'], 'PHPUnit_Framework_TestCase')) {
108                 $testDetails['type'] = 'phpunit';
109             } else {
110                 $testDetails = $this->test_discovery
111                     ->getTestInfo($testDetails['name']);
112                 $testDetails['type'] = 'simpletest';
113             }
114
115             $io->comment($testDetails['name']);
116
117             $testInfo = [];
118             foreach ($testDetails as $key => $value) {
119                 $testInfo [] = [$key, $value];
120             }
121
122             $io->table([], $testInfo);
123
124             if ($class) {
125                 $methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
126                 $io->info($this->trans('commands.test.debug.messages.methods'));
127                 foreach ($methods as $method) {
128                     if ($method->class == $testDetails['name'] && strpos($method->name, 'test') === 0) {
129                         $io->simple($method->name);
130                     }
131                 }
132             }
133         } else {
134             $io->error($this->trans('commands.test.debug.messages.not-found'));
135         }
136     }
137
138     protected function testList(DrupalStyle $io, $group)
139     {
140         $testingGroups = $this->test_discovery
141             ->getTestClasses(null);
142
143         if (empty($group)) {
144             $tableHeader = [$this->trans('commands.test.debug.messages.group')];
145         } else {
146             $tableHeader = [
147               $this->trans('commands.test.debug.messages.class'),
148               $this->trans('commands.test.debug.messages.type')
149             ];
150
151             $io->writeln(
152                 sprintf(
153                     '%s: %s',
154                     $this->trans('commands.test.debug.messages.group'),
155                     $group
156                 )
157             );
158         }
159
160         $tableRows = [];
161         foreach ($testingGroups as $testing_group => $tests) {
162             if (empty($group)) {
163                 $tableRows[] =[$testing_group];
164                 continue;
165             }
166
167             if (!empty($group) && $group != $testing_group) {
168                 continue;
169             }
170
171             foreach ($tests as $test) {
172                 if (is_subclass_of($test['name'], 'PHPUnit_Framework_TestCase')) {
173                     $test['type'] = 'phpunit';
174                 } else {
175                     $test['type'] = 'simpletest';
176                 }
177                 $tableRows[] =[
178                   $test['name'],
179                   $test['type']
180                 ];
181             }
182         }
183         $io->table($tableHeader, $tableRows, 'compact');
184
185         if ($group) {
186             $io->success(
187                 sprintf(
188                     $this->trans('commands.test.debug.messages.success-group'),
189                     $group
190                 )
191             );
192         } else {
193             $io->success(
194                 $this->trans('commands.test.debug.messages.success-groups')
195             );
196         }
197     }
198 }