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