AsyncTest::failed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Tleckie\Async\Tests;
4
5
use Exception;
6
use PHPUnit\Framework\TestCase;
7
use Tleckie\Async\Async;
8
9
/**
10
 * Class AsyncTest
11
 *
12
 * @backupGlobals disabled
13
 * @package       Tleckie\Async\Tests
14
 * @author        Teodoro Leckie Westberg <[email protected]>
15
 */
16
class AsyncTest extends TestCase
17
{
18
    /** @var Async */
19
    private Async $async;
20
21
    public function setUp(): void
22
    {
23
        $this->async = new Async(null, null, 0);
24
    }
25
26
    /**
27
     * @test
28
     */
29
    public function add(): void
30
    {
31
        $this->async->add(static function () {
32
            usleep(8000);
33
34
            return 555;
35
        });
36
        $this->async->add(static function () {
37
            return 2;
38
        });
39
40
        $values = $this->async->wait();
41
        static::assertContainsEquals(555, $values);
42
        static::assertContainsEquals(2, $values);
43
    }
44
45
    /**
46
     * @test
47
     * @runTestsInSeparateProcesses
48
     */
49
    public function then(): void
50
    {
51
        $this->async->add(static function () {
52
            return 555;
53
        })->then(static function (int $value) {
54
            static::assertEquals(555, $value);
55
        });
56
57
        $this->async->wait();
58
    }
59
60
    /**
61
     * @test
62
     * @runTestsInSeparateProcesses
63
     */
64
    public function failed(): void
65
    {
66
        $this->async->add(static function () {
67
            throw new Exception('Test message');
68
        })->catch(function ($exception) {
69
            static::assertStringContainsString('Test message', $exception->getMessage());
70
        });
71
72
        $this->async->wait();
73
    }
74
}
75