ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
NewObjectTransformationTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use Error;
24use Exception;
27use PHPUnit\Framework\TestCase;
28use TypeError;
29
30class NewObjectTransformationTest extends TestCase
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
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 {
131 }
132}
133
135{
136 public function __construct(string $string, int $integer)
137 {
138 throw new Exception();
139 }
140}
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Error.php:32
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:31
ilErrorHandling $error
Definition: class.ilias.php:69