3 declare(strict_types=1);
7 require_once(
"libs/composer/vendor/autoload.php");
19 private ?Data\Factory
$f;
21 protected function setUp(): void
23 $this->f =
new Data\Factory();
33 $result = $this->f->ok(3.154);
34 $this->assertEquals(3.154, $result->value());
39 $result = $this->f->error(
"Something went wrong");
48 $this->assertTrue($raised);
53 $result = $this->f->ok(3.154);
54 $this->assertTrue($result->isOk());
55 $this->assertFalse($result->isError());
60 $result = $this->f->error(
"Something went wrong");
61 $this->assertEquals(
"Something went wrong", $result->error());
66 $result = $this->f->ok(3.154);
75 $this->assertTrue($raised);
80 $result = $this->f->error(
"Something went wrong");
81 $this->assertTrue($result->isError());
82 $this->assertFalse($result->isOk());
87 $result = $this->f->ok(3.154);
88 $this->assertEquals(3.154, $result->valueOr(5));
93 $result = $this->f->error(
"Something went wrong");
94 $this->assertEquals(5, $result->valueOr(5));
99 $result = $this->f->ok(3);
101 $new_result = $result->map(
function ($v) use ($multiplicator) {
102 return $v * $multiplicator;
105 $this->assertInstanceOf(Data\Result::class, $new_result);
106 $this->assertNotEquals($result, $new_result);
107 $this->assertEquals(9, $new_result->value());
112 $result = $this->f->error(
"Something went wrong");
114 $new_result = $result->map(
function ($v) use ($multiplicator) {
115 return $v * $multiplicator;
118 $this->assertEquals($result, $new_result);
123 $result = $this->f->ok(3);
125 $new_result = $result->then(
function ($v) use ($multiplicator) {
126 return $this->f->ok(($v * $multiplicator));
129 $this->assertInstanceOf(Data\Result::class, $new_result);
130 $this->assertNotEquals($result, $new_result);
131 $this->assertEquals(9, $new_result->value());
136 $result = $this->f->ok(3);
137 $new_result = $result->then(
function ($v) {
141 $this->assertInstanceOf(Data\Result::class, $new_result);
142 $this->assertEquals($result, $new_result);
147 $result = $this->f->error(
"Something went wrong");
149 $new_result = $result->then(
function ($v) use ($multiplicator) {
150 return $this->f->ok(($v * $multiplicator));
153 $this->assertInstanceOf(Data\Result::class, $new_result);
154 $this->assertEquals($result, $new_result);
159 $result = $this->f->ok(3);
162 $new_result = $result->then(
function ($v) {
171 $this->assertTrue($raised);
176 $result = $this->f->error(
"Something went wrong");
177 $exception =
"Something else went wrong";
179 $new_result = $result->except(
function ($v) use ($exception) {
180 return $this->f->error($exception);
183 $this->assertInstanceOf(Data\Result::class, $new_result);
184 $this->assertNotEquals($result, $new_result);
185 $this->assertEquals(
"Something else went wrong", $new_result->error());
190 $result = $this->f->error(
"Something went wrong");
191 $exception =
"Something else went wrong";
193 $new_result = $result->except(
function ($v) {
197 $this->assertInstanceOf(Data\Result::class, $new_result);
198 $this->assertEquals($result, $new_result);
203 $result = $this->f->ok(3);
204 $exception =
"Something else went wrong";
206 $new_result = $result->except(
function ($v) use ($exception) {
207 return $this->f->error($exception);
210 $this->assertInstanceOf(Data\Result::class, $new_result);
211 $this->assertEquals($result, $new_result);
216 $result = $this->f->error(
"Something went wrong");
219 $new_result = $result->except(
function ($v) {
220 return "New error text";
228 $this->assertTrue($raised);
Tests working with result object.