Repository navigation
Expand file tree
/
Copy pathScenario.php
More file actions
157 lines (132 loc) · 4.26 KB
/
Copy pathScenario.php
File metadata and controls
157 lines (132 loc) · 4.26 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
<?php
declare(strict_types=1);
namespace Codeception;
use Codeception\Event\StepEvent;
use Codeception\Exception\ConditionalAssertionFailed;
use Codeception\Exception\InjectionException;
use Codeception\Step\Comment;
use Codeception\Step\Meta;
use Codeception\Test\Metadata;
use Exception;
use PHPUnit\Framework\IncompleteTestError;
use PHPUnit\Framework\SkippedTestError;
class Scenario
{
protected TestInterface $test;
protected Metadata $metadata;
protected array $steps = [];
protected string $feature;
protected ?Meta $metaStep = null;
public function __construct(TestInterface $test)
{
$this->metadata = $test->getMetadata();
$this->test = $test;
}
public function setFeature(string $feature): void
{
$this->metadata->setFeature($feature);
}
public function getFeature(): string
{
return $this->metadata->getFeature();
}
public function getGroups(): array
{
return $this->metadata->getGroups();
}
public function current(?string $key)
{
return $this->metadata->getCurrent($key);
}
/**
* @return mixed
* @throws InjectionException
*/
public function runStep(Step $step)
{
$step->saveTrace();
if ($this->metaStep instanceof Meta) {
$step->setMetaStep($this->metaStep);
}
$this->steps[] = $step;
$result = null;
$dispatcher = $this->metadata->getService('dispatcher');
$dispatcher->dispatch(new StepEvent($this->test, $step), Events::STEP_BEFORE);
try {
$result = $step->run($this->metadata->getService('modules'));
} catch (ConditionalAssertionFailed $failed) {
$result = $this->test->getTestResultObject();
if (is_null($result)) {
$dispatcher->dispatch(new StepEvent($this->test, $step), Events::STEP_AFTER);
throw $failed;
} else {
$result->addFailure(clone($this->test), $failed, $result->time());
}
} catch (Exception $exception) {
$dispatcher->dispatch(new StepEvent($this->test, $step), Events::STEP_AFTER);
throw $exception;
}
$dispatcher->dispatch(new StepEvent($this->test, $step), Events::STEP_AFTER);
$step->executed = true;
return $result;
}
public function addStep(Step $step): void
{
$this->steps[] = $step;
}
/**
* Returns the steps of this scenario.
*/
public function getSteps(): array
{
return $this->steps;
}
public function getHtml(): string
{
$text = '';
foreach ($this->getSteps() as $step) {
/** @var Step $step */
if ($step->getName() !== 'Comment') {
$text .= $step->getHtml() . '<br/>';
} else {
$text .= trim($step->getHumanizedArguments(), '"') . '<br/>';
}
}
$text = str_replace(['"\'', '\'"'], ["'", "'"], $text);
return "<h3>" . mb_strtoupper('I want to ' . $this->getFeature(), 'utf-8') . "</h3>" . $text;
}
public function getText(): string
{
$text = '';
foreach ($this->getSteps() as $step) {
$text .= $step->getPrefix() . "{$step} \r\n";
}
$text = trim(str_replace(['"\'', '\'"'], ["'", "'"], $text));
return mb_strtoupper('I want to ' . $this->getFeature(), 'utf-8') . "\r\n\r\n" . $text . "\r\n\r\n";
}
public function comment(string $comment): void
{
$this->runStep(new Comment($comment, []));
}
public function skip(string $message = ''): void
{
throw new SkippedTestError($message);
}
public function incomplete(string $message = ''): void
{
throw new IncompleteTestError($message);
}
public function __call(string $method, array $args)
{
// all methods were deprecated and removed from here
trigger_error(sprintf('Codeception: $scenario->%s() has been deprecated and removed. Use annotations to pass scenario params', $method), E_USER_DEPRECATED);
}
public function setMetaStep(?Meta $metaStep): void
{
$this->metaStep = $metaStep;
}
public function getMetaStep(): ?Meta
{
return $this->metaStep;
}
}