|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tleckie\Async\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Opis\Closure\SerializableClosure; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Tleckie\Async\Child; |
|
9
|
|
|
use Tleckie\Async\Encoder; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ChildTest |
|
13
|
|
|
* |
|
14
|
|
|
* @package Tleckie\Async\Tests |
|
15
|
|
|
* @author Teodoro Leckie Westberg <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class ChildTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var Child */ |
|
20
|
|
|
private Child $child; |
|
21
|
|
|
|
|
22
|
|
|
/** @var Encoder */ |
|
23
|
|
|
private Encoder $encoder; |
|
24
|
|
|
|
|
25
|
|
|
public function setUp(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$this->encoder = new Encoder(); |
|
28
|
|
|
$this->child = new Child( |
|
29
|
|
|
$this->encoder |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @test |
|
35
|
|
|
*/ |
|
36
|
|
|
public function handle(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$closure = function () { |
|
39
|
|
|
return 33; |
|
40
|
|
|
}; |
|
41
|
|
|
|
|
42
|
|
|
$encode = $this->encoder->encode(new SerializableClosure($closure)); |
|
43
|
|
|
|
|
44
|
|
|
$output = $this->encoder->decode( |
|
45
|
|
|
$this->child->handle($encode)->output() |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
static::assertEquals(33, $output); |
|
49
|
|
|
static::assertFalse($this->child->hasError()); |
|
50
|
|
|
static::assertNull($this->child->exception()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @test |
|
55
|
|
|
*/ |
|
56
|
|
|
public function handleException(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$closure = function () { |
|
59
|
|
|
throw new Exception('Test message'); |
|
60
|
|
|
}; |
|
61
|
|
|
|
|
62
|
|
|
$encode = $this->encoder->encode(new SerializableClosure($closure)); |
|
63
|
|
|
|
|
64
|
|
|
$output = $this->encoder->decode( |
|
65
|
|
|
$this->child->handle($encode)->exception() |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
static::assertEquals('Test message', $output->getMessage()); |
|
69
|
|
|
static::assertTrue($this->child->hasError()); |
|
70
|
|
|
static::assertNull($this->child->output()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @test |
|
75
|
|
|
*/ |
|
76
|
|
|
public function write(): void |
|
77
|
|
|
{ |
|
78
|
|
|
$closure = function () { |
|
79
|
|
|
return 'Same return value'; |
|
80
|
|
|
}; |
|
81
|
|
|
|
|
82
|
|
|
$encode = $this->encoder->encode(new SerializableClosure($closure)); |
|
83
|
|
|
|
|
84
|
|
|
$this->child->handle($encode); |
|
85
|
|
|
static::assertInstanceOf(Child::class, $this->child->write()); |
|
86
|
|
|
} |
|
87
|
|
|
} |