phpunit 能否 mock 支付宝回调呢

phpunit 能否 mock 支付宝回调呢

需要 触发支付宝回调 测试 数据是否正常 phpunit能够做到吗

chowjiawei
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
最佳答案

可以的

// GuzzleHttp 测试用例
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\RequestException;

// Create a mock and queue two responses.
$mock = new MockHandler([
    new Response(200, ['X-Foo' => 'Bar']),
    new Response(202, ['Content-Length' => 0]),
    new RequestException("Error Communicating with Server", new Request('GET', 'test'))
]);

$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);

// The first request is intercepted with the first response.
echo $client->request('GET', '/')->getStatusCode();
//> 200
// The second request is intercepted with the second response.
echo $client->request('GET', '/')->getStatusCode();
//> 202

如果不是用的 GuzzleHttp,那就需要手动去创建 phpunit 的 mock 对象。然后返回数据

// 简单的测试用例
namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

class MockTest extends BaseTestCase
{
    public function testMock()
    {
        $map = [
            [1,2,3],
            [10,5, 15]
        ];
        $test = $this->createMock(test::class);
        $test->expects($this->any())
            ->method('plus')
            ->will($this->returnValueMap($map));

        $this->assertSame(3, $test->plus(1,2));
        $this->assertSame(15, $test->plus(10,5));
        $this->assertSame(16, $test->plus(10,6)); // error: $map 没有设置,mock 不认识
    }
}


interface test
{
    public function plus(int $a, int $b);
}
1年前 评论
讨论数量: 5

可以的

// GuzzleHttp 测试用例
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\RequestException;

// Create a mock and queue two responses.
$mock = new MockHandler([
    new Response(200, ['X-Foo' => 'Bar']),
    new Response(202, ['Content-Length' => 0]),
    new RequestException("Error Communicating with Server", new Request('GET', 'test'))
]);

$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);

// The first request is intercepted with the first response.
echo $client->request('GET', '/')->getStatusCode();
//> 200
// The second request is intercepted with the second response.
echo $client->request('GET', '/')->getStatusCode();
//> 202

如果不是用的 GuzzleHttp,那就需要手动去创建 phpunit 的 mock 对象。然后返回数据

// 简单的测试用例
namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

class MockTest extends BaseTestCase
{
    public function testMock()
    {
        $map = [
            [1,2,3],
            [10,5, 15]
        ];
        $test = $this->createMock(test::class);
        $test->expects($this->any())
            ->method('plus')
            ->will($this->returnValueMap($map));

        $this->assertSame(3, $test->plus(1,2));
        $this->assertSame(15, $test->plus(10,5));
        $this->assertSame(16, $test->plus(10,6)); // error: $map 没有设置,mock 不认识
    }
}


interface test
{
    public function plus(int $a, int $b);
}
1年前 评论
chowjiawei

@kis龍 支付宝的回调数据 就得自己来伪造了吧 :joy:

1年前 评论
kis龍 1年前
kis龍 1年前
chowjiawei (作者) (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!