Repository navigation
Expand file tree
/
Copy pathCest.php
More file actions
137 lines (114 loc) · 4.1 KB
/
Copy pathCest.php
File metadata and controls
137 lines (114 loc) · 4.1 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
<?php
namespace Codeception\TestCase;
use Codeception\Events;
use Codeception\Event\TestEvent;
use Codeception\Util\Annotation;
class Cest extends Cept
{
protected $testClassInstance = null;
protected $testMethod = null;
protected $guy;
public function __construct($dispatcher, array $data = array(), $dataName = '')
{
parent::__construct($dispatcher, $data, $dataName);
$this->testClassInstance = $data['instance'];
$this->testMethod = $data['method'];
$this->guy = $data['guy'];
}
public function preload()
{
$this->scenario->setFeature($this->getSpecFromMethod());
parent::preload();
}
public function getRawBody()
{
$method = new \ReflectionMethod($this->testClassInstance, $this->testMethod);
$start_line = $method->getStartLine() - 1; // it's actually - 1, otherwise you wont get the function() block
$end_line = $method->getEndLine();
$source = file($method->getFileName());
return implode("", array_slice($source, $start_line, $end_line - $start_line));
}
public function testCodecept()
{
$this->fire(Events::TEST_BEFORE, new TestEvent($this));
if (file_exists($this->bootstrap)) {
require $this->bootstrap;
}
$this->scenario->run();
$I = $this->makeIObject();
try {
$this->executeBefore($this->testMethod, $I);
$this->executeTestMethod($I);
} catch (\Exception $e) {
$this->executeAfter($this->testMethod, $I);
// fails and errors are now handled by Codeception\PHPUnit\Listener
throw $e;
}
$this->executeAfter($this->testMethod, $I);
$this->fire(Events::TEST_AFTER, new TestEvent($this));
}
protected function executeBefore($testMethod, $I)
{
if ($before = Annotation::forClass($this->testClassInstance)->method($testMethod)->fetch('before')) {
$this->executeContextMethod($before, $I);
}
}
protected function executeAfter($testMethod, $I)
{
if ($after = Annotation::forClass($this->testClassInstance)->method($testMethod)->fetch('after')) {
$this->executeContextMethod($after, $I);
}
}
protected function executeContextMethod($context, $I)
{
if (method_exists($this->testClassInstance, $context)) {
$this->executeBefore($context, $I);
$contextMethod = new \ReflectionMethod($this->testClassInstance, $context);
$contextMethod->setAccessible(true);
$contextMethod->invoke($this->testClassInstance, $I);
$this->executeAfter($context, $I);
return;
}
throw new \LogicException(
"Method $context defined in annotation but does not exists in " . get_class($this->testClassInstance)
);
}
protected function makeIObject()
{
$className = '\\' . $this->guy;
$I = new $className($this->scenario);
if ($spec = $this->getSpecFromMethod()) {
$I->wantTo($spec);
}
return $I;
}
protected function executeTestMethod($I)
{
$testMethodSignature = array($this->testClassInstance, $this->testMethod);
if (! is_callable($testMethodSignature)) {
throw new \Exception("Method {$this->testMethod} can't be found in tested class");
}
call_user_func($testMethodSignature, $I, $this->scenario);
}
public function getTestClass()
{
return $this->testClassInstance;
}
public function getTestMethod()
{
return $this->testMethod;
}
public function getSpecFromMethod()
{
$text = $this->testMethod;
$text = preg_replace('/([A-Z]+)([A-Z][a-z])/', '\\1 \\2', $text);
$text = preg_replace('/([a-z\d])([A-Z])/', '\\1 \\2', $text);
$text = strtolower($text);
return $text;
}
public function getFileName()
{
$class = str_replace('\\', '.', get_class($this->getTestClass()));
return $class . "." . $this->getTestMethod();
}
}