Passed
Push — master ( 141848...a80b50 )
by Philippe
06:19
created

MisspellingTest::testContextNonOverridingMerge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
use PhpSpellcheck\Exception\InvalidArgumentException;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, InvalidArgumentException. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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