Passed
Push — master ( ba009d...b62909 )
by Aimeos
02:13
created

PcntlTest::access()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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