Repository navigation
Expand file tree
/
Copy pathCodecept.php
More file actions
199 lines (167 loc) · 6.37 KB
/
Copy pathCodecept.php
File metadata and controls
199 lines (167 loc) · 6.37 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace Codeception;
use Codeception\Configuration;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use \Symfony\Component\Finder\Finder;
use \Symfony\Component\EventDispatcher\EventDispatcher;
use Codeception\Exception\Configuration as ConfigurationException;
class Codecept
{
const VERSION = "2.0.6";
/**
* @var \Codeception\PHPUnit\Runner
*/
protected $runner;
/**
* @var \PHPUnit_Framework_TestResult
*/
protected $result;
/**
* @var \Symfony\Component\EventDispatcher\EventDispatcher
*/
protected $dispatcher;
/**
* @var array
*/
protected $options = array(
'silent' => false,
'debug' => false,
'steps' => false,
'html' => false,
'xml' => false,
'json' => false,
'tap' => false,
'report' => false,
'colors' => false,
'coverage' => false,
'coverage-xml' => false,
'coverage-html' => false,
'coverage-text' => false,
'groups' => null,
'excludeGroups' => null,
'filter' => null,
'env' => null,
'fail-fast' => false,
'verbosity' => 1
);
/**
* @var array
*/
protected $extensions = array();
public function __construct($options = array()) {
$this->result = new \PHPUnit_Framework_TestResult;
$this->dispatcher = new EventDispatcher();
$this->loadExtensions($options);
$this->config = Configuration::config();
$this->options = $this->mergeOptions($options);
$this->registerSubscribers();
$this->registerPHPUnitListeners();
$printer = new PHPUnit\ResultPrinter\UI($this->dispatcher, $this->options);
$this->runner = new PHPUnit\Runner($this->options);
$this->runner->setPrinter($printer);
}
private function mergeOptions($options) {
foreach ($this->options as $option => $default) {
if (!isset($options[$option])) {
$options[$option] = $default;
}
if (isset($this->config['settings'][$option])) {
$options[$option] = $this->config['settings'][$option];
}
}
return $options;
}
protected function loadExtensions($options)
{
$config = Configuration::config();
// custom event listeners
foreach ($config['extensions']['enabled'] as $extension) {
if (!class_exists($extension)) {
throw new ConfigurationException("Class $extension not defined. Autoload it or include into '_bootstrap.php' file of 'tests' directory");
}
if ($extension instanceof EventSubscriberInterface) {
throw new ConfigurationException("Class $extension is not a EventListener. Please create it as Extension or Group class.");
}
$extensionConfig = isset($config['extensions']['config'][$extension])
? $config['extensions']['config'][$extension]
: [];
$this->extensions[] = new $extension($extensionConfig, $options);
}
}
protected function registerPHPUnitListeners() {
$listener = new PHPUnit\Listener($this->dispatcher);
$this->result->addListener($listener);
}
public function registerSubscribers() {
// required
$this->dispatcher->addSubscriber(new Subscriber\ErrorHandler());
$this->dispatcher->addSubscriber(new Subscriber\Bootstrap());
$this->dispatcher->addSubscriber(new Subscriber\Module());
$this->dispatcher->addSubscriber(new Subscriber\BeforeAfterTest());
$this->dispatcher->addSubscriber(new Subscriber\AutoRebuild());
// optional
if (!$this->options['silent']) $this->dispatcher->addSubscriber(new Subscriber\Console($this->options));
if ($this->options['fail-fast']) $this->dispatcher->addSubscriber(new Subscriber\FailFast());
if ($this->options['coverage']) {
$this->dispatcher->addSubscriber(new Coverage\Subscriber\Local($this->options));
$this->dispatcher->addSubscriber(new Coverage\Subscriber\LocalServer($this->options));
$this->dispatcher->addSubscriber(new Coverage\Subscriber\RemoteServer($this->options));
$this->dispatcher->addSubscriber(new Coverage\Subscriber\Printer($this->options));
}
// extensions
foreach ($this->extensions as $subscriber) {
$this->dispatcher->addSubscriber($subscriber);
}
}
public function run($suite, $test = null)
{
ini_set('memory_limit', isset($this->config['settings']['memory_limit']) ? $this->config['settings']['memory_limit'] : '1024M');
$settings = Configuration::suiteSettings($suite, Configuration::config());
$selectedEnvironments = $this->options['env'];
$environments = Configuration::suiteEnvironments($suite);
if (!$selectedEnvironments or empty($environments)) {
$this->runSuite($settings, $suite, $test);
return;
}
foreach ($environments as $env => $config) {
if (!in_array($env, $selectedEnvironments)) {
continue;
}
$suiteToRun = is_int($env) ? $suite : "{$suite}-{$env}";
$this->runSuite($config, $suiteToRun, $test);
}
}
public function runSuite($settings, $suite, $test = null) {
$suiteManager = new SuiteManager($this->dispatcher, $suite, $settings);
$suiteManager->initialize();
$suiteManager->loadTests($test);
$suiteManager->run($this->runner, $this->result, $this->options);
return $this->result;
}
public static function versionString() {
return 'Codeception PHP Testing Framework v'.self::VERSION;
}
public function printResult() {
$result = $this->getResult();
$result->flushListeners();
$printer = $this->runner->getPrinter();
$printer->printResult($result);
$this->dispatcher->dispatch(Events::RESULT_PRINT_AFTER, new Event\PrintResultEvent($result, $printer));
}
/**
* @return \PHPUnit_Framework_TestResult
*/
public function getResult() {
return $this->result;
}
public function getOptions() {
return $this->options;
}
/**
* @return EventDispatcher
*/
public function getDispatcher()
{
return $this->dispatcher;
}
}