Merge pull request #1142 from limingxinleo/1.1-db

Fixed bug that Register::resolveConnection will return null.
This commit is contained in:
李铭昕 2019-12-16 10:27:18 +08:00 committed by GitHub
commit 046a8be026
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 135 additions and 14 deletions

View File

@ -1,5 +1,9 @@
# v1.1.11 - TBD
## Fixed
- [#1142](https://github.com/hyperf/hyperf/pull/1142) Fixed bug that Register::resolveConnection will return null.
# v1.1.10 - 2019-12-12
## Fixed

View File

@ -27,6 +27,7 @@ use Hyperf\Database\ConnectionResolverInterface;
use Hyperf\Database\Connectors\ConnectionFactory;
use Hyperf\Database\Connectors\MySqlConnector;
use Hyperf\Database\Migrations\MigrationRepositoryInterface;
use Hyperf\DbConnection\Listener\RegisterConnectionResolverListener;
use Hyperf\DbConnection\Pool\PoolFactory;
class ConfigProvider
@ -54,6 +55,9 @@ class ConfigProvider
GenSeederCommand::class,
SeedCommand::class,
],
'listeners' => [
RegisterConnectionResolverListener::class,
],
'annotations' => [
'scan' => [
'paths' => [

View File

@ -14,6 +14,7 @@ namespace Hyperf\DbConnection;
use Generator;
use Hyperf\Database\ConnectionInterface;
use Hyperf\Database\ConnectionResolverInterface;
use Hyperf\Database\Query\Builder;
use Hyperf\Database\Query\Expression;
use Hyperf\Utils\ApplicationContext;
@ -72,7 +73,7 @@ class Db
private function __connection($pool = 'default'): ConnectionInterface
{
$resolver = $this->container->get(ConnectionResolver::class);
$resolver = $this->container->get(ConnectionResolverInterface::class);
return $resolver->connection($pool);
}
}

View File

@ -0,0 +1,48 @@
<?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 Hyperf\DbConnection\Listener;
use Hyperf\Contract\ContainerInterface;
use Hyperf\Database\ConnectionResolverInterface;
use Hyperf\Database\Model\Register;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\BootApplication;
class RegisterConnectionResolverListener implements ListenerInterface
{
/**
* @var ContainerInterface
*/
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function listen(): array
{
return [
BootApplication::class,
];
}
public function process(object $event)
{
if ($this->container->has(ConnectionResolverInterface::class)) {
Register::setConnectionResolver(
$this->container->get(ConnectionResolverInterface::class)
);
}
}
}

View File

@ -13,8 +13,8 @@ declare(strict_types=1);
namespace Hyperf\DbConnection\Model;
use Hyperf\Database\ConnectionInterface;
use Hyperf\Database\ConnectionResolverInterface;
use Hyperf\Database\Model\Model as BaseModel;
use Hyperf\DbConnection\ConnectionResolver;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
@ -33,7 +33,7 @@ class Model extends BaseModel
public function getConnection(): ConnectionInterface
{
$connectionName = $this->getConnectionName();
$resolver = $this->getContainer()->get(ConnectionResolver::class);
$resolver = $this->getContainer()->get(ConnectionResolverInterface::class);
return $resolver->connection($connectionName);
}

View File

@ -13,8 +13,8 @@ declare(strict_types=1);
namespace HyperfTest\DbConnection;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Database\ConnectionResolverInterface;
use Hyperf\DbConnection\Connection;
use Hyperf\DbConnection\ConnectionResolver;
use Hyperf\DbConnection\Pool\PoolFactory;
use Hyperf\Utils\Context;
use HyperfTest\DbConnection\Stubs\ConnectionStub;
@ -39,7 +39,7 @@ class ConnectionTest extends TestCase
{
$container = ContainerStub::mockContainer();
$resolver = $container->get(ConnectionResolver::class);
$resolver = $container->get(ConnectionResolverInterface::class);
$connection = $resolver->connection();
@ -65,7 +65,7 @@ class ConnectionTest extends TestCase
{
$container = ContainerStub::mockContainer();
$resolver = $container->get(ConnectionResolver::class);
$resolver = $container->get(ConnectionResolverInterface::class);
/** @var \Hyperf\Database\Connection $connection */
$connection = $resolver->connection();
@ -95,7 +95,7 @@ class ConnectionTest extends TestCase
{
$container = ContainerStub::mockReadWriteContainer();
$resolver = $container->get(ConnectionResolver::class);
$resolver = $container->get(ConnectionResolverInterface::class);
/** @var \Hyperf\Database\Connection $connection */
$connection = $resolver->connection();
@ -112,7 +112,7 @@ class ConnectionTest extends TestCase
$container = ContainerStub::mockReadWriteContainer();
parallel([function () use ($container) {
$resolver = $container->get(ConnectionResolver::class);
$resolver = $container->get(ConnectionResolverInterface::class);
/** @var \Hyperf\Database\Connection $connection */
$connection = $resolver->connection();
@ -126,7 +126,7 @@ class ConnectionTest extends TestCase
}]);
parallel([function () use ($container) {
$resolver = $container->get(ConnectionResolver::class);
$resolver = $container->get(ConnectionResolverInterface::class);
/** @var \Hyperf\Database\Connection $connection */
$connection = $resolver->connection();

View File

@ -0,0 +1,19 @@
<?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\DbConnection;
use Hyperf\DbConnection\Model\Model;
class FooModel extends Model
{
}

View File

@ -0,0 +1,44 @@
<?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\DbConnection;
use Hyperf\Database\ConnectionResolverInterface;
use Hyperf\Database\Model\Register;
use Hyperf\Database\Model\Relations\Pivot;
use Hyperf\Utils\Context;
use HyperfTest\DbConnection\Stubs\ContainerStub;
use Mockery;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class RelationTest extends TestCase
{
protected function tearDown()
{
Mockery::close();
Context::set('database.connection.default', null);
}
public function testPivot()
{
$container = ContainerStub::mockContainer();
Register::setConnectionResolver($container->get(ConnectionResolverInterface::class));
$pivot = Pivot::fromAttributes(new FooModel(), ['created_at' => '2019-12-15 00:00:00'], 'foo', true);
$this->assertSame(['created_at' => '2019-12-15 00:00:00'], $pivot->toArray());
}
}

View File

@ -15,6 +15,7 @@ namespace HyperfTest\DbConnection\Stubs;
use Hyperf\Config\Config;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Database\ConnectionResolverInterface;
use Hyperf\Database\Connectors\ConnectionFactory;
use Hyperf\Database\Connectors\MySqlConnector;
use Hyperf\DbConnection\ConnectionResolver;
@ -40,7 +41,7 @@ class ContainerStub
$container->shouldReceive('get')->with(PoolFactory::class)->andReturn($factory);
$resolver = new ConnectionResolver($container);
$container->shouldReceive('get')->with(ConnectionResolver::class)->andReturn($resolver);
$container->shouldReceive('get')->with(ConnectionResolverInterface::class)->andReturn($resolver);
$config = new Config([
StdoutLoggerInterface::class => [
@ -103,7 +104,7 @@ class ContainerStub
$container->shouldReceive('get')->with(PoolFactory::class)->andReturn($factory);
$resolver = new ConnectionResolver($container);
$container->shouldReceive('get')->with(ConnectionResolver::class)->andReturn($resolver);
$container->shouldReceive('get')->with(ConnectionResolverInterface::class)->andReturn($resolver);
$config = new Config([
'databases' => [

View File

@ -15,6 +15,7 @@ namespace HyperfTest\ModelCache\Stub;
use Hyperf\Config\Config;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Database\ConnectionResolverInterface;
use Hyperf\Database\Connectors\ConnectionFactory;
use Hyperf\Database\Connectors\MySqlConnector;
use Hyperf\DbConnection\ConnectionResolver;
@ -47,7 +48,7 @@ class ContainerStub
$container->shouldReceive('get')->with(PoolFactory::class)->andReturn($factory);
$resolver = new ConnectionResolver($container);
$container->shouldReceive('get')->with(ConnectionResolver::class)->andReturn($resolver);
$container->shouldReceive('get')->with(ConnectionResolverInterface::class)->andReturn($resolver);
$config = new Config([
StdoutLoggerInterface::class => [

View File

@ -19,7 +19,6 @@ use Hyperf\Database\Connectors\ConnectionFactory;
use Hyperf\Database\Connectors\MySqlConnector;
use Hyperf\Database\Model\Register;
use Hyperf\Database\Schema\Builder;
use Hyperf\DbConnection\ConnectionResolver as DBConnectionResolver;
use Hyperf\DbConnection\Model\Model;
use Hyperf\Server\Entry\EventDispatcher;
use Hyperf\Translation\ArrayLoader;
@ -60,7 +59,7 @@ class ValidationExistsRuleTest extends TestCase
];
$connection = $connector->make($dbConfig);
$resolver = new ConnectionResolver(['default' => $connection]);
$container->shouldReceive('get')->with(DBConnectionResolver::class)->andReturn($resolver);
$container->shouldReceive('get')->with(ConnectionResolverInterface::class)->andReturn($resolver);
ApplicationContext::setContainer($container);
Register::setConnectionResolver($resolver);
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturn(new EventDispatcher());