ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
TransformationsCustomTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\Refinery\Factory as Refinery;
22use ILIAS\Data\Factory as DataFactory;
24use PHPUnit\Framework\TestCase;
25
26class TransformationsCustomTest extends TestCase
27{
28 private const TEST_STRING = "Test";
29
31 private ?Refinery $f;
32
33 protected function setUp(): void
34 {
35 $language = $this->createMock(ILIAS\Language\Language::class);
36 $this->f = new Refinery(new DataFactory(), $language);
37
38 $this->custom = $this->f->custom()->transformation(
39 function ($value) {
40 if (!is_string($value)) {
41 throw new InvalidArgumentException("'" . gettype($value) . "' is not a string.");
42 }
43 return $value;
44 }
45 );
46 }
47
48 protected function tearDown(): void
49 {
50 $this->f = null;
51 $this->custom = null;
52 }
53
54 public function testTransform(): void
55 {
56 $result = $this->custom->transform(self::TEST_STRING);
57 $this->assertEquals(self::TEST_STRING, $result);
58 }
59
60 public function testTransformFails(): void
61 {
62 $raised = false;
63 try {
64 $lower_string = $this->custom->transform([]);
65 } catch (InvalidArgumentException $e) {
66 $this->assertEquals("'array' is not a string.", $e->getMessage());
67 $raised = true;
68 }
69 $this->assertTrue($raised);
70
71 $raised = false;
72 try {
73 $lower_string = $this->custom->transform(12345);
74 } catch (InvalidArgumentException $e) {
75 $this->assertEquals("'integer' is not a string.", $e->getMessage());
76 $raised = true;
77 }
78 $this->assertTrue($raised);
79
80 $raised = false;
81 try {
82 $std_class = new stdClass();
83 $lower_string = $this->custom->transform($std_class);
84 } catch (InvalidArgumentException $e) {
85 $this->assertEquals("'object' is not a string.", $e->getMessage());
86 $raised = true;
87 }
88 $this->assertTrue($raised);
89 }
90
91 public function testInvoke(): void
92 {
93 $custom = $this->f->custom()->transformation(
94 function ($value) {
95 if (!is_string($value)) {
96 throw new InvalidArgumentException("'" . gettype($value) . "' is not a string.");
97 }
98 return $value;
99 }
100 );
101
102 $result = $custom(self::TEST_STRING);
103 $this->assertEquals(self::TEST_STRING, $result);
104 }
105
106 public function testInvokeFails(): void
107 {
108 $custom = $this->f->custom()->transformation(
109 function ($value) {
110 if (!is_string($value)) {
111 throw new InvalidArgumentException("'" . gettype($value) . "' is not a string.");
112 }
113 return $value;
114 }
115 );
116
117 $raised = false;
118 try {
119 $lower_string = $custom([]);
120 } catch (InvalidArgumentException $e) {
121 $this->assertEquals("'array' is not a string.", $e->getMessage());
122 $raised = true;
123 }
124 $this->assertTrue($raised);
125
126 $raised = false;
127 try {
128 $lower_string = $custom(12345);
129 } catch (InvalidArgumentException $e) {
130 $this->assertEquals("'integer' is not a string.", $e->getMessage());
131 $raised = true;
132 }
133 $this->assertTrue($raised);
134
135 $raised = false;
136 try {
137 $std_class = new stdClass();
138 $lower_string = $custom($std_class);
139 } catch (InvalidArgumentException $e) {
140 $this->assertEquals("'object' is not a string.", $e->getMessage());
141 $raised = true;
142 }
143 $this->assertTrue($raised);
144 }
145
147 {
148 $factory = new DataFactory();
149 $valueObject = $factory->ok(self::TEST_STRING);
150
151 $resultObject = $this->custom->applyTo($valueObject);
152
153 $this->assertEquals(self::TEST_STRING, $resultObject->value());
154 $this->assertFalse($resultObject->isError());
155 }
156}
Builds data types.
Definition: Factory.php:36
A transformation is a function from one datatype to another.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.