19declare(strict_types=1);
26use PHPUnit\Framework\TestCase;
32use PHPUnit\Framework\Attributes\Depends;
38 $this->assertInstanceOf(Transform::class,
new Transform());
41 #[Depends('testConstruct')]
45 $transformed =
new stdClass();
46 $transformation = $this->getMockBuilder(Transformation::class)->getMock();
47 $transformation->method(
'applyTo')->willReturnCallback(
function (
Result $x) use ($transformed):
Result {
48 $this->assertEquals(
'x', $x->
value());
49 return new Ok($transformed);
52 $parse = $transform->to($transformation,
function (
Intermediate $x, Closure $cc):
Result {
53 return $cc($x->accept());
56 $end =
new stdClass();
59 $this->assertEquals([$transformed], $x->
value()->accepted());
63 $this->assertEquals($end, $x->
value());
66 #[Depends('testConstruct')]
70 $transformation = $this->getMockBuilder(Transformation::class)->getMock();
71 $transformation->method(
'applyTo')->willReturnCallback(
function (
Result $x):
Result {
72 $this->assertEquals(
'x', $x->
value());
73 return new Error(
'Sorryy.');
76 $parse = $transform->to($transformation,
function (
Intermediate $x, Closure $cc):
Result {
77 return $cc($x->accept());
80 $end =
new stdClass();
83 $this->assertFalse($x->
isOK());
87 $this->assertEquals($end, $x->
value());
90 #[Depends('testConstruct')]
94 $transformCalled =
false;
95 $transformation = $this->getMockBuilder(Transformation::class)->getMock();
96 $transformation->method(
'applyTo')->willReturnCallback(
function (
Result $x) use (&$transformCalled):
Result {
97 $transformCalled =
true;
101 $parse = $transform->to($transformation,
function (
Intermediate $x, Closure $cc):
Result {
102 return $cc($x->reject());
105 $end =
new stdClass();
108 $this->assertFalse($x->
isOK());
112 $this->assertEquals($end, $x->
value());
113 $this->assertFalse($transformCalled);
127 $this->assertTrue($result->isOK());
128 $this->assertEquals([
'hej' =>
'l'], $result->value());
A result encapsulates a value or an error and simplifies the handling of those.
A result encapsulates a value or an error and simplifies the handling of those.
isOK()
Get to know if the result is ok.
value()
Get the encapsulated value.