Skip to content

Commit

Permalink
添加单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Varorbc committed Nov 22, 2017
1 parent 27861c5 commit d36f1b5
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 11 deletions.
3 changes: 1 addition & 2 deletions sample/ICanPay.Demo/Controllers/RefundQueryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ private Alipay.Notify RefundQueryAlipayOrder(string id)
return (Alipay.Notify)gateway.RefundQuery(new Alipay.Auxiliary
{
OutTradeNo = id,
TradeNo = "12",
OutRefundNo = "123"
OutRefundNo = "13"
});
}

Expand Down
5 changes: 1 addition & 4 deletions sample/ICanPay.Demo/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ public void ConfigureServices(IServiceCollection services)
FrontUrl = "http://localhost:61337/Notify"
};

gateways.Add(new AlipayGateway(alipayMerchant)
{
GatewayUrl = "https://openapi.alipaydev.com/gateway.do?charset=UTF-8"
});
gateways.Add(new AlipayGateway(alipayMerchant));
gateways.Add(new WechatpayGateway(wechatpayMerchant));
gateways.Add(new UnionpayGateway(unionpayMerchant));

Expand Down
16 changes: 14 additions & 2 deletions src/ICanPay.Alipay/AlipayGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ public sealed class AlipayGateway
{

#region 私有字段

#if DEBUG
private const string GATEWAYURL = "https://openapi.alipaydev.com/gateway.do?charset=UTF-8";
#else
private const string GATEWAYURL = "https://openapi.alipay.com/gateway.do?charset=UTF-8";
#endif
private readonly Merchant _merchant;

#endregion
Expand All @@ -44,7 +47,11 @@ public AlipayGateway(Merchant merchant)

public new Merchant Merchant => _merchant;

public new Order Order => (Order)base.Order;
public new Order Order
{
get => (Order)base.Order;
set => base.Order = value;
}

public new Notify Notify => (Notify)base.Notify;

Expand Down Expand Up @@ -162,6 +169,11 @@ public void BuildBarcodePayment()
.GetAwaiter()
.GetResult();
}

OnPaymentFailed(new PaymentFailedEventArgs(this)
{
Message = Notify.SubMessage
});
}

public void InitBarcodePayment()
Expand Down
2 changes: 1 addition & 1 deletion src/ICanPay.Alipay/Auxiliary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Auxiliary : IAuxiliary
/// </summary>
[StringLength(64, ErrorMessage = "退款请求号最大长度为64位")]
[Necessary(GatewayAuxiliaryType.RefundQuery, ErrorMessage = "请设置退款请求号")]
[ReName(Name = Constant.OUT_REQUEST_NO)]
[JsonProperty(Constant.OUT_REQUEST_NO)]
public string OutRefundNo { get; set; }

[JsonIgnore]
Expand Down
4 changes: 4 additions & 0 deletions src/ICanPay.Core/Events/PaymentEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ public abstract class PaymentEventArgs : EventArgs
protected PaymentEventArgs(GatewayBase gateway)
{
_gateway = gateway;
#if DEBUG
_notifyServerHostAddress = "127.0.0.1";
#else
_notifyServerHostAddress = HttpUtil.RemoteIpAddress.ToString();
#endif
}

#endregion
Expand Down
6 changes: 5 additions & 1 deletion src/ICanPay.Unionpay/UnionpayGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public UnionpayGateway(Merchant merchant)

public new Merchant Merchant => _merchant;

public new Order Order => (Order)base.Order;
public new Order Order
{
get => (Order)base.Order;
set => base.Order = value;
}

public new Notify Notify => (Notify)base.Notify;

Expand Down
11 changes: 10 additions & 1 deletion src/ICanPay.Wechatpay/WechatpayGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public WechatpayGateway(Merchant merchant)

public new Merchant Merchant => _merchant;

public new Order Order => (Order)base.Order;
public new Order Order
{
get => (Order)base.Order;
set => base.Order = value;
}

public new Notify Notify => (Notify)base.Notify;

Expand Down Expand Up @@ -210,6 +214,11 @@ public void BuildBarcodePayment()
.GetAwaiter()
.GetResult();
}

OnPaymentFailed(new PaymentFailedEventArgs(this)
{
Message = Notify.ReturnMsg
});
}

public void InitBarcodePayment()
Expand Down
183 changes: 183 additions & 0 deletions test/ICanPay.UnitTest/AlipayGatewayTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
using ICanPay.Alipay;
using ICanPay.Core;
using ICanPay.Core.Utils;
using System;
using System.Net;
using Xunit;
using Xunit.Abstractions;

namespace ICanPay.UnitTest
{
public class AlipayGatewayTest
{
private readonly ITestOutputHelper _output;
private readonly AlipayGateway _alipayGateway;
private readonly Merchant _merchant;
private readonly Order _order;
private string _outTradeNo => DateTime.Now.ToString("yyyyMMddhhmmss");

public AlipayGatewayTest(ITestOutputHelper output)
{
_output = output;

_merchant = new Merchant
{
AppId = "2016081600256163",
NotifyUrl = "http://localhost:61337/Notify",
ReturnUrl = "http://localhost:61337/Return",
AlipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsW6+mN2E3Oji2DPjSKuYgRzK6MlH9q6W0iM0Yk3R0qbpp5wSesSXqudr2K25gIBOTCchiIbXO7GXt/zEdnhnC32eOaTnonDsnuBWIp+q7LoVx/gvKIX5LTHistCvGli8VW4EDGsu2jAyQXyMPgPrIz+/NzWis/gZsa4TaqVY4SpWRuSgMXxleh2ERB6k0ijK0IYM+Cv5fz1ZPDCgk7EbII2jk2fDxtlMLoN5UYEJCcD8OUyivm3Hti3u1kPolckCCf0xk+80g/4EdmzFAffsVgPeXZrkm5EIuiTTOIeRHXlTg3HtkkCw2Wl0CpYSKBr9Vzv7x0gNvb1wnXPmBJNRgQIDAQAB",
Privatekey = "MIIEpAIBAAKCAQEAyC43UbsE5XZ2Pmqg1YgzeCqAMk4HOH8fYHslseeSgKxyDjybjqM0yjGIJry1FRmVvLnY7v8jURgwr7d/pDCSRdoHa6zaxuSzg0OlieNmujae34YZ54PmFxULZW0BHSdzmx3OIYK2GarRECkds531ZzpbLdRXqsxQf5G26JZLIFxmNuh/VjBjJ6Hic1WOFT+FCYyi8om+LkPn3jELeA7LPLXzFqzzxx0vo4yiAePrsX5W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xV/sACXC6jCiO06IqreIbXdlJ41RBw2op0Ss5gM5pBRLUS58V+HP7GBWKrnrofofXtAq6zZ8txok4EQKBgQCXrTeGMs7ECfehLz64qZtPkiQbNwupg938Z40Qru/G1GR9u0kmN7ibTyYauI6NNVHGEZa373EBEkacfN+kkkLQMs1tj5Zrlw+iITm+ad/irpXQZS/NHCcrg6h82vu0LcgiKnHKlmW6K5ne0w4LqmsmRCm7JdJjt9WlapAs0ticiw=="
};

_order = new Order()
{
Amount = 0.01,
Subject = "支付宝测试"
};

_alipayGateway = new AlipayGateway(_merchant);
}

[Fact]
public void TestWebPay()
{
_order.OutTradeNo = _outTradeNo;
_alipayGateway.Order = _order;
_alipayGateway.InitFormPayment();

Assert.Throws<WebException>(() =>
{
HttpUtil.Post(_alipayGateway.GatewayUrl, _alipayGateway.GatewayData.ToUrl());
});
}

[Fact]
public void TestWapPay()
{
_order.OutTradeNo = _outTradeNo;
_alipayGateway.Order = _order;
string url = _alipayGateway.BuildUrlPayment();

Assert.Throws<WebException>(() =>
{
HttpUtil.Get(url);
});
}

[Fact]
public void TestAppPay()
{
_order.OutTradeNo = _outTradeNo;
_alipayGateway.Order = _order;
string result = _alipayGateway.BuildAppPayment();

_output.WriteLine(result);
}

[Fact]
public void TestBarcodePay()
{
_order.OutTradeNo = _outTradeNo;
_alipayGateway.Order = _order;
_alipayGateway.PaymentSucceed += _alipayGateway_PaymentSucceed;
_alipayGateway.PaymentFailed += _alipayGateway_PaymentFailed;
_alipayGateway.BuildBarcodePayment();
}

private void _alipayGateway_PaymentSucceed(object arg1, PaymentSucceedEventArgs arg2)
{
_output.WriteLine("支付成功");
}

private void _alipayGateway_PaymentFailed(object arg1, PaymentFailedEventArgs arg2)
{
_output.WriteLine(arg2.Message);
}

[Fact]
public void TestScanPay()
{
_order.OutTradeNo = _outTradeNo;
_alipayGateway.Order = _order;
string result = _alipayGateway.BuildScanPayment();

Assert.Contains("http", result);
_output.WriteLine(result);
}

[Fact]
public void TestQuery()
{
var notify = (Notify)_alipayGateway.BuildQuery(new Auxiliary
{
OutTradeNo = _outTradeNo
});

Assert.Contains("交易不存在", notify.SubMessage);
_output.WriteLine(Util.SerializeObject(notify));
}

[Fact]
public void TestCancel()
{
var notify = (Notify)_alipayGateway.BuildCancel(new Auxiliary
{
OutTradeNo = _outTradeNo
});

Assert.Contains("10000", notify.Code);
_output.WriteLine(Util.SerializeObject(notify));
}

[Fact]
public void TestClose()
{
var notify = (Notify)_alipayGateway.BuildClose(new Auxiliary
{
OutTradeNo = _outTradeNo
});

Assert.Contains("交易不存在", notify.SubMessage);
_output.WriteLine(Util.SerializeObject(notify));
}

[Fact]
public void TestRefund()
{
var notify = (Notify)_alipayGateway.BuildRefund(new Auxiliary
{
OutTradeNo = _outTradeNo,
RefundAmount = 0.01,
OutRefundNo = _outTradeNo
});

Assert.Contains("交易不存在", notify.SubMessage);
_output.WriteLine(Util.SerializeObject(notify));
}

[Fact]
public void TestRefundQuery()
{
var notify = (Notify)_alipayGateway.BuildRefundQuery(new Auxiliary
{
OutTradeNo = _outTradeNo,
OutRefundNo = "123"
});

Assert.Contains("交易不存在", notify.SubMessage);
_output.WriteLine(Util.SerializeObject(notify));
}

[Fact]
public void TestBillDownload()
{
var fileStream = _alipayGateway.BuildBillDownload(new Auxiliary
{
BillType = "trade",
BillDate = "2017-10-31"
});

Assert.True(fileStream.Length > 0);
}
}
}
Loading

0 comments on commit d36f1b5

Please sign in to comment.