Skip to content

Commit d8c4f59

Browse files
committed
Merge pull request #21 from Xobb/plans-api
Add Plans Api request and response messages.
2 parents ea56bde + db0aa10 commit d8c4f59

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed

src/Gateway.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ public function cancelSubscription($subscriptionId)
250250
return $this->createRequest('\Omnipay\Braintree\Message\CancelSubscriptionRequest', array('id' => $subscriptionId));
251251
}
252252

253+
/**
254+
* @return \Omnipay\Common\Message\PlansRequest
255+
*/
256+
public function plans()
257+
{
258+
return $this->createRequest('\Omnipay\Braintree\Message\PlanRequest', array());
259+
}
260+
253261
/**
254262
* @param array $parameters
255263
*

src/Message/PlanRequest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* PlanRequest Class
4+
*/
5+
6+
namespace Omnipay\Braintree\Message;
7+
8+
class PlanRequest extends AbstractRequest
9+
{
10+
/**
11+
* @return null
12+
*/
13+
public function getData()
14+
{
15+
return null;
16+
}
17+
18+
/**
19+
* @param null $data
20+
* @return PlanResponse
21+
*/
22+
public function sendData($data = null)
23+
{
24+
$response = $this->braintree->plan()->all();
25+
return $this->response = new PlanResponse($this, $response);
26+
}
27+
}

src/Message/PlanResponse.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* PlanResponse class
4+
*/
5+
namespace Omnipay\Braintree\Message;
6+
7+
8+
class PlanResponse extends Response
9+
{
10+
/**
11+
* Returns array of Braintree_Plans objects with available plans
12+
* If there aren't any plans created it will return empty array
13+
* @return array
14+
*/
15+
public function getPlansData()
16+
{
17+
if (isset($this->data)) {
18+
return $this->data;
19+
}
20+
return array();
21+
}
22+
}

tests/Message/PlanRequestTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: xobb
5+
* Date: 1/22/16
6+
* Time: 5:53 PM
7+
*/
8+
9+
namespace Omnipay\Braintree\Message;
10+
11+
use Omnipay\Tests\TestCase;
12+
13+
class PlanRequestTest extends TestCase
14+
{
15+
/** @var PlanRequest */
16+
private $request;
17+
18+
public function setUp()
19+
{
20+
parent::setUp();
21+
22+
$gateway = $this->buildMockGateway();
23+
$this->request = new PlanRequest($this->getHttpClient(), $this->getHttpRequest(), $gateway);
24+
$this->request->initialize(array());
25+
}
26+
27+
public function testGetData()
28+
{
29+
$data = $this->request->getData();
30+
$this->assertNull($data);
31+
}
32+
33+
public function testSendData()
34+
{
35+
$data = array();
36+
$response = $this->request->sendData($data);
37+
38+
$this->assertInstanceOf('Omnipay\BrainTree\Message\PlanResponse', $response);
39+
}
40+
41+
protected function buildMockGateway()
42+
{
43+
$gateway = $this->getMockBuilder('\Braintree_Gateway')
44+
->disableOriginalConstructor()
45+
->setMethods(array(
46+
'plan'
47+
))
48+
->getMock();
49+
50+
$plan = $this->getMockBuilder('\Braintree_PlanGateway')
51+
->disableOriginalConstructor()
52+
->getMock();
53+
54+
$gateway->expects($this->any())
55+
->method('plan')
56+
->will($this->returnValue($plan));
57+
58+
return $gateway;
59+
}
60+
}

tests/Message/PlanResponseTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: xobb
5+
* Date: 1/22/16
6+
* Time: 5:53 PM
7+
*/
8+
9+
namespace Omnipay\Braintree\Message;
10+
use Omnipay\Tests\TestCase;
11+
12+
class PlanResponseTest extends TestCase
13+
{
14+
/** @var PlanRequest */
15+
private $request;
16+
17+
public function setUp()
18+
{
19+
parent::setUp();
20+
21+
$this->request = new PlanRequest(
22+
$this->getHttpClient(), $this->getHttpRequest(), \Braintree_Configuration::gateway()
23+
);
24+
}
25+
26+
public function testGetPlansData()
27+
{
28+
$data = null;
29+
30+
$response = new PlanResponse($this->request, $data);
31+
$this->assertTrue(is_array($response->getPlansData()));
32+
$this->assertTrue(count($response->getPlansData()) === 0);
33+
34+
$data = "planData";
35+
36+
$response = new PlanResponse($this->request, $data);
37+
$this->assertEquals('planData', $response->getPlansData());
38+
}
39+
}

0 commit comments

Comments
 (0)