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
24$is_windows8_or_above = false;
25$ps_output = shell_exec("PowerShell -NoProfile \"(Get-Host).UI.RawUI.WindowTitle\"");
26if ($ps_output === null)
27{
28  echo "Get-Host failed\n";
29  die();
30}
31
32$ps_output = trim($ps_output);
33$end_title_windows8 = ": Windows PowerShell";
34if (($ps_output == "Windows PowerShell") || (strlen($ps_output) > strlen($end_title_windows8) && substr($ps_output,-strlen($end_title_windows8)) === $end_title_windows8) || PHP_WINDOWS_VERSION_MAJOR >= 10)
35  $is_windows8_or_above = true;
36
37echo "*** Testing setting the process title ***\n";
38
39$original_title = uniqid("title", true);
40$pid = getmypid();
41
42if (cli_set_process_title($original_title) === true)
43  echo "Successfully set title\n";
44
45if ($is_windows8_or_above)
46{
47  $loaded_title = $original_title;
48}
49else
50{
51  $loaded_title = shell_exec("PowerShell -NoProfile \"get-process cmd*,powershell* | Select-Object mainWindowTitle | ft -hide\"");
52
53  if ($loaded_title === null)
54  {
55    echo "Reading title using get-process failed\n";
56    die();
57  }
58
59  // Kind of convoluted. So when the test is run on Windows 7 or older, the console where
60  // the run-tests.php is executed forks a php.exe, which forks a cmd.exe, which then forks
61  // a final php.exe to run the actual test. But the console title is set for the original console.
62  // I couldn't figure out a good way to navigate this, so we're "grep'ing" all possible
63  // console windows for our very unique title. It should occur exactly once in the grep
64  // output.
65  if (substr_count($loaded_title, $original_title, 0) == 1)
66    $loaded_title = $original_title;
67}
68
69if ($loaded_title == $original_title)
70  echo "Successfully verified title using get-process\n";
71else
72  echo "Actually loaded from get-process: $loaded_title\n";
73
74$read_title = cli_get_process_title();
75if (substr_count($read_title, $original_title, 0) == 1)
76  echo "Successfully verified title using get\n";
77else
78  echo "Actually loaded from get: $read_title\n";
79
80?>
81--EXPECTF--
82*** Testing setting the process title ***
83Successfully set title
84Successfully verified title using get-process
85Successfully verified title using get
86