Manager

Testing Is Documentation

tests/Manager/ManagerTest.phpManager - 图1

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

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

Uses

  1. <?php
  2. use Leevel\Di\Container;
  3. use Leevel\Di\IContainer;
  4. use Leevel\Manager\Manager;
  5. use Leevel\Option\Option;

基础使用方法

fixture 定义

Tests\Manager\Test1

  1. namespace Tests\Manager;
  2. class Test1 extends Manager
  3. {
  4. protected function getOptionNamespace(): string
  5. {
  6. return 'test1';
  7. }
  8. protected function makeConnectFoo(): Foo
  9. {
  10. return new Foo($this->normalizeConnectOption('foo'));
  11. }
  12. protected function makeConnectBar($options = []): Bar
  13. {
  14. return new Bar($this->normalizeConnectOption('bar'));
  15. }
  16. protected function getConnectOption(string $connect): array
  17. {
  18. return $this->filterNullOfOption(parent::getConnectOption($connect));
  19. }
  20. }

Tests\Manager\IConnect

  1. namespace Tests\Manager;
  2. interface IConnect
  3. {
  4. public function option(): array;
  5. public function foo(): string;
  6. public function bar(string $arg1): string;
  7. }

Tests\Manager\Foo

  1. namespace Tests\Manager;
  2. class Foo implements IConnect
  3. {
  4. protected $option = [];
  5. public function __construct(array $option)
  6. {
  7. $this->option = $option;
  8. }
  9. public function option(): array
  10. {
  11. return $this->option;
  12. }
  13. public function foo(): string
  14. {
  15. return 'hello foo';
  16. }
  17. public function bar(string $arg1): string
  18. {
  19. return 'hello foo '.$arg1;
  20. }
  21. }

Tests\Manager\Bar

  1. namespace Tests\Manager;
  2. class Bar implements IConnect
  3. {
  4. protected $option = [];
  5. public function __construct(array $option)
  6. {
  7. $this->option = $option;
  8. }
  9. public function option(): array
  10. {
  11. return $this->option;
  12. }
  13. public function foo(): string
  14. {
  15. return 'hello bar';
  16. }
  17. public function bar(string $arg1): string
  18. {
  19. return 'hello bar '.$arg1;
  20. }
  21. }

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

  1. public function testBaseUse(): void
  2. {
  3. $manager = $this->createManager();
  4. $foo = $manager->connect('foo');
  5. $bar = $manager->connect('bar');
  6. $this->assertSame([
  7. 'driver' => 'foo',
  8. 'option1' => 'world',
  9. ], $foo->option());
  10. $this->assertSame('hello foo', $foo->foo());
  11. $this->assertSame('hello foo bar', $foo->bar('bar'));
  12. $this->assertSame('hello foo 1', $foo->bar('1'));
  13. $this->assertSame('hello foo 2', $foo->bar('2'));
  14. $this->assertSame([
  15. 'driver' => 'bar',
  16. 'option1' => 'foo',
  17. 'option2' => 'bar',
  18. ], $bar->option());
  19. $this->assertSame('hello bar', $bar->foo());
  20. $this->assertSame('hello bar bar', $bar->bar('bar'));
  21. $this->assertSame('hello bar 1', $bar->bar('1'));
  22. $this->assertSame('hello bar 2', $bar->bar('2'));
  23. }

extend 扩展自定义连接

fixture 定义

Tests\Manager\FooExtend

  1. namespace Tests\Manager;
  2. class FooExtend implements IConnect
  3. {
  4. protected $option = [];
  5. public function __construct(array $option)
  6. {
  7. $this->option = $option;
  8. }
  9. public function option(): array
  10. {
  11. return $this->option;
  12. }
  13. public function foo(): string
  14. {
  15. return 'hello extend foo';
  16. }
  17. public function bar(string $arg1): string
  18. {
  19. return 'hello extend foo '.$arg1;
  20. }
  21. }
  1. public function testExtend(): void
  2. {
  3. $manager = $this->createManager();
  4. $foo = $manager->connect('foo');
  5. $this->assertSame('hello foo', $foo->foo());
  6. $this->assertSame('hello foo bar', $foo->bar('bar'));
  7. $manager->extend('foo', function (Manager $manager): FooExtend {
  8. return new FooExtend($manager->normalizeConnectOption('foo'));
  9. });
  10. $manager->disconnect('foo');
  11. $foo = $manager->connect('foo');
  12. $this->assertSame('hello extend foo', $foo->foo());
  13. $this->assertSame('hello extend foo bar', $foo->bar('bar'));
  14. }

TIP

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

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

  1. public function testConnectCache(): void
  2. {
  3. $manager = $this->createManager();
  4. $foo = $manager->connect('foo');
  5. $bar = $manager->connect('bar');
  6. $foo2 = $manager->connect('foo');
  7. $bar2 = $manager->connect('bar');
  8. $this->assertSame($foo, $foo2);
  9. $this->assertSame($bar, $bar2);
  10. }

reconnect 重新连接

  1. public function testReconnect(): void
  2. {
  3. $manager = $this->createManager();
  4. $foo = $manager->connect('foo');
  5. $bar = $manager->connect('bar');
  6. $foo2 = $manager->reconnect('foo');
  7. $bar2 = $manager->reconnect('bar');
  8. $this->assertFalse($foo === $foo2);
  9. $this->assertFalse($bar === $bar2);
  10. }

disconnect 删除连接

  1. public function testDisconnect(): void
  2. {
  3. $manager = $this->createManager();
  4. $foo = $manager->connect('foo');
  5. $bar = $manager->connect('bar');
  6. $manager->disconnect('foo');
  7. $manager->disconnect('bar');
  8. $foo2 = $manager->connect('foo');
  9. $bar2 = $manager->connect('bar');
  10. $this->assertFalse($foo === $foo2);
  11. $this->assertFalse($bar === $bar2);
  12. }

manager 默认连接调用

  1. public function testCallWithDefaultDriver(): void
  2. {
  3. $manager = $this->createManager();
  4. $this->assertSame('hello foo', $manager->foo());
  5. $this->assertSame('hello foo bar', $manager->bar('bar'));
  6. $this->assertSame('hello foo 1', $manager->bar('1'));
  7. $this->assertSame('hello foo 2', $manager->bar('2'));
  8. }

getConnects 取回所有连接

  1. public function testGetConnects(): void
  2. {
  3. $manager = $this->createManager();
  4. $this->assertCount(0, $manager->getConnects());
  5. $manager->connect('foo');
  6. $manager->connect('bar');
  7. $this->assertCount(2, $manager->getConnects());
  8. $manager->disconnect('foo');
  9. $this->assertCount(1, $manager->getConnects());
  10. $manager->disconnect('bar');
  11. $this->assertCount(0, $manager->getConnects());
  12. }

setDefaultConnect 设置默认驱动

  1. public function testSetDefaultDriver(): void
  2. {
  3. $manager = $this->createManager();
  4. $this->assertSame('hello foo', $manager->foo());
  5. $this->assertSame('hello foo bar', $manager->bar('bar'));
  6. $this->assertSame('hello foo 1', $manager->bar('1'));
  7. $this->assertSame('hello foo 2', $manager->bar('2'));
  8. $manager->disconnect();
  9. $manager->setDefaultConnect('bar');
  10. $this->assertSame('hello bar', $manager->foo());
  11. $this->assertSame('hello bar bar', $manager->bar('bar'));
  12. $this->assertSame('hello bar 1', $manager->bar('1'));
  13. $this->assertSame('hello bar 2', $manager->bar('2'));
  14. }