ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
6 
13 {
14  const TEST_STRING = "Test";
15 
16  protected function setUp()
17  {
18  $this->f = new Transformation\Factory();
19  $this->custom = $this->f->custom(
20  function ($value) {
21  if (!is_string($value)) {
22  throw new InvalidArgumentException("'" . gettype($value) . "' is not a string.");
23  }
24  return $value;
25  }
26  );
27  }
28 
29  protected function tearDown()
30  {
31  $this->f = null;
32  $this->custom = null;
33  }
34 
35  public function testTransform()
36  {
37  $result = $this->custom->transform(self::TEST_STRING);
38  $this->assertEquals(self::TEST_STRING, $result);
39  }
40 
41  public function testTransformFails()
42  {
43  $raised = false;
44  try {
45  $lower_string = $this->custom->transform(array());
46  } catch (InvalidArgumentException $e) {
47  $this->assertEquals("'array' is not a string.", $e->getMessage());
48  $raised = true;
49  }
50  $this->assertTrue($raised);
51 
52  $raised = false;
53  try {
54  $lower_string = $this->custom->transform(12345);
55  } catch (InvalidArgumentException $e) {
56  $this->assertEquals("'integer' is not a string.", $e->getMessage());
57  $raised = true;
58  }
59  $this->assertTrue($raised);
60 
61  $raised = false;
62  try {
63  $std_class = new stdClass();
64  $lower_string = $this->custom->transform($std_class);
65  } catch (InvalidArgumentException $e) {
66  $this->assertEquals("'object' is not a string.", $e->getMessage());
67  $raised = true;
68  }
69  $this->assertTrue($raised);
70  }
71 
72  public function testInvoke()
73  {
74  $custom = $this->f->custom(
75  function ($value) {
76  if (!is_string($value)) {
77  throw new InvalidArgumentException("'" . gettype($value) . "' is not a string.");
78  }
79  return $value;
80  }
81  );
82 
83  $result = $custom(self::TEST_STRING);
84  $this->assertEquals(self::TEST_STRING, $result);
85  }
86 
87  public function testInvokeFails()
88  {
89  $custom = $this->f->custom(
90  function ($value) {
91  if (!is_string($value)) {
92  throw new InvalidArgumentException("'" . gettype($value) . "' is not a string.");
93  }
94  return $value;
95  }
96  );
97 
98  $raised = false;
99  try {
100  $lower_string = $custom(array());
101  } catch (InvalidArgumentException $e) {
102  $this->assertEquals("'array' is not a string.", $e->getMessage());
103  $raised = true;
104  }
105  $this->assertTrue($raised);
106 
107  $raised = false;
108  try {
109  $lower_string = $custom(12345);
110  } catch (InvalidArgumentException $e) {
111  $this->assertEquals("'integer' is not a string.", $e->getMessage());
112  $raised = true;
113  }
114  $this->assertTrue($raised);
115 
116  $raised = false;
117  try {
118  $std_class = new stdClass();
119  $lower_string = $custom($std_class);
120  } catch (InvalidArgumentException $e) {
121  $this->assertEquals("'object' is not a string.", $e->getMessage());
122  $raised = true;
123  }
124  $this->assertTrue($raised);
125  }
126 }
TestCase for Custom transformations.
$result