Repository navigation
Expand file tree
/
Copy pathOnTaskListenerTest.php
More file actions
95 lines (81 loc) · 2.96 KB
/
Copy pathOnTaskListenerTest.php
File metadata and controls
95 lines (81 loc) · 2.96 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
88
89
90
91
92
93
94
95
<?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\Task;
use Hyperf\Framework\Event\OnTask;
use Hyperf\Serializer\ExceptionNormalizer;
use Hyperf\Task\ChannelFactory;
use Hyperf\Task\Exception;
use Hyperf\Task\Finish;
use Hyperf\Task\Listener\OnTaskListener;
use Hyperf\Task\Task;
use Hyperf\Task\TaskExecutor;
use HyperfTest\Task\Stub\Foo;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use RuntimeException;
use Swoole\Server;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class OnTaskListenerTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
}
public function testProcess()
{
$container = $this->getContainer();
$listener = new OnTaskListener($container);
$id = uniqid();
$event = Mockery::mock(OnTask::class);
$event->task = new Server\Task();
$event->task->data = new Task([Foo::class, 'get'], [$id]);
$event->shouldReceive('setResult')->with(Mockery::any())->andReturnUsing(function ($result) use ($id, $event) {
$this->assertInstanceOf(Finish::class, $result);
$this->assertSame($id, $result->data);
return $event;
});
$listener->process($event);
}
public function testProcessException()
{
$container = $this->getContainer();
$listener = new OnTaskListener($container);
$id = uniqid();
$event = Mockery::mock(OnTask::class);
$event->task = new Server\Task();
$event->task->data = new Task([Foo::class, 'exception'], [$id]);
$event->shouldReceive('setResult')->with(Mockery::any())->andReturnUsing(function ($result) use ($event) {
$this->assertInstanceOf(Finish::class, $result);
$this->assertInstanceOf(Exception::class, $result->data);
$this->assertSame(RuntimeException::class, $result->data->class);
$this->assertSame('Foo::exception failed.', $result->data->attributes['message']);
$this->assertSame(0, $result->data->attributes['code']);
return $event;
});
$listener->process($event);
}
protected function getContainer()
{
$container = Mockery::mock(ContainerInterface::class);
$normalizer = new ExceptionNormalizer();
$container->shouldReceive('get')->with(ExceptionNormalizer::class)->andReturn($normalizer);
$container->shouldReceive('has')->with(Mockery::any())->andReturn(true);
$container->shouldReceive('get')->with(TaskExecutor::class)->andReturn(new TaskExecutor(new ChannelFactory(), $normalizer));
$container->shouldReceive('get')->with(Foo::class)->andReturn(new Foo());
return $container;
}
}