Passed
Pull Request — master (#17)
by Harry
02:26
created

testCallbackRunImplementsRunInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of graze/parallel-process.
4
 *
5
 * Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/parallel-process/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/parallel-process
12
 */
13
14
namespace Graze\ParallelProcess\Test\Unit;
15
16
use Graze\ParallelProcess\CallbackRun;
17
use Graze\ParallelProcess\Event\RunEvent;
18
use Graze\ParallelProcess\OutputterInterface;
19
use Graze\ParallelProcess\RunInterface;
20
use Graze\ParallelProcess\Test\TestCase;
21
use RuntimeException;
22
23
class CallbackRunTest extends TestCase
24
{
25
    public function testCallbackRunImplementsRunInterface()
26
    {
27
        $run = new CallbackRun(function () {
28
            return true;
29
        });
30
        $this->assertInstanceOf(RunInterface::class, $run);
31
        $this->assertInstanceOf(OutputterInterface::class, $run);
32
    }
33
34
    public function testInitialState()
35
    {
36
        $run = new CallbackRun(function () {
37
            return true;
38
        });
39
40
        $this->assertFalse($run->isRunning(), 'should not be running');
41
        $this->assertFalse($run->hasStarted(), 'should not have started');
42
        $this->assertFalse($run->isSuccessful(), 'should not be successful');
43
        $this->assertEquals(0, $run->getDuration());
44
        $this->assertEquals([], $run->getExceptions(), 'no exceptions should be returned');
45
        $this->assertEquals([], $run->getTags(), 'no tags should be returned');
46
        $this->assertEquals('', $run->getLastMessageType());
47
        $this->assertEquals('', $run->getLastMessage());
48
    }
49
50
    public function testRun()
51
    {
52
        $run = new CallbackRun(function () {
53
            return true;
54
        });
55
56
        $run->start();
57
58
        $this->assertFalse($run->poll());
59
        $this->assertTrue($run->isSuccessful());
60
        $this->assertTrue($run->hasStarted());
61
        $this->assertFalse($run->isRunning());
62
        $this->assertGreaterThan(0, $run->getDuration());
63
    }
64
65
    public function testOnStart()
66
    {
67
        $hit = false;
68
69
        $run = new CallbackRun(function () {
70
            return true;
71
        });
72
        $run->addListener(
73
            RunEvent::STARTED,
74
            function (RunEvent $event) use (&$run, &$hit) {
75
                $this->assertSame($event->getRun(), $run);
76
                $hit = true;
77
            }
78
        );
79
80
        $run->start();
81
        $this->assertFalse($run->poll());
82
    }
83
84
    public function testOnSuccess()
85
    {
86
        $hit = false;
87
88
        $run = new CallbackRun(function () {
89
            return true;
90
        });
91
        $run->addListener(
92
            RunEvent::COMPLETED,
93
            function (RunEvent $event) use (&$run, &$hit) {
94
                $this->assertSame($event->getRun(), $run);
95
                $hit = true;
96
            }
97
        );
98
99
        $run->start();
100
        $this->assertFalse($run->poll());
101
    }
102
103
    public function testOnFailure()
104
    {
105
        $hit = false;
106
107
        $exception = new RuntimeException('some text');
108
        $run = new CallbackRun(function () use ($exception) {
109
            throw $exception;
110
        });
111
        $run->addListener(
112
            RunEvent::FAILED,
113
            function (RunEvent $event) use (&$run, &$hit) {
114
                $this->assertSame($event->getRun(), $run);
115
                $hit = true;
116
            }
117
        );
118
119
        $run->start();
120
        $this->assertFalse($run->poll());
121
        $this->assertEquals([$exception], $run->getExceptions());
122
    }
123
124
    public function testOnProgress()
125
    {
126
        $hit = false;
127
128
        $run = new CallbackRun(function () {
129
            return ['line 1', 'line 2'];
130
        });
131
        $run->addListener(
132
            RunEvent::UPDATED,
133
            function (RunEvent $event) use (&$run, &$hit) {
134
                $this->assertSame($event->getRun(), $run);
135
                $this->assertContains($run->getLastMessage(), ['line 1', 'line 2']);
136
                $this->assertNull($run->getProgress());
137
                $hit = true;
138
            }
139
        );
140
141
        $run->start();
142
        $this->assertFalse($run->poll());
143
    }
144
145
    public function testEventsProvideDurationAndLastMessage()
146
    {
147
        $run = new CallbackRun(function () {
148
            return "line 1\nline 2";
149
        });
150
        $run->addListener(
151
            RunEvent::UPDATED,
152
            function (RunEvent $event) use (&$run, &$hits) {
153
                $this->assertSame($event->getRun(), $run);
154
                $this->assertInternalType('float', $run->getDuration());
155
                $this->assertContains($run->getLastMessage(), ['line 1', 'line 2']);
156
                $hits++;
157
            }
158
        );
159
160
        $run->start();
161
        $this->assertFalse($run->poll());
162
    }
163
}
164