1
+ <?php declare (strict_types=1 );
2
+
3
+ use PHPUnit \Framework \TestCase ;
4
+ use PKPass \FinanceOrder ;
5
+
6
+ final class FinanceOrderTest extends TestCase
7
+ {
8
+ private function validateOrder ($ orer , $ expected_files = [])
9
+ {
10
+ // basic string validation
11
+ $ this ->assertIsString ($ orer );
12
+ $ this ->assertGreaterThan (100 , strlen ($ orer ));
13
+ $ this ->assertStringContainsString ('logo.png ' , $ orer );
14
+ $ this ->assertStringContainsString ('ws03-xs-red.jpg ' , $ orer );
15
+ $ this ->assertStringContainsString ('manifest.json ' , $ orer );
16
+
17
+ // try to read the ZIP file
18
+ $ temp_name = tempnam (sys_get_temp_dir (), 'pkpass ' );
19
+ file_put_contents ($ temp_name , $ orer );
20
+ $ zip = new ZipArchive ();
21
+ $ res = $ zip ->open ($ temp_name );
22
+ $ this ->assertTrue ($ res , 'Invalid ZIP file. ' );
23
+ $ this ->assertEquals (count ($ expected_files ), $ zip ->numFiles );
24
+
25
+ // extract zip to temp dir
26
+ $ temp_dir = $ temp_name . '_dir ' ;
27
+ mkdir ($ temp_dir );
28
+ $ zip ->extractTo ($ temp_dir );
29
+ $ zip ->close ();
30
+ echo $ temp_dir ;
31
+ foreach ($ expected_files as $ file ) {
32
+ $ this ->assertFileExists ($ temp_dir . DIRECTORY_SEPARATOR . $ file );
33
+ }
34
+ }
35
+
36
+ public function testBasicGeneration ()
37
+ {
38
+ $ pass = new FinanceOrder (__DIR__ . '/fixtures/example-certificate.p12 ' , 'password ' );
39
+ $ pass ->setData ([
40
+ "createdAt " => "2024-02-01T19:45:50+00:00 " ,
41
+ "merchant " => [
42
+ "displayName " => "Luma " ,
43
+ "merchantIdentifier " => "merchant.com.pkpass.unit-test " ,
44
+ "url " => "https://demo-store.test/ " ,
45
+ 'logo ' => 'logo.png ' ,
46
+ ],
47
+ "orderIdentifier " => "1 " ,
48
+ "orderManagementURL " => "https://demo-store.test/sales/order/view " ,
49
+ 'orderNumber ' => '#000000001 ' ,
50
+ "orderType " => "ecommerce " ,
51
+ "orderTypeIdentifier " => "order.com.pkpass.unit-test " ,
52
+ 'payment ' => [
53
+ 'summaryItems ' => [
54
+ [
55
+ 'label ' => 'Shipping & Handling ' ,
56
+ 'value ' => [
57
+ 'amount ' => '5.00 ' ,
58
+ 'currency ' => 'USD ' ,
59
+ ]
60
+ ],
61
+ ],
62
+ 'total ' => [
63
+ 'amount ' => '36.39 ' ,
64
+ 'currency ' => 'USB ' ,
65
+ ],
66
+ 'status ' => 'paid '
67
+ ],
68
+ "status " => "open " ,
69
+ "updatedAt " => "2024-02-01T19:45:50+00:00 " ,
70
+ 'customer ' => [
71
+ 'emailAddress ' => 'roni_cost@example.com ' ,
72
+ 'familyName ' => 'Veronica ' ,
73
+ 'givenName ' => 'Costello ' ,
74
+ ],
75
+ 'lineItems ' => [
76
+ [
77
+ 'image ' => 'ws03-xs-red.jpg ' ,
78
+ 'price ' => [
79
+ 'amount ' => '31.39 ' ,
80
+ 'currency ' => 'USD ' ,
81
+ ],
82
+ 'quantity ' => 1 ,
83
+ 'title ' => 'Iris Workout Top ' ,
84
+ 'sku ' => 'WS03-XS-Red ' ,
85
+ ],
86
+ ],
87
+ "schemaVersion " => 1 ,
88
+ ]);
89
+ $ pass ->addFile (__DIR__ . '/fixtures/order/logo.png ' );
90
+ $ pass ->addFile (__DIR__ . '/fixtures/order/ws03-xs-red.jpg ' );
91
+ $ value = $ pass ->create ();
92
+ $ this ->validateOrder ($ value , [
93
+ 'logo.png ' ,
94
+ 'ws03-xs-red.jpg ' ,
95
+ 'manifest.json ' ,
96
+ 'order.json ' ,
97
+ 'signature ' ,
98
+ ]);
99
+ }
100
+ }
0 commit comments