-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.php
41 lines (23 loc) · 1.24 KB
/
hooks.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
<?php
use Dredd\Hooks;
$stash = [];
Hooks::after("Login/Register > /api/v1/login > Login to the site > 200", function(&$transaction) use (&$stash) {
$stash["token"] = json_decode($transaction->real->body);
});
Hooks::beforeEach(function(&$transaction) use (&$stash) {
if ($stash["token"]) {
$transaction->request->headers->{'Authorization'} = "Bearer {$stash['token']}";
}
});
Hooks::after("Recipes > /api/v1/recipe > Create a new recipe > 201", function(&$transaction) use (&$stash) {
$responseBody = json_decode($transaction->real->body);
$stash["recipeId"] = $responseBody->recipe->id;
});
Hooks::before("Recipes > /api/v1/recipe/{recipe} > Update a recipe > 200", function(&$transaction) use (&$stash) {
$transaction->request->uri = preg_replace('/1$/', $stash["recipeId"], $transaction->request->uri);
$transaction->fullPath = preg_replace('/1$/', $stash["recipeId"], $transaction->fullPath);
});
Hooks::before("Recipes > /api/v1/recipe/{recipe} > Delete a recipe > 204", function(&$transaction) use (&$stash) {
$transaction->request->uri = preg_replace('/1$/', $stash["recipeId"], $transaction->request->uri);
$transaction->fullPath = preg_replace('/1$/', $stash["recipeId"], $transaction->fullPath);
});