Passed
Pull Request — master (#698)
by Konrad
08:37 queued 05:26
created

PagesTest::testPullRequest698DontOverride()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 29
rs 9.8333
1
<?php
2
3
/**
4
 * @file This file is part of the PdfParser library.
5
 *
6
 * @author  Konrad Abicht <[email protected]>
7
 *
8
 * @date    2024-04-19
9
 *
10
 * @license LGPLv3
11
 *
12
 * @url     <https://github.com/smalot/pdfparser>
13
 *
14
 * PdfParser is a pdf library written in PHP, extraction oriented.
15
 * Copyright (C) 2017 - Sébastien MALOT <[email protected]>
16
 *
17
 * This program is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Lesser General Public License as published by
19
 * the Free Software Foundation, either version 3 of the License, or
20
 * (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU Lesser General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Lesser General Public License
28
 * along with this program.
29
 * If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>.
30
 */
31
32
namespace PHPUnitTests\Integration;
33
34
use PHPUnitTests\TestCase;
35
use Smalot\PdfParser\Document;
36
use Smalot\PdfParser\Element\ElementArray;
37
use Smalot\PdfParser\Font;
38
use Smalot\PdfParser\Header;
39
use Smalot\PdfParser\Page;
40
use Smalot\PdfParser\Pages;
41
42
/**
43
 * @internal only for test purposes
44
 */
45
class PagesDummy extends Pages
46
{
47
    /**
48
     * @param array<\Smalot\PdfParser\Font> $fonts
49
     *
50
     * @return void
51
     */
52
    public function setFonts($fonts)
53
    {
54
        $this->fonts = $fonts;
55
    }
56
}
57
58
class PagesTest extends TestCase
59
{
60
    /**
61
     * If fonts are not stored in Page instances but in the Pages instance.
62
     *
63
     * @see https://github.com/smalot/pdfparser/pull/698
64
     */
65
    public function testPullRequest698NoFontsSet(): void
66
    {
67
        $document = $this->createMock(Document::class);
68
69
        // create a Page mock and tell PHPUnit that its setFonts has to be called once
70
        // otherwise an error is raised
71
        $page1 = $this->createMock(Page::class);
72
        $page1->expects($this->once())->method('setFonts');
73
74
        // setup header
75
        $header = new Header([
76
            'Kids' => new ElementArray([
77
                $page1,
78
            ]),
79
        ], $document);
80
81
        $font1 = $this->createMock(Font::class);
82
83
        $pages = new PagesDummy($document, $header);
84
        $pages->setFonts([$font1]);
85
86
        // we expect setFonts is called on $page1
87
        $pages->getPages(true);
88
    }
89
90
    /**
91
     * Dont override fonts list in Page, if available.
92
     *
93
     * @see https://github.com/smalot/pdfparser/pull/698
94
     */
95
    public function testPullRequest698DontOverride(): void
96
    {
97
        $document = $this->createMock(Document::class);
98
99
        // create a Page mock and tell PHPUnit that its setFonts has to be called once
100
        // otherwise an error is raised
101
        $font2 = new Font($document);
102
        $page1 = new Page($document);
103
        $page1->setFonts([$font2]);
104
105
        // setup header
106
        $header = new Header([
107
            'Kids' => new ElementArray([
108
                $page1,
109
            ]),
110
        ], $document);
111
112
        $font1 = $this->createMock(Font::class);
113
114
        $pages = new PagesDummy($document, $header);
115
        $pages->setFonts([$font1]);
116
117
        $pages->getPages(true);
118
119
        // note: $font1 and $font2 are intenionally not both of the same type.
120
        // one is a mock and the other one a real instance of Font.
121
        // this way we can simply check the return value of getFonts here.
122
        // if both were one of the other, we had to use a different assertation approach.
123
        $this->assertEquals([$font2], $page1->getFonts());
124
    }
125
}
126