-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProcessTest.php
More file actions
87 lines (76 loc) · 2.46 KB
/
ProcessTest.php
File metadata and controls
87 lines (76 loc) · 2.46 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
87
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Process;
use Hyperf\Context\ApplicationContext;
use Hyperf\Process\Event\AfterProcessHandle;
use Hyperf\Process\Event\BeforeProcessHandle;
use HyperfTest\Process\Stub\FooProcess;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use ReflectionClass;
use Swoole\Server;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
/**
* @internal
* @coversNothing
*/
class ProcessTest extends TestCase
{
public static $dispatched = [];
protected function tearDown(): void
{
Mockery::close();
self::$dispatched = [];
}
#[Group('NonCoroutine')]
public function testEventWhenThrowExceptionInProcess()
{
$container = $this->getContainer();
$process = new FooProcess($container);
$process->bind($this->getServer());
$this->assertInstanceOf(BeforeProcessHandle::class, self::$dispatched[0]);
$this->assertInstanceOf(AfterProcessHandle::class, self::$dispatched[1]);
}
protected function getContainer()
{
$container = Mockery::mock(ContainerInterface::class);
ApplicationContext::setContainer($container);
$container->shouldReceive('has')->with(EventDispatcherInterface::class)->andReturn(true);
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturnUsing(function () {
$dispatcher = Mockery::mock(EventDispatcherInterface::class);
$dispatcher->shouldReceive('dispatch')->withAnyArgs()->andReturnUsing(function ($event) {
self::$dispatched[] = $event;
});
return $dispatcher;
});
return $container;
}
protected function getServer()
{
$server = Mockery::mock(Server::class);
$server->shouldReceive('addProcess')->withAnyArgs()->andReturnUsing(function ($process) {
$ref = new ReflectionClass($process);
$property = $ref->getProperty('callback');
$callback = $property->getValue($process);
$callback($process);
return 1;
});
return $server;
}
}