Passed
Pull Request — master (#698)
by Konrad
02:54
created

PagesDummy::setFonts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
     * The purpose of this function is to bypass the tedious
49
     * work to setup instances which lead to a valid $fonts variable.
50
     *
51
     * @param array<\Smalot\PdfParser\Font> $fonts
52
     *
53
     * @return void
54
     */
55
    public function setFonts($fonts)
56
    {
57
        $this->fonts = $fonts;
58
    }
59
}
60
61
class PagesTest extends TestCase
62
{
63
    /**
64
     * If fonts are not stored in Page instances but in the Pages instance.
65
     *
66
     *      Pages
67
     *        |   `--- fonts = Font[]           <=== will be used to override fonts in Page1 ...
68
     *        |
69
     *        |
70
     *        `--+ Page1
71
     *        |         `--- fonts = null       <=== Will be overwritten with the content of Pages.fonts
72
     *        `--+ ...
73
     *
74
     * @see https://github.com/smalot/pdfparser/pull/698
75
     */
76
    public function testPullRequest698NoFontsSet(): void
77
    {
78
        $document = $this->createMock(Document::class);
79
80
        // Create a Page mock and tell PHPUnit that its setFonts has to be called once
81
        // otherwise an error is raised
82
        $page1 = $this->createMock(Page::class);
83
        $page1->expects($this->once())->method('setFonts');
84
85
        // setup header
86
        $header = new Header([
87
            'Kids' => new ElementArray([
88
                $page1,
89
            ]),
90
        ], $document);
91
92
        $font1 = $this->createMock(Font::class);
93
94
        // Preset fonts variable so we don't have to prepare all the
95
        // prerequisites manually (like creating a Ressources instance
96
        // with Font instances, see Pages::setupFonts())
97
        $pages = new PagesDummy($document, $header);
98
        $pages->setFonts([$font1]);
99
100
        // We expect setFonts is called on $page1, therefore no assertion here
101
        $pages->getPages(true);
102
    }
103
104
    /**
105
     * Dont override fonts list in a Page instance, if available.
106
     *
107
     *      Pages
108
     *        |   `--- fonts = Font[]           <=== Has to be ignored because fonts in Page1 is set
109
     *        |
110
     *        |
111
     *        `--+ Page1
112
     *        |         `--- fonts = Font[]     <=== must not be overwritten
113
     *        `--+ ...
114
     *
115
     * @see https://github.com/smalot/pdfparser/pull/698
116
     */
117
    public function testPullRequest698DontOverride(): void
118
    {
119
        $document = $this->createMock(Document::class);
120
121
        // Setup an empty Page instance and insert a Font instance.
122
        // We wanna see later on, if $font2 is overwritten by $font1.
123
        $font2 = new Font($document);
124
        $page1 = new Page($document);
125
        $page1->setFonts([$font2]);
126
127
        // setup header
128
        $header = new Header([
129
            'Kids' => new ElementArray([
130
                $page1,
131
            ]),
132
        ], $document);
133
134
        $font1 = $this->createMock(Font::class);
135
136
        $pages = new PagesDummy($document, $header);
137
        $pages->setFonts([$font1]);
138
139
        // Trigger setupFonts method in $pages
140
        $pages->getPages(true);
141
142
        // Note:
143
        // $font1 and $font2 are intenionally not both of the same type.
144
        // One is a mock and the other one a real instance of Font.
145
        // This way we can simply check the return value of getFonts here.
146
        // If both were one of the other, we had to use a different assertation approach.
147
        $this->assertEquals([$font2], $page1->getFonts());
148
    }
149
}
150