AsyncTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 91
Duplicated Lines 21.98 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 20
loc 91
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testSuccessEvent() 0 19 1
A testErrorEvent() 0 25 2
A testTimeoutEvent() 0 19 1
A testAsync() 10 10 1
A testWait() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Yii;
11
12
use Exception;
13
14
use vxm\async\Async;
15
use vxm\async\event\Event;
16
use vxm\async\event\ErrorEvent;
17
use vxm\async\event\SuccessEvent;
18
19
/**
20
 * Class AsyncTest
21
 *
22
 * @author Vuong Minh <[email protected]>
23
 * @since 1.0.0
24
 */
25
class AsyncTest extends TestCase
26
{
27
28 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...
29
    {
30
        $this->stopwatch->start('test');
31
32
        Yii::$app->async->run(function () {
33
            sleep(3);
34
        });
35
36
        $this->assertLessThan(3000, $this->stopwatch->stop('test')->getDuration());
37
    }
38
39 View Code Duplication
    public function testWait()
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...
40
    {
41
        $this->stopwatch->start('test');
42
43
        Yii::$app->async->run(function () {
44
            sleep(3);
45
        })->wait();
46
47
        $this->assertGreaterThan(3000, $this->stopwatch->stop('test')->getDuration());
48
    }
49
50
    public function testSuccessEvent()
51
    {
52
        $result = 0;
53
54
        Yii::$app->async->on(Async::EVENT_SUCCESS, function (SuccessEvent $event) use (&$result) {
55
            $result += $event->output;
56
        });
57
58
        Yii::$app->async->run(function () {
59
60
            return 5;
61
        }, [
62
            'success' => function ($output) use (&$result) {
63
                $result += $output;
64
            }
65
        ])->wait();
66
67
        $this->assertEquals(10, $result);
68
    }
69
70
    public function testErrorEvent()
71
    {
72
        /** @var Exception[] $exceptions */
73
        $exceptions = [];
74
        Yii::$app->async->on(Async::EVENT_ERROR, function (ErrorEvent $event) use (&$exceptions) {
75
76
            $exceptions[] = $event->throwable;
77
        });
78
79
        Yii::$app->async->run(function () {
80
81
            throw new Exception('Error');
82
        }, [
83
            'error' => function (Exception $exception) use (&$exceptions) {
84
85
                $exceptions[] = $exception;
86
            }
87
        ])->wait();
88
89
        foreach ($exceptions as $exception) {
90
91
            $this->assertEquals(Exception::class, get_class($exception));
92
        }
93
94
    }
95
96
    public function testTimeoutEvent()
97
    {
98
        $counter = 0;
99
100
        Yii::$app->async->on(Async::EVENT_TIMEOUT, function (Event $event) use (&$counter) {
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...
101
            $counter++;
102
        });
103
104
        Yii::$app->async->run(function () {
105
106
            sleep(6);
107
        }, [
108
            'timeout' => function () use (&$counter) {
109
                $counter++;
110
            }
111
        ])->wait();
112
113
        $this->assertEquals(2, $counter);
114
    }
115
}
116