Skip to content

Commit

Permalink
Merge pull request #255 from tianyong90/develop
Browse files Browse the repository at this point in the history
Add tests for LuckyMoney.
  • Loading branch information
overtrue committed Jan 16, 2016
2 parents bfb86d2 + f50aceb commit 183e0b9
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Foundation/ServiceProviders/PaymentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function register(Container $pimple)
return new Payment($pimple['merchant']);
};

$pimple['luckymoney'] = function ($pimple) {
$pimple['lucky_money'] = function ($pimple) {
return new LuckyMoney($pimple['merchant']);
};
}
Expand Down
13 changes: 8 additions & 5 deletions src/Payment/LuckyMoney/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function __construct(Merchant $merchant)
/**
* Prepare luckymoney.
*
* @param array $params
*
* @return \EasyWeChat\Support\Collection
*/
Expand All @@ -86,21 +87,23 @@ public function prepare(array $params)
/**
* Query luckymoney.
*
* @param string $orderNo
* @param string $mchBillNo
*
* @return \EasyWeChat\Support\Collection
*/
public function query($orderNo)
public function query($mchBillNo)
{
$params = [
'appid' => $this->merchant->app_id,
'mch_billno' => $orderNo,
'mch_billno' => $mchBillNo,
'bill_type' => 'MCHT',
];

return $this->request(self::API_QUERY, $params);
}

/**
* Send Luckymoney.
* Send LuckyMoney.
*
* @param array $params
* @param string $type
Expand All @@ -117,7 +120,7 @@ public function send(array $params, $type = self::TYPE_NORMAL)
}

/**
* Send normal lucnymoney.
* Send normal LuckyMoney.
*
* @param array $params
*
Expand Down
138 changes: 138 additions & 0 deletions tests/Payment/PaymentLuckyMoneyAPITest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?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.
*/

use EasyWeChat\Core\Http;
use EasyWeChat\Payment\LuckyMoney\API;
use EasyWeChat\Payment\Merchant;
use EasyWeChat\Support\XML;

class PaymentLuckyMoneyAPITest extends PHPUnit_Framework_TestCase
{
/**
* Build API instance.
*
* @return API
*/
public function getAPI()
{
$http = Mockery::mock(Http::class);

$http->shouldReceive('request')->andReturnUsing(function ($api, $method, $options) {
$options['body'] = XML::parse($options['body']);

return XML::build(compact('api', 'options'));
});

$merchant = new Merchant([
'merchant_id' => 'testMerchantId',
'app_id' => 'wxTestAppId',
'key' => 'testKey',
'cert_path' => 'testCertPath',
'key_path' => 'testKeyPath',
]);

$api = Mockery::mock('EasyWeChat\Payment\LuckyMoney\API[getHttp]', [$merchant]);
$api->shouldReceive('getHttp')->andReturn($http);

return $api;
}

/**
* Test prepare().
*/
public function testPrepare()
{
$api = $this->getAPI();

$response = $api->prepare(['foo' => 'bar']);

$this->assertEquals(API::API_PREPARE, $response['api']);
$this->assertEquals('wxTestAppId', $response['options']['body']['wxappid']);
$this->assertEquals('testMerchantId', $response['options']['body']['mch_id']);

$this->assertEquals('1000052601', $response['options']['body']['auth_mchid']);
$this->assertEquals('wxbf42bd79c4391863', $response['options']['body']['auth_appid']);

$this->assertEquals('ALL_RAND', $response['options']['body']['amt_type']);

$this->assertEquals('bar', $response['options']['body']['foo']);
}

/**
* Test query().
*/
public function testQuery()
{
$api = $this->getAPI();
$response = $api->query('testTradeNoFoo');

$this->assertEquals(API::API_QUERY, $response['api']);
$this->assertEquals('testTradeNoFoo', $response['options']['body']['mch_billno']);
}

/**
* Test send().
*/
public function testSend()
{
$api = $this->getAPI();

$response = $api->send(['foo' => 'bar'], API::TYPE_NORMAL);
$this->assertEquals(API::API_SEND, $response['api']);
$this->assertEquals('bar', $response['options']['body']['foo']);

$this->assertEquals('wxTestAppId', $response['options']['body']['wxappid']);

$response = $api->send(['foo' => 'bar'], API::TYPE_GROUP);
$this->assertEquals(API::API_SEND_GROUP, $response['api']);
$this->assertEquals('bar', $response['options']['body']['foo']);

$this->assertEquals('wxTestAppId', $response['options']['body']['wxappid']);
}

/**
* Test sendNormal().
*/
public function testSendNormal()
{
$api = $this->getAPI();

$response = $api->sendNormal(['foo' => 'bar']);
$this->assertEquals(API::API_SEND, $response['api']);
$this->assertEquals('bar', $response['options']['body']['foo']);

$this->assertEquals(1, $response['options']['body']['total_num']);
}

/**
* Test sendGroup().
*/
public function testSendGroup()
{
$api = $this->getAPI();

$response = $api->sendGroup(['foo' => 'bar']);
$this->assertEquals(API::API_SEND_GROUP, $response['api']);
$this->assertEquals('bar', $response['options']['body']['foo']);
}

/**
* Test setMerchant() and getMerchant().
*/
public function testMerchantGetterAndSetter()
{
$api = $this->getAPI();
$merchant = Mockery::mock(Merchant::class);
$api->setMerchant($merchant);

$this->assertEquals($merchant, $api->getMerchant());
}
}
58 changes: 58 additions & 0 deletions tests/Payment/PaymentLuckyMoneyLuckyMoneyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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.
*/

use EasyWeChat\Payment\LuckyMoney\API;
use EasyWeChat\Payment\Merchant;
use EasyWeChat\Payment\LuckyMoney\LuckyMoney;

class PaymentLuckyMoneyLuckyMoneyTest extends PHPUnit_Framework_TestCase
{
/**
* Return LuckyMoney instance.
*
* @return LuckyMoney
*/
public function getLuckyMoney()
{
$merchant = new Merchant([
'merchant_id' => 'testMerchantId',
'app_id' => 'wxTestAppId',
'key' => 'testKey',
'cert_path' => 'testCertPath',
'key_path' => 'testKeyPath',
]);

return new LuckyMoney($merchant);
}

/**
* Test setMerchant()、getMerchant()、setAPI() and getAPI().
*/
public function testSetterAndGetter()
{
$luckyMoney = $this->getLuckyMoney();

$this->assertInstanceOf(API::class, $luckyMoney->getAPI());
$this->assertInstanceOf(Merchant::class, $luckyMoney->getMerchant());

$api = Mockery::mock(API::class);
$luckyMoney->setAPI($api);
$this->assertEquals($api, $luckyMoney->getAPI());

$merchant = Mockery::mock(Merchant::class);
$api = Mockery::mock(API::class);

$luckyMoney->setAPI($api);
$luckyMoney->setMerchant($merchant);
$this->assertEquals($merchant, $luckyMoney->getMerchant());
$this->assertEquals($merchant, $merchant);
}
}

0 comments on commit 183e0b9

Please sign in to comment.