Skip to content

Commit 5b9bf17

Browse files
committed
Extending tests to reach 100% unit-test coverage.
1 parent 6c7ae87 commit 5b9bf17

File tree

2 files changed

+139
-1
lines changed

2 files changed

+139
-1
lines changed

tests/GatewayTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ public function testSHASign()
3636
'ORDERID' => '1234',
3737
'AMOUNT' => 1500,
3838
'CURRENCY' => 'EUR',
39-
'LANGUAGE' => 'en_US'
39+
'LANGUAGE' => 'en_US',
40+
// add a bogus SHASIGN data, it should be ignored during calculation of a hash
41+
'SHASIGN' => 'bogus',
42+
// add an empty value. Empty fields should be ignored as well!
43+
'EMPTY_MUST_IGNORE' => ''
4044
);
4145

4246
// reconstruct the hashes from the official documentation and ensure we calculate the same hashes
@@ -87,10 +91,29 @@ public function testPurchase()
8791
{
8892
$response = $this->gateway->purchase($this->options)->send();
8993

94+
// Expected redirect-data for the default options
95+
$data = array(
96+
'PSPID' => 'testPspId',
97+
'ORDERID' => '1',
98+
'AMOUNT' => 1000,
99+
'CURRENCY' => 'CHF',
100+
'LANGUAGE' => 'en_US',
101+
'COM' => null,
102+
'ACCEPTURL' => 'https://www.example.com/return',
103+
'CANCELURL' => null,
104+
'EXCEPTIONURL' => null,
105+
'DECLINEURL' => null,
106+
'OPERATION' => ''
107+
);
108+
109+
// sign the data
110+
$data['SHASIGN'] = Helper::create_sha_hash($data, $this->gateway->getShaIn());
111+
90112
$this->assertInstanceOf('\Omnipay\Postfinance\Message\PurchaseResponse', $response);
91113
$this->assertFalse($response->isSuccessful());
92114
$this->assertTrue($response->isRedirect());
93115
$this->assertEquals('POST', $response->getRedirectMethod());
116+
$this->assertEquals($data, $response->getRedirectData());
94117
$this->assertStringStartsWith('https://e-payment.postfinance.ch/ncol/', $response->getRedirectUrl());
95118
}
96119

@@ -119,6 +142,7 @@ public function testCompletePurchaseSuccess()
119142
$this->assertFalse($response->isRedirect());
120143
$this->assertTrue($response->isSuccessful());
121144
$this->assertEquals('32100123', $response->getTransactionReference());
145+
$this->assertEquals('', $response->getMessage());
122146
}
123147

124148

@@ -139,6 +163,7 @@ public function testCompletePurchaseError()
139163
$response = $this->gateway->completePurchase($this->options)->send();
140164

141165
$this->assertFalse($response->isSuccessful());
166+
$this->assertEquals(500, $response->getMessage());
142167
}
143168

144169
/**

tests/Message/PurchaseRequestTest.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace Omnipay\Postfinance\Message;
4+
5+
use Omnipay\Common\CreditCard;
6+
use Omnipay\Tests\TestCase;
7+
8+
class PurchaseRequestTest extends TestCase
9+
{
10+
/**
11+
* @var PurchaseRequest
12+
*/
13+
private $request;
14+
15+
public function setUp()
16+
{
17+
parent::setUp();
18+
19+
$this->request = new PurchaseRequest($this->getHttpClient(), $this->getHttpRequest());
20+
}
21+
22+
public function testGetDataWithoutCard()
23+
{
24+
$this->request->initialize(array(
25+
'pspId' => 'testPspId',
26+
'language' => 'en_US',
27+
'shaIn' => 'MyShaInSecret',
28+
'amount' => '12.00',
29+
'currency' => 'CHF',
30+
'transactionId' => '123',
31+
'description' => 'Order Description',
32+
'returnUrl' => 'https://www.example.com/return',
33+
'cancelUrl' => 'https://www.example.com/cancel',
34+
'title' => 'My shop title'
35+
));
36+
37+
$expected = array(
38+
'PSPID' => 'testPspId',
39+
'LANGUAGE' => 'en_US',
40+
// Currency has to be converted to integer, so 12.00 becomes 1200
41+
'AMOUNT' => 1200,
42+
'CURRENCY' => 'CHF',
43+
// transactionId maps to ORDERID
44+
'ORDERID' => '123',
45+
// description maps to COM
46+
'COM' => 'Order Description',
47+
'ACCEPTURL' => 'https://www.example.com/return',
48+
'CANCELURL' => 'https://www.example.com/cancel',
49+
'EXCEPTIONURL' => null,
50+
'DECLINEURL' => null,
51+
'OPERATION' => null,
52+
'TITLE' => 'My shop title'
53+
);
54+
55+
$this->assertEquals($expected, $this->request->getData());
56+
}
57+
58+
public function testGetDataWithCard()
59+
{
60+
$this->request->initialize(array(
61+
'pspId' => 'testPspId',
62+
'language' => 'en_US',
63+
'shaIn' => 'MyShaInSecret',
64+
'amount' => '12.00',
65+
'currency' => 'CHF',
66+
'transactionId' => '123',
67+
'description' => 'Order Description',
68+
'returnUrl' => 'https://www.example.com/return',
69+
'cancelUrl' => 'https://www.example.com/cancel'
70+
));
71+
72+
$card = new CreditCard(array(
73+
'name' => 'Hans Muster',
74+
'address1' => 'Teststrasse 123',
75+
'address2' => 'Postfach 321',
76+
'city' => 'Bern',
77+
'country' => 'CH',
78+
'postcode' => '3000',
79+
'phone' => '098 765 43 21',
80+
'email' => 'test@test.ch',
81+
));
82+
$this->request->setCard($card);
83+
84+
$expected = array(
85+
'PSPID' => 'testPspId',
86+
'LANGUAGE' => 'en_US',
87+
// Currency has to be converted to integer, so 12.00 becomes 1200
88+
'AMOUNT' => 1200,
89+
'CURRENCY' => 'CHF',
90+
// transactionId maps to ORDERID
91+
'ORDERID' => '123',
92+
// description maps to COM
93+
'COM' => 'Order Description',
94+
'ACCEPTURL' => 'https://www.example.com/return',
95+
'CANCELURL' => 'https://www.example.com/cancel',
96+
'EXCEPTIONURL' => null,
97+
'DECLINEURL' => null,
98+
'OPERATION' => null,
99+
100+
// Credit card data
101+
'CN' => 'Hans Muster',
102+
'EMAIL' => 'test@test.ch',
103+
'OWNERADDRESS' => 'Teststrasse 123 / Postfach 321',
104+
'OWNERZIP' => '3000',
105+
'OWNERTOWN' => 'Bern',
106+
'OWNERCTY' => 'CH',
107+
'OWNERTELNO' => '098 765 43 21'
108+
);
109+
110+
$this->assertEquals($expected, $this->request->getData());
111+
}
112+
113+
}

0 commit comments

Comments
 (0)