-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathFreeBddStyleDSLTest.php
277 lines (232 loc) · 6.87 KB
/
FreeBddStyleDSLTest.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
declare(strict_types=1);
namespace example;
use Exception;
use FunctionalPHP\FantasyLand\Functor;
use PHPUnit\Framework\TestCase;
use Widmogrod\Monad\Free\MonadFree;
use Widmogrod\Monad\Free\Pure;
use Widmogrod\Monad\State;
use function Widmogrod\Functional\curryN;
use function Widmogrod\Functional\push_;
use function Widmogrod\Monad\Free\foldFree;
use function Widmogrod\Monad\Free\liftF;
use function Widmogrod\Useful\matchPatterns;
use const Widmogrod\Monad\State\value;
interface ScenarioF extends Functor
{
}
class Given implements ScenarioF
{
public $desc;
public $state;
public $next;
public function __construct(string $desc, $state, MonadFree $next)
{
$this->desc = $desc;
$this->state = $state;
$this->next = $next;
}
/**
* @inheritdoc
*/
public function map(callable $function): Functor
{
return new self(
$this->desc,
$this->state,
$function($this->next)
);
}
}
class When implements ScenarioF
{
public $action;
public $next;
public function __construct(string $action, MonadFree $next)
{
$this->action = $action;
$this->next = $next;
}
/**
* @inheritdoc
*/
public function map(callable $function): Functor
{
return new self(
$this->action,
$function($this->next)
);
}
}
class Then implements ScenarioF
{
public $assertion;
public $next;
public function __construct(string $assertion, MonadFree $next)
{
$this->assertion = $assertion;
$this->next = $next;
}
/**
* @inheritdoc
*/
public function map(callable $function): Functor
{
return new self(
$this->assertion,
$function($this->next)
);
}
}
function scenario(string $desc, $state): Scenario
{
return new Scenario(given_($desc, $state));
}
function given_(string $desc, $state): MonadFree
{
return liftF(new Given($desc, $state, Pure::of('-given-')));
}
function when_(string $action): MonadFree
{
return liftF(new When($action, Pure::of('-when-')));
}
function then_(string $assertion): MonadFree
{
return liftF(new Then($assertion, Pure::of('-then-')));
}
class Scenario
{
private $free;
public function __construct(MonadFree $free)
{
$this->free = $free;
}
public function When(string $action): self
{
return new self($this->free->bind(function () use ($action) {
return when_($action);
}));
}
public function Then(string $assertion): self
{
return new self($this->free->bind(function () use ($assertion) {
return then_($assertion);
}));
}
public function Run(array $when, array $then)
{
$interpretAction = curryN(3, interpretAction);
$interpretAssertion = curryN(3, interpretAssertion);
$interpretScenario = curryN(3, interpretScenario);
$interpret = $interpretScenario($interpretAction($when), $interpretAssertion($then));
$state = foldFree($interpret, $this->free, value);
$result = State\execState($state, []);
return $result;
}
}
function Given(string $desc, $state): Scenario
{
return new Scenario(given_($desc, $state));
}
const interpretScenario = 'example\interpretScenario';
/**
* interpretScenario :: (a -> b) -> (a -> Bool) -> ScenarioF -> State MonadFree b
*/
function interpretScenario(callable $interpretAction, callable $interpretAssertion, ScenarioF $f)
{
return matchPatterns([
Given::class => function (Given $a): State {
return State::of(function () use ($a) {
return [$a->next, $a->state];
});
},
When::class => function (When $a) use ($interpretAction): State {
return State::of(function ($state) use ($interpretAction, $a) {
$state = $interpretAction($a->action, $state);
return [$a->next, $state];
});
},
Then::class => function (Then $a) use ($interpretAssertion): State {
return State::of(function ($state) use ($interpretAssertion, $a) {
$ok = $interpretAssertion($a->assertion, $state);
assert($ok, $a->assertion);
return [$a->next, $state];
});
},
], $f);
}
const interpretAction = 'example\interpretAction';
/**
* interpretAction :: List (String -> (a -> a)) -> String -> a -> a
*/
function interpretAction(array $patterns, string $s, $state)
{
return matchRegexp(wrapWithState($patterns, $state), $s);
}
const interpretAssertion = 'example\interpretAssertion';
/**
* interpretAssertion :: List (String -> (a -> Bool)) -> String -> a -> Bool
*/
function interpretAssertion($patterns, string $s, $state): bool
{
return matchRegexp(wrapWithState($patterns, $state), $s);
}
function wrapWithState(array $patterns, $state)
{
return array_map(function (callable $fn) use ($state) {
return function () use ($fn, $state) {
$args = push_([$state], func_get_args());
return call_user_func_array($fn, $args);
};
}, $patterns);
}
function matchRegexp(array $patterns, $value = null)
{
return curryN(2, function (array $patterns, $value) {
foreach ($patterns as $pattern => $fn) {
if (false !== preg_match($pattern, $value, $matches)) {
return call_user_func_array($fn, array_slice($matches, 1));
}
}
throw new Exception(sprintf(
'Cannot match "%s" to list of regexp %s',
$value,
implode(', ', array_keys($patterns))
));
})(...func_get_args());
}
/**
* Inspired by https://github.com/politrons/TestDSL
*/
class FreeBddStyleDSLTest extends TestCase
{
public function test_it_should_interpret_bdd_scenario()
{
$state = [
'productsCount' => 0,
'products' => [],
];
$scenario =
Given('Product in cart', $state)
->When("I add product 'coca-cola'")
->When("I add product 'milk'")
->Then("The number of products is '2'");
$result = $scenario->Run([
"/^I add product '(.*)'/" => function ($state, $productName) {
$state['productsCount'] += 1;
$state['products'][] = $productName;
return $state;
},
], [
"/^The number of products is '(\d+)'/" => function ($state, int $expected) {
return $state['productsCount'] === $expected;
},
]);
$this->assertIsArray($result);
$this->assertArrayHasKey('productsCount', $result);
$this->assertArrayHasKey('products', $result);
$this->assertEquals(2, $result['productsCount']);
$this->assertEquals(['coca-cola', 'milk'], $result['products']);
}
}