Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Recca Tsai committed Feb 7, 2017
1 parent 0d81b28 commit a331f62
Show file tree
Hide file tree
Showing 18 changed files with 352 additions and 708 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/build export-ignore
/tests export-ignore
/documents
/docs
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
Expand Down
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,14 @@
"psr-4": {
"PayumTW\\Mypay\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"PayumTW\\Mypay\\Tests\\": "tests/"
}
},
"config": {
"preferred-install": "dist",
"sort-packages": true
}
}
1 change: 1 addition & 0 deletions src/Action/Api/CreateTransactionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function execute($request)
$details = ArrayObject::ensureArrayObject($request->getModel());

$result = $this->api->createTransaction((array) $details);

$details->replace($result);

if (isset($result['url']) === false) {
Expand Down
2 changes: 1 addition & 1 deletion src/Action/NotifyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function execute($request)
$httpRequest = new GetHttpRequest();
$this->gateway->execute($httpRequest);

if ($this->api->verifyHash($httpRequest->request, $details) === false) {
if ($this->api->verifyHash($httpRequest->request, (array) $details) === false) {
throw new HttpResponse('key verify fail.', 400, ['Content-Type' => 'text/plain']);
}

Expand Down
19 changes: 7 additions & 12 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,14 @@ public function __construct(array $options, HttpClientInterface $client, Message
*/
protected function doRequest(array $fields)
{
$headers = [
$request = $this->messageFactory->createRequest('POST', $this->getApiEndpoint(), [
'Content-Type' => 'application/x-www-form-urlencoded',
];

$request = $this->messageFactory->createRequest('POST', $this->getApiEndpoint(), $headers, http_build_query($fields));
], http_build_query($fields));

$response = $this->client->send($request);

if (false == ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300)) {
$statusCode = $response->getStatusCode();
if (false == ($statusCode >= 200 && $statusCode < 300)) {
throw HttpException::factory($request, $response);
}

Expand Down Expand Up @@ -214,9 +213,7 @@ public function createTransaction(array $params)
array_intersect_key($params, $supportedParams)
));

$result = $this->call($params, 'api/orders');

return $result;
return $this->call($params, 'api/orders');
}

/**
Expand Down Expand Up @@ -263,16 +260,14 @@ public function verifyHash(array $params, $details)
*/
protected function call($params, $cmd)
{
$postData = [
return $this->doRequest([
'store_uid' => $this->options['store_uid'],
'service' => $this->calculateHash([
'service_name' => 'api',
'cmd' => $cmd,
]),
'encry_data' => $this->calculateHash($params),
];

return $this->doRequest($postData);
]);
}

/**
Expand Down
98 changes: 22 additions & 76 deletions tests/Action/Api/CreateTransactionActionTest.php
Original file line number Diff line number Diff line change
@@ -1,101 +1,47 @@
<?php

namespace PayumTW\Mypay\Tests\Action\Api;

use Mockery as m;
use Payum\Core\Reply\HttpResponse;
use PHPUnit\Framework\TestCase;
use Payum\Core\Bridge\Spl\ArrayObject;
use PayumTW\Mypay\Request\Api\CreateTransaction;
use PayumTW\Mypay\Action\Api\CreateTransactionAction;

class CreateTransactionActionTest extends PHPUnit_Framework_TestCase
class CreateTransactionActionTest extends TestCase
{
public function tearDown()
protected function tearDown()
{
m::close();
}

public function test_redirect_to_mypay()
/**
* @expectedException Payum\Core\Reply\HttpRedirect
*/
public function testExecute()
{
/*
|------------------------------------------------------------
| Arrange
|------------------------------------------------------------
*/

$api = m::spy('PayumTW\Mypay\Api');
$request = m::spy('PayumTW\Mypay\Request\Api\CreateTransaction');
$details = new ArrayObject([
'url' => 'foo.url',
]);

/*
|------------------------------------------------------------
| Act
|------------------------------------------------------------
*/

$request
->shouldReceive('getModel')->twice()->andReturn($details);

$api
->shouldReceive('createTransaction')->once()->andReturn($details->toUnsafeArray());

$action = new CreateTransactionAction();
$action->setApi($api);

/*
|------------------------------------------------------------
| Assert
|------------------------------------------------------------
*/
$request = new CreateTransaction($details = new ArrayObject(['foo' => 'bar']));
$action->setApi(
$api = m::mock('PayumTW\Mypay\Api')
);
$api->shouldReceive('createTransaction')->once()->with((array) $details)->andReturn(['url' => 'foo']);

try {
$action->execute($request);
} catch (HttpResponse $response) {
$this->assertInstanceOf('Payum\Core\Reply\HttpRedirect', $response);
}

$request->shouldHaveReceived('getModel')->twice();
$api->shouldHaveReceived('createTransaction')->once();
$action->execute($request);
}

/**
* @expectedException LogicException
*/
public function test_redirect_to_mypay_fail()
public function testExecuteFail()
{
/*
|------------------------------------------------------------
| Arrange
|------------------------------------------------------------
*/

$api = m::spy('PayumTW\Mypay\Api');
$request = m::spy('PayumTW\Mypay\Request\Api\CreateTransaction');
$details = new ArrayObject([
]);

/*
|------------------------------------------------------------
| Act
|------------------------------------------------------------
*/

$request
->shouldReceive('getModel')->twice()->andReturn($details);

$api
->shouldReceive('createTransaction')->once()->andReturn($details->toUnsafeArray());

$action = new CreateTransactionAction();
$action->setApi($api);

/*
|------------------------------------------------------------
| Assert
|------------------------------------------------------------
*/
$request = new CreateTransaction($details = new ArrayObject(['foo' => 'bar']));
$action->setApi(
$api = m::mock('PayumTW\Mypay\Api')
);
$api->shouldReceive('createTransaction')->once()->with((array) $details)->andReturn(['foo' => 'foo']);

$action->execute($request);
$request->shouldHaveReceived('getModel')->twice();
$api->shouldHaveReceived('createTransaction')->once();
}
}
51 changes: 13 additions & 38 deletions tests/Action/Api/GetTransactionDataActionTest.php
Original file line number Diff line number Diff line change
@@ -1,54 +1,29 @@
<?php

namespace PayumTW\Mypay\Tests\Action\Api;

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Payum\Core\Bridge\Spl\ArrayObject;
use PayumTW\Mypay\Request\Api\GetTransactionData;
use PayumTW\Mypay\Action\Api\GetTransactionDataAction;

class GetTransactionDataActionTest extends PHPUnit_Framework_TestCase
class GetTransactionDataActionTest extends TestCase
{
public function tearDown()
protected function tearDown()
{
m::close();
}

public function test_get_transaction_data()
public function testExecute()
{
/*
|------------------------------------------------------------
| Arrange
|------------------------------------------------------------
*/

$api = m::spy('PayumTW\Mypay\Api');
$request = m::spy('PayumTW\Mypay\Request\Api\GetTransactionData');
$details = m::mock(new ArrayObject([]));

/*
|------------------------------------------------------------
| Act
|------------------------------------------------------------
*/

$request
->shouldReceive('getModel')->andReturn($details);

$api
->shouldReceive('getTransactionData')->andReturn([
'RspCode' => '1',
]);

$action = new GetTransactionDataAction();
$action->setApi($api);
$request = new GetTransactionData($details = new ArrayObject(['url' => 'foo']));
$action->setApi(
$api = m::mock('PayumTW\Mypay\Api')
);
$api->shouldReceive('getTransactionData')->once()->with((array) $details)->andReturn($result = ['url' => 'bar']);
$action->execute($request);

/*
|------------------------------------------------------------
| Assert
|------------------------------------------------------------
*/

$request->shouldHaveReceived('getModel')->twice();
$api->shouldHaveReceived('getTransactionData')->once();
$details->shouldHaveReceived('replace')->once();
$this->assertSame($result, (array) $request->getModel());
}
}
Loading

0 comments on commit a331f62

Please sign in to comment.