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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Codeception\Template;
use Codeception\InitTemplate;
use Symfony\Component\Yaml\Yaml;
class Bootstrap extends InitTemplate
{
// defaults
protected $supportDir = 'tests/_support';
protected $outputDir = 'tests/_output';
protected $dataDir = 'tests/_data';
protected $envsDir = 'tests/_envs';
public function setup()
{
$this->checkInstalled($this->workDir);
$input = $this->input;
if ($input->getOption('namespace')) {
$this->namespace = trim($input->getOption('namespace'), '\\') . '\\';
}
if ($input->hasOption('actor') && $input->getOption('actor')) {
$this->actorSuffix = $input->getOption('actor');
}
$this->say(
"<fg=white;bg=magenta> Bootstrapping Codeception </fg=white;bg=magenta>\n"
);
$this->createGlobalConfig();
$this->say("File codeception.yml created <- global configuration");
$this->createDirs();
if ($input->hasOption('empty') && $input->getOption('empty')) {
return;
}
$this->createUnitSuite();
$this->say("tests/unit created <- unit tests");
$this->say("tests/unit.suite.yml written <- unit tests suite configuration");
$this->createFunctionalSuite();
$this->say("tests/functional created <- functional tests");
$this->say("tests/functional.suite.yml written <- functional tests suite configuration");
$this->createAcceptanceSuite();
$this->say("tests/acceptance created <- acceptance tests");
$this->say("tests/acceptance.suite.yml written <- acceptance tests suite configuration");
$this->say(" --- ");
$this->say();
$this->saySuccess('Codeception is installed for acceptance, functional, and unit testing');
$this->say();
$this->say("<bold>Next steps:</bold>");
$this->say('1. Edit <bold>tests/acceptance.suite.yml</bold> to set url of your application. Change PhpBrowser to WebDriver to enable browser testing');
$this->say("2. Edit <bold>tests/functional.suite.yml</bold> to enable a framework module. Remove this file if you don't use a framework");
$this->say("3. Create your first acceptance tests using <comment>codecept g:cest acceptance First</comment>");
$this->say("4. Write first test in <bold>tests/acceptance/FirstCest.php</bold>");
$this->say("5. Run tests using: <comment>codecept run</comment>");
}
protected function createDirs()
{
$this->createDirectoryFor('tests');
$this->createEmptyDirectory($this->outputDir);
$this->createEmptyDirectory($this->dataDir);
$this->createDirectoryFor($this->supportDir . DIRECTORY_SEPARATOR . '_generated');
$this->createDirectoryFor($this->supportDir . DIRECTORY_SEPARATOR . "Helper");
$this->gitIgnore('tests/_output');
$this->gitIgnore('tests/_support/_generated');
}
protected function createFunctionalSuite($actor = 'Functional')
{
$suiteConfig = <<<EOF
# Codeception Test Suite Configuration
#
# Suite for functional tests
# Emulate web requests and make application process them
# Include one of framework modules (Symfony2, Yii2, Laravel5) to use it
# Remove this suite if you don't use frameworks
actor: $actor{$this->actorSuffix}
modules:
enabled:
# add a framework module here
- \\{$this->namespace}Helper\Functional
EOF;
$this->createSuite('functional', $actor, $suiteConfig);
}
protected function createAcceptanceSuite($actor = 'Acceptance')
{
$suiteConfig = <<<EOF
# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.
actor: $actor{$this->actorSuffix}
modules:
enabled:
- PhpBrowser:
url: http://localhost/myapp
- \\{$this->namespace}Helper\Acceptance
EOF;
$this->createSuite('acceptance', $actor, $suiteConfig);
}
protected function createUnitSuite($actor = 'Unit')
{
$suiteConfig = <<<EOF
# Codeception Test Suite Configuration
#
# Suite for unit or integration tests.
actor: $actor{$this->actorSuffix}
modules:
enabled:
- Asserts
- \\{$this->namespace}Helper\Unit
EOF;
$this->createSuite('unit', $actor, $suiteConfig);
}
public function createGlobalConfig()
{
$basicConfig = [
'paths' => [
'tests' => 'tests',
'output' => $this->outputDir,
'data' => $this->dataDir,
'support' => $this->supportDir,
'envs' => $this->envsDir,
],
'actor_suffix' => 'Tester',
'extensions' => [
'enabled' => ['Codeception\Extension\RunFailed']
]
];
$str = Yaml::dump($basicConfig, 4);
if ($this->namespace) {
$namespace = rtrim($this->namespace, '\\');
$str = "namespace: $namespace\n" . $str;
}
$this->createFile('codeception.yml', $str);
}
protected function createSuite($suite, $actor, $config)
{
$this->createDirectoryFor("tests/$suite", "$suite.suite.yml");
$this->createHelper($actor, $this->supportDir);
$this->createActor($actor . $this->actorSuffix, $this->supportDir, Yaml::parse($config));
$this->createFile('tests' . DIRECTORY_SEPARATOR . "$suite.suite.yml", $config);
}
}