|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BrainExe\Core\Console\TestGenerator; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @codeCoverageIgnore |
|
13
|
|
|
*/ |
|
14
|
|
|
class HandleExistingFile |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param string $originalTest |
|
19
|
|
|
* @param string $newTest |
|
20
|
|
|
* @throws Exception |
|
21
|
|
|
* @return string |
|
22
|
|
|
*/ |
|
23
|
|
|
private function replaceHeaderOnly($originalTest, $newTest) |
|
24
|
|
|
{ |
|
25
|
|
|
if (!preg_match('/^.*?}/s', $newTest, $matches)) { |
|
26
|
|
|
throw new Exception('No header found in new test'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$header = $matches[0]; |
|
30
|
|
|
|
|
31
|
|
|
return preg_replace('/^.*?}/s', $header, $originalTest); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param InputInterface $input |
|
36
|
|
|
* @param OutputInterface $output |
|
37
|
|
|
* @param QuestionHelper $helper |
|
38
|
|
|
* @param string $serviceId |
|
39
|
|
|
* @param string $testFileName |
|
40
|
|
|
* @param string $template |
|
41
|
|
|
* @return string |
|
42
|
|
|
* @throws Exception |
|
43
|
|
|
*/ |
|
44
|
|
|
public function handleExistingFile( |
|
|
|
|
|
|
45
|
|
|
InputInterface $input, |
|
46
|
|
|
OutputInterface $output, |
|
47
|
|
|
QuestionHelper $helper, |
|
48
|
|
|
$serviceId, |
|
49
|
|
|
$testFileName, |
|
50
|
|
|
$template |
|
51
|
|
|
) { |
|
52
|
|
|
if ($input->getOption('no-interaction')) { |
|
53
|
|
|
$output->writeln(sprintf("Test for '<info>%s</info>' already exist", $serviceId)); |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$choices = [ |
|
58
|
|
|
'stop' => 'Stop', |
|
59
|
|
|
'replace' => 'Replace full test file', |
|
60
|
|
|
'diff' => 'Display the diff', |
|
61
|
|
|
'header' => 'full setup only', |
|
62
|
|
|
]; |
|
63
|
|
|
$question = new ChoiceQuestion( |
|
64
|
|
|
'<error>The test file already exist. What should i do now?</error>', |
|
65
|
|
|
$choices |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$answer = $helper->ask($input, $output, $question); |
|
69
|
|
|
$originalTest = file_get_contents($testFileName); |
|
70
|
|
|
|
|
71
|
|
|
$answerId = array_flip($choices)[$answer]; |
|
72
|
|
|
|
|
73
|
|
|
return $this->handleQuestion($output, $template, $answerId, $originalTest); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param OutputInterface $output |
|
78
|
|
|
* @param $template |
|
79
|
|
|
* @param $answerId |
|
80
|
|
|
* @param $originalTest |
|
81
|
|
|
* @return bool|string |
|
82
|
|
|
* @throws Exception |
|
83
|
|
|
*/ |
|
84
|
|
|
private function handleQuestion(OutputInterface $output, string $template, string $answerId, $originalTest) |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
switch ($answerId) { |
|
87
|
|
|
case 'replace': |
|
88
|
|
|
break; |
|
89
|
|
|
case 'header': |
|
90
|
|
|
$template = $this->replaceHeaderOnly($originalTest, $template); |
|
91
|
|
|
break; |
|
92
|
|
|
case 'stop': |
|
93
|
|
|
default: |
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $template; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|