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