forked from widmogrod/php-functional
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComplexErrorDrivenDevelopmentTest.php
143 lines (125 loc) · 3.4 KB
/
ComplexErrorDrivenDevelopmentTest.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
<?php
require_once 'vendor/autoload.php';
use Widmogrod\Monad\Either as E;
use Widmogrod\Functional as f;
function validateName(array $request)
{
return $request['name'] === ''
? E\Left::of('Request name is empty')
: E\Right::of($request);
}
function validateEmail(array $request)
{
return $request['email'] === ''
? E\Left::of('Request e-mail is empty')
: E\Right::of($request);
}
function validateNameLength(array $request)
{
return strlen($request['name']) > 30
? E\Left::of('Request name is to long')
: E\Right::of($request);
}
function validateInput(array $request)
{
return E\Right::of($request)
->bind('validateName')
->bind('validateEmail')
->bind('validateNameLength');
}
function canonizeEmail(array $request)
{
$request['email'] = strtolower(trim($request['email']));
return $request;
}
function updateDatabase(array $request)
{
// may throw exception
}
function updateDatabaseStep(array $request)
{
return call_user_func(E\tryCatch(
f\tee('updateDatabase'),
function (\Exception $e) {
return $e->getMessage();
}
), $request);
}
// sendMessage :: Either a b -> Either c d
function sendMessage(E\Either $either)
{
return E\doubleMap('returnFailure', 'returnMessage', $either);
}
function returnMessage(array $request)
{
return [
'status' => 200,
];
}
function returnFailure($data)
{
return [
'error' => (string) $data,
];
}
function handleRequest(array $request)
{
return call_user_func(f\pipeline(
'validateInput',
f\map('canonizeEmail'),
f\bind('updateDatabaseStep'),
'sendMessage'
), $request);
}
class ComplexErrorDrivenDevelopmentTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideData
*/
public function test_it_should_be_prepared_of_errors(array $request, $isError, $expected)
{
$result = handleRequest($request);
$this->assertInstanceOf(
$isError ? E\Left::class : E\Right::class,
$result
);
$this->assertEquals($expected, f\valueOf($result));
}
public function provideData()
{
return [
'success case' => [
'$request' => [
'name' => 'Jone Doe',
'email' => '[email protected]'
],
'$isError' => false,
'$expected' => ['status' => 200],
],
'username to short' => [
'$request' => [
'name' => '',
'email' => '[email protected]'
],
'$isError' => true,
'$expected' => ['error' => 'Request name is empty'],
],
'username to long' => [
'$request' => [
'name' => 'asd asdasdlaks askl djalskd jalskdjaslkdjasldjadsa asd',
'email' => '[email protected]'
],
'$isError' => true,
'$expected' => ['error' => 'Request name is to long'],
],
'email empty' => [
'$request' => [
'name' => 'Jone Doe',
'email' => ''
],
'$isError' => true,
'$expected' => ['error' => 'Request e-mail is empty'],
],
];
}
}