ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ResultTest.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 2017 Stefan Hecken <stefan.hecken@concepts-and-training.de> Extended GPL, see docs/LICENSE */
3 
4 require_once("libs/composer/vendor/autoload.php");
5 
6 use ILIAS\Data;
7 
14 {
15  protected function setUp()
16  {
17  $this->f = new Data\Factory();
18  }
19 
20  protected function tearDown()
21  {
22  $this->f = null;
23  }
24 
25  public function testValue()
26  {
27  $result = $this->f->ok(3.154);
28  $this->assertEquals(3.154, $result->value());
29  }
30 
31  public function testNoValue()
32  {
33  $result = $this->f->error("Something went wrong");
34 
35  try {
36  $result->value();
37  $raised = false;
38  } catch (\Exception $e) {
39  $raised = true;
40  }
41 
42  $this->assertTrue($raised);
43  }
44 
45  public function testIsOk()
46  {
47  $result = $this->f->ok(3.154);
48  $this->assertTrue($result->isOk());
49  $this->assertFalse($result->isError());
50  }
51 
52  public function testError()
53  {
54  $result = $this->f->error("Something went wrong");
55  $this->assertEquals("Something went wrong", $result->error());
56  }
57 
58  public function testNoError()
59  {
60  $result = $this->f->ok(3.154);
61 
62  try {
63  $result->error();
64  $raised = false;
65  } catch (\LogicException $e) {
66  $raised = true;
67  }
68 
69  $this->assertTrue($raised);
70  }
71 
72  public function testIsError()
73  {
74  $result = $this->f->error("Something went wrong");
75  $this->assertTrue($result->isError());
76  $this->assertFalse($result->isOk());
77  }
78 
79  public function testValueOr()
80  {
81  $result = $this->f->ok(3.154);
82  $this->assertEquals(3.154, $result->valueOr(5));
83  }
84 
85  public function testValueOrDefault()
86  {
87  $result = $this->f->error("Something went wrong");
88  $this->assertEquals(5, $result->valueOr(5));
89  }
90 
91  public function testMapOk()
92  {
93  $result = $this->f->ok(3);
94  $multiplicator = 3;
95  $new_result = $result->map(function ($v) use ($multiplicator) {
96  return $v * $multiplicator;
97  });
98 
99  $this->assertInstanceOf(Data\Result::class, $new_result);
100  $this->assertNotEquals($result, $new_result);
101  $this->assertEquals(9, $new_result->value());
102  }
103 
104  public function testMapError()
105  {
106  $result = $this->f->error("Something went wrong");
107  $multiplicator = 3;
108  $new_result = $result->map(function ($v) use ($multiplicator) {
109  return $v * $multiplicator;
110  });
111 
112  $this->assertEquals($result, $new_result);
113  }
114 
115  public function testThenOk()
116  {
117  $result = $this->f->ok(3);
118  $multiplicator = 3;
119  $new_result = $result->then(function ($v) use ($multiplicator) {
120  $ret = $this->f->ok(($v * $multiplicator));
121  return $ret;
122  });
123 
124  $this->assertInstanceOf(Data\Result::class, $new_result);
125  $this->assertNotEquals($result, $new_result);
126  $this->assertEquals(9, $new_result->value());
127  }
128 
129  public function testThenCallableNull()
130  {
131  $result = $this->f->ok(3);
132  $new_result = $result->then(function ($v) {
133  return null;
134  });
135 
136  $this->assertInstanceOf(Data\Result::class, $new_result);
137  $this->assertEquals($result, $new_result);
138  }
139 
140  public function testThenError()
141  {
142  $result = $this->f->error("Something went wrong");
143  $multiplicator = 3;
144  $new_result = $result->then(function ($v) use ($multiplicator) {
145  $ret = $this->f->ok(($v * $multiplicator));
146  return $ret;
147  });
148 
149  $this->assertInstanceOf(Data\Result::class, $new_result);
150  $this->assertEquals($result, $new_result);
151  }
152 
153  public function testThenNoResult()
154  {
155  $result = $this->f->ok(3);
156 
157  try {
158  $new_result = $result->then(function ($v) {
159  return 4;
160  });
161 
162  $raised = false;
163  } catch (\UnexpectedValueException $e) {
164  $raised = true;
165  }
166 
167  $this->assertTrue($raised);
168  }
169 
170  public function testExceptError()
171  {
172  $result = $this->f->error("Something went wrong");
173  $exception = "Something else went wrong";
174 
175  $new_result = $result->except(function ($v) use ($exception) {
176  $ret = $this->f->error($exception);
177  return $ret;
178  });
179 
180  $this->assertInstanceOf(Data\Result::class, $new_result);
181  $this->assertNotEquals($result, $new_result);
182  $this->assertEquals("Something else went wrong", $new_result->error());
183  }
184 
185  public function testExceptCallableNull()
186  {
187  $result = $this->f->error("Something went wrong");
188  $exception = "Something else went wrong";
189 
190  $new_result = $result->except(function ($v) {
191  return null;
192  });
193 
194  $this->assertInstanceOf(Data\Result::class, $new_result);
195  $this->assertEquals($result, $new_result);
196  }
197 
198  public function testExceptOk()
199  {
200  $result = $this->f->ok(3);
201  $exception = "Something else went wrong";
202 
203  $new_result = $result->except(function ($v) use ($exception) {
204  $ret = $this->f->error($exception);
205  return $ret;
206  });
207 
208  $this->assertInstanceOf(Data\Result::class, $new_result);
209  $this->assertEquals($result, $new_result);
210  }
211 
212  public function testExceptNoResult()
213  {
214  $result = $this->f->error("Something went wrong");
215 
216  try {
217  $new_result = $result->except(function ($v) {
218  return "New error text";
219  });
220 
221  $raised = false;
222  } catch (\UnexpectedValueException $e) {
223  $raised = true;
224  }
225 
226  $this->assertTrue($raised);
227  }
228 }
$result
testValueOrDefault()
Definition: ResultTest.php:85
testThenCallableNull()
Definition: ResultTest.php:129
testExceptCallableNull()
Definition: ResultTest.php:185
testExceptNoResult()
Definition: ResultTest.php:212
Tests working with result object.
Definition: ResultTest.php:13
$ret
Definition: parser.php:6
testThenNoResult()
Definition: ResultTest.php:153