ILIAS  release_7 Revision v7.30-3-g800a261c036
LevenshteinTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26use ilLanguage;
27use InvalidArgumentException;
28
30{
34 private $group;
35
39 private $factory;
40
44 private $test_multibyte_string = "你好";
45
49 private $test_emoji = "😮‍💨";
50
51 public function setUp(): void
52 {
53 $this->factory = new Factory();
54 $language = $this->getMockBuilder(ilLanguage::class)
55 ->disableOriginalConstructor()
56 ->getMock();
57 $this->group = new Group($this->factory, $language);
58 }
59
60 // Code paths
61 public function testSizeReturn()
62 {
63 $transformation = $this->group->levenshtein()->standard("book", 3);
64
65 $this->assertEquals(-1.0, $transformation->transform("bookshelf"));
66 }
67
68 public function testExceedMaximumDistance()
69 {
70 $transformation = $this->group->levenshtein()->standard("book", 1);
71
72 $this->assertEquals(-1.0, $transformation->transform("back"));
73 }
74
75 public function testSuccessfulReturn()
76 {
77 $transformation = $this->group->levenshtein()->standard("book", 1);
78
79 $this->assertEquals(0.0, $transformation->transform("book"));
80 }
81
82 public function testNoMaximumDistance()
83 {
84 $transformation = $this->group->levenshtein()->standard("book", 0);
85
86 $this->assertEquals(2.0, $transformation->transform("back"));
87 }
88
89 public function testException()
90 {
91 $transformation = $this->group->levenshtein()->standard("book", 0);
92 $this->expectException(InvalidArgumentException::class);
93
94 $this->assertEquals(2.0, $transformation->transform(496));
95 }
96
98 {
99 $transformation = $this->group->levenshtein()->standard("Juni", 2);
100
101 $this->assertEquals(-1, $transformation->transform("Januar"));
102 }
103
104 // Numerical
105 public function testCustomCostsMixed()
106 {
107 $transformation = $this->group->levenshtein()->custom("back", 20, 2.0, 1.0, 1.5);
108
109 $this->assertEquals(12, $transformation->transform("bookshelf"));
110 }
111
112 public function testCustomCostsInsert()
113 {
114 $transformation = $this->group->levenshtein()->custom("book", 5, 2.0, 1.0, 1.5);
115
116 $this->assertEquals(2, $transformation->transform("books"));
117 }
118
119 public function testCustomCostsDeletion()
120 {
121 $transformation = $this->group->levenshtein()->custom("bookshelf", 10, 2.0, 1.0, 1.5);
122
123 $this->assertEquals(7.5, $transformation->transform("book"));
124 }
125
127 {
128 $transformation = $this->group->levenshtein()->custom("bookshelf", 10, 2.0, 1.0, 1.5);
129
130 $this->assertEquals(4.0, $transformation->transform("bookstore"));
131 }
132
133 // test apply to
135 {
136 $transformation = $this->group->levenshtein()->standard("bookshelf", 10);
137 $value_object = $this->factory->ok("book");
138 $result_object = $transformation->applyTo($value_object);
139
140 $this->assertEquals(5, $result_object->value());
141 }
142
144 {
145 $transformation = $this->group->levenshtein()->custom("bookshelf", 10, 2.0, 1.0, 1.5);
146 $value_object = $this->factory->ok("book");
147 $result_object = $transformation->applyTo($value_object);
148
149 $this->assertEquals(7.5, $result_object->value());
150 }
151
152 public function testApplyToWrongType()
153 {
154 $transformation = $this->group->levenshtein()->custom("bookshelf", 10, 2.0, 1.0, 1.5);
155 $value_object = $this->factory->ok(496);
156 $result_object = $transformation->applyTo($value_object);
157
158 $this->assertTrue($result_object->isError());
159 }
160
161 // test Multibyte strings:
163 {
164 $transformation = $this->group->levenshtein()->custom($this->test_multibyte_string, 10, 2.0, 1.0, 1.5);
165
166 $this->assertEquals(0, $transformation->transform($this->test_multibyte_string));
167 }
168
169 public function testEmoji()
170 {
171 $transformation = $this->group->levenshtein()->custom("book", 20, 2.0, 1.0, 1.5);
172
173 $this->assertEquals(4.5, $transformation->transform($this->test_emoji));
174 }
175
177 {
178 $transformation = $this->group->levenshtein()->custom("bookshelf", 20, 2.0, 1.0, 1.5);
179 $value_object = $this->factory->ok($this->test_multibyte_string);
180 $result_object = $transformation->applyTo($value_object);
181
182 $this->assertEquals(12.5, $result_object->value());
183 }
184}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
language handling