Skip to content

Commit 690e055

Browse files
committed
Improve test coverage
1 parent 923e89d commit 690e055

10 files changed

+270
-12
lines changed

src/Omnipay/Common/Message/AbstractRequest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public function __construct(ClientInterface $httpClient, HttpRequest $httpReques
5858
* @param array $parameters An associative array of parameters
5959
*
6060
* @return $this
61-
*
6261
* @throws RuntimeException
6362
*/
6463
public function initialize(array $parameters = array())

src/Omnipay/Common/Message/AbstractResponse.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public function getTransactionReference()
5555
*
5656
* This method is meant to be a helper for simple scenarios. If you want to customize the
5757
* redirection page, just call the getRedirectUrl() and getRedirectData() methods directly.
58+
*
59+
* @codeCoverageIgnore
5860
*/
5961
public function redirect()
6062
{

tests/Omnipay/Common/AbstractGatewayTest.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33
namespace Omnipay\Common;
44

55
use Mockery as m;
6+
use Omnipay\Common\Message\AbstractRequest;
67
use Omnipay\Tests\TestCase;
78
use Symfony\Component\HttpFoundation\ParameterBag;
89

910
class AbstractGatewayTest extends TestCase
1011
{
1112
public function setUp()
1213
{
13-
$this->gateway = m::mock("\Omnipay\Common\AbstractGateway")->makePartial();
14+
$this->gateway = m::mock('\Omnipay\Common\AbstractGateway')->makePartial();
1415
$this->gateway->initialize();
1516
}
1617

18+
public function testConstruct()
19+
{
20+
$this->gateway = new AbstractGatewayTest_MockAbstractGateway;
21+
$this->assertInstanceOf('\Guzzle\Http\Client', $this->gateway->getProtectedHttpClient());
22+
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Request', $this->gateway->getProtectedHttpRequest());
23+
$this->assertSame(array(), $this->gateway->getParameters());
24+
}
25+
1726
public function testGetShortName()
1827
{
1928
$this->assertSame('\\'.get_class($this->gateway), $this->gateway->getShortName());
@@ -123,4 +132,44 @@ public function testSupportsUpdateCard()
123132
{
124133
$this->assertFalse($this->gateway->supportsUpdateCard());
125134
}
135+
136+
public function testCreateRequest()
137+
{
138+
$this->gateway = new AbstractGatewayTest_MockAbstractGateway;
139+
$request = $this->gateway->callCreateRequest(
140+
'\Omnipay\Common\AbstractGatewayTest_MockAbstractRequest',
141+
array('currency' => 'THB')
142+
);
143+
144+
$this->assertSame(array('currency' => 'THB'), $request->getParameters());
145+
}
146+
}
147+
148+
class AbstractGatewayTest_MockAbstractGateway extends AbstractGateway
149+
{
150+
public function getName()
151+
{
152+
return 'Mock Gateway Implementation';
153+
}
154+
155+
public function getProtectedHttpClient()
156+
{
157+
return $this->httpClient;
158+
}
159+
160+
public function getProtectedHttpRequest()
161+
{
162+
return $this->httpRequest;
163+
}
164+
165+
public function callCreateRequest($class, array $parameters)
166+
{
167+
return $this->createRequest($class, $parameters);
168+
}
169+
}
170+
171+
class AbstractGatewayTest_MockAbstractRequest extends AbstractRequest
172+
{
173+
public function getData() {}
174+
public function sendData($data) {}
126175
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Omnipay\Common\Exception;
4+
5+
use Omnipay\Tests\TestCase;
6+
7+
class BadMethodCallExceptionTest extends TestCase
8+
{
9+
public function testConstruct()
10+
{
11+
$exception = new BadMethodCallException('Oops');
12+
$this->assertSame('Oops', $exception->getMessage());
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Omnipay\Common\Exception;
4+
5+
use Omnipay\Tests\TestCase;
6+
7+
class InvalidCreditCardExceptionTest extends TestCase
8+
{
9+
public function testConstruct()
10+
{
11+
$exception = new InvalidCreditCardException('Oops');
12+
$this->assertSame('Oops', $exception->getMessage());
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Omnipay\Common\Exception;
4+
5+
use Omnipay\Tests\TestCase;
6+
7+
class InvalidRequestExceptionTest extends TestCase
8+
{
9+
public function testConstruct()
10+
{
11+
$exception = new InvalidRequestException('Oops');
12+
$this->assertSame('Oops', $exception->getMessage());
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Omnipay\Common\Exception;
4+
5+
use Omnipay\Tests\TestCase;
6+
7+
class InvalidResponseExceptionTest extends TestCase
8+
{
9+
public function testConstructWithDefaultMessage()
10+
{
11+
$exception = new InvalidResponseException();
12+
$this->assertSame('Invalid response from payment gateway', $exception->getMessage());
13+
}
14+
15+
public function testConstructWithCustomMessage()
16+
{
17+
$exception = new InvalidResponseException('Oops');
18+
$this->assertSame('Oops', $exception->getMessage());
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Omnipay\Common\Exception;
4+
5+
use Omnipay\Tests\TestCase;
6+
7+
class RuntimeExceptionTest extends TestCase
8+
{
9+
public function testConstruct()
10+
{
11+
$exception = new RuntimeException('Oops');
12+
$this->assertSame('Oops', $exception->getMessage());
13+
}
14+
}

tests/Omnipay/Common/Message/AbstractRequestTest.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,30 @@ public function setUp()
1515
$this->request->initialize();
1616
}
1717

18+
public function testConstruct()
19+
{
20+
$this->request = new AbstractRequestTest_MockAbstractRequest($this->getHttpClient(), $this->getHttpRequest());
21+
$this->assertSame(array(), $this->request->getParameters());
22+
}
23+
1824
public function testInitializeWithParams()
1925
{
2026
$this->assertSame($this->request, $this->request->initialize(array('amount' => '1.23')));
2127
$this->assertSame('1.23', $this->request->getAmount());
2228
}
2329

30+
/**
31+
* @expectedException \Omnipay\Common\Exception\RuntimeException
32+
* @expectedExceptionMessage Request cannot be modified after it has been sent!
33+
*/
34+
public function testInitializeAfterRequestSent()
35+
{
36+
$this->request = new AbstractRequestTest_MockAbstractRequest($this->getHttpClient(), $this->getHttpRequest());
37+
$this->request->send();
38+
39+
$this->request->initialize();
40+
}
41+
2442
public function testCard()
2543
{
2644
// no type checking on card parameter
@@ -250,6 +268,18 @@ public function testGetParameters()
250268
$this->assertEquals($expected, $this->request->getParameters());
251269
}
252270

271+
/**
272+
* @expectedException \Omnipay\Common\Exception\RuntimeException
273+
* @expectedExceptionMessage Request cannot be modified after it has been sent!
274+
*/
275+
public function testSetParameterAfterRequestSent()
276+
{
277+
$this->request = new AbstractRequestTest_MockAbstractRequest($this->getHttpClient(), $this->getHttpRequest());
278+
$this->request->send();
279+
280+
$this->request->setCurrency('PHP');
281+
}
282+
253283
public function testCanValidateExistingParameters()
254284
{
255285
$this->request->setTestMode(true);
@@ -286,9 +316,30 @@ public function testSend()
286316

287317
/**
288318
* @expectedException \Omnipay\Common\Exception\RuntimeException
319+
* @expectedExceptionMessage You must call send() before accessing the Response!
289320
*/
290-
public function testMustSendRequestBeforeGettingResponse()
321+
public function testGetResponseBeforeRequestSent()
291322
{
323+
$this->request = new AbstractRequestTest_MockAbstractRequest($this->getHttpClient(), $this->getHttpRequest());
292324
$this->request->getResponse();
293325
}
326+
327+
public function testGetResponseAfterRequestSent()
328+
{
329+
$this->request = new AbstractRequestTest_MockAbstractRequest($this->getHttpClient(), $this->getHttpRequest());
330+
$this->request->send();
331+
332+
$response = $this->request->getResponse();
333+
$this->assertInstanceOf('\Omnipay\Common\Message\ResponseInterface', $response);
334+
}
335+
}
336+
337+
class AbstractRequestTest_MockAbstractRequest extends AbstractRequest
338+
{
339+
public function getData() {}
340+
341+
public function sendData($data)
342+
{
343+
$this->response = m::mock('\Omnipay\Common\Message\AbstractResponse');
344+
}
294345
}

tests/Omnipay/Common/Message/AbstractResponseTest.php

Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,105 @@
77

88
class AbstractResponseTest extends TestCase
99
{
10+
public function setUp()
11+
{
12+
$this->response = m::mock('\Omnipay\Common\Message\AbstractResponse')->makePartial();
13+
}
14+
15+
public function testConstruct()
16+
{
17+
$data = array('foo' => 'bar');
18+
$request = $this->getMockRequest();
19+
$this->response = m::mock('\Omnipay\Common\Message\AbstractResponse', array($request, $data))->makePartial();
20+
21+
$this->assertSame($request, $this->response->getRequest());
22+
$this->assertSame($data, $this->response->getData());
23+
}
24+
1025
public function testDefaultMethods()
1126
{
12-
$response = m::mock('\Omnipay\Common\Message\AbstractResponse')->makePartial();
27+
$this->assertFalse($this->response->isRedirect());
28+
$this->assertNull($this->response->getData());
29+
$this->assertNull($this->response->getTransactionReference());
30+
$this->assertNull($this->response->getMessage());
31+
$this->assertNull($this->response->getCode());
32+
}
1333

14-
$this->assertFalse($response->isRedirect());
15-
$this->assertNull($response->getData());
16-
$this->assertNull($response->getTransactionReference());
17-
$this->assertNull($response->getMessage());
18-
$this->assertNull($response->getCode());
34+
/**
35+
* @expectedException \Omnipay\Common\Exception\RuntimeException
36+
* @expectedExceptionMessage This response does not support redirection.
37+
*/
38+
public function testGetRedirectResponseNotImplemented()
39+
{
40+
$this->response->getRedirectResponse();
1941
}
2042

2143
/**
2244
* @expectedException \Omnipay\Common\Exception\RuntimeException
45+
* @expectedExceptionMessage This response does not support redirection.
2346
*/
24-
public function testCannotRedirectResponseThatIsNotRedirectResponseInterface()
47+
public function testGetRedirectResponseNotSupported()
48+
{
49+
$this->response = m::mock('\Omnipay\Common\Message\AbstractResponseTest_MockRedirectResponse')->makePartial();
50+
$this->response->shouldReceive('isRedirect')->once()->andReturn(false);
51+
52+
$this->response->getRedirectResponse();
53+
}
54+
55+
public function testGetRedirectResponseGet()
56+
{
57+
$this->response = m::mock('\Omnipay\Common\Message\AbstractResponseTest_MockRedirectResponse')->makePartial();
58+
$this->response->shouldReceive('getRedirectMethod')->andReturn('GET');
59+
60+
$httpResponse = $this->response->getRedirectResponse();
61+
$this->assertSame(302, $httpResponse->getStatusCode());
62+
$this->assertSame('https://example.com/redirect?a=1&b=2', $httpResponse->getTargetUrl());
63+
}
64+
65+
public function testGetRedirectResponsePost()
2566
{
26-
$response = m::mock('\Omnipay\Common\Message\AbstractResponse')->makePartial();
67+
$data = array('foo' => 'bar', 'key&"' => '<value>');
68+
$this->response = m::mock('\Omnipay\Common\Message\AbstractResponseTest_MockRedirectResponse')->makePartial();
69+
$this->response->shouldReceive('getRedirectMethod')->andReturn('POST');
70+
$this->response->shouldReceive('getRedirectData')->andReturn($data);
2771

28-
$response->getRedirectResponse();
72+
$httpResponse = $this->response->getRedirectResponse();
73+
$this->assertSame(200, $httpResponse->getStatusCode());
74+
$this->assertContains('<form action="https://example.com/redirect?a=1&amp;b=2" method="post">', $httpResponse->getContent());
75+
$this->assertContains('<input type="hidden" name="foo" value="bar" />', $httpResponse->getContent());
76+
$this->assertContains('<input type="hidden" name="key&amp;&quot;" value="&lt;value&gt;" />', $httpResponse->getContent());
2977
}
78+
79+
/**
80+
* @expectedException \Omnipay\Common\Exception\RuntimeException
81+
* @expectedExceptionMessage Invalid redirect method "DELETE".
82+
*/
83+
public function testGetRedirectResponseInvalidMethod()
84+
{
85+
$this->response = m::mock('\Omnipay\Common\Message\AbstractResponseTest_MockRedirectResponse')->makePartial();
86+
$this->response->shouldReceive('getRedirectMethod')->andReturn('DELETE');
87+
88+
$this->response->getRedirectResponse();
89+
}
90+
}
91+
92+
class AbstractResponseTest_MockRedirectResponse extends AbstractResponse implements RedirectResponseInterface
93+
{
94+
public function isSuccessful()
95+
{
96+
return false;
97+
}
98+
99+
public function isRedirect()
100+
{
101+
return true;
102+
}
103+
104+
public function getRedirectUrl()
105+
{
106+
return 'https://example.com/redirect?a=1&b=2';
107+
}
108+
109+
public function getRedirectMethod() {}
110+
public function getRedirectData() {}
30111
}

0 commit comments

Comments
 (0)