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