4 require_once(
"libs/composer/vendor/autoload.php");
17 $this->f =
new Data\Factory();
28 $this->assertEquals(3.154,
$result->value());
33 $result = $this->f->error(
"Something went wrong");
42 $this->assertTrue($raised);
48 $this->assertTrue(
$result->isOk());
49 $this->assertFalse(
$result->isError());
54 $result = $this->f->error(
"Something went wrong");
55 $this->assertEquals(
"Something went wrong",
$result->error());
69 $this->assertTrue($raised);
74 $result = $this->f->error(
"Something went wrong");
75 $this->assertTrue(
$result->isError());
76 $this->assertFalse(
$result->isOk());
82 $this->assertEquals(3.154,
$result->valueOr(5));
87 $result = $this->f->error(
"Something went wrong");
88 $this->assertEquals(5,
$result->valueOr(5));
95 $new_result =
$result->map(
function ($v) use ($multiplicator) {
96 return $v * $multiplicator;
99 $this->assertInstanceOf(Data\Result::class, $new_result);
100 $this->assertNotEquals(
$result, $new_result);
101 $this->assertEquals(9, $new_result->value());
106 $result = $this->f->error(
"Something went wrong");
108 $new_result =
$result->map(
function ($v) use ($multiplicator) {
109 return $v * $multiplicator;
112 $this->assertEquals(
$result, $new_result);
119 $new_result =
$result->then(
function ($v) use ($multiplicator) {
120 $ret = $this->f->ok(($v * $multiplicator));
124 $this->assertInstanceOf(Data\Result::class, $new_result);
125 $this->assertNotEquals(
$result, $new_result);
126 $this->assertEquals(9, $new_result->value());
132 $new_result =
$result->then(
function ($v) {
136 $this->assertInstanceOf(Data\Result::class, $new_result);
137 $this->assertEquals(
$result, $new_result);
142 $result = $this->f->error(
"Something went wrong");
144 $new_result =
$result->then(
function ($v) use ($multiplicator) {
145 $ret = $this->f->ok(($v * $multiplicator));
149 $this->assertInstanceOf(Data\Result::class, $new_result);
150 $this->assertEquals(
$result, $new_result);
158 $new_result =
$result->then(
function ($v) {
167 $this->assertTrue($raised);
172 $result = $this->f->error(
"Something went wrong");
173 $exception =
"Something else went wrong";
175 $new_result =
$result->except(
function ($v) use ($exception) {
176 $ret = $this->f->error($exception);
180 $this->assertInstanceOf(Data\Result::class, $new_result);
181 $this->assertNotEquals(
$result, $new_result);
182 $this->assertEquals(
"Something else went wrong", $new_result->error());
187 $result = $this->f->error(
"Something went wrong");
188 $exception =
"Something else went wrong";
190 $new_result =
$result->except(
function ($v) {
194 $this->assertInstanceOf(Data\Result::class, $new_result);
195 $this->assertEquals(
$result, $new_result);
201 $exception =
"Something else went wrong";
203 $new_result =
$result->except(
function ($v) use ($exception) {
204 $ret = $this->f->error($exception);
208 $this->assertInstanceOf(Data\Result::class, $new_result);
209 $this->assertEquals(
$result, $new_result);
214 $result = $this->f->error(
"Something went wrong");
217 $new_result =
$result->except(
function ($v) {
218 return "New error text";
226 $this->assertTrue($raised);
Tests working with result object.