|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace WeChatPay\Tests\OpenAPI\V2\Papay; |
| 4 | + |
| 5 | +use GuzzleHttp\Handler\MockHandler; |
| 6 | +use GuzzleHttp\HandlerStack; |
| 7 | +use GuzzleHttp\Psr7\Response; |
| 8 | +use Psr\Http\Message\ResponseInterface; |
| 9 | +use WeChatPay\Builder; |
| 10 | +use WeChatPay\Crypto\Hash; |
| 11 | +use WeChatPay\Formatter; |
| 12 | +use WeChatPay\Transformer; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +class H5entrustwebTest extends TestCase |
| 16 | +{ |
| 17 | + /** @var MockHandler $mock */ |
| 18 | + private $mock; |
| 19 | + |
| 20 | + private function guzzleMockStack(): HandlerStack |
| 21 | + { |
| 22 | + $this->mock = new MockHandler(); |
| 23 | + |
| 24 | + return HandlerStack::create($this->mock); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @param string $mchid |
| 29 | + * @return array{\WeChatPay\BuilderChainable} |
| 30 | + */ |
| 31 | + private function prepareEnvironment(string $mchid, string $secret): array |
| 32 | + { |
| 33 | + $instance = Builder::factory([ |
| 34 | + 'mchid' => $mchid, |
| 35 | + 'serial' => 'nop', |
| 36 | + 'privateKey' => 'any', |
| 37 | + 'certs' => ['any' => null], |
| 38 | + 'secret' => $secret, |
| 39 | + 'handler' => $this->guzzleMockStack(), |
| 40 | + ]); |
| 41 | + |
| 42 | + $endpoint = $instance->chain('v2/papay/h5entrustweb'); |
| 43 | + |
| 44 | + return [$endpoint]; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return array<string,array{string,string,array<string,int|string>,ResponseInterface}> |
| 49 | + */ |
| 50 | + public function mockRequestsDataProvider(): array |
| 51 | + { |
| 52 | + $mchid = '1230000109'; |
| 53 | + $secret = Formatter::nonce(32); |
| 54 | + $queryData = [ |
| 55 | + 'appid' => 'wxcbda96de0b165486', |
| 56 | + 'mch_id' => $mchid, |
| 57 | + 'plan_id' => '12535', |
| 58 | + 'contract_code' => '100000', |
| 59 | + 'request_serial' => 1000, |
| 60 | + 'contract_display_account' => '微信代扣', |
| 61 | + 'notify_url' => 'https://weixin.qq.com', |
| 62 | + 'version' => '1.0', |
| 63 | + 'sign_type' => 'HMAC-SHA256', |
| 64 | + 'timestamp' => Formatter::timestamp(), |
| 65 | + 'return_appid' => 'wxcbda96de0b165486', |
| 66 | + ]; |
| 67 | + $responseData = [ |
| 68 | + 'return_code' => 'SUCCESS', |
| 69 | + 'return_msg' => '', |
| 70 | + 'result_code' => 'SUCCESS', |
| 71 | + 'result_msg' => '', |
| 72 | + 'redirect_url' => 'https://payapp.weixin.qq.com', |
| 73 | + ]; |
| 74 | + |
| 75 | + return [ |
| 76 | + 'return_code=SUCCESS' => [$mchid, $secret, $queryData, new Response(200, [], Transformer::toXml($responseData))], |
| 77 | + ]; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @dataProvider mockRequestsDataProvider |
| 82 | + * @param string $mchid |
| 83 | + * @param string $secret |
| 84 | + * @param array<string,int|string> $query |
| 85 | + * @param ResponseInterface $respondor |
| 86 | + */ |
| 87 | + public function testGet(string $mchid, string $secret, array $query, ResponseInterface $respondor): void |
| 88 | + { |
| 89 | + [$endpoint] = $this->prepareEnvironment($mchid, $secret); |
| 90 | + |
| 91 | + $this->mock->reset(); |
| 92 | + $this->mock->append($respondor); |
| 93 | + |
| 94 | + $res = $endpoint->get(['nonceless' => true, 'query' => $query]); |
| 95 | + self::responseAssertion($res); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param ResponseInterface $response |
| 100 | + */ |
| 101 | + private static function responseAssertion(ResponseInterface $response): void |
| 102 | + { |
| 103 | + $txt = (string) $response->getBody(); |
| 104 | + $array = Transformer::toArray($txt); |
| 105 | + static::assertArrayHasKey('redirect_url', $array); |
| 106 | + static::assertArrayHasKey('return_code', $array); |
| 107 | + static::assertArrayHasKey('result_code', $array); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @dataProvider mockRequestsDataProvider |
| 112 | + * @param string $mchid |
| 113 | + * @param string $secret |
| 114 | + * @param array<string,string> $query |
| 115 | + * @param ResponseInterface $respondor |
| 116 | + */ |
| 117 | + public function testGetAsync(string $mchid, string $secret, array $query, ResponseInterface $respondor): void |
| 118 | + { |
| 119 | + [$endpoint] = $this->prepareEnvironment($mchid, $secret); |
| 120 | + |
| 121 | + $this->mock->reset(); |
| 122 | + $this->mock->append($respondor); |
| 123 | + |
| 124 | + $endpoint->getAsync([ |
| 125 | + 'nonceless' => true, |
| 126 | + 'query' => $query, |
| 127 | + ])->then(static function(ResponseInterface $res) { |
| 128 | + self::responseAssertion($res); |
| 129 | + })->wait(); |
| 130 | + } |
| 131 | +} |
0 commit comments