ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
NewObjectTransformationTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 
23 use Error;
24 use Exception;
28 use TypeError;
29 
31 {
35  public function testNewObjectTransformation(): void
36  {
37  $transformation = new NewObjectTransformation(MyClass::class);
38 
39  $object = $transformation->transform(['hello', 42]);
40 
41  $result = $object->myMethod();
42 
43  $this->assertEquals(['hello', 42], $result);
44  }
45 
47  {
48  $this->expectNotToPerformAssertions();
49 
50  $transformation = new NewObjectTransformation(MyClass::class);
51 
52  try {
53  $object = $transformation->transform(['hello', 'world']);
54  } catch (TypeError $exception) {
55  return;
56  }
57 
58  $this->fail();
59  }
60 
64  public function testNewObjectApply(): void
65  {
66  $transformation = new NewObjectTransformation(MyClass::class);
67 
68  $resultObject = $transformation->applyTo(new Ok(['hello', 42]));
69 
70  $object = $resultObject->value();
71 
72  $result = $object->myMethod();
73 
74  $this->assertEquals(['hello', 42], $result);
75  }
76 
78  {
79  $this->expectNotToPerformAssertions();
80 
81  $transformation = new NewObjectTransformation(MyClass::class);
82 
83  try {
84  $resultObject = $transformation->applyTo(new Ok(['hello', 'world']));
85  } catch (Error $error) {
86  return;
87  }
88 
89  $this->fail();
90  }
91 
93  {
94  $transformation = new NewObjectTransformation(MyClassThrowsException::class);
95 
96  $resultObject = $transformation->applyTo(new Ok(['hello', 100]));
97 
98  $this->assertTrue($resultObject->isError());
99  }
100 
102  {
103  $this->expectNotToPerformAssertions();
104 
105  $transformation = new NewObjectTransformation(MyClassThrowsException::class);
106 
107  try {
108  $resultObject = $transformation->transform(['hello', 100]);
109  } catch (Exception $exception) {
110  return;
111  }
112 
113  $this->fail();
114  }
115 }
116 
117 class MyClass
118 {
119  private string $string;
120  private int $integer;
121 
122  public function __construct(string $string, int $integer)
123  {
124  $this->string = $string;
125  $this->integer = $integer;
126  }
127 
128  public function myMethod(): array
129  {
130  return [$this->string, $this->integer];
131  }
132 }
133 
135 {
136  public function __construct(string $string, int $integer)
137  {
138  throw new Exception();
139  }
140 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:16
ilErrorHandling $error
Definition: class.ilias.php:55