Skip to content

Commit 333d686

Browse files
authored
Merge pull request #23 from phuonghuynh/feature/refactor-and-fix
Fix Dispute test
2 parents 1b15805 + 15a8fd3 commit 333d686

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

src/Finix/Bootstrap.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Finix;
44
use Finix\Resources;
5+
use Finix\Http\Auth\BasicAuthentication;
56

67
/**
78
* Bootstrapper for Finix does autoloading and resource initialization.
@@ -76,4 +77,19 @@ private static function initializeResources()
7677

7778
self::$initialized = true;
7879
}
79-
}
80+
81+
public static function createClient()
82+
{
83+
if (Settings::$username == null || Settings::$password == null) {
84+
$client = new Hal\Client(Settings::$url_root, '/');
85+
}
86+
else {
87+
$client = new Hal\Client(
88+
Settings::$url_root,
89+
'/',
90+
null,
91+
new BasicAuthentication(Settings::$username, Settings::$password));
92+
}
93+
return $client;
94+
}
95+
}

src/Finix/Pagination.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Finix;
4+
5+
6+
class Pagination extends Resource
7+
{
8+
9+
public function __construct(Hal\Resource $halResource, $class)
10+
{
11+
parent::__construct($halResource->getState(), $halResource->getAllLinks());
12+
$resources = $halResource->getAllEmbeddedResources();
13+
$resources = reset($resources);
14+
$items = array();
15+
foreach ($resources as $name => $resource) {
16+
array_push($items, new $class($resource->getState(), $resource->getAllLinks()));
17+
}
18+
$this->state->items = $items;
19+
}
20+
}

src/Finix/Resource.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,10 @@ public static function init()
5858

5959
public function __construct(array $state = null, array $links = null)
6060
{
61-
$this->client = self::createClient();
61+
$this->client = Bootstrap::createClient();
6262
$this->setResource(new Hal\Resource($state, $links));
6363
}
6464

65-
private static function createClient()
66-
{
67-
if (Settings::$username == null || Settings::$password == null) {
68-
$client = new Hal\Client(Settings::$url_root, '/');
69-
}
70-
else {
71-
$client = new Hal\Client(
72-
Settings::$url_root,
73-
'/',
74-
null,
75-
new BasicAuthentication(Settings::$username, Settings::$password));
76-
}
77-
return $client;
78-
}
79-
8065
public function __get($name)
8166
{
8267
if ($this->state->has_key($name)) {
@@ -123,12 +108,19 @@ public function __isset($name)
123108
public static function retrieve($id)
124109
{
125110
$uri = self::getHrefSpec()->collection_uri . '/' . $id;
126-
$resource = self::createClient()->sendRequest(new Request($uri));
111+
$resource = Bootstrap::createClient()->sendRequest(new Request($uri));
127112
$class = get_called_class();
128113
return new $class($resource->getState(), $resource->getAllLinks());
129114
}
130115

131-
public function refresh() {
116+
public static function getPagination($href)
117+
{
118+
$resource = Bootstrap::createClient()->sendRequest(new Request($href));
119+
return new Pagination($resource, get_called_class());
120+
}
121+
122+
public function refresh()
123+
{
132124
$request = new Request(
133125
$this->resource->getLink("self")->getHref(),
134126
'GET'
@@ -204,11 +196,12 @@ private function isUpdate()
204196

205197

206198
/**
199+
* @param string $rel
207200
* @return string
208201
*/
209-
public function getHref()
202+
public function getHref($rel = "self")
210203
{
211-
return $this->resource->getLink("self")->getHref();
204+
return $this->resource->getLink($rel)->getHref();
212205
}
213206

214207
/**

tests/Finix/ScenariosTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44

55
use Finix\Resources\Dispute;
6+
use Finix\Resources\Transfer;
67
use Finix\Resources\Verification;
78
use Finix\Settings;
89

@@ -26,12 +27,14 @@ class ScenariosTest extends \PHPUnit_Framework_TestCase
2627
private $pushFundTransfer2;
2728
private $pushFundTransfer1;
2829
private $settlement;
30+
private $receiptImage;
2931

3032
protected function setUp()
3133
{
32-
Settings::configure(["username" => null, "password" => null]);
34+
$this->receiptImage = realpath("../../data/receipt.jpg");
3335

3436
date_default_timezone_set("UTC");
37+
Settings::configure(["username" => null, "password" => null]);
3538

3639
$this->user = Fixtures::createAdminUser();
3740

@@ -124,6 +127,12 @@ public function testSettlement()
124127
});
125128

126129
$this->settlement = Fixtures::createSettlement($this->identity);
127-
var_dump($this->settlement);
130+
}
131+
132+
public function testDispute() {
133+
$disputePage = Dispute::getPagination($this->pushFundTransfer->getHref("disputes"));
134+
$dispute = $disputePage->items[0];
135+
$file = $dispute->uploadEvidence($this->receiptImage);
136+
$this->assertEquals($file->dispute, $dispute->id);
128137
}
129138
}

0 commit comments

Comments
 (0)