1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RMiller\BehatSpec\Extension\ExemplifyExtension\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
11
|
|
|
|
12
|
|
|
class ExemplifyCommand extends Command |
13
|
|
|
{ |
14
|
|
|
protected function configure() |
15
|
|
|
{ |
16
|
|
|
$this |
17
|
|
|
->setName('exemplify') |
18
|
|
|
->setDefinition(array( |
19
|
|
|
new InputArgument('class', InputArgument::REQUIRED, 'Class method belongs to'), |
20
|
|
|
new InputArgument('method', InputArgument::REQUIRED, 'Method to describe'), |
21
|
|
|
)) |
22
|
|
|
->setDescription('Adds an example for a method') |
23
|
|
|
->addOption('confirm', null, InputOption::VALUE_NONE, 'Ask for confirmation before creating example') |
24
|
|
|
->setHelp(<<<EOF |
25
|
|
|
The <info>%command.name%</info> command creates an example for a method: |
26
|
|
|
|
27
|
|
|
<info>php %command.full_name% ClassName MethodName</info> |
28
|
|
|
|
29
|
|
|
Will generate an example in the ClassNameSpec. |
30
|
|
|
|
31
|
|
|
EOF |
32
|
|
|
) |
33
|
|
|
; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param InputInterface $input |
38
|
|
|
* @param OutputInterface $output |
39
|
|
|
* |
40
|
|
|
* @return int|null|void |
41
|
|
|
*/ |
42
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
43
|
|
|
{ |
44
|
|
|
$container = $this->getApplication()->getContainer(); |
|
|
|
|
45
|
|
|
$container->configure(); |
46
|
|
|
|
47
|
|
|
$classname = $input->getArgument('class'); |
48
|
|
|
$method = $input->getArgument('method'); |
49
|
|
|
|
50
|
|
|
if (!$this->confirm($input, $classname, $method)) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$resource = $container->get('locator.resource_manager')->createResource($classname); |
55
|
|
|
|
56
|
|
|
$container->get('code_generator')->generate($resource, 'specification_method', [ |
57
|
|
|
'method' => $method, |
58
|
|
|
'type' => $this->confirmMethodType($input, $output), |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param InputInterface $input |
64
|
|
|
* @param $classname |
65
|
|
|
* @param $method |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
private function confirm(InputInterface $input, $classname, $method) |
69
|
|
|
{ |
70
|
|
|
if (!$input->getOption('confirm')) { |
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$question = sprintf('Do you want to generate an example for %s::%s? (Y/n)', $classname, $method); |
75
|
|
|
$io = $this->getApplication()->getContainer()->get('console.io'); |
|
|
|
|
76
|
|
|
|
77
|
|
|
return $io->askConfirmation($question, true); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param OutputInterface $output |
82
|
|
|
*/ |
83
|
|
|
private function confirmMethodType(InputInterface $input, OutputInterface $output) |
84
|
|
|
{ |
85
|
|
|
$formattedMethodTypes = ['instance method','named constructor', 'static method']; |
86
|
|
|
|
87
|
|
|
return str_replace(' ', '-', $this->getHelper('question')->ask( |
|
|
|
|
88
|
|
|
$input, |
89
|
|
|
$output, |
90
|
|
|
new ChoiceQuestion( |
91
|
|
|
'Please select the method type (defaults to instance method)', |
92
|
|
|
$formattedMethodTypes, |
93
|
|
|
0 |
94
|
|
|
) |
95
|
|
|
)); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: