Repository navigation
Expand file tree
/
Copy pathConnectionTest.php
More file actions
86 lines (73 loc) · 3.45 KB
/
Copy pathConnectionTest.php
File metadata and controls
86 lines (73 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Pool;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Pool\Event\ReleaseConnection;
use Hyperf\Pool\Pool;
use Hyperf\Pool\PoolOption;
use HyperfTest\Pool\Stub\ActiveConnectionStub;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class ConnectionTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
}
public function testGetActiveConnectionAgain()
{
$container = Mockery::mock(ContainerInterface::class);
$logger = Mockery::mock(StdoutLoggerInterface::class);
$logger->shouldReceive('warning')->withAnyArgs()->once()->andReturnTrue();
$container->shouldReceive('has')->with(StdoutLoggerInterface::class)->once()->andReturnTrue();
$container->shouldReceive('get')->with(StdoutLoggerInterface::class)->once()->andReturn($logger);
$container->shouldReceive('has')->with(EventDispatcherInterface::class)->andReturnFalse();
$connection = new ActiveConnectionStub($container, Mockery::mock(Pool::class));
$this->assertEquals($connection, $connection->getConnection());
}
public function testReleaseConnectionEvent()
{
$assert = 0;
$container = Mockery::mock(ContainerInterface::class);
$container->shouldReceive('has')->with(StdoutLoggerInterface::class)->once()->andReturnFalse();
$container->shouldReceive('has')->with(EventDispatcherInterface::class)->andReturnTrue();
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturn($dispatcher = Mockery::mock(EventDispatcherInterface::class));
$dispatcher->shouldReceive('dispatch')->once()->with(ReleaseConnection::class)->andReturnUsing(function (ReleaseConnection $event) use (&$assert) {
$assert = $event->connection->getLastReleaseTime();
});
$connection = new ActiveConnectionStub($container, $pool = Mockery::mock(Pool::class));
$pool->shouldReceive('release')->withAnyArgs()->andReturnNull();
$pool->shouldReceive('getOption')->andReturn(new PoolOption(events: [ReleaseConnection::class]));
$connection->release();
$this->assertTrue($assert > 0);
}
public function testDontHaveEvents()
{
$container = Mockery::mock(ContainerInterface::class);
$container->shouldReceive('has')->with(StdoutLoggerInterface::class)->once()->andReturnFalse();
$container->shouldReceive('has')->with(EventDispatcherInterface::class)->andReturnTrue();
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturn($dispatcher = Mockery::mock(EventDispatcherInterface::class));
$dispatcher->shouldReceive('dispatch')->never()->with(ReleaseConnection::class)->andReturnNull();
$connection = new ActiveConnectionStub($container, $pool = Mockery::mock(Pool::class));
$pool->shouldReceive('release')->withAnyArgs()->andReturnNull();
$pool->shouldReceive('getOption')->andReturn(new PoolOption(events: []));
$connection->release();
$this->assertTrue(true);
}
}