Passed
Push — master ( b62909...01ab92 )
by Aimeos
02:37
created

PcntlTest::testRun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 7
c 3
b 0
f 0
dl 0
loc 11
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
		$this->assertEquals( 0, $this->access( 'exec' )->invokeArgs( new \Aimeos\Base\Process\Pcntl(), [$fcn, []] ) );
27
	}
28
29
30
	public function testExecException()
31
	{
32
		$fcn = function() { throw new \Exception(); };
33
34
		stream_filter_register( "redirect", "\Aimeos\Base\Process\DiscardFilter" );
35
		$filter = stream_filter_prepend( STDERR, "redirect", STREAM_FILTER_WRITE );
36
37
		$this->assertEquals( 1, $this->access( 'exec' )->invokeArgs( new \Aimeos\Base\Process\Pcntl(), [$fcn, []] ) );
38
39
		stream_filter_remove( $filter );
40
	}
41
42
43
	public function testIsAvailable()
44
	{
45
		$object = new \Aimeos\Base\Process\Pcntl();
46
		$this->assertTrue( $object->isAvailable() );
47
	}
48
49
50
	public function testRun()
51
	{
52
		$object = new \Aimeos\Base\Process\Pcntl( 1 );
53
		$fcn = function() { sleep( 1 ); };
54
55
		$start = microtime( true );
56
		$return = $object->start( $fcn, [\TestHelper::getConfig()] )->start( $fcn, [] )->wait();
57
		$msec = ( microtime( true ) - $start );
58
59
		$this->assertInstanceOf( \Aimeos\Base\Process\Iface::class, $return );
60
		$this->assertGreaterThan( 1, $msec );
61
	}
62
63
64
	protected function access( $name )
65
	{
66
		$class = new \ReflectionClass( \Aimeos\Base\Process\Pcntl::class );
67
		$method = $class->getMethod( $name );
68
		$method->setAccessible( true );
69
70
		return $method;
71
	}
72
}
73
74
75
76
class DiscardFilter extends \php_user_filter
77
{
78
	public function filter( $in, $out, &$consumed, $closing ) : int
79
	{
80
		while( $bucket = stream_bucket_make_writeable( $in ) )
81
		{
82
			$bucket->data = '';
83
			$consumed += $bucket->datalen;
84
			stream_bucket_append( $out, $bucket );
85
		}
86
		return PSFS_PASS_ON;
87
	}
88
}
89