Completed
Push — master ( c4e200...4a09e4 )
by Níckolas Daniel
05:14 queued 10s
created

fetchTestableEpisodesCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace PODEntender\Application\Service\FileProcessing;
4
5
use PHPUnit\Framework\TestCase;
6
use PODEntender\Domain\Model\FileProcessing\OutputFile;
7
use PODEntender\Domain\Model\FileProcessing\OutputFileCollection;
8
use PODEntender\Domain\Model\FileProcessing\OutputFileRepository;
9
use PODEntender\Domain\Model\Post\AudioEpisode;
10
use PODEntender\Domain\Model\Post\AudioEpisodeCollection;
11
use PODEntender\Domain\Model\Post\PostCollection;
12
use PODEntender\Domain\Model\Post\PostRepository;
13
use Symfony\Component\DomCrawler\Crawler;
14
15
class PostProcessFilesTest extends TestCase
16
{
17
    const TEST_INPUT_FILE_NAME = 'test.html';
18
19
    /** @var OutputFileRepository */
20
    private $outputFileRepository;
21
22
    /** @var PostRepository */
23
    private $postRepository;
24
25
    /** @var PostProcessFiles */
26
    private $postProcessFilesService;
27
28
    protected function setUp(): void
29
    {
30
        $this->outputFileRepository = $this->prophesize(OutputFileRepository::class);
31
        $this->outputFileRepository->all()->willReturn($this->fetchTestableInputFilesCollection());
32
33
        $this->postRepository = $this->prophesize(PostRepository::class);
34
        $this->postRepository->withAudio()->willReturn($this->fetchTestableEpisodesCollection());
35
36
        $this->postProcessFilesService = new PostProcessFiles(
37
            $this->outputFileRepository->reveal(),
38
            $this->postRepository->reveal()
39
        );
40
    }
41
42
    public function testExecuteWillMutateParagraphs()
43
    {
44
        $testFileContent = $this->postProcessFilesService
45
            ->execute(true)
46
            ->fetchByPath(self::TEST_INPUT_FILE_NAME)
47
            ->content();
48
49
        $paragraphClasses = implode(
50
            ' ',
51
            (new Crawler($testFileContent))
52
                ->filter('p, strong')
53
                ->each(function (Crawler $elm) {
54
                    return $elm->attr('class');
55
                })
56
        );
57
58
        $this->assertStringContainsString('paragraph', $paragraphClasses);
59
        $this->assertStringNotContainsString('paragraph--justified', $paragraphClasses);
60
        $this->assertStringContainsString('paragraph--bold', $paragraphClasses);
61
    }
62
63
    public function testExecuteWillMutateLinks()
64
    {
65
        $testFileContent = $this->postProcessFilesService
66
            ->execute(true)
67
            ->fetchByPath(self::TEST_INPUT_FILE_NAME)
68
            ->content();
69
70
        $link = (new Crawler($testFileContent))->filter('a')->first();
71
        $this->assertStringContainsString('link', $link->attr('class'));
72
        $this->assertEquals('_blank', $link->attr('target'));
73
    }
74
75
    public function testExecuteWillMutateNonFirstLevelHeadings()
76
    {
77
        $testFileContent = $this->postProcessFilesService
78
            ->execute(true)
79
            ->fetchByPath(self::TEST_INPUT_FILE_NAME)
80
            ->content();
81
82
        $link = (new Crawler($testFileContent))->filter('h2')->first();
83
        $this->assertStringContainsString('heading', $link->attr('class'));
84
        $this->assertStringContainsString('heading__secondary', $link->attr('class'));
85
    }
86
87
    public function testJekyllHeadersWontBePresentWhenProductionModeIsOff()
88
    {
89
        $testFileContent = $this->postProcessFilesService
90
            ->execute(false)
91
            ->fetchByPath(self::TEST_INPUT_FILE_NAME)
92
            ->content();
93
94
        $lines = explode(PHP_EOL, $testFileContent);
95
        $this->assertNotEquals('---', $lines[0]);
96
    }
97
98
    public function testJekyllHeadersWillBePresentWhenProductionModeIsOn()
99
    {
100
        $testFileContent = $this->postProcessFilesService
101
            ->execute(true)
102
            ->fetchByPath(self::TEST_INPUT_FILE_NAME)
103
            ->content();
104
105
        $lines = explode(PHP_EOL, $testFileContent);
106
        $this->assertEquals('---', $lines[0]);
107
        $this->assertEquals('redirect_from:', $lines[1]);
108
        $this->assertEquals('  - /first/redirect', $lines[2]);
109
        $this->assertEquals('  - /second-redirect', $lines[3]);
110
        $this->assertEquals('---', $lines[4]);
111
    }
112
113
    private function fetchTestableInputFilesCollection(): OutputFileCollection
114
    {
115
        return new OutputFileCollection([
116
            new OutputFile(
117
                self::TEST_INPUT_FILE_NAME,
118
                <<<HTML
119
<link rel="canonical" href="/test-canonical-url">
120
<div class="paragraphs-list">
121
    <h2>Wooow, look at this test heading</h2>
122
    <p>
123
        This amazing paragraph with a <strong>bold</strong> element and an amazing <a class="pretty" href="#">link</a>.
124
    </p>
125
</div>
126
HTML
127
            ),
128
        ]);
129
    }
130
131
    private function fetchTestableEpisodesCollection(): PostCollection
132
    {
133
        $post = $this->prophesize(AudioEpisode::class);
134
        $post->url()->willReturn('/test-canonical-url');
135
        $post->redirects()->willReturn([
136
            '/first/redirect',
137
            '/second-redirect',
138
        ]);
139
140
        return new AudioEpisodeCollection([$post->reveal()]);
141
    }
142
}
143