ILIAS  release_7 Revision v7.30-3-g800a261c036
SplitStringTest.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2017 Stefan Hecken <stefan.hecken@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
6use PHPUnit\Framework\TestCase;
7
13class SplitStringTest extends TestCase
14{
15 const STRING_TO_SPLIT = "I am#a test string#for split";
16 protected static $result = array("I am", "a test string", "for split");
17
22
23 protected function setUp() : void
24 {
25 $dataFactory = new \ILIAS\Data\Factory();
26 $language = $this->createMock('\ilLanguage');
27 $this->f = new \ILIAS\Refinery\Factory($dataFactory, $language);
28 $this->split_string = $this->f->string()->splitString("#");
29 }
30
31 protected function tearDown() : void
32 {
33 $this->f = null;
34 $this->split_string = null;
35 }
36
37 public function testTransform()
38 {
39 $arr = $this->split_string->transform(self::STRING_TO_SPLIT);
40 $this->assertEquals(static::$result, $arr);
41 }
42
43 public function testTransformFails()
44 {
45 $raised = false;
46 try {
47 $arr = [];
48 $next_arr = $this->split_string->transform($arr);
49 } catch (InvalidArgumentException $e) {
50 $raised = true;
51 }
52 $this->assertTrue($raised);
53
54 $raised = false;
55 try {
56 $without = 1001;
57 $with = $this->split_string->transform($without);
58 } catch (InvalidArgumentException $e) {
59 $raised = true;
60 }
61 $this->assertTrue($raised);
62
63 $raised = false;
64 try {
65 $std_class = new stdClass();
66 $with = $this->split_string->transform($std_class);
67 } catch (InvalidArgumentException $e) {
68 $raised = true;
69 }
70 $this->assertTrue($raised);
71 }
72
73 public function testInvoke()
74 {
75 $split_string = $this->f->string()->splitString("#");
76 $arr = $split_string(self::STRING_TO_SPLIT);
77 $this->assertEquals(static::$result, $arr);
78 }
79
80 public function testInvokeFails()
81 {
82 $split_string = $this->f->string()->splitString("#");
83
84 $raised = false;
85 try {
86 $arr = [];
87 $next_arr = $split_string($arr);
88 } catch (InvalidArgumentException $e) {
89 $raised = true;
90 }
91 $this->assertTrue($raised);
92
93 $raised = false;
94 try {
95 $number = 1001;
96 $with = $split_string($number);
97 } catch (InvalidArgumentException $e) {
98 $raised = true;
99 }
100 $this->assertTrue($raised);
101
102 $raised = false;
103 try {
104 $std_class = new stdClass();
105 $with = $split_string($std_class);
106 } catch (InvalidArgumentException $e) {
107 $raised = true;
108 }
109 $this->assertTrue($raised);
110 }
111
113 {
114 $factory = new \ILIAS\Data\Factory();
115 $valueObject = $factory->ok(self::STRING_TO_SPLIT);
116
117 $resultObject = $this->split_string->applyTo($valueObject);
118
119 $this->assertEquals(self::$result, $resultObject->value());
120 $this->assertFalse($resultObject->isError());
121 }
122
124 {
125 $factory = new \ILIAS\Data\Factory();
126 $valueObject = $factory->ok(42);
127
128 $resultObject = $this->split_string->applyTo($valueObject);
129
130 $this->assertTrue($resultObject->isError());
131 }
132}
$result
An exception for terminatinating execution or to throw for unit testing.
TestCase for SplitString transformations.
testApplyToWithValidValueReturnsAnOkResult()
testApplyToWithInvalidValueWillLeadToErrorResult()
$factory
Definition: metadata.php:58