1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
use Behat\Behat\Context\Context; |
6
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
7
|
|
|
use Behat\Gherkin\Node\TableNode; |
8
|
|
|
use Behat\Behat\Tester\Exception\PendingException; |
9
|
|
|
use iio\libmergepdf\Merger; |
10
|
|
|
use iio\libmergepdf\Pages; |
11
|
|
|
use Smalot\PdfParser\Parser as PdfParser; |
12
|
|
|
|
13
|
|
|
final class FeatureContext implements Context |
14
|
|
|
{ |
15
|
|
|
/** @var Merger */ |
16
|
|
|
private $merger; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $generatedPdf = ''; |
20
|
|
|
|
21
|
|
|
/** @var ?\Exception */ |
22
|
|
|
private $mergeException; |
23
|
|
|
|
24
|
|
|
public function __construct(string $driverName) |
25
|
|
|
{ |
26
|
|
|
$driverClass = "iio\libmergepdf\Driver\\$driverName"; |
27
|
|
|
|
28
|
|
|
/** @var \iio\libmergepdf\Driver\DriverInterface $driver */ |
29
|
|
|
$driver = new $driverClass; |
30
|
|
|
|
31
|
|
|
$this->merger = new Merger($driver); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @Given the :driver driver |
36
|
|
|
*/ |
37
|
|
|
public function theDriver(string $driver) |
38
|
|
|
{ |
39
|
|
|
$driverClass = "iio\libmergepdf\Driver\\$driver"; |
40
|
|
|
$this->merger = new Merger(new $driverClass); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @Given a pdf |
45
|
|
|
*/ |
46
|
|
|
public function aPdf() |
47
|
|
|
{ |
48
|
|
|
$this->aPdfOfVersion('1.4'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @Given a pdf with pages :pages |
53
|
|
|
*/ |
54
|
|
|
public function aPdfWithPages($pages) |
55
|
|
|
{ |
56
|
|
|
$this->aPdfOfVersionWithPages('1.4', $pages); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @Given a pdf of version :version |
61
|
|
|
*/ |
62
|
|
|
public function aPdfOfVersion(string $version) |
63
|
|
|
{ |
64
|
|
|
$this->aPdfOfVersionWithPages($version, ''); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @Given a pdf of version :version with pages :pages |
69
|
|
|
*/ |
70
|
|
|
public function aPdfOfVersionWithPages(string $version, string $pages) |
71
|
|
|
{ |
72
|
|
|
$this->merger->addFile(__DIR__ . "/../files/$version.pdf", new Pages($pages)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @Given a pdf with a header including text HEADER |
77
|
|
|
*/ |
78
|
|
|
public function aPdfWithAHeaderIncludingTextHeader() |
79
|
|
|
{ |
80
|
|
|
$this->merger->addFile(__DIR__ . "/../files/header.pdf"); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @Given a blank pdf |
85
|
|
|
*/ |
86
|
|
|
public function aBlankPdf() |
87
|
|
|
{ |
88
|
|
|
$this->merger->addFile(__DIR__ . "/../files/blank.pdf"); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @When I merge |
93
|
|
|
*/ |
94
|
|
|
public function iMerge() |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
|
|
$this->generatedPdf = $this->merger->merge(); |
98
|
|
|
$this->mergeException = null; |
99
|
|
|
} catch (\Exception $e) { |
100
|
|
|
$this->mergeException = $e; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @Then there is no error |
106
|
|
|
*/ |
107
|
|
|
public function thereIsNoError() |
108
|
|
|
{ |
109
|
|
|
if ($this->mergeException) { |
110
|
|
|
throw $this->mergeException; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @Then there is an error |
116
|
|
|
*/ |
117
|
|
|
public function thereIsAnError() |
118
|
|
|
{ |
119
|
|
|
if (!$this->mergeException) { |
120
|
|
|
throw new \Exception('Expecting error during merge'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @Then a pdf is generated |
126
|
|
|
*/ |
127
|
|
|
public function aPdfIsGenerated() |
128
|
|
|
{ |
129
|
|
|
$this->thereIsNoError(); |
130
|
|
|
(new PdfParser)->parseContent($this->generatedPdf); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @Then a pdf with :expectedCount pages is generated |
135
|
|
|
*/ |
136
|
|
|
public function aPdfWithPagesIsGenerated(string $expectedCount) |
137
|
|
|
{ |
138
|
|
|
$this->thereIsNoError(); |
139
|
|
|
|
140
|
|
|
$pageCount = @count((new PdfParser)->parseContent($this->generatedPdf)->getPages()); |
141
|
|
|
|
142
|
|
|
if ($pageCount != $expectedCount) { |
143
|
|
|
throw new Exception("A pdf with $pageCount pages was created, expected $expectedCount pages."); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @Then a pdf including text :expectedText is generated |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
public function aPdfIncludingTextIsGenerated(string $expectedText) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$this->thereIsNoError(); |
153
|
|
|
|
154
|
|
|
$text = @(new PdfParser)->parseContent($this->generatedPdf)->getText(); |
155
|
|
|
|
156
|
|
|
$regexp = preg_quote($expectedText, '/'); |
157
|
|
|
|
158
|
|
|
if (!preg_match("/$regexp/", $text)) { |
159
|
|
|
throw new Exception("A pdf with text '$text' was created, expected '$expectedText'."); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @Then a pdf not including text :unexpectedText is generated |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
public function aPdfNotIncludingTextIsGenerated(string $unexpectedText) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$this->thereIsNoError(); |
169
|
|
|
|
170
|
|
|
$text = @(new PdfParser)->parseContent($this->generatedPdf)->getText(); |
171
|
|
|
|
172
|
|
|
$regexp = preg_quote($unexpectedText, '/'); |
173
|
|
|
|
174
|
|
|
if (preg_match("/$regexp/", $text)) { |
175
|
|
|
throw new Exception("A pdf with unexpected text '$unexpectedText' was created."); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.