|
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
|
|
|
public function testFontsArePassedFromPagesToPage(): void |
|
64
|
|
|
{ |
|
65
|
|
|
// Create mock Document, Font and Page objects |
|
66
|
|
|
$document = $this->createMock(Document::class); |
|
67
|
|
|
$font1 = new Font($document); |
|
68
|
|
|
$page = new Page($document); |
|
69
|
|
|
|
|
70
|
|
|
// Create a Header object that indicates $page is a child |
|
71
|
|
|
$header = new Header([ |
|
72
|
|
|
'Kids' => new ElementArray([ |
|
73
|
|
|
$page, |
|
74
|
|
|
]), |
|
75
|
|
|
], $document); |
|
76
|
|
|
|
|
77
|
|
|
// Use this header to create a mock Pages object |
|
78
|
|
|
$pages = new PagesDummy($document, $header); |
|
79
|
|
|
|
|
80
|
|
|
// Apply $font1 as a Font object to this Pages object; |
|
81
|
|
|
// setFonts is used here as part of PagesDummy, only to access |
|
82
|
|
|
// the protected Pages::fonts variable; it is not a method |
|
83
|
|
|
// available in production |
|
84
|
|
|
$pages->setFonts([$font1]); |
|
85
|
|
|
|
|
86
|
|
|
// Trigger setupFonts method in $pages |
|
87
|
|
|
$pages->getPages(true); |
|
88
|
|
|
|
|
89
|
|
|
// Since the $page object font list is empty, $font1 from Pages |
|
90
|
|
|
// object must be passed to the Page object |
|
91
|
|
|
$this->assertEquals([$font1], $page->getFonts()); |
|
92
|
|
|
|
|
93
|
|
|
// Create a second $font2 using a different method |
|
94
|
|
|
$font2 = $this->createMock(Font::class); |
|
95
|
|
|
|
|
96
|
|
|
// Update the fonts in $pages |
|
97
|
|
|
$pages->setFonts([$font1, $font2]); |
|
98
|
|
|
|
|
99
|
|
|
// Trigger setupFonts method in $pages |
|
100
|
|
|
$pages->getPages(true); |
|
101
|
|
|
|
|
102
|
|
|
// Now that $page already has a font, updates from $pages |
|
103
|
|
|
// should not overwrite it |
|
104
|
|
|
$this->assertEquals([$font1], $page->getFonts()); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|