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

MultiSourceTest::testToTexts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 22
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
use PhpSpellcheck\Source\MultiSource;
6
use PhpSpellcheck\Source\SourceInterface;
7
use PhpSpellcheck\TextInterface;
8
use PhpSpellcheck\Utils\TextEncoding;
9
use PHPUnit\Framework\MockObject\MockObject;
10
use PHPUnit\Framework\TestCase;
11
12
class MultiSourceTest extends TestCase
13
{
14
    public function testToTexts(): void
15
    {
16
        $mockText1 = $this->generateMockText('mispelling1', ['ctx' => null]);
17
        $mockText1AfterContextMerge = $this->generateMockText('mispelling1AfterMerge', ['ctx' => 'merged']);
18
        $mockText1->method('mergeContext')
19
            ->willReturn($mockText1AfterContextMerge);
20
        $mockText2 = $this->generateMockText('mispelling2');
21
        $mockText2->method('mergeContext')
22
            ->willReturn($mockText2);
23
        $mockSource1 = $this->generateMockSource([$mockText1]);
24
        $mockSource2 = $this->generateMockSource([$mockText2]);
25
26
        $source = new MultiSource(
27
            [
28
                $mockSource1,
29
                $mockSource2,
30
            ]
31
        );
32
33
        $expectedTexts = [$mockText1AfterContextMerge, $mockText2];
34
35
        $this->assertSame($expectedTexts, iterator_to_array($source->toTexts()));
36
    }
37
38
    /**
39
     * @return MockObject|TextInterface
40
     */
41
    private function generateMockText(string $content, array $context = []): MockObject
42
    {
43
        $textMock = $this->createMock(TextInterface::class);
44
        $textMock->method('getContext')
45
            ->willReturn($context);
46
        $textMock->method('getEncoding')
47
            ->willReturn(TextEncoding::UTF8);
48
        $textMock->method('getContent')
49
            ->willReturn($content);
50
51
        return $textMock;
52
    }
53
54
    /**
55
     * @return MockObject|SourceInterface
56
     */
57
    private function generateMockSource(array $texts): MockObject
58
    {
59
        $sourceMock = $this->createMock(SourceInterface::class);
60
        $sourceMock->expects($this->once())
61
            ->method('toTexts')
62
            ->willReturn($texts);
63
64
        return $sourceMock;
65
    }
66
}
67