forked from widmogrod/php-functional
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateMonadTest.php
122 lines (106 loc) · 2.87 KB
/
StateMonadTest.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
<?php
namespace example;
use Widmogrod\Monad\State as S;
use Widmogrod\Monad\Maybe;
/**
* Caching is an example state that you could have in your application.
*/
interface Cacher
{
/**
* @param string $key
*
* @return Maybe\Maybe
*/
public function get($key);
/**
* @param string $key
* @param mixed $value
*
* @return self
*/
public function put($key, $value);
}
class InMemoryCache implements Cacher
{
private $data = [];
public function __construct(array $data)
{
$this->data = $data;
}
public function get($key)
{
return array_key_exists($key, $this->data)
? Maybe\just($this->data[$key])
: Maybe\nothing();
}
public function put($key, $value)
{
return new self(array_merge(
$this->data,
[$key => $value]
));
}
}
// checkRelatedInCache :: String -> State (Maybe a, s)
function checkRelatedInCache($productName)
{
return S::of(function (Cacher $cache) use ($productName) {
return [$cache->get($productName), $cache];
});
}
// searchRelated :: String -> State (s, [])
function searchRelated($productName)
{
// TODO: 1. try use doM
// TODO: 2. try implement getOrElse
return checkRelatedInCache($productName)
->bind(function (Maybe\Maybe $products) use ($productName) {
switch (get_class($products)) {
case Maybe\Just::class:
return S\value($products->extract());
case Maybe\Nothing::class:
return retrieveRelated($productName);
}
});
}
// retrieveRelated :: String -> State (Cacher, [])
function retrieveRelated($productName)
{
return S::of(function (Cacher $cache) use ($productName) {
// do some database work
$products = ['iPhone 5', 'iPhone 6s'];
return [$products, $cache->put($productName, $products)];
});
}
class StateMonadTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideData
*/
public function test_demonstrate_state_monad($expectedProducts)
{
$initialState = new InMemoryCache([]);
list($result1, $outputState1) = S\runState(
searchRelated('asia'),
$initialState
);
$this->assertEquals($expectedProducts, $result1);
list($result2, $outputState2) = S\runState(
searchRelated('asia'),
$outputState1
);
$this->assertEquals($expectedProducts, $result2);
// After second computation, state shouldn't be modified
// Because we have item already in cache
$this->assertSame($outputState1, $outputState2);
}
public function provideData()
{
return [
'default' => [
'$expectedProducts' => ['iPhone 5', 'iPhone 6s'],
],
];
}
}