Completed
Push — master ( 9e5240...db4f23 )
by
unknown
02:08
created

PassFactoryTest::setUp()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 26
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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