Skip to content

Manager

Testing Is Documentation

tests/Support/ManagerTest.php

QueryPHP 为驱动类组件统一抽象了一个基础管理类 \Leevel\Manager\Manager,驱动类组件可以轻松接入。

系统一些关键服务,比如说日志、数据库和缓存等驱动类组件均接入了统一的抽象层。

Uses

php
<?php

use Leevel\Config\Config;
use Leevel\Di\Container;
use Leevel\Di\IContainer;
use Leevel\Kernel\Utils\Api;
use Leevel\Support\Manager;

基础使用方法

fixture 定义

Tests\Manager\Test1

php
namespace Tests\Support;

class Test1 extends Manager
{
    protected function getConfigNamespace(): string
    {
        return 'test1';
    }

    protected function makeConnectFoo(): Foo
    {
        return new Foo($this->normalizeConnectConfig('foo'));
    }

    protected function makeConnectBar($configs = []): Bar
    {
        return new Bar($this->normalizeConnectConfig('bar'));
    }

    protected function getConnectConfig(string $connect): array
    {
        return $this->filterNullOfConfig(parent::getConnectConfig($connect));
    }
}

Tests\Manager\IConnect

php
namespace Tests\Support;

interface IConnect
{
    public function config(): array;

    public function foo(): string;

    public function bar(string $arg1): string;
}

Tests\Manager\Foo

php
namespace Tests\Support;

class Foo implements IConnect
{
    protected $config = [];

    public function __construct(array $config)
    {
        $this->config = $config;
    }

    public function config(): array
    {
        return $this->config;
    }

    public function foo(): string
    {
        return 'hello foo';
    }

    public function bar(string $arg1): string
    {
        return 'hello foo '.$arg1;
    }
}

Tests\Manager\Bar

php
namespace Tests\Support;

class Bar implements IConnect
{
    protected $config = [];

    public function __construct(array $config)
    {
        $this->config = $config;
    }

    public function config(): array
    {
        return $this->config;
    }

    public function foo(): string
    {
        return 'hello bar';
    }

    public function bar(string $arg1): string
    {
        return 'hello bar '.$arg1;
    }
}

可以通过 connect 方法连接并返回连接对象,然后可以执行相应的操作。

php
public function testBaseUse(): void
{
    $manager = $this->createManager();

    $foo = $manager->connect('foo');
    $bar = $manager->connect('bar');

    self::assertSame([
        'driver' => 'foo',
        'config1' => 'world',
    ], $foo->config());

    self::assertSame('hello foo', $foo->foo());
    self::assertSame('hello foo bar', $foo->bar('bar'));
    self::assertSame('hello foo 1', $foo->bar('1'));
    self::assertSame('hello foo 2', $foo->bar('2'));

    self::assertSame([
        'driver' => 'bar',
        'config1' => 'foo',
        'config2' => 'bar',
    ], $bar->config());
    self::assertSame('hello bar', $bar->foo());
    self::assertSame('hello bar bar', $bar->bar('bar'));
    self::assertSame('hello bar 1', $bar->bar('1'));
    self::assertSame('hello bar 2', $bar->bar('2'));
}

connect 连接并返回连接对象支持每次都返回新的实例

php
public function test1(): void
{
    $manager = $this->createManager();

    $foo = $manager->connect('foo');
    $bar = $manager->connect('foo');

    self::assertSame($foo, $bar);

    $foo1 = $manager->connect('foo', true);
    $bar1 = $manager->connect('foo', true);

    self::assertFalse($foo1 === $bar1);
}

extend 扩展自定义连接

fixture 定义

Tests\Manager\FooExtend

php
namespace Tests\Support;

class FooExtend implements IConnect
{
    protected $config = [];

    public function __construct(array $config)
    {
        $this->config = $config;
    }

    public function config(): array
    {
        return $this->config;
    }

    public function foo(): string
    {
        return 'hello extend foo';
    }

    public function bar(string $arg1): string
    {
        return 'hello extend foo '.$arg1;
    }
}
php
public function testExtend(): void
{
    $manager = $this->createManager();

    $foo = $manager->connect('foo');
    self::assertSame('hello foo', $foo->foo());
    self::assertSame('hello foo bar', $foo->bar('bar'));

    $manager->extend('foo', static function (Manager $manager): FooExtend {
        return new FooExtend($manager->normalizeConnectConfig('foo'));
    });

    $manager->disconnect('foo');
    $foo = $manager->connect('foo');
    self::assertSame('hello extend foo', $foo->foo());
    self::assertSame('hello extend foo bar', $foo->bar('bar'));
}

TIP

如果驱动存在则会替换,否则新增驱动。

connect 连接并返回连接对象支持缓存

php
public function testConnectCache(): void
{
    $manager = $this->createManager();

    $foo = $manager->connect('foo');
    $bar = $manager->connect('bar');
    $foo2 = $manager->connect('foo');
    $bar2 = $manager->connect('bar');

    self::assertSame($foo, $foo2);
    self::assertSame($bar, $bar2);
}

reconnect 重新连接

php
public function testReconnect(): void
{
    $manager = $this->createManager();

    $foo = $manager->connect('foo');
    $bar = $manager->connect('bar');
    $foo2 = $manager->reconnect('foo');
    $bar2 = $manager->reconnect('bar');

    self::assertFalse($foo === $foo2);
    self::assertFalse($bar === $bar2);
}

disconnect 删除连接

php
public function testDisconnect(): void
{
    $manager = $this->createManager();

    $foo = $manager->connect('foo');
    $bar = $manager->connect('bar');

    $manager->disconnect('foo');
    $manager->disconnect('bar');

    $foo2 = $manager->connect('foo');
    $bar2 = $manager->connect('bar');

    self::assertFalse($foo === $foo2);
    self::assertFalse($bar === $bar2);
}

manager 默认连接调用

php
public function testCallWithDefaultDriver(): void
{
    $manager = $this->createManager();

    self::assertSame('hello foo', $manager->foo());
    self::assertSame('hello foo bar', $manager->bar('bar'));
    self::assertSame('hello foo 1', $manager->bar('1'));
    self::assertSame('hello foo 2', $manager->bar('2'));
}

getConnects 取回所有连接

php
public function testGetConnects(): void
{
    $manager = $this->createManager();
    self::assertCount(0, $manager->getConnects());

    $manager->connect('foo');
    $manager->connect('bar');
    self::assertCount(2, $manager->getConnects());

    $manager->disconnect('foo');
    self::assertCount(1, $manager->getConnects());

    $manager->disconnect('bar');
    self::assertCount(0, $manager->getConnects());
}

setDefaultConnect 设置默认驱动

php
public function testSetDefaultDriver(): void
{
    $manager = $this->createManager();

    self::assertSame('hello foo', $manager->foo());
    self::assertSame('hello foo bar', $manager->bar('bar'));
    self::assertSame('hello foo 1', $manager->bar('1'));
    self::assertSame('hello foo 2', $manager->bar('2'));

    $manager->disconnect();
    $manager->setDefaultConnect('bar');

    self::assertSame('hello bar', $manager->foo());
    self::assertSame('hello bar bar', $manager->bar('bar'));
    self::assertSame('hello bar 1', $manager->bar('1'));
    self::assertSame('hello bar 2', $manager->bar('2'));
}