19declare(strict_types=1);
26use PHPUnit\Framework\TestCase;
32use PHPUnit\Framework\Attributes\Depends;
33use PHPUnit\Framework\MockObject\Stub;
39 $this->assertInstanceOf(Transform::class,
new Transform());
42 #[Depends('testConstruct')]
43 public function testTo(): void
46 $transformed =
new stdClass();
48 $transformation = $this->createStub(Transformation::class);
49 $transformation->method(
'applyTo')->willReturnCallback(
function (
Result $x) use ($transformed):
Result {
50 $this->assertEquals(
'x', $x->
value());
51 return new Ok($transformed);
54 $parse = $transform->to($transformation,
function (Intermediate $x, Closure $cc):
Result {
55 return $cc($x->accept());
58 $end =
new stdClass();
60 $x = $parse(
new Intermediate(
'x'),
function (
Result $x) use ($transformed, $end):
Result {
61 $this->assertEquals([$transformed], $x->
value()->accepted());
65 $this->assertEquals($end, $x->
value());
68 #[Depends('testConstruct')]
69 public function testFailedToTransform(): void
71 $transform =
new Transform();
73 $transformation = $this->createStub(Transformation::class);
74 $transformation->method(
'applyTo')->willReturnCallback(
function (
Result $x):
Result {
75 $this->assertEquals(
'x', $x->
value());
76 return new Error(
'Sorryy.');
79 $parse = $transform->to($transformation,
function (Intermediate $x, Closure $cc): Result {
80 return $cc($x->accept());
83 $end =
new stdClass();
85 $x = $parse(
new Intermediate(
'x'),
function (Result $x) use ($end): Result {
86 $this->assertFalse($x->isOK());
90 $this->assertEquals($end, $x->value());
93 #[Depends('testConstruct')]
94 public function testFailedToParse(): void
96 $transform =
new Transform();
97 $transformCalled =
false;
99 $transformation = $this->createStub(Transformation::class);
100 $transformation->method(
'applyTo')->willReturnCallback(
function (Result $x) use (&$transformCalled): Result {
101 $transformCalled =
true;
105 $parse = $transform->to($transformation,
function (Intermediate $x, Closure $cc): Result {
106 return $cc($x->reject());
109 $end =
new stdClass();
111 $x = $parse(
new Intermediate(
'x'),
function (Result $x) use ($end): Result {
112 $this->assertFalse($x->isOK());
116 $this->assertEquals($end, $x->value());
117 $this->assertFalse($transformCalled);
131 $this->assertTrue($result->isOK());
132 $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.
value()
Get the encapsulated value.