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