Skip to content

Commit

Permalink
tests on HOWTO download the complaint images, also cover onto guzzl…
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNorthMemory committed Mar 11, 2022
1 parent 9745acf commit 0383b25
Showing 1 changed file with 78 additions and 9 deletions.
87 changes: 78 additions & 9 deletions tests/OpenAPI/V3/MerchantService/Images/DownloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\LazyOpenStream;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -36,9 +37,10 @@ private function guzzleMockStack(): HandlerStack

/**
* @param array<string,mixed> $config
* @param string $assertMethod
* @return array{\WeChatPay\BuilderChainable,HandlerStack}
*/
private function newInstance(array $config): array
private function newInstance(array $config, string $assertMethod): array
{
$instance = Builder::factory($config + ['handler' => $this->guzzleMockStack(),]);

Expand All @@ -47,14 +49,18 @@ private function newInstance(array $config): array
$stack = clone $stack;
$stack->remove('verifier');

$stack->push(Middleware::tap(/* before */static function (RequestInterface $request) {
// Note here: because the `$target` is used onto the `signature`, **IT IS NOT SAME TO** the original URI.
// And **NO IDEA** about the platform HOW TO VERIFY the `media_slot_url` while there contains the double pct-encoded characters.
$stack->push(Middleware::tap(/* before */static function (RequestInterface $request) use ($assertMethod) {
self::assertTrue($request->hasHeader('Authorization'));
self::assertStringStartsWith('WECHATPAY2-SHA256-RSA2048', $request->getHeaderLine('Authorization'));

$target = $request->getRequestTarget();
self::assertNotEquals('/v3/merchant-service/images/' . self::MEDIA_ID, $target);
}, /* after */static function (RequestInterface $request) {
self::{$assertMethod}('/v3/merchant-service/images/' . self::MEDIA_ID, $target);
}, /* after */static function (RequestInterface $request) use ($assertMethod) {
self::assertTrue($request->hasHeader('Authorization'));
self::assertStringStartsWith('WECHATPAY2-SHA256-RSA2048', $request->getHeaderLine('Authorization'));

$target = $request->getRequestTarget();
self::assertNotEquals('/v3/merchant-service/images/' . self::MEDIA_ID, $target);
self::{$assertMethod}('/v3/merchant-service/images/' . self::MEDIA_ID, $target);
}));

return [$instance, $stack];
Expand Down Expand Up @@ -88,7 +94,11 @@ public function mockDataProvider(): array
*/
public function testGet(array $config, string $slot, ResponseInterface $respondor): void
{
[$endpoint, $stack] = $this->newInstance($config);
// Note here: using the `UriTemplate` may be caused that, **IT IS NOT SAME TO** the original URI,
// because the `$slot` is used onto the `signature` algorithm.
// More @see https://github.com/guzzle/uri-template/issues/18
// And **NO IDEA** about the platform HOW TO VERIFY the `$slot` while there contains the double pct-encoded characters.
[$endpoint, $stack] = $this->newInstance($config, 'assertNotEquals');

$this->mock->reset();
$this->mock->append($respondor);
Expand Down Expand Up @@ -124,7 +134,11 @@ private static function responseAssertion(ResponseInterface $response): void
*/
public function testGetAsync(array $config, string $slot, ResponseInterface $respondor): void
{
[$endpoint, $stack] = $this->newInstance($config);
// Note here: using the `UriTemplate` may be caused that, **IT IS NOT SAME TO** the original URI,
// because the `$slot` is used onto the `signature` algorithm.
// More @see https://github.com/guzzle/uri-template/issues/18
// And **NO IDEA** about the platform HOW TO VERIFY the `media_slot_url` while there contains the double pct-encoded characters.
[$endpoint, $stack] = $this->newInstance($config, 'assertNotEquals');

$this->mock->reset();
$this->mock->append($respondor);
Expand All @@ -144,4 +158,59 @@ public function testGetAsync(array $config, string $slot, ResponseInterface $res
self::responseAssertion($response);
})->wait();
}

/**
* @dataProvider mockDataProvider
* @param array<string,mixed> $config
* @param string $slot
* @param ResponseInterface $respondor
*/
public function testUseStandardGuzzleHttpClient(array $config, string $slot, ResponseInterface $respondor): void
{
[$endpoint, $stack] = $this->newInstance($config, 'assertEquals');

$relativeUrl = 'v3/merchant-service/images/' . $slot;
$fullUri = 'https://api.mch.weixin.qq.com/' . $relativeUrl;

$apiv3Client = $endpoint->getDriver()->select();
self::assertInstanceOf(ClientInterface::class, $apiv3Client);

$this->mock->reset();

$this->mock->append($respondor);
$response = $apiv3Client->request('GET', $relativeUrl, ['handler' => $stack]);
self::responseAssertion($response);

$this->mock->append($respondor);
$response = $apiv3Client->request('GET', $fullUri, ['handler' => $stack]);
self::responseAssertion($response);

$this->mock->append($respondor);
/** @phpstan-ignore-next-line because of \GuzzleHttp\ClientInterface no signature `get` method */
$response = $apiv3Client->get($relativeUrl, ['handler' => $stack]);
self::responseAssertion($response);

$this->mock->append($respondor);
/** @phpstan-ignore-next-line because of \GuzzleHttp\ClientInterface no signature `get` method */
$response = $apiv3Client->get($fullUri, ['handler' => $stack]);
self::responseAssertion($response);

$asyncAssertion = static function (ResponseInterface $response) {
self::responseAssertion($response);
};

$this->mock->append($respondor);
/** @phpstan-ignore-next-line because of \GuzzleHttp\ClientInterface no signature `getAsync` method */
$response = $apiv3Client->getAsync($fullUri, ['handler' => $stack])->then($asyncAssertion)->wait();

$this->mock->append($respondor);
/** @phpstan-ignore-next-line because of \GuzzleHttp\ClientInterface no signature `getAsync` method */
$response = $apiv3Client->getAsync($relativeUrl, ['handler' => $stack])->then($asyncAssertion)->wait();

$this->mock->append($respondor);
$response = $apiv3Client->requestAsync('GET', $relativeUrl, ['handler' => $stack])->then($asyncAssertion)->wait();

$this->mock->append($respondor);
$response = $apiv3Client->requestAsync('GET', $fullUri, ['handler' => $stack])->then($asyncAssertion)->wait();
}
}

0 comments on commit 0383b25

Please sign in to comment.