1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace Codeception\Command;
use Codeception\Configuration;
use Codeception\Lib\Generator\Actions as ActionsGenerator;
use Codeception\Lib\Generator\Actor as ActorGenerator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Generates Actor classes (initially Guy classes) from suite configs.
* Starting from Codeception 2.0 actor classes are auto-generated. Use this command to generate them manually.
*
* * `codecept build`
* * `codecept build path/to/project`
*
*/
class Build extends Command
{
use Shared\Config;
use Shared\FileSystem;
protected $inheritedMethodTemplate = ' * @method void %s(%s)';
/**
* @var OutputInterface
*/
protected $output;
public function getDescription()
{
return 'Generates base classes for all suites';
}
protected function configure()
{
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->buildActorsForConfig($input->getOption('config'));
}
private function buildActor(array $settings)
{
$actorGenerator = new ActorGenerator($settings);
$this->output->writeln(
'<info>' . Configuration::config()['namespace'] . '\\' . $actorGenerator->getActorName()
. "</info> includes modules: " . implode(', ', $actorGenerator->getModules())
);
$content = $actorGenerator->produce();
$file = $this->buildPath(
Configuration::supportDir(),
$settings['class_name']
) . $this->getClassName($settings['class_name']);
$file .= '.php';
return $this->save($file, $content);
}
private function buildActions(array $settings)
{
$actionsGenerator = new ActionsGenerator($settings);
$this->output->writeln(
" -> {$settings['class_name']}Actions.php generated successfully. "
. $actionsGenerator->getNumMethods() . " methods added"
);
$content = $actionsGenerator->produce();
$file = $this->buildPath(Configuration::supportDir() . '_generated', $settings['class_name']);
$file .= $this->getClassName($settings['class_name']) . 'Actions.php';
return $this->save($file, $content, true);
}
private function buildSuiteActors($configFile)
{
$suites = $this->getSuites($configFile);
if (!empty($suites)) {
$this->output->writeln("<info>Building Actor classes for suites: " . implode(', ', $suites) . '</info>');
}
foreach ($suites as $suite) {
$settings = $this->getSuiteConfig($suite, $configFile);
$this->buildActions($settings);
$actorBuilt = $this->buildActor($settings);
if ($actorBuilt) {
$this->output->writeln("{$settings['class_name']}.php created.");
}
}
}
protected function buildActorsForConfig($configFile)
{
$config = $this->getGlobalConfig($configFile);
$path = pathinfo($configFile);
$dir = isset($path['dirname']) ? $path['dirname'] : getcwd();
foreach ($config['include'] as $subConfig) {
$this->output->writeln("<comment>Included Configuration: $subConfig</comment>");
$this->buildActorsForConfig($dir . DIRECTORY_SEPARATOR . $subConfig);
}
$this->buildSuiteActors($configFile);
}
}