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