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

testGetSupportedLanguagesFromFakeBinaries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Ispell;
10
use PhpSpellcheck\Tests\TextTest;
11
use PhpSpellcheck\Utils\CommandLine;
12
use PhpSpellcheck\Utils\TextEncoding;
13
use PHPUnit\Framework\TestCase;
14
15
class IspellTest extends TestCase
16
{
17
    private const FAKE_BINARIES_PATH = __DIR__ . '/../Fixtures/Ispell/bin/ispell.sh';
18
    public const FAKE_BAD_BINARIES_PATH = __DIR__ . '/../Fixtures/Ispell/bin/empty_output.sh';
19
20
    public function testSpellcheckFromFakeBinaries()
21
    {
22
        $this->assertWorkingSpellcheck(self::FAKE_BINARIES_PATH);
23
    }
24
25
    public function testGetSupportedLanguagesFromFakeBinaries()
26
    {
27
        $this->assertWorkingSupportedLanguages(self::FAKE_BINARIES_PATH, self:: FAKE_BINARIES_PATH);
28
    }
29
30
    public function testBadCheckRequest()
31
    {
32
        $this->expectException(ProcessHasErrorOutputException::class);
33
        Ispell::create(self::FAKE_BAD_BINARIES_PATH)->check('bla');
34
    }
35
36
    /**
37
     * @group integration
38
     */
39
    public function testSpellcheckFromRealBinaries()
40
    {
41
        $this->assertWorkingSpellcheck(self::realBinaryPath());
42
    }
43
44
    /**
45
     * @group integration
46
     */
47
    public function testGetSupportedLanguagesFromRealBinaries()
48
    {
49
        $this->assertWorkingSupportedLanguages(self::realBinaryPath(), self::realShellPath());
50
    }
51
52
    public function getTextInput()
53
    {
54
        return TextTest::CONTENT_STUB;
55
    }
56
57
    public function getFakeDicts()
58
    {
59
        return explode(PHP_EOL, file_get_contents(__DIR__ . '/../Fixtures/Ispell/dicts.txt'));
60
    }
61
62
    private function assertWorkingSpellcheck($binaries)
63
    {
64
        $ispell = new Ispell(new CommandLine($binaries));
65
        /** @var Misspelling[] $misspellings */
66
        $misspellings = iterator_to_array(
67
            $ispell->check(
68
                $this->getTextInput(),
69
                ['american'],
70
                ['ctx'],
71
                TextEncoding::UTF8
72
            )
73
        );
74
75
        $this->assertSame($misspellings[0]->getContext(), ['ctx']);
76
        $this->assertSame($misspellings[0]->getWord(), 'Tigr');
77
        $this->assertSame($misspellings[0]->getOffset(), 0);
78
        $this->assertSame($misspellings[0]->getLineNumber(), 1);
79
        $this->assertNotEmpty($misspellings[0]->getSuggestions());
80
81
        $this->assertSame($misspellings[1]->getContext(), ['ctx']);
82
        $this->assertSame($misspellings[1]->getWord(), 'theforests');
83
        $this->assertSame($misspellings[1]->getOffset(), 3);
84
        $this->assertSame($misspellings[1]->getLineNumber(), 2);
85
        $this->assertNotEmpty($misspellings[1]->getSuggestions());
86
    }
87
88
    public function assertWorkingSupportedLanguages($binaries, $shellEntryPoint = null)
89
    {
90
        $ispell = new Ispell(
91
            new CommandLine($binaries),
92
            $shellEntryPoint !== null ? new CommandLine($shellEntryPoint) : null
93
        );
94
        $this->assertNotFalse(array_search('american', $ispell->getSupportedLanguages()));
0 ignored issues
show
Bug introduced by
It seems like array_search('american',...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

94
        $this->assertNotFalse(/** @scrutinizer ignore-type */ array_search('american', $ispell->getSupportedLanguages()));
Loading history...
95
    }
96
97
    public static function realBinaryPath(): string
98
    {
99
        if (getenv('ISPELL_BINARY_PATH') === false) {
100
            throw new \RuntimeException('"ISPELL_BINARY_PATH" env must be set to find the executable to run tests on');
101
        }
102
103
        return getenv('ISPELL_BINARY_PATH');
104
    }
105
106
    public static function realShellPath(): ?string
107
    {
108
        if (getenv('ISPELL_SHELL_PATH') === false) {
109
            throw new \RuntimeException('"ISPELL_SHELL_PATH" env must be set to find the executable to run tests on');
110
        }
111
112
        return getenv('ISPELL_SHELL_PATH') ? getenv('ISPELL_SHELL_PATH') : null;
113
    }
114
}
115