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

AspellTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 11

10 Methods

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

90
        $this->assertNotFalse(array_search('en_GB', /** @scrutinizer ignore-type */ $aspell->getSupportedLanguages()));
Loading history...
Bug introduced by
It seems like array_search('en_GB', $a...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

90
        $this->assertNotFalse(/** @scrutinizer ignore-type */ array_search('en_GB', $aspell->getSupportedLanguages()));
Loading history...
91
    }
92
93
    public static function realBinaryPath(): string
94
    {
95
        if (getenv('ASPELL_BINARY_PATH') === false) {
96
            throw new \RuntimeException('"ASPELL_BINARY_PATH" env must be set to find the executable to run tests on');
97
        }
98
99
        return getenv('ASPELL_BINARY_PATH');
100
    }
101
}
102