1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Passbook\Tests; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Passbook\Exception\PassInvalidException; |
8
|
|
|
use Passbook\Pass; |
9
|
|
|
use Passbook\Apple\Localization; |
10
|
|
|
use Passbook\ApplePassFactory; |
11
|
|
|
use Passbook\PassValidator; |
12
|
|
|
use Passbook\Type\EventTicket; |
13
|
|
|
use Passbook\Apple\Field; |
14
|
|
|
use Passbook\Apple\NumberField; |
15
|
|
|
use Passbook\Apple\Barcode; |
16
|
|
|
use Passbook\Apple\Image; |
17
|
|
|
use Passbook\Apple\PassFields; |
18
|
|
|
use Passbook\Apple\Structure; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class ApplePassFactoryTest |
23
|
|
|
* @package Passbook\Tests |
24
|
|
|
*/ |
25
|
|
|
class ApplePassFactoryTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var ApplePassFactory |
29
|
|
|
*/ |
30
|
|
|
protected $factory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var boolean |
34
|
|
|
*/ |
35
|
|
|
private $skipPackageTest = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
40
|
|
|
protected function setUp(): void |
41
|
|
|
{ |
42
|
|
|
// The PassFactory defaults can be overwritten by setting environment |
43
|
|
|
// variables or through phpunit configuration when testing. |
44
|
|
|
|
45
|
|
|
$p12File = getenv('P12_CERT_PATH') ?: __DIR__ . '/../../cert/pass.com.example.testpass.p12'; |
46
|
|
|
$p12Pass = getenv('P12_CERT_PASS') ?: '123456'; |
47
|
|
|
$wwdrFile = getenv('WWDR_CERT_PATH') ?: __DIR__ . '/../../cert/wwdr.pem'; |
48
|
|
|
$passTypeIdentifier = getenv('PASS_TYPE_ID') ?: 'pass-type-identifier'; |
49
|
|
|
$teamIdentifier = getenv('TEAM_ID') ?: 'team-identifier'; |
50
|
|
|
$orgName = getenv('ORG_NAME') ?: 'organization-name'; |
51
|
|
|
|
52
|
|
|
$this->factory = new ApplePassFactory($passTypeIdentifier, $teamIdentifier, $orgName, $p12File, $p12Pass, $wwdrFile); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Factory methods |
57
|
|
|
*/ |
58
|
|
|
public function testFactoryMethods() |
59
|
|
|
{ |
60
|
|
|
$this->factory->setOverwrite(true); |
61
|
|
|
$this->assertTrue($this->factory->isOverwrite()); |
62
|
|
|
|
63
|
|
|
$this->factory->setOutputPath('/tmp'); |
64
|
|
|
$this->assertEquals($this->factory->getOutputPath(), '/tmp'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Factory package |
69
|
|
|
* @throws Exception |
70
|
|
|
*/ |
71
|
|
|
public function testFactoryPackage() |
72
|
|
|
{ |
73
|
|
|
// Create an event ticket |
74
|
|
|
$pass = new EventTicket(time(), 'The Beat Goes On'); |
75
|
|
|
$pass->setBackgroundColor('rgb(60, 65, 76)'); |
76
|
|
|
$pass->setLogoText('Apple Inc.'); |
77
|
|
|
|
78
|
|
|
// Create pass structure |
79
|
|
|
// $structure = new PassFields(); |
80
|
|
|
$structure = new Structure(); |
|
|
|
|
81
|
|
|
|
82
|
|
|
// Add primary field |
83
|
|
|
$primary = new Field('event', 'The Beat Goes On'); |
84
|
|
|
$primary->setLabel('Event'); |
85
|
|
|
$structure->addPrimaryField($primary); |
86
|
|
|
|
87
|
|
|
// Add secondary field |
88
|
|
|
$secondary = new Field('location', 'Moscone West'); |
89
|
|
|
$secondary->setLabel('Location'); |
90
|
|
|
$structure->addSecondaryField($secondary); |
91
|
|
|
|
92
|
|
|
// Add auxiliary field |
93
|
|
|
$auxiliary = new Field('datetime', '2013-04-15 @10:25'); |
94
|
|
|
$auxiliary->setLabel('Date & Time'); |
95
|
|
|
$structure->addAuxiliaryField($auxiliary); |
96
|
|
|
|
97
|
|
|
$auxiliary = new NumberField('price', '12.34'); |
98
|
|
|
$auxiliary->setLabel('Price'); |
99
|
|
|
$auxiliary->setCurrencyCode('USD'); |
100
|
|
|
$structure->addAuxiliaryField($auxiliary); |
101
|
|
|
|
102
|
|
|
// Add icon image |
103
|
|
|
$icon = new Image(__DIR__ . '/../../img/icon.png', 'icon'); |
104
|
|
|
$pass->addImage($icon); |
105
|
|
|
|
106
|
|
|
// Set pass structure |
107
|
|
|
$pass->setStructure($structure); |
|
|
|
|
108
|
|
|
|
109
|
|
|
// Add barcode |
110
|
|
|
$barcode = new Barcode(Barcode::TYPE_QR, 'barcodeMessage'); |
111
|
|
|
$pass->setBarcode($barcode); |
112
|
|
|
|
113
|
|
|
// Add Localizations (this also tests zipping subdirectories) |
114
|
|
|
$englishText = [ |
115
|
|
|
'created_by' => 'Pass produced by php-passbook' |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
$spanishText = [ |
119
|
|
|
'created_by' => 'Pase producido por php-passbook' |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
$es = new Localization('es'); |
123
|
|
|
$es->addStrings($spanishText); |
124
|
|
|
$pass->addLocalization($es); |
125
|
|
|
|
126
|
|
|
$en = new Localization('en'); |
127
|
|
|
$en->addStrings($englishText); |
128
|
|
|
$pass->addLocalization($en); |
129
|
|
|
|
130
|
|
|
$field = new Field('exclusive_card', 'created_by'); |
131
|
|
|
$structure->addBackField($field); |
132
|
|
|
|
133
|
|
|
if ($this->skipPackageTest) { |
134
|
|
|
$this->markTestIncomplete( |
135
|
|
|
'P12 and/or WWDR certificate(s) not found' |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->factory->setOutputPath(__DIR__ . '/../../../www/passes'); |
140
|
|
|
$file = $this->factory->package($pass); |
141
|
|
|
$this->assertInstanceOf('SplFileObject', $file); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @throws InvalidArgumentException|Exception |
146
|
|
|
*/ |
147
|
|
|
public function testPackagePassWithoutSerialNumberThrowsException() |
148
|
|
|
{ |
149
|
|
|
$this->expectException(InvalidArgumentException::class); |
150
|
|
|
$pass = new Pass('', 'pass without a serial number'); |
151
|
|
|
|
152
|
|
|
$this->factory->setOutputPath('/tmp'); |
153
|
|
|
$this->factory->package($pass); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @throws Exception |
158
|
|
|
*/ |
159
|
|
|
public function testRequiredInformationInPassNotOverwrittenByFactory() |
160
|
|
|
{ |
161
|
|
|
$passOrganizationName = 'organization name in pass'; |
162
|
|
|
$passTeamIdentifier = 'team identifier in pass'; |
163
|
|
|
$passPassTypeIdentifier = 'pass type identifier in pass'; |
164
|
|
|
|
165
|
|
|
$pass = new Pass('serial_number', 'description'); |
166
|
|
|
$pass->setOrganizationName($passOrganizationName); |
167
|
|
|
$pass->setTeamIdentifier($passTeamIdentifier); |
168
|
|
|
$pass->setPassTypeIdentifier($passPassTypeIdentifier); |
169
|
|
|
|
170
|
|
|
// Icon is required |
171
|
|
|
$icon = new Image(__DIR__ . '/../../img/icon.png', 'icon'); |
172
|
|
|
$pass->addImage($icon); |
173
|
|
|
|
174
|
|
|
$this->factory->setOutputPath('/tmp'); |
175
|
|
|
$this->factory->setOverwrite(true); |
176
|
|
|
$this->factory->setSkipSignature(true); |
177
|
|
|
$this->factory->package($pass); |
178
|
|
|
|
179
|
|
|
self::assertEquals($passOrganizationName, $pass->getOrganizationName()); |
180
|
|
|
self::assertEquals($passTeamIdentifier, $pass->getTeamIdentifier()); |
181
|
|
|
self::assertEquals($passPassTypeIdentifier, $pass->getPassTypeIdentifier()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* |
186
|
|
|
*/ |
187
|
|
|
public function testNormalizedOutputPath() |
188
|
|
|
{ |
189
|
|
|
$s = DIRECTORY_SEPARATOR; |
190
|
|
|
|
191
|
|
|
$this->factory->setOutputPath("path-ending-with-separator{$s}"); |
192
|
|
|
self::assertEquals("path-ending-with-separator{$s}", $this->factory->getNormalizedOutputPath()); |
193
|
|
|
|
194
|
|
|
$this->factory->setOutputPath('path-not-ending-with-separator'); |
195
|
|
|
self::assertEquals("path-not-ending-with-separator{$s}", $this->factory->getNormalizedOutputPath()); |
196
|
|
|
|
197
|
|
|
$this->factory->setOutputPath("path-ending-with-multiple-separators{$s}{$s}"); |
198
|
|
|
self::assertEquals("path-ending-with-multiple-separators{$s}", $this->factory->getNormalizedOutputPath()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @throws PassInvalidException|Exception |
203
|
|
|
*/ |
204
|
|
|
public function testPassThatFailsValidationThrowsException() |
205
|
|
|
{ |
206
|
|
|
$this->expectException(PassInvalidException::class); |
207
|
|
|
$this->factory->setPassValidator(new PassValidator()); |
208
|
|
|
|
209
|
|
|
$invalidPass = new Pass('serial number', 'description'); |
210
|
|
|
$this->factory->package($invalidPass); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @throws Exception |
215
|
|
|
*/ |
216
|
|
|
public function testSpecifyPassName() |
217
|
|
|
{ |
218
|
|
|
// Make sure the file doesn't already exist as that would invalidate the test. |
219
|
|
|
if (file_exists('/tmp/passname.pkpass')) { |
220
|
|
|
unlink('/tmp/passname.pkpass'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$pass = new Pass('serial number', 'description'); |
224
|
|
|
|
225
|
|
|
// Icon is required |
226
|
|
|
$icon = new Image(__DIR__ . '/../../img/icon.png', 'icon'); |
227
|
|
|
$pass->addImage($icon); |
228
|
|
|
|
229
|
|
|
$this->factory->setOutputPath('/tmp'); |
230
|
|
|
$this->factory->setSkipSignature(true); |
231
|
|
|
$this->factory->package($pass, 'pass name'); |
232
|
|
|
|
233
|
|
|
self::assertTrue(file_exists('/tmp/passname.pkpass')); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|