1--TEST--
2Check cli_process_title support in Windows
3--SKIPIF--
4<?php
5if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')
6  die("skip this test is for Windows platforms only");
7if (shell_exec('PowerShell -Help') === NULL)
8  die("skip this test requires powershell.exe");
9?>
10--FILE--
11<?php
12
13// On Windows 8 and Server 2012, this test does not work the same way. When the PowerShell
14// command "get-process" is executed using shell_exec, it overwrites the ConsoleTitle with
15// "Windows PowerShell" and this title ONLY clears away when the php.exe process exits
16// i.e. the test finishes.
17// On older versions like Windows 7 though, running the command appends
18// "Windows PowerShell" to the ConsoleTitle temporarily and the title reverts
19// back to the original once shell_exec is done.
20// Hence on Windows 8, we don't verify that the title is actually set by
21// cli_set_process_title(). We're only making the API calls to ensure there are
22// no warnings/errors.
23$is_windows8_or_above = PHP_WINDOWS_VERSION_MAJOR >= 10 || PHP_WINDOWS_VERSION_MAJOR >= 6 && PHP_WINDOWS_VERSION_MINOR >= 2;
24
25echo "*** Testing setting the process title ***\n";
26
27$original_title = uniqid("title", true);
28$pid = getmypid();
29
30if (cli_set_process_title($original_title) === true)
31  echo "Successfully set title\n";
32
33if ($is_windows8_or_above)
34{
35  $loaded_title = $original_title;
36}
37else
38{
39  $loaded_title = shell_exec("PowerShell -NoProfile \"get-process cmd*,powershell* | Select-Object mainWindowTitle | ft -hide\"");
40
41  if ($loaded_title === null)
42  {
43    echo "Reading title using get-process failed\n";
44    die();
45  }
46
47  // Kind of convoluted. So when the test is run on Windows 7 or older, the console where
48  // the run-tests.php is executed forks a php.exe, which forks a cmd.exe, which then forks
49  // a final php.exe to run the actual test. But the console title is set for the original console.
50  // I couldn't figure out a good way to navigate this, so we're "grep'ing" all possible
51  // console windows for our very unique title. It should occur exactly once in the grep
52  // output.
53  if (substr_count($loaded_title, $original_title, 0) == 1)
54    $loaded_title = $original_title;
55}
56
57if ($loaded_title == $original_title)
58  echo "Successfully verified title using get-process\n";
59else
60  echo "Actually loaded from get-process: $loaded_title\n";
61
62$read_title = cli_get_process_title();
63if (substr_count($read_title, $original_title, 0) == 1)
64  echo "Successfully verified title using get\n";
65else
66  echo "Actually loaded from get: $read_title\n";
67
68?>
69--EXPECTF--
70*** Testing setting the process title ***
71Successfully set title
72Successfully verified title using get-process
73Successfully verified title using get
74