ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
TransformTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
30 use Closure;
31 use stdClass;
33 
34 class TransformTest extends TestCase
35 {
36  public function testConstruct(): void
37  {
38  $this->assertInstanceOf(Transform::class, new Transform());
39  }
40 
41  #[Depends('testConstruct')]
42  public function testTo(): void
43  {
44  $transform = new Transform();
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);
50  });
51 
52  $parse = $transform->to($transformation, function (Intermediate $x, Closure $cc): Result {
53  return $cc($x->accept());
54  });
55 
56  $end = new stdClass();
57 
58  $x = $parse(new Intermediate('x'), function (Result $x) use ($transformed, $end): Result {
59  $this->assertEquals([$transformed], $x->value()->accepted());
60  return new Ok($end);
61  });
62 
63  $this->assertEquals($end, $x->value());
64  }
65 
66  #[Depends('testConstruct')]
67  public function testFailedToTransform(): void
68  {
69  $transform = new Transform();
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.');
74  });
75 
76  $parse = $transform->to($transformation, function (Intermediate $x, Closure $cc): Result {
77  return $cc($x->accept());
78  });
79 
80  $end = new stdClass();
81 
82  $x = $parse(new Intermediate('x'), function (Result $x) use ($end): Result {
83  $this->assertFalse($x->isOK());
84  return new Ok($end);
85  });
86 
87  $this->assertEquals($end, $x->value());
88  }
89 
90  #[Depends('testConstruct')]
91  public function testFailedToParse(): void
92  {
93  $transform = new Transform();
94  $transformCalled = false;
95  $transformation = $this->getMockBuilder(Transformation::class)->getMock();
96  $transformation->method('applyTo')->willReturnCallback(function (Result $x) use (&$transformCalled): Result {
97  $transformCalled = true;
98  return $x;
99  });
100 
101  $parse = $transform->to($transformation, function (Intermediate $x, Closure $cc): Result {
102  return $cc($x->reject());
103  });
104 
105  $end = new stdClass();
106 
107  $x = $parse(new Intermediate('x'), function (Result $x) use ($end): Result {
108  $this->assertFalse($x->isOK());
109  return new Ok($end);
110  });
111 
112  $this->assertEquals($end, $x->value());
113  $this->assertFalse($transformCalled);
114  }
115 
116  public function testAs(): void
117  {
118  $transform = new Transform();
119  $parse = $transform->as('hej', function (Intermediate $x, Closure $cc): Result {
120  return $cc($x->accept());
121  });
122 
123  $result = $parse(new Intermediate('lorem'), static function (Result $x): Result {
124  return $x->map(static fn(Intermediate $x): array => $x->accepted());
125  });
126 
127  $this->assertTrue($result->isOK());
128  $this->assertEquals(['hej' => 'l'], $result->value());
129  }
130 }
isOK()
Get to know if the result is ok.
value()
Get the encapsulated value.
map(callable $f)
Create a new result where the contained value is modified with $f.
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:30