Test Failed
Push — main ( 77af0a...f72bce )
by Teodoro
02:42
created

AsyncTest::failed()   A

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
 * @package Tleckie\Async\Tests
13
 * @author  Teodoro Leckie Westberg <[email protected]>
14
 */
15
class AsyncTest extends TestCase
16
{
17
    /** @var Async */
18
    private Async $async;
19
20
    public function setUp(): void
21
    {
22
        $this->async = new Async(null, null, 0);
23
    }
24
25
    /**
26
     * @test
27
     */
28
    public function add(): void
29
    {
30
        $this->async->add(static function () {
31
            usleep(8000);
32
33
            return 555;
34
        });
35
        $this->async->add(static function () {
36
            return 2;
37
        });
38
39
        $values = $this->async->wait();
40
        static::assertContainsEquals(555, $values);
41
        static::assertContainsEquals(2, $values);
42
    }
43
44
    /**
45
     * @test
46
     * @runTestsInSeparateProcesses
47
     */
48
    public function then(): void
49
    {
50
        $this->async->add(static function () {
51
            return 555;
52
        })->then(static function (int $value) {
53
            static::assertEquals(555, $value);
54
        });
55
56
        $this->async->wait();
57
    }
58
59
    /**
60
     * @test
61
     * @runTestsInSeparateProcesses
62
     */
63
    public function failed(): void
64
    {
65
        $this->async->add(static function () {
66
            throw new Exception('Test message');
67
        })->catch(function ($exception) {
68
            static::assertEquals('Test message', $exception->getMessage());
69
        });
70
71
        $this->async->wait();
72
    }
73
}
74