Open Monograph Press  3.3.0
AbstractRequestTest.php
1 <?php
2 
3 namespace Omnipay\Common\Message;
4 
5 use Mockery as m;
8 use Omnipay\Tests\TestCase;
9 
10 class AbstractRequestTest extends TestCase
11 {
12  public function setUp()
13  {
14  $this->request = m::mock('\Omnipay\Common\Message\AbstractRequest')->makePartial();
15  $this->request->initialize();
16  }
17 
24  private function changeProtectedProperty($property, $value = true)
25  {
26  $reflection = new \ReflectionClass($this->request);
27 
28  $reflection_property = $reflection->getProperty($property);
29  $reflection_property->setAccessible(true);
30  $reflection_property->setValue($this->request, $value);
31  $reflection_property->setAccessible(false);
32  }
33 
34  public function testConstruct()
35  {
36  $this->request = new AbstractRequestTest_MockAbstractRequest($this->getHttpClient(), $this->getHttpRequest());
37  $this->assertSame(array(), $this->request->getParameters());
38  }
39 
40  public function testInitializeWithParams()
41  {
42  $this->assertSame($this->request, $this->request->initialize(array('amount' => '1.23')));
43  $this->assertSame('1.23', $this->request->getAmount());
44  }
45 
51  {
52  $this->request = new AbstractRequestTest_MockAbstractRequest($this->getHttpClient(), $this->getHttpRequest());
53  $this->request->send();
54 
55  $this->request->initialize();
56  }
57 
58  public function testCard()
59  {
60  // no type checking on card parameter
61  $card = new CreditCard;
62  $this->assertSame($this->request, $this->request->setCard($card));
63  $this->assertSame($card, $this->request->getCard());
64  }
65 
66  public function testSetCardWithArray()
67  {
68  // passing array should create CreditCard object
69  $this->assertSame($this->request, $this->request->setCard(array('number' => '1234')));
70 
71  $card = $this->request->getCard();
72  $this->assertInstanceOf('\Omnipay\Common\CreditCard', $card);
73  $this->assertSame('1234', $card->getNumber());
74  }
75 
76  public function testToken()
77  {
78  $this->assertSame($this->request, $this->request->setToken('12345'));
79  $this->assertSame('12345', $this->request->getToken());
80  }
81 
82  public function testCardReference()
83  {
84  $this->assertSame($this->request, $this->request->setCardReference('12345'));
85  $this->assertSame('12345', $this->request->getCardReference());
86  }
87 
88  public function testAmount()
89  {
90  $this->assertSame($this->request, $this->request->setAmount('2.00'));
91  $this->assertSame('2.00', $this->request->getAmount());
92  }
93 
94  public function testAmountWithFloat()
95  {
96  $this->assertSame($this->request, $this->request->setAmount(2.0));
97  $this->assertSame('2.00', $this->request->getAmount());
98  }
99 
100  public function testAmountWithEmpty()
101  {
102  $this->assertSame($this->request, $this->request->setAmount(null));
103  $this->assertSame(null, $this->request->getAmount());
104  }
105 
106  public function testAmountZeroFloat()
107  {
108  $this->assertSame($this->request, $this->request->setAmount(0.0));
109  $this->assertSame('0.00', $this->request->getAmount());
110  }
111 
112  public function testAmountZeroString()
113  {
114  $this->assertSame($this->request, $this->request->setAmount('0.000000'));
115  $this->assertSame('0.00', $this->request->getAmount());
116  }
117 
122  public function testAmountZeroNotAllowed()
123  {
124  $this->changeProtectedProperty('zeroAmountAllowed', false);
125  $this->request->setAmount('0.00');
126  $this->request->getAmount();
127  }
128 
129  // See https://github.com/thephpleague/omnipay-common/issues/69
130  public function testAmountPrecision()
131  {
132  // The default precision for PHP is 6 decimal places.
133  ini_set('precision', 6);
134  $this->assertSame($this->request, $this->request->setAmount('67.10'));
135  $this->assertSame('67.10', $this->request->getAmount());
136 
137  // At 17 decimal places, 67.10 will echo as 67.09999...
138  // This is *why* PHP sets the default precision at 6.
139  ini_set('precision', 17);
140  $this->assertSame('67.10', $this->request->getAmount());
141 
142  $this->assertSame($this->request, $this->request->setAmount('67.01'));
143  $this->assertSame('67.01', $this->request->getAmount());
144 
145  $this->assertSame($this->request, $this->request->setAmount('0.10'));
146  $this->assertSame('0.10', $this->request->getAmount());
147 
148  $this->assertSame($this->request, $this->request->setAmount('0.01'));
149  $this->assertSame('0.01', $this->request->getAmount());
150  }
151 
158  public function testAmountPrecisionTooHigh()
159  {
160  $this->assertSame($this->request, $this->request->setAmount('123.005'));
161  $this->assertSame('123.005', $this->request->getAmount());
162  }
163 
164  public function testGetAmountNoDecimals()
165  {
166  $this->assertSame($this->request, $this->request->setCurrency('JPY'));
167  $this->assertSame($this->request, $this->request->setAmount('1366'));
168  $this->assertSame('1366', $this->request->getAmount());
169  }
170 
175  {
176  // There will not be any rounding; the amount is sent as requested or not at all.
177  $this->assertSame($this->request, $this->request->setAmount('136.5'));
178  $this->assertSame($this->request, $this->request->setCurrency('JPY'));
179  $this->request->getAmount();
180  }
181 
186  {
187  // ambiguous value, avoid errors upgrading from v0.9
188  $this->assertSame($this->request, $this->request->setAmount(10));
189  $this->request->getAmount();
190  }
191 
196  {
197  // ambiguous value, avoid errors upgrading from v0.9
198  // Some currencies only take integers, so an integer (in a string) should be valid.
199  $this->assertSame($this->request, $this->request->setAmount('10'));
200  $this->request->getAmount();
201  }
202 
203  public function testGetAmountInteger()
204  {
205  $this->assertSame($this->request, $this->request->setAmount('13.66'));
206  $this->assertSame(1366, $this->request->getAmountInteger());
207  }
208 
210  {
211  $this->assertSame($this->request, $this->request->setCurrency('JPY'));
212  $this->assertSame($this->request, $this->request->setAmount('1366'));
213  $this->assertSame(1366, $this->request->getAmountInteger());
214  }
215 
220  {
221  $this->assertSame($this->request, $this->request->setAmount('1,234.00'));
222  $this->request->getAmount();
223  }
224 
229  {
230  $this->assertSame($this->request, $this->request->setAmount('1.234.00'));
231  $this->request->getAmount();
232  }
233 
238  {
239  $this->assertSame($this->request, $this->request->setAmount(true));
240  $this->request->getAmount();
241  }
242 
247  {
248  $this->assertSame($this->request, $this->request->setAmount('-123.00'));
249  $this->request->getAmount();
250  }
251 
256  {
257  $this->assertSame($this->request, $this->request->setAmount(-123.00));
258  $this->request->getAmount();
259  }
260 
261  public function testCurrency()
262  {
263  $this->assertSame($this->request, $this->request->setCurrency('USD'));
264  $this->assertSame('USD', $this->request->getCurrency());
265  }
266 
267  public function testCurrencyLowercase()
268  {
269  $this->assertSame($this->request, $this->request->setCurrency('usd'));
270  $this->assertSame('USD', $this->request->getCurrency());
271  }
272 
273  public function testCurrencyNull()
274  {
275  $this->assertSame($this->request, $this->request->setCurrency(null));
276  $this->assertNull($this->request->getCurrency());
277  }
278 
279  public function testCurrencyNumeric()
280  {
281  $this->assertSame($this->request, $this->request->setCurrency('USD'));
282  $this->assertSame('840', $this->request->getCurrencyNumeric());
283  }
284 
285  public function testCurrencyDecimals()
286  {
287  $this->assertSame($this->request, $this->request->setCurrency('JPY'));
288  $this->assertSame(0, $this->request->getCurrencyDecimalPlaces());
289  }
290 
291  public function testFormatCurrency()
292  {
293  $this->assertSame('1234.00', $this->request->formatCurrency(1234));
294  }
295 
297  {
298  $this->request->setCurrency('JPY');
299  $this->assertSame('1234', $this->request->formatCurrency(1234));
300  }
301 
302  public function testDescription()
303  {
304  $this->assertSame($this->request, $this->request->setDescription('Cool product'));
305  $this->assertSame('Cool product', $this->request->getDescription());
306  }
307 
308  public function testTransactionId()
309  {
310  $this->assertSame($this->request, $this->request->setTransactionId(87));
311  $this->assertSame(87, $this->request->getTransactionId());
312  }
313 
314  public function testTransactionReference()
315  {
316  $this->assertSame($this->request, $this->request->setTransactionReference('xyz'));
317  $this->assertSame('xyz', $this->request->getTransactionReference());
318  }
319 
320  public function testItemsArray()
321  {
322  $this->assertSame($this->request, $this->request->setItems(array(
323  array('name' => 'Floppy Disk'),
324  array('name' => 'CD-ROM'),
325  )));
326 
327  $itemBag = $this->request->getItems();
328  $this->assertInstanceOf('\Omnipay\Common\ItemBag', $itemBag);
329 
330  $items = $itemBag->all();
331  $this->assertSame('Floppy Disk', $items[0]->getName());
332  $this->assertSame('CD-ROM', $items[1]->getName());
333  }
334 
335  public function testItemsBag()
336  {
337  $itemBag = new ItemBag;
338  $itemBag->add(array('name' => 'Floppy Disk'));
339 
340  $this->assertSame($this->request, $this->request->setItems($itemBag));
341  $this->assertSame($itemBag, $this->request->getItems());
342  }
343 
344  public function testClientIp()
345  {
346  $this->assertSame($this->request, $this->request->setClientIp('127.0.0.1'));
347  $this->assertSame('127.0.0.1', $this->request->getClientIp());
348  }
349 
350  public function testReturnUrl()
351  {
352  $this->assertSame($this->request, $this->request->setReturnUrl('https://www.example.com/return'));
353  $this->assertSame('https://www.example.com/return', $this->request->getReturnUrl());
354  }
355 
356  public function testCancelUrl()
357  {
358  $this->assertSame($this->request, $this->request->setCancelUrl('https://www.example.com/cancel'));
359  $this->assertSame('https://www.example.com/cancel', $this->request->getCancelUrl());
360  }
361 
362  public function testNotifyUrl()
363  {
364  $this->assertSame($this->request, $this->request->setNotifyUrl('https://www.example.com/notify'));
365  $this->assertSame('https://www.example.com/notify', $this->request->getNotifyUrl());
366  }
367 
368  public function testIssuer()
369  {
370  $this->assertSame($this->request, $this->request->setIssuer('some-bank'));
371  $this->assertSame('some-bank', $this->request->getIssuer());
372  }
373 
374  public function testPaymentMethod()
375  {
376  $this->assertSame($this->request, $this->request->setPaymentMethod('ideal'));
377  $this->assertSame('ideal', $this->request->getPaymentMethod());
378  }
379 
381  {
382  $params = array('testMode' => 'success');
383 
384  $this->request->initialize($params);
385 
386  $this->assertSame($this->request->getTestMode(), 'success');
387  }
388 
389  public function testGetParameters()
390  {
391  $this->request->setTestMode(true);
392  $this->request->setToken('asdf');
393 
394  $expected = array(
395  'testMode' => true,
396  'token' => 'asdf',
397  );
398  $this->assertEquals($expected, $this->request->getParameters());
399  }
400 
406  {
407  $this->request = new AbstractRequestTest_MockAbstractRequest($this->getHttpClient(), $this->getHttpRequest());
408  $this->request->send();
409 
410  $this->request->setCurrency('PHP');
411  }
412 
414  {
415  $this->request->setTestMode(true);
416  $this->request->setToken('asdf');
417 
418  $this->assertNull($this->request->validate('testMode', 'token'));
419  }
420 
425  {
426  $this->request->setTestMode(true);
427 
428  $this->request->validate('testMode', 'token');
429  }
430 
432  {
433  $this->assertNull($this->request->getCurrencyNumeric());
434  }
435 
436  public function testSend()
437  {
438  $response = m::mock('\Omnipay\Common\Message\ResponseInterface');
439  $data = array('request data');
440 
441  $this->request->shouldReceive('getData')->once()->andReturn($data);
442  $this->request->shouldReceive('sendData')->once()->with($data)->andReturn($response);
443 
444  $this->assertSame($response, $this->request->send());
445  }
446 
452  {
453  $this->request = new AbstractRequestTest_MockAbstractRequest($this->getHttpClient(), $this->getHttpRequest());
454  $this->request->getResponse();
455  }
456 
458  {
459  $this->request = new AbstractRequestTest_MockAbstractRequest($this->getHttpClient(), $this->getHttpRequest());
460  $this->request->send();
461 
462  $response = $this->request->getResponse();
463  $this->assertInstanceOf('\Omnipay\Common\Message\ResponseInterface', $response);
464  }
465 }
466 
468 {
469  public function getData() {}
470 
471  public function sendData($data)
472  {
473  $this->response = m::mock('\Omnipay\Common\Message\AbstractResponse');
474  }
475 }
Omnipay\Common\Message\AbstractRequestTest\testAmountWithIntStringThrowsException
testAmountWithIntStringThrowsException()
Definition: AbstractRequestTest.php:195
Omnipay\Common\Message\AbstractRequestTest\testPaymentMethod
testPaymentMethod()
Definition: AbstractRequestTest.php:374
Omnipay\Common\Message\AbstractRequestTest\testClientIp
testClientIp()
Definition: AbstractRequestTest.php:344
Omnipay\Common\Message\AbstractRequestTest\setUp
setUp()
Definition: AbstractRequestTest.php:12
Omnipay\Common\Message\AbstractRequestTest\testReturnUrl
testReturnUrl()
Definition: AbstractRequestTest.php:350
Omnipay\Common\Message\AbstractRequest
Definition: lib/vendor/omnipay/common/src/Omnipay/Common/Message/AbstractRequest.php:62
Omnipay\Common\Message\AbstractRequestTest\testDescription
testDescription()
Definition: AbstractRequestTest.php:302
Omnipay\Common\Message\AbstractRequestTest\testGetAmountInteger
testGetAmountInteger()
Definition: AbstractRequestTest.php:203
Omnipay\Common\Message\AbstractRequestTest\testGetParameters
testGetParameters()
Definition: AbstractRequestTest.php:389
Omnipay\Common\Message\AbstractRequestTest\testGetAmountNoDecimalsRounding
testGetAmountNoDecimalsRounding()
Definition: AbstractRequestTest.php:174
Omnipay\Common\Message\AbstractRequestTest\testCancelUrl
testCancelUrl()
Definition: AbstractRequestTest.php:356
Omnipay\Common\Message\AbstractRequestTest\testGetResponseBeforeRequestSent
testGetResponseBeforeRequestSent()
Definition: AbstractRequestTest.php:451
Omnipay\Common\Message\AbstractRequestTest\testCard
testCard()
Definition: AbstractRequestTest.php:58
Omnipay\Common\Message\AbstractRequestTest\testAmountWithEmpty
testAmountWithEmpty()
Definition: AbstractRequestTest.php:100
Omnipay\Common\Message\AbstractRequestTest\testInitializedParametersAreSet
testInitializedParametersAreSet()
Definition: AbstractRequestTest.php:380
Omnipay\Common\Message\AbstractRequestTest\testSetParameterAfterRequestSent
testSetParameterAfterRequestSent()
Definition: AbstractRequestTest.php:405
Omnipay\Common\Message\AbstractRequestTest\testCurrencyDecimals
testCurrencyDecimals()
Definition: AbstractRequestTest.php:285
Omnipay\Common\Message\AbstractRequestTest_MockAbstractRequest\getData
getData()
Definition: AbstractRequestTest.php:469
Omnipay\Common\Message\AbstractRequestTest\testGetAmountIntegerNoDecimals
testGetAmountIntegerNoDecimals()
Definition: AbstractRequestTest.php:209
Omnipay\Common\Message\AbstractRequestTest
Definition: AbstractRequestTest.php:10
Omnipay\Common\Message\AbstractRequestTest\testTransactionId
testTransactionId()
Definition: AbstractRequestTest.php:308
Omnipay\Common\Message\AbstractRequestTest\testSetCardWithArray
testSetCardWithArray()
Definition: AbstractRequestTest.php:66
Omnipay\Common\Message\AbstractRequestTest_MockAbstractRequest
Definition: AbstractRequestTest.php:467
Omnipay\Common\Message\AbstractRequestTest\testAmount
testAmount()
Definition: AbstractRequestTest.php:88
Omnipay\Common\Message\AbstractRequestTest\testAmountNegativeFloatThrowsException
testAmountNegativeFloatThrowsException()
Definition: AbstractRequestTest.php:255
Omnipay\Common\Message\AbstractRequestTest\testTransactionReference
testTransactionReference()
Definition: AbstractRequestTest.php:314
Omnipay\Common\Message\AbstractRequestTest\testAmountPrecisionTooHigh
testAmountPrecisionTooHigh()
Definition: AbstractRequestTest.php:158
Omnipay\Common\Message\AbstractRequestTest\testIssuer
testIssuer()
Definition: AbstractRequestTest.php:368
Omnipay\Common\Message\AbstractRequestTest\testCanValidateExistingParameters
testCanValidateExistingParameters()
Definition: AbstractRequestTest.php:413
Omnipay\Common\Message\AbstractRequestTest\testSend
testSend()
Definition: AbstractRequestTest.php:436
Omnipay\Common\Message\AbstractRequestTest\testAmountInvalidTypeThrowsException
testAmountInvalidTypeThrowsException()
Definition: AbstractRequestTest.php:237
Omnipay\Common\Message\AbstractRequestTest\testAmountThousandsSepThrowsException
testAmountThousandsSepThrowsException()
Definition: AbstractRequestTest.php:219
Omnipay\Common\Message\AbstractRequestTest\testCurrencyNumeric
testCurrencyNumeric()
Definition: AbstractRequestTest.php:279
Omnipay\Common\Message\AbstractRequestTest\testAmountZeroFloat
testAmountZeroFloat()
Definition: AbstractRequestTest.php:106
Omnipay\Common\Message\AbstractRequestTest\testAmountInvalidFormatThrowsException
testAmountInvalidFormatThrowsException()
Definition: AbstractRequestTest.php:228
Omnipay\Common\Message\AbstractRequestTest\testNoCurrencyReturnedIfCurrencyNotSet
testNoCurrencyReturnedIfCurrencyNotSet()
Definition: AbstractRequestTest.php:431
Omnipay\Common\Message
Definition: lib/vendor/omnipay/common/src/Omnipay/Common/Message/AbstractRequest.php:6
Omnipay\Common\Message\AbstractRequestTest\testInvalidParametersThrowsException
testInvalidParametersThrowsException()
Definition: AbstractRequestTest.php:424
Omnipay\Common\Message\AbstractRequestTest\testItemsBag
testItemsBag()
Definition: AbstractRequestTest.php:335
Omnipay\Common\Message\AbstractRequestTest\testAmountWithIntThrowsException
testAmountWithIntThrowsException()
Definition: AbstractRequestTest.php:185
Omnipay\Common\Message\AbstractRequestTest\testAmountPrecision
testAmountPrecision()
Definition: AbstractRequestTest.php:130
Omnipay\Common\Message\AbstractRequestTest\testInitializeWithParams
testInitializeWithParams()
Definition: AbstractRequestTest.php:40
Omnipay\Common\Message\AbstractRequestTest\testGetResponseAfterRequestSent
testGetResponseAfterRequestSent()
Definition: AbstractRequestTest.php:457
Omnipay\Common\Message\AbstractRequestTest\testFormatCurrencyNoDecimals
testFormatCurrencyNoDecimals()
Definition: AbstractRequestTest.php:296
Omnipay\Common\Message\AbstractRequestTest\testAmountWithFloat
testAmountWithFloat()
Definition: AbstractRequestTest.php:94
Omnipay\Common\Message\AbstractRequestTest\testCurrency
testCurrency()
Definition: AbstractRequestTest.php:261
Omnipay\Common\ItemBag
Definition: lib/vendor/omnipay/common/src/Omnipay/Common/ItemBag.php:16
Omnipay\Common\Message\AbstractRequestTest\testAmountZeroNotAllowed
testAmountZeroNotAllowed()
Definition: AbstractRequestTest.php:122
Omnipay\Common\Message\AbstractRequestTest\testGetAmountNoDecimals
testGetAmountNoDecimals()
Definition: AbstractRequestTest.php:164
Omnipay\Common\Message\AbstractRequestTest_MockAbstractRequest\sendData
sendData($data)
Definition: AbstractRequestTest.php:471
Omnipay\Common\CreditCard
Definition: lib/vendor/omnipay/common/src/Omnipay/Common/CreditCard.php:94
Omnipay\Common\Message\AbstractRequestTest\testInitializeAfterRequestSent
testInitializeAfterRequestSent()
Definition: AbstractRequestTest.php:50
Omnipay\Common\Message\AbstractRequestTest\testCurrencyLowercase
testCurrencyLowercase()
Definition: AbstractRequestTest.php:267
Omnipay\Common\Message\AbstractRequestTest\testNotifyUrl
testNotifyUrl()
Definition: AbstractRequestTest.php:362
Omnipay\Common\Message\AbstractRequestTest\testConstruct
testConstruct()
Definition: AbstractRequestTest.php:34
Omnipay\Common\ItemBag\add
add($item)
Definition: lib/vendor/omnipay/common/src/Omnipay/Common/ItemBag.php:75
Omnipay\Common\Message\AbstractRequestTest\testAmountNegativeStringThrowsException
testAmountNegativeStringThrowsException()
Definition: AbstractRequestTest.php:246
Omnipay\Common\Message\AbstractRequestTest\testItemsArray
testItemsArray()
Definition: AbstractRequestTest.php:320
Omnipay\Common\Message\AbstractRequestTest\testFormatCurrency
testFormatCurrency()
Definition: AbstractRequestTest.php:291
Omnipay\Common\Message\AbstractRequestTest\testToken
testToken()
Definition: AbstractRequestTest.php:76
Omnipay\Common\Message\AbstractRequestTest\testCurrencyNull
testCurrencyNull()
Definition: AbstractRequestTest.php:273
Omnipay\Common\Message\AbstractRequestTest\testAmountZeroString
testAmountZeroString()
Definition: AbstractRequestTest.php:112
Omnipay\Common\Message\AbstractRequestTest\testCardReference
testCardReference()
Definition: AbstractRequestTest.php:82