/
TestCase.php
170 lines (138 loc) · 5.2 KB
/
TestCase.php
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
<?php
namespace Codeception;
abstract class TestCase extends \PHPUnit_Framework_TestCase implements \PHPUnit_Framework_SelfDescribing
{
protected $testfile = null;
protected $output;
protected $debug;
protected $features = array();
protected $scenario;
protected $bootstrap = null;
protected $stopped = false;
protected $trace = array();
protected $logger = null;
public function __construct($name, array $data = array(), $dataName = '')
{
parent::__construct('testCodecept', $data, $dataName);
if (!isset($data['file'])) throw new \Exception('File with test scenario not set. Use array(file => filepath) to set a scenario');
$this->specName = $name;
$this->scenario = new \Codeception\Scenario($this);
$this->testfile = $data['file'];
$this->bootstrap = isset($data['bootstrap']) ? $data['bootstrap'] : null;
$this->debug = isset($data['debug']) ? $data['debug'] : false;
$this->output = new Output(false); // no output by default
$this->logger = new \Monolog\Logger($this->specName);
}
public function getFileName()
{
return $this->getSpecName() . 'Spec.php';
}
public function getSpecName() {
return $this->specName;
}
/**
* @return \Codeception\Scenario
*/
public function getScenario()
{
return $this->scenario;
}
public function getScenarioText()
{
$text = implode("\r\n", $this->scenario->getSteps());
$text = str_replace(array('((', '))'), array('...', ''), $text);
return $text = strtoupper('I want to ' . $this->scenario->getFeature()) . "\n\n" . $text;
}
public function __call($command, $args)
{
if (strrpos('Test', $command) !== 0) return;
$this->testCodecept();
}
public function setUp() {
if (file_exists($this->bootstrap)) require $this->bootstrap;
foreach (\Codeception\SuiteManager::$modules as $module) {
$module->_before($this);
}
$this->loadScenario();
}
abstract public function loadScenario();
/**
* @test
*/
public function testCodecept()
{
$this->logger->info("\n\n".strtoupper('I '.$this->scenario->getFeature()));
$this->output->writeln("Trying to [[{$this->scenario->getFeature()}]] (" . basename($this->testfile) . ") ");
if ($this->debug && count($this->scenario->getSteps())) $this->output->writeln("Scenario:\n");
try {
$this->scenario->run();
} catch (\PHPUnit_Framework_ExpectationFailedException $fail) {
foreach (\Codeception\SuiteManager::$modules as $module) {
$module->_failed($this, $fail);
}
$this->logger->alert("*** last command failed ***");
$this->logger->alert($fail->getMessage());
throw $fail;
}
}
public function tearDown() {
foreach (\Codeception\SuiteManager::$modules as $module) {
$module->_after($this);
}
}
public function runStep(\Codeception\Step $step)
{
$this->logger->info($step);
if ($this->debug) $this->output->put("\n* " . $step->__toString());
if ($step->getName() == 'Comment') return;
$this->trace[] = $step;
$action = $step->getAction();
$arguments = array_merge($step->getArguments());
if (!isset(\Codeception\SuiteManager::$methods[$action])) {
$this->logger->crit("Action $action not defined");
$this->stopped = true;
$this->fail("Action $action not defined");
return;
}
foreach (\Codeception\SuiteManager::$modules as $module) {
$module->_beforeStep($step);
}
$activeModule = \Codeception\SuiteManager::$modules[\Codeception\SuiteManager::$methods[$action]];
try {
if (is_callable(array($activeModule, $action))) {
call_user_func_array(array($activeModule, $action), $arguments);
} else {
throw new \RuntimeException("Action can't be called");
}
} catch (\PHPUnit_Framework_ExpectationFailedException $fail) {
$this->logger->alert($fail->getMessage());
if ($activeModule->_getDebugOutput() && $this->debug) $this->output->debug($activeModule->_getDebugOutput());
throw $fail;
// TODO: put normal handling of errors
} catch (\Exception $e) {
$this->logger->crit($e->getMessage());
throw $e;
}
foreach (\Codeception\SuiteManager::$modules as $module) {
$module->_afterStep($step);
}
$output = $activeModule->_getDebugOutput();
if ($output) {
if ($this->debug) $this->output->debug($output);
}
}
public function toString()
{
return $this->scenario->getFeature() . ' (' . $this->getFileName() . ')';
}
public function getTrace()
{
return $this->trace;
}
public function setOutput(Output $output) {
$this->output = $output;
}
public function setLogHandler(\Monolog\Handler\HandlerInterface $handler) {
$this->logger->pushHandler($handler);
}
}