ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
TransformTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26use PHPUnit\Framework\TestCase;
30use Closure;
31use stdClass;
32use PHPUnit\Framework\Attributes\Depends;
33use PHPUnit\Framework\MockObject\Stub;
34
35class TransformTest extends TestCase
36{
37 public function testConstruct(): void
38 {
39 $this->assertInstanceOf(Transform::class, new Transform());
40 }
41
42 #[Depends('testConstruct')]
43 public function testTo(): void
44 {
45 $transform = new Transform();
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);
52 });
53
54 $parse = $transform->to($transformation, function (Intermediate $x, Closure $cc): Result {
55 return $cc($x->accept());
56 });
57
58 $end = new stdClass();
59
60 $x = $parse(new Intermediate('x'), function (Result $x) use ($transformed, $end): Result {
61 $this->assertEquals([$transformed], $x->value()->accepted());
62 return new Ok($end);
63 });
64
65 $this->assertEquals($end, $x->value());
66 }
67
68 #[Depends('testConstruct')]
69 public function testFailedToTransform(): void
70 {
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.');
77 });
78
79 $parse = $transform->to($transformation, function (Intermediate $x, Closure $cc): Result {
80 return $cc($x->accept());
81 });
82
83 $end = new stdClass();
84
85 $x = $parse(new Intermediate('x'), function (Result $x) use ($end): Result {
86 $this->assertFalse($x->isOK());
87 return new Ok($end);
88 });
89
90 $this->assertEquals($end, $x->value());
91 }
92
93 #[Depends('testConstruct')]
94 public function testFailedToParse(): void
95 {
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;
102 return $x;
103 });
104
105 $parse = $transform->to($transformation, function (Intermediate $x, Closure $cc): Result {
106 return $cc($x->reject());
107 });
108
109 $end = new stdClass();
110
111 $x = $parse(new Intermediate('x'), function (Result $x) use ($end): Result {
112 $this->assertFalse($x->isOK());
113 return new Ok($end);
114 });
115
116 $this->assertEquals($end, $x->value());
117 $this->assertFalse($transformCalled);
118 }
119
120 public function testAs(): void
121 {
122 $transform = new Transform();
123 $parse = $transform->as('hej', function (Intermediate $x, Closure $cc): Result {
124 return $cc($x->accept());
125 });
126
127 $result = $parse(new Intermediate('lorem'), static function (Result $x): Result {
128 return $x->map(static fn(Intermediate $x): array => $x->accepted());
129 });
130
131 $this->assertTrue($result->isOK());
132 $this->assertEquals(['hej' => 'l'], $result->value());
133 }
134}
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:31
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:29
value()
Get the encapsulated value.
A transformation is a function from one datatype to another.