Completed
Push — master ( 07cf6a...9e5240 )
by
unknown
02:11
created

PassFactoryTest::testFactoryPackage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 71
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 71
rs 9.1369
cc 2
eloc 41
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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