Skip to content

Commit

Permalink
Merge.
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Aug 10, 2017
2 parents 279a7ff + 1fcaadb commit 90423ea
Show file tree
Hide file tree
Showing 14 changed files with 264 additions and 191 deletions.
2 changes: 1 addition & 1 deletion src/OfficialAccount/Server/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function validate(string $token)
return $this;
}

if ($this->app['request']->getRealMethod() == 'GET' && $this->app['request']->get('echostr')) {
if ($this->app['request']->getRealMethod() === 'GET' && $this->app['request']->get('echostr')) {
return $this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Payment/Traits/HandleNotify.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function handleScanNotify(callable $callback)
*
* @return \EasyWeChat\Payment\Notify
*/
public function getNotify()
public function getNotify(): Notify
{
return new Notify($this->app['merchant']);
}
Expand Down
5 changes: 4 additions & 1 deletion tests/Payment/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class ApplicationTest extends TestCase
{
public function testMagicCall()
{
$app = new Application();
$app = new Application([
'app_id' => 'wx123456',
'merchant_id' => 'foo-merchant-id',
]);

$this->assertInstanceOf(Client::class, $app->sandboxMode(true));

Expand Down
30 changes: 17 additions & 13 deletions tests/Payment/BaseClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@ class BaseClientTest extends TestCase
public function testPrepends()
{
$app = new Application();
$client = $this->mockApiClient(BaseClient::class, 'prepends', $app)->makePartial();

$client = $this->mockApiClient(BaseClient::class, 'prepends', $app)->shouldDeferMissing();

// assert result of prepends()
$this->assertEmpty($client->prepends());
$this->assertSame([], $client->prepends());
}

public function testRequest()
{
$app = new Application();
$client = $this->mockApiClient(BaseClient::class, ['performRequest', 'resolveResponse', 'prepends', 'getSignKey'], $app)->makePartial();

$client = $this->mockApiClient(BaseClient::class, ['performRequest', 'resolveResponse', 'prepends', 'getSignKey'], $app)->shouldDeferMissing();

$api = 'http://easywechat.org';
$params = ['foo' => 'bar'];
$method = 'post';
$method = \Mockery::anyOf(['get', 'post']);
$options = ['foo' => 'bar'];

$mockResponse = new Response(200, [], 'response-content');
Expand All @@ -55,7 +58,6 @@ public function testRequest()

$client->expects()->resolveResponse()
->with($mockResponse, \Mockery::any())
->once()
->andReturn(['foo' => 'mock-bar']);

// $returnResponse = false
Expand All @@ -69,14 +71,15 @@ public function testRequest()
public function testRequestRaw()
{
$app = new Application();

$client = $this->mockApiClient(BaseClient::class, ['request', 'requestRaw'], $app)->makePartial();

$api = 'http://easywechat.org';
$params = ['foo' => 'bar'];
$method = 'post';
$method = \Mockery::anyOf(['get', 'post']);
$options = [];

$client->expects()->request($api, $params, $method, $options, true)->andReturn('mock-result')->once();
$client->expects()->request($api, $params, $method, $options, true)->andReturn('mock-result');

$this->assertSame('mock-result', $client->requestRaw($api, $params, $method, $options));
}
Expand All @@ -88,18 +91,19 @@ public function testSafeRequest()
'cert_path' => 'foo',
'key_path' => 'bar',
]);
$client = $this->mockApiClient(BaseClient::class, ['request', 'safeRequest'], $app)->makePartial();

$client = $this->mockApiClient(BaseClient::class, ['safeRequest'], $app)->makePartial();

$api = 'http://easywechat.org';
$params = ['foo' => 'bar'];
$method = 'post';
$method = \Mockery::anyOf(['get', 'post']);

$options = [
'cert' => $app['merchant']->get('cert_path'),
'ssl_key' => $app['merchant']->get('key_path'),
];
$client->expects()->request($api, $params, $method, \Mockery::on(function ($options) use ($app) {
$this->assertSame($options['cert'], $app['merchant']->get('cert_path'));
$this->assertSame($options['ssl_key'], $app['merchant']->get('key_path'));

$client->expects()->request($api, $params, $method, $options)->andReturn('mock-result')->once();
return true;
}))->andReturn('mock-result');

$this->assertSame('mock-result', $client->safeRequest($api, $params, $method));
}
Expand Down
65 changes: 43 additions & 22 deletions tests/Payment/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ClientTest extends TestCase
public function testSchema()
{
$app = new Application();

$mock = $this->mockApiClient(Client::class, 'scheme', $app)->makePartial();

$productId = '1';
Expand All @@ -33,12 +34,13 @@ public function testSchema()
public function testPay()
{
$app = new Application();

$client = $this->mockApiClient(Client::class, ['pay', 'wrapApi'], $app)->makePartial();

// mock order
$order = \Mockery::mock(Order::class.'[all]')->shouldAllowMockingProtectedMethods()->makePartial();
$order = \Mockery::mock(Order::class.'[all]')->shouldDeferMissing();

$client->expects()->request($client->wrapApi('pay/micropay'), $order->all())->andReturn('mock-result')->once();
$client->expects()->request($client->wrapApi('pay/micropay'), $order->all())->andReturn('mock-result');
$this->assertSame('mock-result', $client->pay($order));
}

Expand All @@ -57,87 +59,92 @@ public function testPrepare()

// $order->spbill_create_ip is null and trade_type === Order::NATIVE
$order->set('trade_type', Order::NATIVE);
$client->expects()->request($client->wrapApi('pay/unifiedorder'), array_merge($order->all(), ['spbill_create_ip' => Support\get_server_ip()]))->once()->andReturn('mock-result');
$client->expects()->request($client->wrapApi('pay/unifiedorder'), array_merge($order->all(), ['spbill_create_ip' => Support\get_server_ip()]))->andReturn('mock-result');

$this->assertSame('mock-result', $client->prepare($order));

// $order->spbill_create_ip is null and trade_type !== Order::NATIVE
$order->set('trade_type', Order::JSAPI);
$client->expects()->request($client->wrapApi('pay/unifiedorder'), array_merge($order->all(), ['spbill_create_ip' => Support\get_client_ip()]))->once()->andReturn('mock-result');
$client->expects()->request($client->wrapApi('pay/unifiedorder'), array_merge($order->all(), ['spbill_create_ip' => Support\get_client_ip()]))->andReturn('mock-result');

$this->assertSame('mock-result', $client->prepare($order));

// $order->spbill_create_ip is not null.
$order->set('spbill_create_ip', '192.168.0.1');
$client->expects()->request($client->wrapApi('pay/unifiedorder'), $order->all())->once()->andReturn('mock-result');
$client->expects()->request($client->wrapApi('pay/unifiedorder'), $order->all())->andReturn('mock-result');

$this->assertSame('mock-result', $client->prepare($order));
}

public function testQuery()
{
$app = new Application();

$client = $this->mockApiClient(Client::class, 'query', $app)->makePartial();

$orderNo = 'foo';
$type = 'bar';

// default type
$client->expects()->request($client->wrapApi('pay/orderquery'), [Client::OUT_TRADE_NO => $orderNo])->andReturn('mock-result')->once();
$client->expects()->request($client->wrapApi('pay/orderquery'), [Client::OUT_TRADE_NO => $orderNo])->andReturn('mock-result');
$this->assertSame('mock-result', $client->query($orderNo));

// pass a type parameter
$client->expects()->request($client->wrapApi('pay/orderquery'), [$type => $orderNo])->andReturn('mock-result')->once();
$client->expects()->request($client->wrapApi('pay/orderquery'), [$type => $orderNo])->andReturn('mock-result');
$this->assertSame('mock-result', $client->query($orderNo, $type));
}

public function testQueryByTransactionId()
{
$app = new Application();

$client = $this->mockApiClient(Client::class, ['query', 'queryByTransactionId'], $app)->makePartial();

$transactionId = 'foo';

$client->expects()->query($transactionId, Client::TRANSACTION_ID)->andReturn('mock-result')->once();
$client->expects()->query($transactionId, Client::TRANSACTION_ID)->andReturn('mock-result');
$this->assertSame('mock-result', $client->queryByTransactionId($transactionId));
}

public function testClose()
{
$app = new Application();

$client = $this->mockApiClient(Client::class, 'close', $app)->makePartial();

$tradeNo = 'foo';

$client->expects()->request($client->wrapApi('pay/closeorder'), ['out_trade_no' => $tradeNo])->andReturn('mock-result')->once();
$client->expects()->request($client->wrapApi('pay/closeorder'), ['out_trade_no' => $tradeNo])->andReturn('mock-result');
$this->assertSame('mock-result', $client->close($tradeNo));
}

public function testReverse()
{
$app = new Application();

$client = $this->mockApiClient(Client::class, ['reverse', 'safeRequest'], $app)->makePartial();

$orderNo = 'foo';

// default type === 'out_trade_no'
$client->expects()->safeRequest($client->wrapApi('secapi/pay/reverse'), [Client::OUT_TRADE_NO => $orderNo])->andReturn('mock-result-out-trade-no')->once();
$client->expects()->safeRequest($client->wrapApi('secapi/pay/reverse'), [Client::OUT_TRADE_NO => $orderNo])->andReturn('mock-result-out-trade-no');
$this->assertSame('mock-result-out-trade-no', $client->reverse($orderNo));

// pass a type parameter
$type = Client::TRANSACTION_ID;
$client->expects()->safeRequest($client->wrapApi('secapi/pay/reverse'), [$type => $orderNo])->andReturn('mock-result-with-type')->once();
$client->expects()->safeRequest($client->wrapApi('secapi/pay/reverse'), [$type => $orderNo])->andReturn('mock-result-with-type');
$this->assertSame('mock-result-with-type', $client->reverse($orderNo, $type));
}

public function testReverseByTransactionId()
{
$app = new Application();

$client = $this->mockApiClient(Client::class, ['reverse', 'reverseByTransactionId'], $app)->makePartial();

$transactionId = 'foo';

$client->expects()->reverse($transactionId, Client::TRANSACTION_ID)->andReturn('mock-result')->once();
$client->expects()->reverse($transactionId, Client::TRANSACTION_ID)->andReturn('mock-result');

$this->assertSame('mock-result', $client->reverseByTransactionId($transactionId));
}
Expand All @@ -148,6 +155,7 @@ public function testRefund()
'app_id' => 'wx123456',
'merchant_id' => 'foo-merchant-id',
]);

$client = $this->mockApiClient(Client::class, ['refund', 'safeRequest'], $app)->makePartial();

$orderNo = 'foo';
Expand All @@ -165,85 +173,95 @@ public function testRefund()
'op_user_id' => $app['merchant']->merchant_id,
], $optional);

$client->expects()->safeRequest($client->wrapApi('secapi/pay/refund'), $params)->andReturn('mock-result')->once();
$client->expects()->safeRequest($client->wrapApi('secapi/pay/refund'), $params)->andReturn('mock-result');

$this->assertSame('mock-result', $client->refund($orderNo, $refundNo, $totalFee, $optional));
}

public function testRefundByTransactionId()
{
$app = new Application();

$client = $this->mockApiClient(Client::class, ['refund', 'refundByTransactionId'], $app)->makePartial();

$orderNo = 'foo';
$refundNo = 'bar';
$totalFee = 1;
$optional = [];

$client->expects()->refund($orderNo, $refundNo, $totalFee, $optional)->andReturn('mock-result')->once();
$client->expects()->refund($orderNo, $refundNo, $totalFee, $optional)->andReturn('mock-result');

$this->assertSame('mock-result', $client->refundByTransactionId($orderNo, $refundNo, $totalFee, $optional));
}

public function testQueryRefund()
{
$app = new Application();

$client = $this->mockApiClient(Client::class, 'queryRefund', $app)->makePartial();

$orderNo = 'foo';
$type = 'bar';

// default type
$client->expects()->request($client->wrapApi('pay/refundquery'), [Client::OUT_TRADE_NO => $orderNo])->andReturn('mock-result')->once();
$client->expects()->request($client->wrapApi('pay/refundquery'), [Client::OUT_TRADE_NO => $orderNo])->andReturn('mock-result');

$this->assertSame('mock-result', $client->queryRefund($orderNo));

// pass a type parameter
$client->expects()->request($client->wrapApi('pay/refundquery'), [$type => $orderNo])->andReturn('mock-result')->once();
$client->expects()->request($client->wrapApi('pay/refundquery'), [$type => $orderNo])->andReturn('mock-result');

$this->assertSame('mock-result', $client->queryRefund($orderNo, $type));
}

public function testQueryRefundByRefundNo()
{
$app = new Application();

$client = $this->mockApiClient(Client::class, ['queryRefund', 'queryRefundByRefundNo'], $app)->makePartial();

$refundNo = 'foo';

$client->expects()->queryRefund($refundNo, Client::OUT_REFUND_NO)->andReturn('mock-result')->once();
$client->expects()->queryRefund($refundNo, Client::OUT_REFUND_NO)->andReturn('mock-result');

$this->assertSame('mock-result', $client->queryRefundByRefundNo($refundNo));
}

public function testQueryRefundByTransactionId()
{
$app = new Application();

$client = $this->mockApiClient(Client::class, ['queryRefund', 'queryRefundByTransactionId'], $app)->makePartial();

$transactionId = 'foo';

$client->expects()->queryRefund($transactionId, Client::TRANSACTION_ID)->andReturn('mock-result')->once();
$client->expects()->queryRefund($transactionId, Client::TRANSACTION_ID)->andReturn('mock-result');

$this->assertSame('mock-result', $client->queryRefundByTransactionId($transactionId));
}

public function testQueryRefundByRefundId()
{
$app = new Application();

$client = $this->mockApiClient(Client::class, ['queryRefund', 'queryRefundByRefundId'], $app)->makePartial();

$refundId = 'foo';

$client->expects()->queryRefund($refundId, Client::REFUND_ID)->andReturn('mock-result')->once();
$client->expects()->queryRefund($refundId, Client::REFUND_ID)->andReturn('mock-result');

$this->assertSame('mock-result', $client->queryRefundByRefundId($refundId));
}

public function testDownloadBill()
{
$app = new Application();

$client = $this->mockApiClient(Client::class, ['downloadBill', 'getBody'], $app)->makePartial();

$data = 'foo';

$client->shouldReceive('request->getBody')
->once()
->andReturn('mock-result');

$this->assertSame('mock-result', $client->downloadBill($data));
Expand All @@ -252,6 +270,7 @@ public function testDownloadBill()
public function testReport()
{
$app = new Application();

$client = $this->mockApiClient(Client::class, 'report', $app)->makePartial();

$api = 'foo';
Expand All @@ -270,19 +289,21 @@ public function testReport()
'time' => time(),
], $optional);

$client->expects()->request($client->wrapApi('payitil/report'), $params)->andReturn('mock-result')->once();
$client->expects()->request($client->wrapApi('payitil/report'), $params)->andReturn('mock-result');

$this->assertSame('mock-result', $client->report($api, $timeConsuming, $resultCode, $returnCode, $optional));
}

public function testAuthCodeToOpenId()
{
$app = new Application();

$client = $this->mockApiClient(Client::class, 'authCodeToOpenId', $app)->makePartial();

$authCode = 'foo';

$client->expects()->request('https://api.mch.weixin.qq.com/tools/authcodetoopenid', ['auth_code' => $authCode])->andReturn('mock-result')->once();
$client->expects()->request('https://api.mch.weixin.qq.com/tools/authcodetoopenid', ['auth_code' => $authCode])->andReturn('mock-result');

$this->assertSame('mock-result', $client->authCodeToOpenId($authCode));
}

Expand Down
Loading

0 comments on commit 90423ea

Please sign in to comment.