-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReport.php
More file actions
124 lines (105 loc) · 2.78 KB
/
Copy pathReport.php
File metadata and controls
124 lines (105 loc) · 2.78 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
<?php
namespace TaskChecker\Reporter;
use TaskChecker\Errors\AssertionFailedError;
use TaskChecker\Errors\BaseTestError;
use TaskChecker\Errors\Error;
use TaskChecker\Step\Step;
/**
* Представляет отчет о проверке решения. Отчет состоит из шагов (Step),
* которые могут быть вложены друг в друга.
*/
class Report
{
/**
* @var Step[]
*/
private $steps = [];
/** @var Step */
private $currentStep;
public function check($comment, callable $action)
{
$step = new Step($comment);
$this->executeStep($step, $action);
}
public function executeStep(Step $step, callable $action)
{
$this->startStep($step);
// TODO: use finally
// В отчет записываются только исключения, вызванные ошибками
// при проверке решения
try {
$action($step);
$step->setSuccess();
} catch (BaseTestError $e) {
$step->setFailed($e);
$this->endStep();
throw $e;
} catch (\Exception $e) {
// Закрываем шаг, но не записываем исключение в отчет
$this->endStep();
throw $e;
} catch (\Throwable $e) {
$this->endStep();
throw $e;
}
$this->endStep();
}
private function startStep(Step $step)
{
assert(!$step->isFinalized());
if ($this->currentStep) {
$this->currentStep->addChild($step);
} else {
$this->steps[] = $step;
}
$this->currentStep = $step;
}
private function endStep()
{
assert(!!$this->currentStep);
$this->currentStep = $this->currentStep->getParent();
}
public function getSteps()
{
return $this->steps;
}
/**
* @return bool
*/
public function isSuccessful()
{
foreach ($this->steps as $step) {
if (!$step->isSuccessful()) {
return false;
}
}
return true;
}
/**
* @return bool
*/
public function isFailed()
{
foreach ($this->steps as $step) {
if ($step->isFailed()) {
return true;
}
}
return false;
}
/**
* @return BaseTestError|null
*/
public function getLastError()
{
$steps = $this->steps;
for ($i = count($steps) - 1; $i >= 0; $i--) {
$step = $steps[$i];
$error = $step->getError();
if ($error) {
return $error;
}
}
return null;
}
}