Failed Conditions
Push — master ( 01d6ae...9ca6d9 )
by Philippe
534:14 queued 469:10
created

HunspellTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 94
rs 10
c 0
b 0
f 0
wmc 12

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testBadCheckRequest() 0 4 1
A testSpellcheckFromFakeBinaries() 0 3 1
A getTextInput() 0 3 1
A assertWorkingSupportedLanguages() 0 4 1
A realBinaryPath() 0 7 2
A testSpellcheckFromRealBinaries() 0 3 1
A getFakeDicts() 0 3 1
A testGetSupportedLanguagesFromFakeBinaries() 0 3 1
A testGetSupportedLanguagesFromRealBinaries() 0 3 1
A testSpellcheckFromRealBinariesLanguage() 0 5 1
A assertWorkingSpellcheck() 0 16 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpSpellcheck\Tests\Spellchecker;
6
7
use PhpSpellcheck\Exception\ProcessHasErrorOutputException;
8
use PhpSpellcheck\Misspelling;
9
use PhpSpellcheck\Spellchecker\Hunspell;
10
use PhpSpellcheck\Tests\TextTest;
11
use PhpSpellcheck\Utils\CommandLine;
12
use PHPUnit\Framework\TestCase;
13
14
class HunspellTest extends TestCase
15
{
16
    private const FAKE_BINARIES_PATH = [PHP_BINARY, __DIR__ . '/../Fixtures/Hunspell/bin/hunspell.php'];
17
18
    public function testSpellcheckFromFakeBinaries()
19
    {
20
        $this->assertWorkingSpellcheck(self::FAKE_BINARIES_PATH);
21
    }
22
23
    public function testGetSupportedLanguagesFromFakeBinaries()
24
    {
25
        $this->assertWorkingSupportedLanguages(self::FAKE_BINARIES_PATH);
26
    }
27
28
    public function testBadCheckRequest()
29
    {
30
        $this->expectException(ProcessHasErrorOutputException::class);
31
        (new Hunspell(new CommandLine(IspellTest::FAKE_BAD_BINARIES_PATH)))->check('bla');
32
    }
33
34
    /**
35
     * @group integration
36
     */
37
    public function testSpellcheckFromRealBinariesLanguage()
38
    {
39
        $hunspell = new Hunspell(new CommandLine(self::realBinaryPath()));
40
        $misspellings = iterator_to_array($hunspell->check('mispell', ['en_US']));
41
        $this->assertInstanceOf(Misspelling::class, $misspellings[0]);
42
    }
43
44
    /**
45
     * @group integration
46
     */
47
    public function testSpellcheckFromRealBinaries()
48
    {
49
        $this->assertWorkingSpellcheck(self::realBinaryPath());
50
    }
51
52
    /**
53
     * @group integration
54
     */
55
    public function testGetSupportedLanguagesFromRealBinaries()
56
    {
57
        $this->assertWorkingSupportedLanguages(self::realBinaryPath());
58
    }
59
60
    public function getTextInput()
61
    {
62
        return TextTest::CONTENT_STUB;
63
    }
64
65
    public function getFakeDicts()
66
    {
67
        return explode(PHP_EOL, file_get_contents(__DIR__ . '/../Fixtures/Hunspell/dicts.txt'));
68
    }
69
70
71
    /**
72
     * @param string|array $binaries
73
     */
74
    private function assertWorkingSpellcheck($binaries)
75
    {
76
        $hunspell = new Hunspell(new CommandLine($binaries));
77
        /** @var Misspelling[] $misspellings */
78
        $misspellings = iterator_to_array($hunspell->check($this->getTextInput(), ['en_US'], ['ctx']));
79
80
        $this->assertSame(['ctx'], $misspellings[0]->getContext());
81
        $this->assertSame('Tigr', $misspellings[0]->getWord());
82
        $this->assertSame(0, $misspellings[0]->getOffset());
83
        $this->assertSame(1, $misspellings[0]->getLineNumber());
84
        $this->assertNotEmpty($misspellings[0]->getSuggestions());
85
86
        $this->assertSame(['ctx'], $misspellings[1]->getContext());
87
        $this->assertSame('страх', $misspellings[1]->getWord());
88
        $this->assertSame(21, $misspellings[1]->getOffset());
89
        $this->assertSame(1, $misspellings[1]->getLineNumber());
90
    }
91
92
    /**
93
     * @param string|array $binaries
94
     */
95
    public function assertWorkingSupportedLanguages($binaries)
96
    {
97
        $hunspell = new Hunspell(new CommandLine($binaries));
98
        $this->assertNotFalse(array_search('en_US', $hunspell->getSupportedLanguages()));
0 ignored issues
show
Bug introduced by
$hunspell->getSupportedLanguages() of type iterable is incompatible with the type array expected by parameter $haystack of array_search(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
        $this->assertNotFalse(array_search('en_US', /** @scrutinizer ignore-type */ $hunspell->getSupportedLanguages()));
Loading history...
Bug introduced by
It seems like array_search('en_US', $h...etSupportedLanguages()) can also be of type integer and string; however, parameter $condition of PHPUnit\Framework\Assert::assertNotFalse() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
        $this->assertNotFalse(/** @scrutinizer ignore-type */ array_search('en_US', $hunspell->getSupportedLanguages()));
Loading history...
99
    }
100
101
    public static function realBinaryPath(): string
102
    {
103
        if (getenv('HUNSPELL_BINARY_PATH') === false) {
104
            throw new \RuntimeException('"HUNSPELL_BINARY_PATH" env must be set to find the executable to run tests on');
105
        }
106
107
        return getenv('HUNSPELL_BINARY_PATH');
108
    }
109
}
110