ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
TransformationsCustomTest.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 
7 
14 {
15  const TEST_STRING = "Test";
16 
20  private $custom;
21 
25  private $f;
26 
27  protected function setUp() : void
28  {
29  $language = $this->createMock(\ilLanguage::class);
30  $this->f = new Factory(new ILIAS\Data\Factory(), $language);
31 
32  $this->custom = $this->f->custom()->transformation(
33  function ($value) {
34  if (!is_string($value)) {
35  throw new InvalidArgumentException("'" . gettype($value) . "' is not a string.");
36  }
37  return $value;
38  }
39  );
40  }
41 
42  protected function tearDown() : void
43  {
44  $this->f = null;
45  $this->custom = null;
46  }
47 
48  public function testTransform()
49  {
50  $result = $this->custom->transform(self::TEST_STRING);
51  $this->assertEquals(self::TEST_STRING, $result);
52  }
53 
54  public function testTransformFails()
55  {
56  $raised = false;
57  try {
58  $lower_string = $this->custom->transform(array());
59  } catch (InvalidArgumentException $e) {
60  $this->assertEquals("'array' is not a string.", $e->getMessage());
61  $raised = true;
62  }
63  $this->assertTrue($raised);
64 
65  $raised = false;
66  try {
67  $lower_string = $this->custom->transform(12345);
68  } catch (InvalidArgumentException $e) {
69  $this->assertEquals("'integer' is not a string.", $e->getMessage());
70  $raised = true;
71  }
72  $this->assertTrue($raised);
73 
74  $raised = false;
75  try {
76  $std_class = new stdClass();
77  $lower_string = $this->custom->transform($std_class);
78  } catch (InvalidArgumentException $e) {
79  $this->assertEquals("'object' is not a string.", $e->getMessage());
80  $raised = true;
81  }
82  $this->assertTrue($raised);
83  }
84 
85  public function testInvoke()
86  {
87  $custom = $this->f->custom()->transformation(
88  function ($value) {
89  if (!is_string($value)) {
90  throw new InvalidArgumentException("'" . gettype($value) . "' is not a string.");
91  }
92  return $value;
93  }
94  );
95 
96  $result = $custom(self::TEST_STRING);
97  $this->assertEquals(self::TEST_STRING, $result);
98  }
99 
100  public function testInvokeFails()
101  {
102  $custom = $this->f->custom()->transformation(
103  function ($value) {
104  if (!is_string($value)) {
105  throw new InvalidArgumentException("'" . gettype($value) . "' is not a string.");
106  }
107  return $value;
108  }
109  );
110 
111  $raised = false;
112  try {
113  $lower_string = $custom(array());
114  } catch (InvalidArgumentException $e) {
115  $this->assertEquals("'array' is not a string.", $e->getMessage());
116  $raised = true;
117  }
118  $this->assertTrue($raised);
119 
120  $raised = false;
121  try {
122  $lower_string = $custom(12345);
123  } catch (InvalidArgumentException $e) {
124  $this->assertEquals("'integer' is not a string.", $e->getMessage());
125  $raised = true;
126  }
127  $this->assertTrue($raised);
128 
129  $raised = false;
130  try {
131  $std_class = new stdClass();
132  $lower_string = $custom($std_class);
133  } catch (InvalidArgumentException $e) {
134  $this->assertEquals("'object' is not a string.", $e->getMessage());
135  $raised = true;
136  }
137  $this->assertTrue($raised);
138  }
139 
141  {
142  $factory = new \ILIAS\Data\Factory();
143  $valueObject = $factory->ok(self::TEST_STRING);
144 
145  $resultObject = $this->custom->applyTo($valueObject);
146 
147  $this->assertEquals(self::TEST_STRING, $resultObject->value());
148  $this->assertFalse($resultObject->isError());
149  }
150 }
TestCase for Custom transformations.
$result
Class ChatMainBarProvider .
$factory
Definition: metadata.php:58