Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Payment 单元测试及部分问题修复 #851

Merged
merged 2 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Payment/Jssdk/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public function bridgeConfig($prepayId, $json = true)
*
* @param string $prepayId
*
* @return array|string
* @return array
*/
public function sdkConfig($prepayId)
{
$config = $this->configForPayment($prepayId, false);
$config = $this->bridgeConfig($prepayId, false);

$config['timestamp'] = $config['timeStamp'];
unset($config['timeStamp']);
Expand Down
4 changes: 2 additions & 2 deletions tests/Kernel/AccessTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public function testCache()
$this->assertInstanceOf(CacheInterface::class, $token->getCache());

// prepended cache instance
$cache = mock(CacheInterface::class);
$cache = \Mockery::mock(CacheInterface::class);
$app['cache'] = function () use ($cache) {
return $cache;
};
$token = mock(AccessToken::class.'[setCache]', [$app]);
$token = \Mockery::mock(AccessToken::class.'[setCache]', [$app]);

$this->assertInstanceOf(CacheInterface::class, $token->getCache());
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Kernel/BaseClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class BaseClientTest extends TestCase
{
public function makeClient($methods = [], ServiceContainer $app = null, AccessToken $accessToken = null)
{
$methods = implode(',', (array) $methods);
$methods = implode(',', (array)$methods);

return \Mockery::mock(BaseClient::class."[{$methods}]", [
$app ?? \Mockery::mock(ServiceContainer::class),
Expand Down Expand Up @@ -106,7 +106,7 @@ public function testRequest()
'response_type' => 'array',
]);
$client = $this->makeClient(['registerHttpMiddlewares', 'performRequest'], $app)
->shouldAllowMockingProtectedMethods();
->shouldAllowMockingProtectedMethods();

// default value
$client->expects()->registerHttpMiddlewares()->once();
Expand Down Expand Up @@ -151,8 +151,8 @@ public function testHttpClient()
public function testRegisterMiddlewares()
{
$client = $this->makeClient(['retryMiddleware', 'accessTokenMiddleware', 'logMiddleware', 'pushMiddleware'])
->shouldAllowMockingProtectedMethods()
->shouldDeferMissing();
->shouldAllowMockingProtectedMethods()
->shouldDeferMissing();
$retryMiddleware = function () {
return 'retry';
};
Expand Down
30 changes: 30 additions & 0 deletions tests/Payment/ApplicationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EasyWeChat\Tests\Payment;

use EasyWeChat\Payment\Application;
use EasyWeChat\Tests\TestCase;
use EasyWeChat\Payment\Client;

class ApplicationTest extends TestCase
{
public function testMagicCall()
{
$app = new Application();

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

// test calling nonexistent method
$this->expectException(\PHPUnit\Framework\Error\Warning::class);
$app->noncexistentMethod('foo');
}
}
107 changes: 107 additions & 0 deletions tests/Payment/BaseClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EasyWeChat\Tests\Payment;

use EasyWeChat\Payment\Application;
use EasyWeChat\Payment\BaseClient;
use EasyWeChat\Kernel\Http\Response;
use EasyWeChat\Tests\TestCase;
use EasyWeChat\Kernel\Support;

class BaseClientTest extends TestCase
{
public function testPrepends()
{
$app = new Application();
$client = $this->mockApiClient(BaseClient::class, 'prepends', $app)->makePartial();

$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();

$api = 'http://easywechat.org';
$params = ['foo' => 'bar'];
$method = 'post';
$options = ['foo' => 'bar'];

$mockResponse = new Response(200, [], 'response-content');

$client->expects()->performRequest($api, $method, \Mockery::on(function ($options) {
$this->assertSame('bar', $options['foo']);
$this->assertInternalType('string', $options['body']);

$bodyInOptions = Support\XML::parse($options['body']);

$this->assertSame($bodyInOptions['foo'], $options['foo']);
$this->assertInternalType('string', $bodyInOptions['nonce_str']);
$this->assertInternalType('string', $bodyInOptions['sign']);

return true;
}))->times(3)->andReturn($mockResponse);


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

// $returnResponse = false
$this->assertSame(['foo' => 'mock-bar'], $client->request($api, $params, $method, $options, false));

// $returnResponse = true
$this->assertInstanceOf(Response::class, $client->request($api, $params, $method, $options, true));
$this->assertSame('response-content', $client->request($api, $params, $method, $options, true)->getBodyContents());
}

public function testRequestRaw()
{
$app = new Application();
$client = $this->mockApiClient(BaseClient::class, ['request', 'requestRaw'], $app)->makePartial();

$api = 'http://easywechat.org';
$params = ['foo' => 'bar'];
$method = 'post';
$options = [];

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

$this->assertSame('mock-result', $client->requestRaw($api, $params, $method, $options));
}

public function testSafeRequest()
{
$app = new Application([
'app_id' => 'wx123456',
'cert_path' => 'foo',
'key_path' => 'bar',
]);
$client = $this->mockApiClient(BaseClient::class, ['request', 'safeRequest'], $app)->makePartial();

$api = 'http://easywechat.org';
$params = ['foo' => 'bar'];
$method = 'post';

$options = [
'cert' => $app['merchant']->get('cert_path'),
'ssl_key' => $app['merchant']->get('key_path'),
];

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

$this->assertSame('mock-result', $client->safeRequest($api, $params, $method));
}
}
Loading