ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
RecordTransformationTest.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
3
9
16
17require_once('./libs/composer/vendor/autoload.php');
18
20{
25 {
26 $recordTransformation = new RecordTransformation(
27 array(
28 'stringTrafo' => new StringTransformation(),
29 'integerTrafo' => new IntegerTransformation())
30 );
31
32 $result = $recordTransformation->transform(array('stringTrafo' => 'hello', 'integerTrafo' => 1));
33
34 $this->assertEquals(array('stringTrafo' => 'hello', 'integerTrafo' => 1), $result);
35 }
36
38 {
39 $this->expectNotToPerformAssertions();
40
41 try {
42 $recordTransformation = new RecordTransformation(
43 array(
46 );
47 } catch (ConstraintViolationException $exception) {
48 return;
49 }
50
51 $this->fail();
52 }
53
55 {
56 $this->expectNotToPerformAssertions();
57
58 $recordTransformation = new RecordTransformation(
59 array(
60 'integerTrafo' => new IntegerTransformation(),
61 'anotherIntTrafo' => new IntegerTransformation())
62 );
63
64 try {
65 $result = $recordTransformation->transform(array('stringTrafo' => 'hello', 'anotherIntTrafo' => 1));
66 } catch (ConstraintViolationException $exception) {
67 return;
68 }
69
70 $this->fail();
71 }
72
73 public function testInvalidValueKey()
74 {
75 $this->expectNotToPerformAssertions();
76
77 $recordTransformation = new RecordTransformation(
78 array(
79 'stringTrafo' => new StringTransformation(),
80 'integerTrafo' => new IntegerTransformation())
81 );
82
83 try {
84 $result = $recordTransformation->transform(array('stringTrafo' => 'hello', 'floatTrafo' => 1));
85 } catch (ConstraintViolationException $exception) {
86 return;
87 }
88
89 $this->fail();
90 }
91
92 public function testInvalidToManyValues()
93 {
94 $this->expectNotToPerformAssertions();
95
96 $recordTransformation = new RecordTransformation(
97 array(
98 'stringTrafo' => new StringTransformation(),
99 'integerTrafo' => new IntegerTransformation())
100 );
101
102
103 try {
104 $result = $recordTransformation->transform(
105 array(
106 'stringTrafo' => 'hello',
107 'integerTrafo' => 1,
108 'floatTrafo' => 1
109 )
110 );
111 } catch (ConstraintViolationException $exception) {
112 return;
113 }
114
115 $this->fail();
116 }
117
119 {
120 $this->expectNotToPerformAssertions();
121
122 $recordTransformation = new RecordTransformation(
123 array(
124 'stringTrafo' => new StringTransformation(),
125 'integerTrafo' => new IntegerTransformation())
126 );
127
128 try {
129 $result = $recordTransformation->transform(array('someKey' => 'hello', 1));
130 } catch (ConstraintViolationException $exception) {
131 return;
132 }
133
134 $this->fail();
135 }
136
140 public function testApplyIsCorrect()
141 {
142 $recordTransformation = new RecordTransformation(
143 array(
144 'stringTrafo' => new StringTransformation(),
145 'integerTrafo' => new IntegerTransformation())
146 );
147
148 $result = $recordTransformation->applyTo(new Ok(array('stringTrafo' => 'hello', 'integerTrafo' => 1)));
149
150 $this->assertEquals(array('stringTrafo' => 'hello', 'integerTrafo' => 1), $result->value());
151 }
152
157 {
158 $recordTransformation = new RecordTransformation(
159 array(
160 'integerTrafo' => new IntegerTransformation(),
161 'anotherIntTrafo' => new IntegerTransformation())
162 );
163
164 $result = $recordTransformation->applyTo(new Ok(array('stringTrafo' => 'hello', 'anotherIntTrafo' => 1)));
165
166 $this->assertTrue($result->isError());
167 }
168
173 {
174 $recordTransformation = new RecordTransformation(
175 array(
176 'stringTrafo' => new StringTransformation(),
177 'integerTrafo' => new IntegerTransformation())
178 );
179
180 $result = $recordTransformation->applyTo(new Ok(array('stringTrafo' => 'hello', 'floatTrafo' => 1)));
181
182 $this->assertTrue($result->isError());
183 }
184
189 {
190 $recordTransformation = new RecordTransformation(
191 array(
192 'stringTrafo' => new StringTransformation(),
193 'integerTrafo' => new IntegerTransformation())
194 );
195
196 $result = $recordTransformation->applyTo(
197 new Ok(
198 array(
199 'stringTrafo' => 'hello',
200 'integerTrafo' => 1,
201 'floatTrafo' => 1
202 )
203 )
204 );
205
206 $this->assertTrue($result->isError());
207 }
208
213 {
214 $recordTransformation = new RecordTransformation(
215 array(
216 'stringTrafo' => new StringTransformation(),
217 'integerTrafo' => new IntegerTransformation())
218 );
219
220 $result = $recordTransformation->applyTo(new Ok(array('someKey' => 'hello', 1)));
221
222 $this->assertTrue($result->isError());
223 }
224}
$result
An exception for terminatinating execution or to throw for unit testing.
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:14