CallbackRunTest::testRun()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
rs 10
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\PriorityChangedEvent;
18
use Graze\ParallelProcess\Event\RunEvent;
19
use Graze\ParallelProcess\OutputterInterface;
20
use Graze\ParallelProcess\RunInterface;
21
use Graze\ParallelProcess\Test\TestCase;
22
use RuntimeException;
23
24
class CallbackRunTest extends TestCase
25
{
26
    public function testCallbackRunImplementsRunInterface()
27
    {
28
        $run = new CallbackRun(function () {
29
            return true;
30
        });
31
        $this->assertInstanceOf(RunInterface::class, $run);
32
        $this->assertInstanceOf(OutputterInterface::class, $run);
33
    }
34
35
    public function testInitialState()
36
    {
37
        $run = new CallbackRun(function () {
38
            return true;
39
        });
40
41
        $this->assertFalse($run->isRunning(), 'should not be running');
42
        $this->assertFalse($run->hasStarted(), 'should not have started');
43
        $this->assertFalse($run->isSuccessful(), 'should not be successful');
44
        $this->assertEquals(0, $run->getDuration());
45
        $this->assertEquals([], $run->getExceptions(), 'no exceptions should be returned');
46
        $this->assertEquals([], $run->getTags(), 'no tags should be returned');
47
        $this->assertEquals('', $run->getLastMessageType());
48
        $this->assertEquals('', $run->getLastMessage());
49
    }
50
51
    public function testRun()
52
    {
53
        $run = new CallbackRun(function () {
54
            return true;
55
        });
56
57
        $run->start();
58
59
        $this->assertFalse($run->poll());
60
        $this->assertTrue($run->isSuccessful());
61
        $this->assertTrue($run->hasStarted());
62
        $this->assertFalse($run->isRunning());
63
        $this->assertGreaterThan(0, $run->getDuration());
64
    }
65
66
    public function testOnStart()
67
    {
68
        $hit = false;
69
70
        $run = new CallbackRun(function () {
71
            return true;
72
        });
73
        $run->addListener(
74
            RunEvent::STARTED,
75
            function (RunEvent $event) use (&$run, &$hit) {
76
                $this->assertSame($event->getRun(), $run);
77
                $hit = true;
78
            }
79
        );
80
81
        $run->start();
82
        $this->assertFalse($run->poll());
83
    }
84
85
    public function testOnSuccess()
86
    {
87
        $hit = false;
88
89
        $run = new CallbackRun(function () {
90
            return true;
91
        });
92
        $run->addListener(
93
            RunEvent::COMPLETED,
94
            function (RunEvent $event) use (&$run, &$hit) {
95
                $this->assertSame($event->getRun(), $run);
96
                $hit = true;
97
            }
98
        );
99
100
        $run->start();
101
        $this->assertFalse($run->poll());
102
    }
103
104
    public function testOnFailure()
105
    {
106
        $hit = false;
107
108
        $exception = new RuntimeException('some text');
109
        $run = new CallbackRun(function () use ($exception) {
110
            throw $exception;
111
        });
112
        $run->addListener(
113
            RunEvent::FAILED,
114
            function (RunEvent $event) use (&$run, &$hit) {
115
                $this->assertSame($event->getRun(), $run);
116
                $hit = true;
117
            }
118
        );
119
120
        $run->start();
121
        $this->assertFalse($run->poll());
122
        $this->assertEquals([$exception], $run->getExceptions());
123
    }
124
125
    /**
126
     * @requires PHP 7.0
127
     */
128
    public function testOnThrowableFailure()
129
    {
130
        $hit = false;
131
132
        $exception = new \TypeError();
133
        $run = new CallbackRun(function () use ($exception) {
134
            throw $exception;
135
        });
136
        $run->addListener(
137
            RunEvent::FAILED,
138
            function (RunEvent $event) use (&$run, &$hit) {
139
                $this->assertSame($event->getRun(), $run);
140
                $hit = true;
141
            }
142
        );
143
144
        $run->start();
145
        $this->assertFalse($run->poll());
146
        $this->assertEquals([$exception], $run->getExceptions());
147
    }
148
149
    public function testOnProgress()
150
    {
151
        $hit = false;
152
153
        $run = new CallbackRun(function () {
154
            return ['line 1', 'line 2'];
155
        });
156
        $run->addListener(
157
            RunEvent::UPDATED,
158
            function (RunEvent $event) use (&$run, &$hit) {
159
                $this->assertSame($event->getRun(), $run);
160
                $this->assertContains($run->getLastMessage(), ['line 1', 'line 2']);
161
                $this->assertNull($run->getProgress());
162
                $hit = true;
163
            }
164
        );
165
166
        $run->start();
167
        $this->assertFalse($run->poll());
168
    }
169
170
    public function testEventsProvideDurationAndLastMessage()
171
    {
172
        $run = new CallbackRun(function () {
173
            return "line 1\nline 2";
174
        });
175
        $run->addListener(
176
            RunEvent::UPDATED,
177
            function (RunEvent $event) use (&$run, &$hits) {
178
                $this->assertSame($event->getRun(), $run);
179
                $this->assertInternalType('float', $run->getDuration());
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertInternalType() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3369 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

179
                /** @scrutinizer ignore-deprecated */ $this->assertInternalType('float', $run->getDuration());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
180
                $this->assertContains($run->getLastMessage(), ['line 1', 'line 2']);
181
                $hits++;
182
            }
183
        );
184
185
        $run->start();
186
        $this->assertFalse($run->poll());
187
    }
188
189
    public function testPriorityAccessors()
190
    {
191
        $run = new CallbackRun(
192
            function () {
193
                return true;
194
            },
195
            ['tag'],
196
            2.1
197
        );
198
199
        $hit = false;
200
        $run->addListener(
201
            PriorityChangedEvent::CHANGED,
202
            function (PriorityChangedEvent $event) use (&$hit, $run) {
203
                $hit = true;
204
                $this->assertSame($run, $event->getItem());
205
            }
206
        );
207
        $this->assertEquals(2.1, $run->getPriority());
208
        $this->assertFalse($hit);
209
        $this->assertSame($run, $run->setPriority(2.2));
210
        $this->assertTrue($hit);
211
        $this->assertEquals(2.2, $run->getPriority());
212
213
        $run->start();
214
215
        $hit = false;
216
        $this->assertFalse($hit);
217
        // don't trigger event if we have already started
218
        $this->assertSame($run, $run->setPriority(2.3));
219
        $this->assertFalse($hit);
220
        $this->assertEquals(2.3, $run->getPriority());
221
    }
222
}
223