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

PagesDummy   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 13
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 2
c 2
b 0
f 1
dl 0
loc 13
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A setFonts() 0 3 1
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
     *        |   `--- Font[]
68
     *        |
69
     *        |
70
     *        `--+ Page1 (no fonts)
71
     *        `--+ ...
72
     *
73
     * @see https://github.com/smalot/pdfparser/pull/698
74
     */
75
    public function testPullRequest698NoFontsSet(): void
76
    {
77
        $document = $this->createMock(Document::class);
78
79
        // Create a Page mock and tell PHPUnit that its setFonts has to be called once
80
        // otherwise an error is raised
81
        $page1 = $this->createMock(Page::class);
82
        $page1->expects($this->once())->method('setFonts');
83
84
        // setup header
85
        $header = new Header([
86
            'Kids' => new ElementArray([
87
                $page1,
88
            ]),
89
        ], $document);
90
91
        $font1 = $this->createMock(Font::class);
92
93
        // Preset fonts variable so we don't have to prepare all the
94
        // prerequisites manually (like creating a Ressources instance
95
        // with Font instances, see Pages::setupFonts())
96
        $pages = new PagesDummy($document, $header);
97
        $pages->setFonts([$font1]);
98
99
        // We expect setFonts is called on $page1, therefore no assertion here
100
        $pages->getPages(true);
101
    }
102
103
    /**
104
     * Dont override fonts list in a Page instance, if available.
105
     *
106
     *      Pages
107
     *        |   `--- Font[]           <=== less important than fonts in Page instance
108
     *        |
109
     *        |
110
     *        `--+ Page1
111
     *                  `--- Font[]     <=== must be kept
112
     *        `--+ ...
113
     *
114
     * @see https://github.com/smalot/pdfparser/pull/698
115
     */
116
    public function testPullRequest698DontOverride(): void
117
    {
118
        $document = $this->createMock(Document::class);
119
120
        // create a Page mock and tell PHPUnit that its setFonts has to be called once
121
        // otherwise an error is raised
122
        $font2 = new Font($document);
123
        $page1 = new Page($document);
124
        $page1->setFonts([$font2]);
125
126
        // setup header
127
        $header = new Header([
128
            'Kids' => new ElementArray([
129
                $page1,
130
            ]),
131
        ], $document);
132
133
        $font1 = $this->createMock(Font::class);
134
135
        $pages = new PagesDummy($document, $header);
136
        $pages->setFonts([$font1]);
137
138
        // Trigger setupFonts method in $pages
139
        $pages->getPages(true);
140
141
        // Note: 
142
        // $font1 and $font2 are intenionally not both of the same type.
143
        // One is a mock and the other one a real instance of Font.
144
        // This way we can simply check the return value of getFonts here.
145
        // If both were one of the other, we had to use a different assertation approach.
146
        $this->assertEquals([$font2], $page1->getFonts());
147
    }
148
}
149