1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Console; |
4
|
|
|
|
5
|
|
|
use PhpUnitGen\Configuration\ConsoleConfigFactoryInterface; |
6
|
|
|
use PhpUnitGen\Configuration\ConsoleConfigInterface; |
7
|
|
|
use PhpUnitGen\Configuration\JsonConsoleConfigFactory; |
8
|
|
|
use PhpUnitGen\Configuration\PhpConsoleConfigFactory; |
9
|
|
|
use PhpUnitGen\Configuration\YamlConsoleConfigFactory; |
10
|
|
|
use PhpUnitGen\Exception\InvalidConfigException; |
11
|
|
|
use Respect\Validation\Validator; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class GenerateCommand. |
17
|
|
|
* |
18
|
|
|
* @author Paul Thébaud <[email protected]>. |
19
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
20
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
21
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
22
|
|
|
* @since Class available since Release 2.0.0. |
23
|
|
|
*/ |
24
|
|
|
class GenerateCommand extends AbstractGenerateCommand |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string[] CONSOLE_CONFIG_FACTORIES Mapping array between file extension and configuration factories. |
28
|
|
|
*/ |
29
|
|
|
const CONSOLE_CONFIG_FACTORIES = [ |
30
|
|
|
'yml' => YamlConsoleConfigFactory::class, |
31
|
|
|
'json' => JsonConsoleConfigFactory::class, |
32
|
|
|
'php' => PhpConsoleConfigFactory::class |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
protected function configure() |
39
|
|
|
{ |
40
|
|
|
$this->setName("generate") |
41
|
|
|
->setDescription("Generate unit tests skeletons") |
42
|
|
|
->setHelp("Use it to generate your unit tests skeletons from a config file") |
43
|
|
|
->addArgument('config-path', InputArgument::REQUIRED, 'The config file path.'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function getConfiguration(InputInterface $input): ConsoleConfigInterface |
50
|
|
|
{ |
51
|
|
|
$configPath = $input->getArgument('config-path'); |
52
|
|
|
|
53
|
|
|
if (! file_exists($configPath)) { |
54
|
|
|
throw new InvalidConfigException(sprintf('Config file "%s" does not exists.', $configPath)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$extension = pathinfo($configPath, PATHINFO_EXTENSION); |
58
|
|
|
if (! Validator::key($extension)->validate(static::CONSOLE_CONFIG_FACTORIES)) { |
59
|
|
|
throw new InvalidConfigException( |
60
|
|
|
sprintf('Config file "%s" must have .yml, .json or .php extension.', $configPath) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @var ConsoleConfigFactoryInterface $factory */ |
65
|
|
|
$factoryClass = static::CONSOLE_CONFIG_FACTORIES[$extension]; |
66
|
|
|
$factory = new $factoryClass(); |
67
|
|
|
return $factory->invoke($configPath); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|