Completed
Push — master ( e48e3f...5ab19a )
by Vuong
01:30
created

AsyncTest::testAsync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @link https://github.com/vuongxuongminh/yii2-async
4
 * @copyright Copyright (c) 2019 Vuong Xuong Minh
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace vxm\test\unit\async;
9
10
use vxm\async\Event;
11
use Yii;
12
use Exception;
13
14
use vxm\async\Async;
15
use vxm\async\ErrorEvent;
16
use vxm\async\SuccessEvent;
17
18
/**
19
 * Class AsyncTest
20
 *
21
 * @author Vuong Minh <[email protected]>
22
 * @since 1.0.0
23
 */
24
class AsyncTest extends TestCase
25
{
26
27 View Code Duplication
    public function testAsync()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29
        $time = time();
30
31
        Yii::$app->async(function () {
32
            sleep(10);
33
        });
34
35
        $this->assertTrue((time() - $time) < 10);
36
    }
37
38 View Code Duplication
    public function testAwait()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        $time = time();
41
42
        Yii::$app->async(function () {
43
            sleep(10);
44
        })->await();
45
46
        $this->assertTrue((time() - $time) >= 10);
47
    }
48
49
    public function testSuccessEvent()
50
    {
51
        Yii::$app->async->on(Async::EVENT_SUCCESS, function (SuccessEvent $event) {
52
53
            $this->assertEquals(123, $event->output);
54
        });
55
56
        Yii::$app->async(function () {
57
58
            return 123;
59
        }, [
60
            'success' => function ($result) {
61
62
                $this->assertEquals(123, $result);
63
            }
64
        ]);
65
    }
66
67
    public function testErrorEvent()
68
    {
69
        Yii::$app->async->on(Async::EVENT_ERROR, function (ErrorEvent $event) {
70
71
            $this->assertEquals(Exception::class, get_class($event->throwable));
72
        });
73
74
        Yii::$app->async(function () {
75
76
            throw new Exception('Error');
77
        }, [
78
            'error' => function (Exception $exception) {
79
80
                $this->assertEquals(Exception::class, get_class($exception));
81
            }
82
        ]);
83
    }
84
85
    public function testTimeoutEvent()
86
    {
87
        Yii::$app->async->on(Async::EVENT_ERROR, function (Event $event) {
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
89
            echo 'global passed';
90
        });
91
92
        Yii::$app->async(function () {
93
94
            sleep(30);
95
        }, [
96
            'timeout' => function () {
97
98
                echo 'custom passed';
99
            }
100
        ])->await();
101
    }
102
}
103