ChildTest::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 15
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Tleckie\Async\Tests;
4
5
use Opis\Closure\SerializableClosure;
6
use PHPUnit\Framework\TestCase;
7
use Tleckie\Async\Child;
8
use Tleckie\Async\Encoder;
9
10
/**
11
 * Class ChildTest
12
 *
13
 * @backupGlobals disabled
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
     * @runTestsInSeparateProcesses
36
     */
37
    public function handle(): void
38
    {
39
        $closure = function () {
40
            return 33;
41
        };
42
43
        $encode = $this->encoder->encode(new SerializableClosure($closure));
44
45
        $output = $this->encoder->decode(
46
            $this->child->handle($encode)->output()
47
        );
48
49
        static::assertEquals(33, $output);
50
        static::assertFalse($this->child->hasError());
51
        static::assertNull($this->child->exception());
52
    }
53
54
    /**
55
     * @test
56
     */
57
    public function handleException(): void
58
    {
59
        $closure = function () {
60
            throw new \Exception('Test message');
61
        };
62
63
        $encode = $this->encoder->encode(new SerializableClosure($closure));
64
65
        $output = $this->encoder->decode(
66
            $this->child->handle($encode)->exception()
67
        );
68
69
        static::assertStringContainsString('Test message', $output->exception()->getMessage());
70
        static::assertTrue($this->child->hasError());
71
        static::assertNull($this->child->output());
72
    }
73
74
    /**
75
     * @test
76
     */
77
    public function write(): void
78
    {
79
        $closure = function () {
80
            return 'Same return value';
81
        };
82
83
        $encode = $this->encoder->encode(new SerializableClosure($closure));
84
85
        $this->child->handle($encode);
86
        static::assertInstanceOf(Child::class, $this->child->write());
87
    }
88
}
89