1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace PHPChunkit\Command; |
6
|
|
|
|
7
|
|
|
use PHPChunkit\Events; |
8
|
|
|
use PHPChunkit\GenerateTestClass; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
15
|
|
|
|
16
|
|
|
class Generate implements CommandInterface |
17
|
|
|
{ |
18
|
|
|
const NAME = 'generate'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var GenerateTestClass |
22
|
|
|
*/ |
23
|
|
|
private $generateTestClass; |
24
|
|
|
|
25
|
|
|
public function __construct(GenerateTestClass $generateTestClass) |
26
|
|
|
{ |
27
|
|
|
$this->generateTestClass = $generateTestClass; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getName() : string |
31
|
|
|
{ |
32
|
|
|
return self::NAME; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function configure(Command $command) |
36
|
|
|
{ |
37
|
|
|
$command |
38
|
|
|
->setDescription('Generate a test skeleton from a class.') |
39
|
|
|
->addArgument('class', InputArgument::REQUIRED, 'Class to generate test for.') |
40
|
|
|
->addOption('file', null, InputOption::VALUE_REQUIRED, 'File path to write test to.') |
41
|
|
|
; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
45
|
|
|
{ |
46
|
|
|
$class = $input->getArgument('class'); |
47
|
|
|
|
48
|
|
|
$code = $this->generateTestClass->generate($class); |
49
|
|
|
|
50
|
|
|
if ($file = $input->getOption('file')) { |
51
|
|
|
if (file_exists($file)) { |
52
|
|
|
throw new \InvalidArgumentException(sprintf('%s already exists.', $file)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$output->writeln(sprintf('Writing test to <info>%s</info>', $file)); |
56
|
|
|
|
57
|
|
|
file_put_contents($file, $code); |
58
|
|
|
} else { |
59
|
|
|
$output->write($code); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|