Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getHandler() for HandlerStackFactory. #1469

Merged
merged 6 commits into from Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# v1.1.23 - TBD

## Added

- [#1469](https://github.com/hyperf/hyperf/pull/1469) Added method `getHandler()` for `Hyperf/Guzzle/HandlerStackFactory` and use `make()` function to create the handler instead of `new` operator when it is possible.

## Fixed

- [#1471](https://github.com/hyperf/hyperf/pull/1471) Fixed data recved failed, when the body is larger than max-output-buffer-size.
Expand Down
23 changes: 16 additions & 7 deletions src/guzzle/src/HandlerStackFactory.php
Expand Up @@ -56,13 +56,7 @@ public function create(array $option = [], array $middlewares = []): HandlerStac
$middlewares = array_merge($this->middlewares, $middlewares);

if (Coroutine::getCid() > 0) {
if ($this->usePoolHandler) {
$handler = make(PoolHandler::class, [
'option' => $option,
]);
} else {
$handler = new CoroutineHandler();
}
$handler = $this->getHandler($option);
}

$stack = HandlerStack::create($handler);
Expand All @@ -80,4 +74,19 @@ public function create(array $option = [], array $middlewares = []): HandlerStac

return $stack;
}

protected function getHandler(array $option)
{
if ($this->usePoolHandler) {
return make(PoolHandler::class, [
'option' => $option,
]);
}

if (class_exists(ApplicationContext::class)) {
return make(CoroutineHandler::class);
}

return new CoroutineHandler();
}
}
25 changes: 25 additions & 0 deletions src/guzzle/tests/Cases/HandlerStackFactoryTest.php
Expand Up @@ -23,6 +23,7 @@
use Hyperf\Pool\SimplePool\PoolFactory;
use Hyperf\Utils\ApplicationContext;
use HyperfTest\Guzzle\Stub\CoroutineHandlerStub;
use HyperfTest\Guzzle\Stub\HandlerStackFactoryStub;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -56,6 +57,30 @@ public function testCreateCoroutineHandler()
}
}

public function testMakeCoroutineHandler()
{
$container = Mockery::mock(Container::class);
ApplicationContext::setContainer($container);
$container->shouldReceive('make')->with(CoroutineHandler::class, Mockery::any())->andReturn(new CoroutineHandler());

$factory = new HandlerStackFactoryStub();
$stack = $factory->create();
$this->assertInstanceOf(HandlerStack::class, $stack);
$this->assertTrue($stack->hasHandler());

$ref = new \ReflectionClass($stack);

$handler = $ref->getProperty('handler');
$handler->setAccessible(true);
$this->assertInstanceOf(CoroutineHandler::class, $handler->getValue($stack));

$property = $ref->getProperty('stack');
$property->setAccessible(true);
foreach ($property->getValue($stack) as $stack) {
$this->assertTrue(in_array($stack[1], ['http_errors', 'allow_redirects', 'cookies', 'prepare_body', 'retry']));
}
}

public function testCreatePoolHandler()
{
$this->setContainer();
Expand Down
23 changes: 23 additions & 0 deletions src/guzzle/tests/Stub/HandlerStackFactoryStub.php
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace HyperfTest\Guzzle\Stub;

use Hyperf\Guzzle\HandlerStackFactory;

class HandlerStackFactoryStub extends HandlerStackFactory
{
public function __construct()
{
$this->usePoolHandler = false;
}
}