|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
use PhpSpellcheck\Exception\InvalidArgumentException; |
|
|
|
|
|
|
6
|
|
|
use PhpSpellcheck\Misspelling; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class MisspellingTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
public function testMergeSuggestions(): void |
|
12
|
|
|
{ |
|
13
|
|
|
$misspelling = new Misspelling('mispelled', 1, 0, ['misspelled']); |
|
14
|
|
|
$misspelling->mergeSuggestions(['misspelling', 'misspelled']); |
|
15
|
|
|
|
|
16
|
|
|
$this->assertSame(['misspelled', 'misspelling'], $misspelling->getSuggestions()); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @dataProvider nonDeterminableUniqueIdentityMisspellings |
|
21
|
|
|
*/ |
|
22
|
|
|
public function testCanDeterminateUniqueIdentity(Misspelling $misspelling): void |
|
23
|
|
|
{ |
|
24
|
|
|
$this->assertFalse($misspelling->canDeterminateUniqueIdentity()); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function nonDeterminableUniqueIdentityMisspellings(): array |
|
28
|
|
|
{ |
|
29
|
|
|
return [ |
|
30
|
|
|
[new Misspelling('mispelled')], |
|
31
|
|
|
[new Misspelling('mispelled', 1)], |
|
32
|
|
|
[new Misspelling('mispelled', null, 1)], |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testContextOverridingMerge(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$misspelling = (new Misspelling('mispelled', 1, 0, [], ['idx' => '1']))->mergeContext([ |
|
39
|
|
|
'idx' => 'foo', |
|
40
|
|
|
'idx2' => '2', |
|
41
|
|
|
]); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertEquals(new Misspelling('mispelled', 1, 0, [], ['idx' => 'foo', 'idx2' => '2']), $misspelling); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testContextNonOverridingMerge(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$misspelling = (new Misspelling('mispelled', 1, 0, [], ['idx' => '1']))->mergeContext([ |
|
49
|
|
|
'idx' => 'foo', |
|
50
|
|
|
'idx2' => '2', |
|
51
|
|
|
], false); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertEquals(new Misspelling('mispelled', 1, 0, [], ['idx' => '1', 'idx2' => '2']), $misspelling); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testExceptionWhenMergingEmptyContext(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
59
|
|
|
(new Misspelling('mispelled', 1, 0, [], []))->mergeContext([]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testImmutableSetContext(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$misspelling = new Misspelling('mispelled', 1, 0, [], []); |
|
65
|
|
|
$misspellingAfterSettingContext = $misspelling->setContext(['test']); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertNotSame($misspelling, $misspellingAfterSettingContext); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: