1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Console; |
4
|
|
|
|
5
|
|
|
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface; |
6
|
|
|
use PhpUnitGen\Configuration\JsonConsoleConfigFactory; |
7
|
|
|
use PhpUnitGen\Configuration\PhpConsoleConfigFactory; |
8
|
|
|
use PhpUnitGen\Configuration\YamlConsoleConfigFactory; |
9
|
|
|
use PhpUnitGen\Container\ContainerInterface\ConsoleContainerFactoryInterface; |
10
|
|
|
use PhpUnitGen\Exception\Exception; |
11
|
|
|
use PhpUnitGen\Exception\InvalidConfigException; |
12
|
|
|
use PhpUnitGen\Executor\ExecutorInterface\ConsoleExecutorInterface; |
13
|
|
|
use Symfony\Component\Console\Command\Command; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
17
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class AbstractGenerateCommand. |
21
|
|
|
* |
22
|
|
|
* @author Paul Thébaud <[email protected]>. |
23
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
24
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
25
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
26
|
|
|
* @since Class available since Release 2.0.0. |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractGenerateCommand extends Command |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var string[] CONSOLE_CONFIG_FACTORIES Mapping array between file extension and configuration factories. |
32
|
|
|
*/ |
33
|
|
|
const CONSOLE_CONFIG_FACTORIES = [ |
34
|
|
|
'yml' => YamlConsoleConfigFactory::class, |
35
|
|
|
'json' => JsonConsoleConfigFactory::class, |
36
|
|
|
'php' => PhpConsoleConfigFactory::class |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ConsoleContainerFactoryInterface $containerFactory A container factory to create container. |
41
|
|
|
*/ |
42
|
|
|
protected $containerFactory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ConsoleExecutorInterface $consoleExecutor A executor to execute PhpUnitGen task. |
46
|
|
|
*/ |
47
|
|
|
protected $consoleExecutor; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var Stopwatch $stopwatch The stopwatch to measure duration and memory usage. |
51
|
|
|
*/ |
52
|
|
|
protected $stopwatch; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* GenerateCommand constructor. |
56
|
|
|
* |
57
|
|
|
* @param ConsoleContainerFactoryInterface $containerFactory A container factory to create container. |
58
|
|
|
*/ |
59
|
|
|
public function __construct(ConsoleContainerFactoryInterface $containerFactory) |
60
|
|
|
{ |
61
|
|
|
parent::__construct(); |
62
|
|
|
|
63
|
|
|
$this->containerFactory = $containerFactory; |
64
|
|
|
|
65
|
|
|
$this->stopwatch = new Stopwatch(true); |
66
|
|
|
$this->stopwatch->start('command'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
73
|
|
|
{ |
74
|
|
|
$styledOutput = new SymfonyStyle($input, $output); |
75
|
|
|
try { |
76
|
|
|
$config = $this->getConfiguration($input); |
77
|
|
|
} catch (InvalidConfigException $exception) { |
78
|
|
|
$styledOutput->error($exception->getMessage()); |
79
|
|
|
return -1; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$container = $this->containerFactory->invoke($config, $styledOutput, $this->stopwatch); |
83
|
|
|
|
84
|
|
|
$this->consoleExecutor = $container->get(ConsoleExecutorInterface::class); |
85
|
|
|
|
86
|
|
|
try { |
87
|
|
|
$this->consoleExecutor->invoke(); |
88
|
|
|
} catch (Exception $exception) { |
89
|
|
|
$styledOutput->error($exception->getMessage()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return 1; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Build a configuration from a configuration file path. |
97
|
|
|
* |
98
|
|
|
* @param InputInterface $input An input interface to retrieve command argument. |
99
|
|
|
* |
100
|
|
|
* @return ConsoleConfigInterface The created configuration. |
101
|
|
|
* |
102
|
|
|
* @throws InvalidConfigException If an error occurs during process. |
103
|
|
|
*/ |
104
|
|
|
abstract protected function getConfiguration(InputInterface $input): ConsoleConfigInterface; |
105
|
|
|
} |
106
|
|
|
|