Skip to content

Commit 1b15805

Browse files
authored
Merge pull request #22 from phuonghuynh/feature/refactor-and-fix
Fix PHP unit failing
2 parents 9fa0fe4 + df713b0 commit 1b15805

32 files changed

+510
-1097
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file is for unifying the coding style for different editors and IDEs
2+
# editorconfig.org
3+
4+
# PHP PSR-2 Coding Standards
5+
# http://www.php-fig.org/psr/psr-2/
6+
7+
root = true
8+
9+
[*.php]
10+
charset = utf-8
11+
end_of_line = lf
12+
insert_final_newline = true
13+
trim_trailing_whitespace = true
14+
indent_style = space
15+
indent_size = 4

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ require_once("/path/to/Finix/Bootstrap.php");
3737

3838
```php
3939
require(__DIR__ . '/src/Finix/Settings.php');
40-
Finix\Settings::configure('http://b.papi.staging.finix.io', '$USERNAME', '$PASSWORD');
40+
Finix\Settings::configure('https://api-staging.finix.io/', '$USERNAME', '$PASSWORD');
4141
require(__DIR__ . '/src/Finix/Bootstrap.php');
4242
\Finix\Bootstrap::init();
4343
```

bootstrap.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
<?php
22

33
require(__DIR__ . '/src/Finix/Settings.php');
4-
require(__DIR__ . '/tests/Finix/SampleData.php');
4+
require(__DIR__ . '/tests/Finix/Fixtures.php');
55
require(__DIR__ . '/src/Finix/Bootstrap.php');
66

7-
use \Finix\Tests\SampleData;
7+
use \Finix\Tests\Fixtures;
88
use \Finix\Settings;
99
use \Finix\Bootstrap;
1010

11-
Settings::configure(
12-
SampleData::$apiUrl,
13-
SampleData::$username,
14-
SampleData::$password
15-
);
11+
Settings::configure([
12+
"root_url" => Fixtures::$apiUrl
13+
]);
1614

17-
Bootstrap::init();
15+
Bootstrap::init();

circle.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ dependencies:
1414
test:
1515
override:
1616
- mkdir -p $CIRCLE_TEST_REPORTS/phpunit
17-
- ./vendor/bin/phpunit --log-junit $CIRCLE_TEST_REPORTS/phpunit/junit.xml
17+
- ./vendor/bin/phpunit --log-junit $CIRCLE_TEST_REPORTS/phpunit/junit.xml:
18+
timeout: 1200

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "finix/processing-php",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "A PHP HTTP Client conforming to the HAL hypermedia type for the Finix processing API",
55
"license": "Apache2",
66
"authors": [

src/Finix/Bootstrap.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ private static function _autoload($base, $classname)
5353
*/
5454
private static function initializeResources()
5555
{
56-
if (self::$initialized)
56+
if (self::$initialized) {
5757
return;
58+
}
5859

5960
Resource::init();
6061

62+
Resources\User::init();
6163
Resources\Application::init();
6264
Resources\Identity::init();
6365
Resources\Processor::init();

src/Finix/Http/Auth/BasicAuthentication.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Finix\Http\Auth;
44

55
use Finix\Http;
6+
use Finix\Settings;
67
use Finix\Http\AbstractClient;
78
use GuzzleHttp\Message\RequestInterface;
89

@@ -86,7 +87,8 @@ private function isRequestAuthorized(RequestInterface $httpRequest)
8687
*/
8788
private function getCredentials()
8889
{
89-
$basic = base64_encode($this->user_id . ':' . $this->password);
90+
$basic = base64_encode(Settings::$username . ':' . Settings::$password);
91+
// $basic = base64_encode($this->user_id . ':' . $this->password);
9092
return $basic;
9193
}
9294
}

src/Finix/Resource.php

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Finix\Http\Auth\BasicAuthentication;
55
use Finix\Http\JsonBody;
66
use Finix\Http\Request;
7+
use Finix\Resources\Verification;
78
use Finix\Utils\ArrayProxy;
89
use \stdClass;
910

@@ -15,7 +16,8 @@ abstract class Resource
1516
protected $state;
1617

1718
protected static $href;
18-
protected static $client;
19+
protected $client;
20+
// protected static $client;
1921
protected static $registry;
2022

2123
/**
@@ -24,10 +26,10 @@ abstract class Resource
2426
*/
2527
public static function getHrefSpec($resource = null)
2628
{
27-
if(is_null($resource)){
29+
if (is_null($resource)) {
2830
$resource = get_called_class();
2931
}
30-
if(!is_string($resource)) {
32+
if (!is_string($resource)) {
3133
$resource = get_class($resource);
3234
}
3335
return self::getRegistry()->getHrefSpecForResource($resource);
@@ -36,9 +38,9 @@ public static function getHrefSpec($resource = null)
3638
/**
3739
* @return Hal\Client
3840
*/
39-
public static function getClient()
41+
public function getClient()
4042
{
41-
return self::$client;
43+
return $this->client;
4244
}
4345

4446
/**
@@ -51,23 +53,33 @@ public static function getRegistry()
5153

5254
public static function init()
5355
{
54-
self::$client = new Hal\Client(
55-
Settings::$url_root,
56-
'/',
57-
null,
58-
new BasicAuthentication(Settings::$username, Settings::$password));
5956
self::$registry = new Registry();
6057
}
6158

6259
public function __construct(array $state = null, array $links = null)
6360
{
61+
$this->client = self::createClient();
6462
$this->setResource(new Hal\Resource($state, $links));
6563
}
6664

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+
6780
public function __get($name)
6881
{
69-
if($this->state->has_key($name))
70-
{
82+
if ($this->state->has_key($name)) {
7183
return $this->state[$name];
7284
}
7385

@@ -92,8 +104,8 @@ public function __set($name, $value)
92104
public function __isset($name)
93105
{
94106
if (array_key_exists($name, $this->resource->getAllLinks()) ||
95-
array_key_exists($name, $this->resource->getState()))
96-
{
107+
array_key_exists($name, $this->resource->getState())
108+
) {
97109
return true;
98110
}
99111

@@ -111,11 +123,21 @@ public function __isset($name)
111123
public static function retrieve($id)
112124
{
113125
$uri = self::getHrefSpec()->collection_uri . '/' . $id;
114-
$resource = self::getClient()->sendRequest(new Request($uri));
126+
$resource = self::createClient()->sendRequest(new Request($uri));
115127
$class = get_called_class();
116128
return new $class($resource->getState(), $resource->getAllLinks());
117129
}
118130

131+
public function refresh() {
132+
$request = new Request(
133+
$this->resource->getLink("self")->getHref(),
134+
'GET'
135+
);
136+
$resource = $this->getClient()->sendRequest($request);
137+
$this->setResource($resource);
138+
return $this;
139+
}
140+
119141
/**
120142
* @return \Finix\Resource
121143
* @throws Hal\Exception\HalClientErrorException
@@ -128,12 +150,11 @@ public static function retrieve($id)
128150
public function save()
129151
{
130152
if (empty($this->state["tags"])) {
131-
$this->state["tags"] = new stdClass();
153+
$this->state["tags"] = new stdClass();
132154
}
133155

134156
$payload = new JsonBody(iterator_to_array($this->state));
135-
if($this->isUpdate())
136-
{
157+
if ($this->isUpdate()) {
137158
$request = new Request(
138159
$this->resource->getLink("self")->getHref(),
139160
'PUT',
@@ -199,5 +220,9 @@ private function setResource($resource)
199220
$this->state = new ArrayProxy($resource->getState());
200221
}
201222

202-
203-
}
223+
public function verifyOn(Verification $verification)
224+
{
225+
$verifyLink = $this->resource->getLink("verifications")->getHref();
226+
return $verification->create($verifyLink);
227+
}
228+
}

src/Finix/Resources/Application.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,13 @@ public static function init()
1111
self::getRegistry()->add(get_called_class(), new HrefSpec('applications', 'id', '/'));
1212
}
1313

14-
}
14+
public function createPartnerUser(User $user)
15+
{
16+
return $user->create($this->resource->getLink("users")->getHref());
17+
}
18+
19+
public function createProcessor(Processor $processor)
20+
{
21+
return $processor->create($this->resource->getLink("processors")->getHref());
22+
}
23+
}

src/Finix/Resources/Authorization.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,16 @@ public static function init()
1111
self::getRegistry()->add(get_called_class(), new HrefSpec('authorizations', 'id', '/'));
1212
}
1313

14-
}
14+
public function capture($amount, $fee = 0)
15+
{
16+
$this->state["capture_amount"] = $amount;
17+
$this->state["fee"] = $fee;
18+
return $this->save();
19+
}
20+
21+
public function void($voidMe)
22+
{
23+
$this->state["void_me"] = $voidMe;
24+
return $this->save();
25+
}
26+
}

0 commit comments

Comments
 (0)