19 declare(strict_types=1);
21 require_once(
"vendor/composer/vendor/autoload.php");
33 private ?Data\Factory
$f;
35 protected function setUp(): void
37 $this->f =
new Data\Factory();
47 $result = $this->f->ok(3.154);
48 $this->assertEquals(3.154, $result->value());
53 $result = $this->f->error(
"Something went wrong");
62 $this->assertTrue($raised);
67 $result = $this->f->ok(3.154);
68 $this->assertTrue($result->isOk());
69 $this->assertFalse($result->isError());
74 $result = $this->f->error(
"Something went wrong");
75 $this->assertEquals(
"Something went wrong", $result->error());
80 $result = $this->f->ok(3.154);
89 $this->assertTrue($raised);
94 $result = $this->f->error(
"Something went wrong");
95 $this->assertTrue($result->isError());
96 $this->assertFalse($result->isOk());
101 $result = $this->f->ok(3.154);
102 $this->assertEquals(3.154, $result->valueOr(5));
107 $result = $this->f->error(
"Something went wrong");
108 $this->assertEquals(5, $result->valueOr(5));
113 $result = $this->f->ok(3);
115 $new_result = $result->map(
function ($v) use ($multiplicator) {
116 return $v * $multiplicator;
119 $this->assertInstanceOf(
Data\Result::class, $new_result);
120 $this->assertNotEquals($result, $new_result);
121 $this->assertEquals(9, $new_result->value());
126 $result = $this->f->error(
"Something went wrong");
128 $new_result = $result->map(
function ($v) use ($multiplicator) {
129 return $v * $multiplicator;
132 $this->assertEquals($result, $new_result);
137 $result = $this->f->ok(3);
139 $new_result = $result->then(
function ($v) use ($multiplicator) {
140 return $this->f->ok(($v * $multiplicator));
143 $this->assertInstanceOf(
Data\Result::class, $new_result);
144 $this->assertNotEquals($result, $new_result);
145 $this->assertEquals(9, $new_result->value());
150 $result = $this->f->ok(3);
151 $new_result = $result->then(
function ($v) {
155 $this->assertInstanceOf(
Data\Result::class, $new_result);
156 $this->assertEquals($result, $new_result);
161 $result = $this->f->error(
"Something went wrong");
163 $new_result = $result->then(
function ($v) use ($multiplicator) {
164 return $this->f->ok(($v * $multiplicator));
167 $this->assertInstanceOf(
Data\Result::class, $new_result);
168 $this->assertEquals($result, $new_result);
173 $result = $this->f->ok(3);
176 $new_result = $result->then(
function ($v) {
185 $this->assertTrue($raised);
190 $result = $this->f->error(
"Something went wrong");
191 $exception =
"Something else went wrong";
193 $new_result = $result->except(
function ($v) use ($exception) {
194 return $this->f->error($exception);
197 $this->assertInstanceOf(
Data\Result::class, $new_result);
198 $this->assertNotEquals($result, $new_result);
199 $this->assertEquals(
"Something else went wrong", $new_result->error());
204 $result = $this->f->error(
"Something went wrong");
205 $exception =
"Something else went wrong";
207 $new_result = $result->except(
function ($v) {
211 $this->assertInstanceOf(
Data\Result::class, $new_result);
212 $this->assertEquals($result, $new_result);
217 $result = $this->f->ok(3);
218 $exception =
"Something else went wrong";
220 $new_result = $result->except(
function ($v) use ($exception) {
221 return $this->f->error($exception);
224 $this->assertInstanceOf(
Data\Result::class, $new_result);
225 $this->assertEquals($result, $new_result);
230 $result = $this->f->error(
"Something went wrong");
233 $new_result = $result->except(
function ($v) {
234 return "New error text";
242 $this->assertTrue($raised);
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Tests working with result object.