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