ILIAS  trunk Revision v11.0_alpha-1846-g895b5f47236
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
LevenshteinTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
27 
28 class LevenshteinTest extends TestCase
29 {
33  private $group;
34 
38  private $factory;
39 
43  private $test_multibyte_string = "你好";
44 
48  private $test_emoji = "😮‍💨";
49 
50  public function setUp(): void
51  {
52  $this->factory = new Factory();
53  $language = $this->getMockBuilder(\ILIAS\Language\Language::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56  $this->group = new Group($this->factory, $language);
57  }
58 
59  // Code paths
60  public function testSizeReturn()
61  {
62  $transformation = $this->group->levenshtein()->standard("book", 3);
63 
64  $this->assertEquals(-1.0, $transformation->transform("bookshelf"));
65  }
66 
67  public function testExceedMaximumDistance()
68  {
69  $transformation = $this->group->levenshtein()->standard("book", 1);
70 
71  $this->assertEquals(-1.0, $transformation->transform("back"));
72  }
73 
74  public function testSuccessfulReturn()
75  {
76  $transformation = $this->group->levenshtein()->standard("book", 1);
77 
78  $this->assertEquals(0.0, $transformation->transform("book"));
79  }
80 
81  public function testNoMaximumDistance()
82  {
83  $transformation = $this->group->levenshtein()->standard("book", 0);
84 
85  $this->assertEquals(2.0, $transformation->transform("back"));
86  }
87 
88  public function testException()
89  {
90  $transformation = $this->group->levenshtein()->standard("book", 0);
91  $this->expectException(InvalidArgumentException::class);
92 
93  $this->assertEquals(2.0, $transformation->transform(496));
94  }
95 
97  {
98  $transformation = $this->group->levenshtein()->standard("Juni", 2);
99 
100  $this->assertEquals(-1, $transformation->transform("Januar"));
101  }
102 
103  // Numerical
104  public function testCustomCostsMixed()
105  {
106  $transformation = $this->group->levenshtein()->custom("back", 20, 2.0, 1.0, 1.5);
107 
108  $this->assertEquals(12, $transformation->transform("bookshelf"));
109  }
110 
111  public function testCustomCostsInsert()
112  {
113  $transformation = $this->group->levenshtein()->custom("book", 5, 2.0, 1.0, 1.5);
114 
115  $this->assertEquals(2, $transformation->transform("books"));
116  }
117 
118  public function testCustomCostsDeletion()
119  {
120  $transformation = $this->group->levenshtein()->custom("bookshelf", 10, 2.0, 1.0, 1.5);
121 
122  $this->assertEquals(7.5, $transformation->transform("book"));
123  }
124 
125  public function testCustomCostsReplacement()
126  {
127  $transformation = $this->group->levenshtein()->custom("bookshelf", 10, 2.0, 1.0, 1.5);
128 
129  $this->assertEquals(4.0, $transformation->transform("bookstore"));
130  }
131 
132  // test apply to
134  {
135  $transformation = $this->group->levenshtein()->standard("bookshelf", 10);
136  $value_object = $this->factory->ok("book");
137  $result_object = $transformation->applyTo($value_object);
138 
139  $this->assertEquals(5, $result_object->value());
140  }
141 
143  {
144  $transformation = $this->group->levenshtein()->custom("bookshelf", 10, 2.0, 1.0, 1.5);
145  $value_object = $this->factory->ok("book");
146  $result_object = $transformation->applyTo($value_object);
147 
148  $this->assertEquals(7.5, $result_object->value());
149  }
150 
151  public function testApplyToWrongType()
152  {
153  $transformation = $this->group->levenshtein()->custom("bookshelf", 10, 2.0, 1.0, 1.5);
154  $value_object = $this->factory->ok(496);
155  $result_object = $transformation->applyTo($value_object);
156 
157  $this->assertTrue($result_object->isError());
158  }
159 
160  // test Multibyte strings:
162  {
163  $transformation = $this->group->levenshtein()->custom($this->test_multibyte_string, 10, 2.0, 1.0, 1.5);
164 
165  $this->assertEquals(0, $transformation->transform($this->test_multibyte_string));
166  }
167 
168  public function testEmoji()
169  {
170  $transformation = $this->group->levenshtein()->custom("book", 20, 2.0, 1.0, 1.5);
171 
172  $this->assertEquals(4.5, $transformation->transform($this->test_emoji));
173  }
174 
175  public function testApplyToMultibyteString()
176  {
177  $transformation = $this->group->levenshtein()->custom("bookshelf", 20, 2.0, 1.0, 1.5);
178  $value_object = $this->factory->ok($this->test_multibyte_string);
179  $result_object = $transformation->applyTo($value_object);
180 
181  $this->assertEquals(12.5, $result_object->value());
182  }
183 }
Interface Observer Contains several chained tasks and infos about them.
factory()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Builds data types.
Definition: Factory.php:35