1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Aimeos\MW\Process; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class PcntlTest extends \PHPUnit\Framework\TestCase |
7
|
|
|
{ |
8
|
|
|
protected function setUp() |
9
|
|
|
{ |
10
|
|
|
if( function_exists( 'pcntl_fork' ) === false ) { |
11
|
|
|
$this->markTestSkipped( 'PCNTL extension not available' ); |
12
|
|
|
} |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
public function testIsAvailable() |
17
|
|
|
{ |
18
|
|
|
$object = new \Aimeos\MW\Process\Pcntl(); |
19
|
|
|
$this->assertTrue( $object->isAvailable() ); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
public function testRun() |
24
|
|
|
{ |
25
|
|
|
$object = new \Aimeos\MW\Process\Pcntl(); |
26
|
|
|
$fcn = function() { sleep( 1 ); }; |
27
|
|
|
|
28
|
|
|
$start = microtime( true ); |
29
|
|
|
$return = $object->start( $fcn, [] )->start( $fcn, [] )->wait(); |
30
|
|
|
$msec = ( microtime( true ) - $start ); |
31
|
|
|
|
32
|
|
|
$this->assertInstanceOf( '\Aimeos\MW\Process\Iface', $return ); |
33
|
|
|
$this->assertGreaterThan( 1, $msec ); |
34
|
|
|
$this->assertLessThan( 2, $msec ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
public function testRunException() |
39
|
|
|
{ |
40
|
|
|
$fcn = function() { throw new \Exception(); }; |
41
|
|
|
|
42
|
|
|
try |
43
|
|
|
{ |
44
|
|
|
stream_filter_register( "redirect", "\Aimeos\MW\Process\DiscardFilter" ); |
45
|
|
|
$filter = stream_filter_prepend( STDERR, "redirect", STREAM_FILTER_WRITE ); |
46
|
|
|
|
47
|
|
|
$object = new \Aimeos\MW\Process\Pcntl(); |
48
|
|
|
$object->start( $fcn, [], true )->wait(); |
49
|
|
|
|
50
|
|
|
stream_filter_remove( $filter ); |
51
|
|
|
} |
52
|
|
|
catch( \Aimeos\MW\Process\Exception $e ) |
53
|
|
|
{ |
54
|
|
|
stream_filter_remove( $filter ); |
|
|
|
|
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->fail( 'Expected exception "\Aimeos\MW\Process\Exception" not thrown' ); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
|
64
|
|
|
class DiscardFilter extends \php_user_filter |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
function filter( $in, $out, &$consumed, $closing ) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
while( $bucket = stream_bucket_make_writeable( $in ) ) |
69
|
|
|
{ |
70
|
|
|
$bucket->data = ''; |
71
|
|
|
$consumed += $bucket->datalen; |
72
|
|
|
stream_bucket_append( $out, $bucket ); |
73
|
|
|
} |
74
|
|
|
return PSFS_PASS_ON; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: