Completed
Pull Request — master (#14)
by Mischa ter
01:48
created

DamerauLevenshteinTest::testSetSubCost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace Oefenweb\DamerauLevenshtein\Test;
3
4
use Oefenweb\DamerauLevenshtein\DamerauLevenshtein;
5
use PHPUnit\Framework\TestCase;
6
7
class DamerauLevenshteinTest extends TestCase
8
{
9
    /**
10
     * Data provider for `getSimilarity`.
11
     *
12
     * @return array
13
     */
14
    public function getSimilarityProvider(): array {
15
        return [
16
            ['foo', 'foo', 0],
17
            ['foo', 'fooo', 1],
18
            ['foo', 'bar', 3],
19
20
            ['123', '12', 1],
21
            ['qwe', 'qwa', 1],
22
            ['awe', 'qwe', 1],
23
            ['фыв', 'фыа', 1],
24
            ['vvvqw', 'vvvwq', 1],
25
            ['qw', 'wq', 1],
26
            ['qq', 'ww', 2],
27
            ['qw', 'qw', 0],
28
            ['пионер', 'плеер', 3],
29
            ['пионер', 'пионеер', 1],
30
            ['пионер', 'поинер', 1],
31
            ['pioner', 'poner', 1],
32
            ['пионер', 'понер', 1],
33
        ];
34
    }
35
36
    /**
37
     * Tests `getSimilarity`.
38
     *
39
     * @param string $firstString
40
     * @param string $secondString
41
     * @param int $expected
42
     * @return void
43
     * @dataProvider getSimilarityProvider
44
     */
45
    public function testGetSimilarity(string $firstString, string $secondString, int $expected): void
46
    {
47
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
48
        $actual = $DamerauLevenshtein->getSimilarity();
49
50
        $this->assertSame($expected, $actual);
51
    }
52
53
    /**
54
     * Tests `getInsCost`.
55
     *
56
     * @return void
57
     */
58 View Code Duplication
    public function testGetInsCost(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        list($firstString, $secondString) = $this->getDefaultStrings();
61
        list($insCost, $delCost, $subCost, $transCost) = $this->getDefaultCosts();
62
63
        // Default insert cost
64
65
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
66
        $actual = $DamerauLevenshtein->getInsCost();
67
        $expected = $insCost;
68
69
        $this->assertSame($expected, $actual);
70
71
        // Non-default insert cost
72
73
        $insCost = 2;
74
75
        $DamerauLevenshtein = new DamerauLevenshtein(
76
            $firstString,
77
            $secondString,
78
            $insCost,
79
            $delCost,
80
            $subCost,
81
            $transCost
82
        );
83
        $actual = $DamerauLevenshtein->getInsCost();
84
        $expected = $insCost;
85
86
        $this->assertSame($expected, $actual);
87
    }
88
89
    /**
90
     * Tests `setInsCost`.
91
     *
92
     * @param int $cost
93
     * @return void
94
     * @dataProvider setXCostProvider
95
     */
96 View Code Duplication
    public function testSetInsCost(int $cost): void {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
        list($firstString, $secondString) = $this->getDefaultStrings();
98
99
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
100
        $DamerauLevenshtein->setInsCost($cost);
101
        $this->assertSame($cost, $DamerauLevenshtein->getInsCost());
102
    }
103
104
    /**
105
     * Tests `getDelCost`.
106
     *
107
     * @return void
108
     */
109 View Code Duplication
    public function testGetDelCost(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        list($firstString, $secondString) = $this->getDefaultStrings();
112
        list($insCost, $delCost, $subCost, $transCost) = $this->getDefaultCosts();
113
114
        // Default delete cost
115
116
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
117
        $actual = $DamerauLevenshtein->getDelCost();
118
        $expected = $delCost;
119
120
        $this->assertSame($expected, $actual);
121
122
        // Non-default delete cost
123
124
        $delCost = 2;
125
126
        $DamerauLevenshtein = new DamerauLevenshtein(
127
            $firstString,
128
            $secondString,
129
            $insCost,
130
            $delCost,
131
            $subCost,
132
            $transCost
133
        );
134
        $actual = $DamerauLevenshtein->getDelCost();
135
        $expected = $delCost;
136
137
        $this->assertSame($expected, $actual);
138
    }
139
140
    /**
141
     * Data provider for `set<x>Cost`.
142
     *
143
     * @return array
144
     */
145
    public function setXCostProvider(): array {
146
        return [
147
            [1],
148
            [2],
149
        ];
150
    }
151
152
    /**
153
     * Tests `setDelCost`.
154
     *
155
     * @param int $cost
156
     * @return void
157
     * @dataProvider setXCostProvider
158
     */
159 View Code Duplication
    public function testSetDelCost(int $cost): void {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
        list($firstString, $secondString) = $this->getDefaultStrings();
161
162
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
163
        $DamerauLevenshtein->setDelCost($cost);
164
        $this->assertSame($cost, $DamerauLevenshtein->getDelCost());
165
    }
166
167
    /**
168
     * Tests `getSubCost`.
169
     *
170
     * @return void
171
     */
172 View Code Duplication
    public function testGetSubCost(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
    {
174
        list($firstString, $secondString) = $this->getDefaultStrings();
175
        list($insCost, $delCost, $subCost, $transCost) = $this->getDefaultCosts();
176
177
        // Default substitution cost
178
179
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
180
        $actual = $DamerauLevenshtein->getSubCost();
181
        $expected = $subCost;
182
183
        $this->assertSame($expected, $actual);
184
185
        // Non-default substitution cost
186
187
        $subCost = 2;
188
189
        $DamerauLevenshtein = new DamerauLevenshtein(
190
            $firstString,
191
            $secondString,
192
            $insCost,
193
            $delCost,
194
            $subCost,
195
            $transCost
196
        );
197
        $actual = $DamerauLevenshtein->getSubCost();
198
        $expected = $subCost;
199
200
        $this->assertSame($expected, $actual);
201
    }
202
203
    /**
204
     * Tests `setSubCost`.
205
     *
206
     * @param int $cost
207
     * @return void
208
     * @dataProvider setXCostProvider
209
     */
210 View Code Duplication
    public function testSetSubCost(int $cost): void {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
211
        list($firstString, $secondString) = $this->getDefaultStrings();
212
213
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
214
        $DamerauLevenshtein->setSubCost($cost);
215
        $this->assertSame($cost, $DamerauLevenshtein->getSubCost());
216
    }
217
218
    /**
219
     * Tests `getTransCost`.
220
     *
221
     * @return void
222
     */
223 View Code Duplication
    public function testGetTransCost(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
224
    {
225
        list($firstString, $secondString) = $this->getDefaultStrings();
226
        list($insCost, $delCost, $subCost, $transCost) = $this->getDefaultCosts();
227
228
        // Default transposition cost
229
230
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
231
        $actual = $DamerauLevenshtein->getTransCost();
232
        $expected = $transCost;
233
234
        $this->assertSame($expected, $actual);
235
236
        // Non-default transposition cost
237
238
        $transCost = 2;
239
240
        $DamerauLevenshtein = new DamerauLevenshtein(
241
            $firstString,
242
            $secondString,
243
            $insCost,
244
            $delCost,
245
            $subCost,
246
            $transCost
247
        );
248
        $actual = $DamerauLevenshtein->getTransCost();
249
        $expected = $transCost;
250
251
        $this->assertSame($expected, $actual);
252
    }
253
254
    /**
255
     * Tests `setTransCost`.
256
     *
257
     * @param int $cost
258
     * @return void
259
     * @dataProvider setXCostProvider
260
     */
261 View Code Duplication
    public function testSetTransCost(int $cost): void {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
262
        list($firstString, $secondString) = $this->getDefaultStrings();
263
264
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
265
        $DamerauLevenshtein->setTransCost($cost);
266
        $this->assertSame($cost, $DamerauLevenshtein->getTransCost());
267
    }
268
269
    /**
270
     * Data provider for `getRelativeDistance`.
271
     *
272
     * @return array
273
     */
274
    public function getRelativeDistanceProvider(): array {
275
        return [
276
            ['O\'Callaghan', 'OCallaghan', 0.90909090909091],
277
            ['Thom', 'Mira', 0.0],
278
            ['Oldeboom', 'Ven', 0.125],
279
            ['ven', 'Ven', 0.66666666666667],
280
            ['enV', 'Ven', 0.3333333333333],
281
        ];
282
    }
283
284
    /**
285
     * Tests `getRelativeDistance`.
286
     *
287
     * @param string $firstString
288
     * @param string $secondString
289
     * @param float $expected
290
     * @return void
291
     * @dataProvider getRelativeDistanceProvider
292
     */
293
    public function testGetRelativeDistance(string $firstString, string $secondString, float $expected): void
294
    {
295
        $delta = pow(10, -4);
296
297
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
298
        $actual = $DamerauLevenshtein->getRelativeDistance();
299
        $this->assertEquals($expected, $actual, '', $delta);
300
    }
301
302
    /**
303
     * Tests `getMatrix`.
304
     *
305
     * @return void
306
     */
307
    public function testGetMatrix(): void
308
    {
309
        list($firstString, $secondString) = $this->getDefaultStrings();
310
311
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
312
        $actual = $DamerauLevenshtein->getMatrix();
313
        $expected = [
314
            [0, 1, 2, 3],
315
            [1, 1, 2, 3],
316
            [2, 2, 2, 3],
317
            [3, 3, 3, 3]
318
        ];
319
        $this->assertSame($expected, $actual);
320
    }
321
322
    /**
323
     * Tests `displayMatrix`.
324
     *
325
     * @return void
326
     */
327
    public function testDisplayMatrix(): void
328
    {
329
        list($firstString, $secondString) = $this->getDefaultStrings();
330
331
        $DamerauLevenshtein = new DamerauLevenshtein($firstString, $secondString);
332
        $actual = $DamerauLevenshtein->displayMatrix();
333
        $expected = implode('', [
334
            "  foo\n",
335
            " 0123\n",
336
            "b1123\n",
337
            "a2223\n",
338
            "r3333\n",
339
        ]);
340
        $this->assertSame($expected, $actual);
341
    }
342
343
    /**
344
     * Returns the default costs.
345
     *
346
     * @return array Costs (insert, delete, substitution, transposition)
347
     */
348
    protected function getDefaultCosts(): array
349
    {
350
        $insCost = 1;
351
        $delCost = 1;
352
        $subCost = 1;
353
        $transCost = 1;
354
355
        return [$insCost, $delCost, $subCost, $transCost];
356
    }
357
358
    /**
359
     * Returns the default strings.
360
     *
361
     * @return array Strings (foo, bar)
362
     */
363
    protected function getDefaultStrings(): array
364
    {
365
        $firstString = 'foo';
366
        $secondString = 'bar';
367
368
        return [$firstString, $secondString];
369
    }
370
}
371