Passed
Push — master ( abc189...ba009d )
by Aimeos
07:42
created

PcntlTest::testClone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Aimeos\Base\Process;
4
5
6
class PcntlTest extends \PHPUnit\Framework\TestCase
7
{
8
	protected function setUp() : void
9
	{
10
		if( function_exists( 'pcntl_fork' ) === false ) {
11
			$this->markTestSkipped( 'PCNTL extension not available' );
12
		}
13
	}
14
15
16
	public function testClone()
17
	{
18
		$object = new \Aimeos\Base\Process\Pcntl();
19
		$this->assertNotSame( $object, clone $object );
20
	}
21
22
23
	public function testExec()
24
	{
25
		$fcn = function() {};
26
		$result = $this->access( 'exec' )->invokeArgs( new \Aimeos\Base\Process\Pcntl(), [$fcn, []] );
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
27
	}
28
29
30
	public function testExecException()
31
	{
32
		$fcn = function() {
33
			throw new \RuntimeException();
34
		};
35
36
		$result = $this->access( 'exec' )->invokeArgs( new \Aimeos\Base\Process\Pcntl(), [$fcn, []] );
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
37
	}
38
39
40
	public function testIsAvailable()
41
	{
42
		$object = new \Aimeos\Base\Process\Pcntl();
43
		$this->assertTrue( $object->isAvailable() );
44
	}
45
46
47
	public function testRun()
48
	{
49
		$object = new \Aimeos\Base\Process\Pcntl( 1 );
50
		$fcn = function() { sleep( 1 ); };
51
52
		$start = microtime( true );
53
		$return = $object->start( $fcn, [\TestHelper::getConfig()] )->start( $fcn, [] )->wait();
54
		$msec = ( microtime( true ) - $start );
55
56
		$this->assertInstanceOf( \Aimeos\Base\Process\Iface::class, $return );
57
		$this->assertGreaterThan( 1, $msec );
58
	}
59
60
61
	public function testRunError()
62
	{
63
		$fcn = function() { throw new \Exception(); };
64
65
		stream_filter_register( "redirect", "\Aimeos\Base\Process\DiscardFilter" );
66
		$filter = stream_filter_prepend( STDERR, "redirect", STREAM_FILTER_WRITE );
67
68
		$object = new \Aimeos\Base\Process\Pcntl();
69
		$result = $object->start( $fcn, [], true )->wait();
70
71
		stream_filter_remove( $filter );
72
73
		$this->assertInstanceOf( \Aimeos\Base\Process\Iface::class, $result );
74
	}
75
76
77
	protected function access( $name )
78
	{
79
		$class = new \ReflectionClass( \Aimeos\Base\Process\Pcntl::class );
80
		$method = $class->getMethod( $name );
81
		$method->setAccessible( true );
82
83
		return $method;
84
	}
85
}
86
87
88
89
class DiscardFilter extends \php_user_filter
90
{
91
	public function filter( $in, $out, &$consumed, $closing ) : int
92
	{
93
		while( $bucket = stream_bucket_make_writeable( $in ) )
94
		{
95
			$bucket->data = '';
96
			$consumed += $bucket->datalen;
97
			stream_bucket_append( $out, $bucket );
98
		}
99
		return PSFS_PASS_ON;
100
	}
101
}
102