Passed
Push — master ( b2b167...7e189a )
by Philippe
01:56
created

HunspellTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 94
rs 10
c 0
b 0
f 0
wmc 13
1
<?php
2
3
declare(strict_types=1);
4
5
use PhpSpellcheck\Exception\ProcessHasErrorOutputException;
6
use PhpSpellcheck\Misspelling;
7
use PhpSpellcheck\Spellchecker\Hunspell;
8
use PhpSpellcheck\Tests\TextTest;
9
use PhpSpellcheck\Utils\CommandLine;
10
use PHPUnit\Framework\TestCase;
11
12
class HunspellTest extends TestCase
13
{
14
    private const FAKE_BINARIES_PATH = [PHP_BINARY, __DIR__ . '/../Fixtures/Hunspell/bin/hunspell.php'];
15
16
    public function testSpellcheckFromFakeBinaries(): void
17
    {
18
        $this->assertWorkingSpellcheck(self::FAKE_BINARIES_PATH);
19
    }
20
21
    public function testGetSupportedLanguagesFromFakeBinaries(): void
22
    {
23
        $this->assertWorkingSupportedLanguages(self::FAKE_BINARIES_PATH);
24
    }
25
26
    public function testBadCheckRequest(): void
27
    {
28
        $this->expectException(ProcessHasErrorOutputException::class);
29
        (new Hunspell(new CommandLine(IspellTest::FAKE_BAD_BINARIES_PATH)))->check('bla');
30
    }
31
32
    /**
33
     * @group integration
34
     */
35
    public function testSpellcheckFromRealBinariesLanguage(): void
36
    {
37
        $hunspell = new Hunspell(new CommandLine(self::realBinaryPath()));
38
        $misspellings = iterator_to_array($hunspell->check('mispell', ['en_US']));
39
        $this->assertInstanceOf(Misspelling::class, $misspellings[0]);
40
    }
41
42
    /**
43
     * @group integration
44
     */
45
    public function testSpellcheckFromRealBinaries(): void
46
    {
47
        $this->assertWorkingSpellcheck(self::realBinaryPath());
48
    }
49
50
    /**
51
     * @group integration
52
     */
53
    public function testGetSupportedLanguagesFromRealBinaries(): void
54
    {
55
        $this->assertWorkingSupportedLanguages(self::realBinaryPath());
56
    }
57
58
    public function getTextInput(): string
59
    {
60
        return TextTest::CONTENT_STUB;
61
    }
62
63
    public function getFakeDicts(): array
64
    {
65
        return explode(PHP_EOL, \Safe\file_get_contents(__DIR__ . '/../Fixtures/Hunspell/dicts.txt'));
66
    }
67
68
    /**
69
     * @param array|string $binaries
70
     */
71
    public function assertWorkingSupportedLanguages($binaries): void
72
    {
73
        $hunspell = new Hunspell(new CommandLine($binaries));
74
        $languages = is_array($hunspell->getSupportedLanguages()) ? $hunspell->getSupportedLanguages() : iterator_to_array($hunspell->getSupportedLanguages());
75
        $this->assertNotFalse(array_search('en_US', $languages, true));
76
    }
77
78
    public static function realBinaryPath(): string
79
    {
80
        if (getenv('HUNSPELL_BINARY_PATH') === false) {
81
            throw new \RuntimeException('"HUNSPELL_BINARY_PATH" env must be set to find the executable to run tests on');
82
        }
83
84
        return getenv('HUNSPELL_BINARY_PATH');
85
    }
86
87
    /**
88
     * @param array|string $binaries
89
     */
90
    private function assertWorkingSpellcheck($binaries): void
91
    {
92
        $hunspell = new Hunspell(new CommandLine($binaries));
93
        /** @var Misspelling[] $misspellings */
94
        $misspellings = iterator_to_array($hunspell->check($this->getTextInput(), ['en_US'], ['ctx']));
95
96
        $this->assertSame(['ctx'], $misspellings[0]->getContext());
97
        $this->assertSame('Tigr', $misspellings[0]->getWord());
98
        $this->assertSame(0, $misspellings[0]->getOffset());
99
        $this->assertSame(1, $misspellings[0]->getLineNumber());
100
        $this->assertNotEmpty($misspellings[0]->getSuggestions());
101
102
        $this->assertSame(['ctx'], $misspellings[1]->getContext());
103
        $this->assertSame('страх', $misspellings[1]->getWord());
104
        $this->assertSame(21, $misspellings[1]->getOffset());
105
        $this->assertSame(1, $misspellings[1]->getLineNumber());
106
    }
107
}
108