xref: /plugin/totext/_test/PdfExtractorTest.php (revision e3bf24fb9dfc9f06d2338a5c5cb7500e628e4f71)
1<?php
2
3namespace dokuwiki\plugin\totext\test;
4
5use dokuwiki\plugin\totext\Exception\ExtractionException;
6use dokuwiki\plugin\totext\Extractor\PdfExtractor;
7use DokuWikiTest;
8
9/**
10 * Tests for the PDF extractor.
11 *
12 * @group plugin_totext
13 */
14class PdfExtractorTest extends DokuWikiTest
15{
16    /** @var string[] */
17    protected $pluginsEnabled = ['totext'];
18
19    /** @var string temp working directory */
20    private $tmp = '';
21
22    /** @var string fixture path */
23    private $fixture = '';
24
25    /** @inheritDoc */
26    public function setUp(): void
27    {
28        parent::setUp();
29        $this->tmp = FixtureBuilder::tempDir();
30        $this->fixture = $this->tmp . '/sample.pdf';
31        FixtureBuilder::buildPdf($this->fixture, 'Hello PDF world');
32    }
33
34    /** @inheritDoc */
35    public function tearDown(): void
36    {
37        FixtureBuilder::cleanup($this->tmp);
38        parent::tearDown();
39    }
40
41    public function testExtractsText()
42    {
43        $text = (new PdfExtractor())->extract($this->fixture);
44        $this->assertStringContainsString('Hello PDF world', $text);
45    }
46
47    public function testMissingFileThrows()
48    {
49        $this->expectException(ExtractionException::class);
50        (new PdfExtractor())->extract($this->tmp . '/nonexistent.pdf');
51    }
52
53    public function testExtractsMultiLineText()
54    {
55        $path = $this->tmp . '/multiline.pdf';
56        FixtureBuilder::buildPdf($path, 'Second sample line');
57        $text = (new PdfExtractor())->extract($path);
58        $this->assertStringContainsString('Second sample line', $text);
59    }
60
61    /**
62     * @return array<string, array{0: string, 1: bool}>
63     */
64    public function provideSupports(): array
65    {
66        return [
67            'pdf' => ['foo.pdf', true],
68            'uppercase' => ['foo.PDF', true],
69            'docx' => ['foo.docx', false],
70        ];
71    }
72
73    /**
74     * @dataProvider provideSupports
75     */
76    public function testSupports(string $path, bool $expected)
77    {
78        $this->assertSame($expected, (new PdfExtractor())->supports($path));
79    }
80}
81