1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace spec\hanneskod\readmetester\Runner; |
6
|
|
|
|
7
|
|
|
use hanneskod\readmetester\Runner\RunnerInterface; |
8
|
|
|
use hanneskod\readmetester\Runner\ErrorOutcome; |
9
|
|
|
use hanneskod\readmetester\Runner\OutputOutcome; |
10
|
|
|
use hanneskod\readmetester\Runner\VoidOutcome; |
11
|
|
|
use hanneskod\readmetester\Example\ArrayExampleStore; |
12
|
|
|
use hanneskod\readmetester\Example\ExampleObj; |
13
|
|
|
use hanneskod\readmetester\Utils\CodeBlock; |
14
|
|
|
use hanneskod\readmetester\Utils\NameObj; |
15
|
|
|
|
16
|
|
|
trait RunnerSpecTrait |
17
|
|
|
{ |
18
|
|
|
function it_is_a_runner() |
19
|
|
|
{ |
20
|
|
|
$this->shouldHaveType(RunnerInterface::class); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
function an_example(string $code): ExampleObj |
24
|
|
|
{ |
25
|
|
|
return new ExampleObj( |
26
|
|
|
new NameObj('', ''), |
27
|
|
|
new CodeBlock($code) |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
function it_returns_void_on_no_outcome() |
32
|
|
|
{ |
33
|
|
|
$this->run(new ArrayExampleStore([$this->an_example('$a = 1 + 2;')])) |
34
|
|
|
->shouldReturnOutcomeInstancesOf([VoidOutcome::class]); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
function it_returns_output() |
38
|
|
|
{ |
39
|
|
|
$this->run(new ArrayExampleStore([$this->an_example('echo "foo";')])) |
40
|
|
|
->shouldReturnOutcomeInstancesOf([OutputOutcome::class]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
function it_returns_error_on_exception() |
44
|
|
|
{ |
45
|
|
|
$this->run(new ArrayExampleStore([$this->an_example('throw new Exception;')])) |
46
|
|
|
->shouldReturnOutcomeInstancesOf([ErrorOutcome::class]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
function it_returns_error_on_trigger_error() |
50
|
|
|
{ |
51
|
|
|
$this->run(new ArrayExampleStore([$this->an_example('trigger_error("ERROR");')])) |
52
|
|
|
->shouldReturnOutcomeInstancesOf([ErrorOutcome::class]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
function it_returns_error_on_fatal_error() |
56
|
|
|
{ |
57
|
|
|
$this->run(new ArrayExampleStore([$this->an_example('this_function_does_not_exist();')])) |
58
|
|
|
->shouldReturnOutcomeInstancesOf([ErrorOutcome::class]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
function it_can_be_namespaced() |
62
|
|
|
{ |
63
|
|
|
$this->run(new ArrayExampleStore([$this->an_example('namespace test;')])) |
64
|
|
|
->shouldReturnOutcomeInstancesOf([VoidOutcome::class]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
function it_can_be_bootstraped() |
68
|
|
|
{ |
69
|
|
|
$this->setBootstrap(__FILE__); |
70
|
|
|
|
71
|
|
|
$example = $this->an_example('new \spec\hanneskod\readmetester\Runner\ClassLoadedFromBootstrap;'); |
72
|
|
|
|
73
|
|
|
$this->run(new ArrayExampleStore([$example])) |
74
|
|
|
->shouldReturnOutcomeInstancesOf([VoidOutcome::class]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
function getMatchers(): array |
78
|
|
|
{ |
79
|
|
|
return [ |
80
|
|
|
'returnOutcomeInstancesOf' => function (iterable $outcomes, array $expectedClassnames) { |
81
|
|
|
if ($outcomes instanceof \Traversable) { |
82
|
|
|
$outcomes = iterator_to_array($outcomes, false); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$classnames = array_map( |
86
|
|
|
fn($outcome) => $outcome::class, |
87
|
|
|
$outcomes |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
if ($classnames == $expectedClassnames) { |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
throw new \Exception( |
95
|
|
|
sprintf( |
96
|
|
|
'Expected outcome instances [%s], found [%s]', |
97
|
|
|
implode(', ', $expectedClassnames), |
98
|
|
|
implode(', ', $classnames), |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
}, |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// phpcs:disable |
107
|
|
|
|
108
|
|
|
class ClassLoadedFromBootstrap |
109
|
|
|
{ |
110
|
|
|
} |
111
|
|
|
|