1--TEST--
2proc_nice() basic behaviour
3--SKIPIF--
4<?php
5/* No function_exists() check, proc_nice() is always available on Windows */
6
7if (!defined('PHP_WINDOWS_VERSION_MAJOR')) {
8    die('skip: Only for Windows');
9}
10
11if (getenv('SKIP_SLOW_TESTS')) {
12    die('skip: Slow test');
13}
14
15if (!shell_exec("where wmic 2>nul")) {
16    die('skip wmic not available');
17}
18?>
19--FILE--
20<?php
21function get_priority_from_wmic() {
22    static $bin, $pid;
23
24    if (!$bin) {
25        $t 	= explode('\\', PHP_BINARY);
26
27        $bin	= end($t);
28        $pid	= getmypid();
29    }
30
31    $t = '';
32    $p = popen('wmic process where name="' . $bin . '"', 'r');
33
34    if (!$p) {
35        return false;
36    }
37
38    while(!feof($p)) {
39        $t .= fread($p, 1024);
40    }
41
42    pclose($p);
43
44    $t = explode(PHP_EOL, $t);
45
46    $f = false;
47    $m = [
48        strpos($t[0], ' ProcessId' ),
49        strpos($t[0], ' Priority ')
50        ];
51
52    foreach ($t as $n => $l) {
53        if (!$n || empty($l)) {
54            continue;
55        }
56
57        $d = [];
58
59        foreach ($m as $c) {
60            $d[] = (int) substr($l, $c + 1, strpos($l, ' ', $c + 2) - ($c + 1));
61        }
62
63        if ($d[0] === $pid) {
64            return $d[1];
65        }
66    }
67
68    return false;
69}
70
71$p = [
72    /* '<verbose name>' => ['<wmic value>', '<proc_nice value>'] */
73
74    'Idle' 		=> [4, 10],
75    'Below normal'	=> [6, 5],
76    'Normal'	=> [8, 0],
77    'Above normal'	=> [10, -5],
78    'High priority'	=> [13, -10]
79    ];
80
81foreach ($p as $test => $data) {
82    printf('Testing \'%s\' (%d): ', $test, $data[1]);
83
84    proc_nice($data[1]);
85
86    print (($wp = get_priority_from_wmic()) === $data[0] ? 'Passed' : 'Failed (' . $wp . ')') . PHP_EOL;
87}
88?>
89--EXPECT--
90Testing 'Idle' (10): Passed
91Testing 'Below normal' (5): Passed
92Testing 'Normal' (0): Passed
93Testing 'Above normal' (-5): Passed
94Testing 'High priority' (-10): Passed
95