| 1 | <?php |
| 2 | /************************************************************* |
| 3 | # 076 License |
| 4 | |
| 5 | Copyright (c) テクニカル諏訪子 |
| 6 | |
| 7 | Permission is hereby granted to any person obtaining a copy of the software |
| 8 | Little Beast (the "Software") to use, modify, merge, copy, publish, distribute, |
| 9 | sublicense, and/or sell copies of the Software, subject to the following conditions: |
| 10 | |
| 11 | 1. **Origin Attribution**: |
| 12 | - You must not misrepresent the origin of the Software; you must not claim |
| 13 | you created the original Software. |
| 14 | - If the Software is used in a product, you must either: |
| 15 | a. Provide clear attribution in the product's documentation, user interface, |
| 16 | or other visible areas, **OR** |
| 17 | b. Pay the original developers a fee they specify in writing. |
| 18 | 2. **Usage Restriction**: |
| 19 | - The Software, or any derivative works, dependencies, or libraries |
| 20 | incorporating it, must not be used for censorship or to suppress freedom of |
| 21 | speech, expression, or creativity. Prohibited uses include, but are not |
| 22 | limited to: |
| 23 | - Censorship of so-called "hate speech", visuals, non-mainstream opinions, |
| 24 | ideas, or objective reality. |
| 25 | - Tools or systems designed to restrict access to information or |
| 26 | artistic works. |
| 27 | 3. **Notice Preservation**: |
| 28 | - This license and the above copyright notice must remain intact in all copies |
| 29 | of the source code. |
| 30 | |
| 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 32 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 33 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 34 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 35 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 36 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 37 | */ |
| 38 | namespace Std\Test\Helper; |
| 39 | |
| 40 | /** |
| 41 | * テスト用の偽物のサーバー(別のPHPプロセスで動かす) |
| 42 | * |
| 43 | * http-server.php(HTTP)とsmtp-server.php(SMTP)を127.0.0.1の空いているポートで起動する。 |
| 44 | * ネットワークに繋がっていなくてもCurl・Openprovider・Mailerをテスト出来る様に。 |
| 45 | * |
| 46 | * 例: |
| 47 | * $srv = MockServer::start('http-server.php'); |
| 48 | * $curl = new Curl($srv->url('/echo')); |
| 49 | * $srv->stop(); |
| 50 | */ |
| 51 | class MockServer { |
| 52 | public int $port; |
| 53 | /** @var resource */ |
| 54 | private $proc; |
| 55 | private array $pipes = []; |
| 56 | |
| 57 | private function __construct() {} |
| 58 | |
| 59 | /** |
| 60 | * サーバーを起動する |
| 61 | * |
| 62 | * @param string $script このディレクトリのスクリプト名 |
| 63 | * @param array $args スクリプトに渡す追加の引数 |
| 64 | * @return self|null 起動出来なかった場合はnull(proc_openが無効等) |
| 65 | */ |
| 66 | public static function start(string $script, array $args = []): ?self { |
| 67 | if (!function_exists('proc_open')) return null; |
| 68 | |
| 69 | // 空いているポートを探す |
| 70 | $probe = @stream_socket_server('tcp://127.0.0.1:0', $errno, $errstr); |
| 71 | if (!$probe) return null; |
| 72 | $port = (int)substr(strrchr(stream_socket_get_name($probe, false), ':'), 1); |
| 73 | fclose($probe); |
| 74 | |
| 75 | $cmd = array_merge([PHP_BINARY, __DIR__.'/'.$script, (string)$port], array_map('strval', $args)); |
| 76 | $spec = [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']]; |
| 77 | $proc = @proc_open($cmd, $spec, $pipes); |
| 78 | if (!is_resource($proc)) return null; |
| 79 | |
| 80 | // 「ready」が出力されるまで待つ(起動に失敗したらfalse) |
| 81 | $line = fgets($pipes[1]); |
| 82 | if (trim((string)$line) !== 'ready') { |
| 83 | proc_terminate($proc); |
| 84 | proc_close($proc); |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | $srv = new self(); |
| 89 | $srv->port = $port; |
| 90 | $srv->proc = $proc; |
| 91 | $srv->pipes = $pipes; |
| 92 | return $srv; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * このサーバーのURL |
| 97 | * |
| 98 | * @param string $path パス |
| 99 | * @param string $host 127.0.0.1又はlocalhost(別のオリジンとして扱う為) |
| 100 | * @return string |
| 101 | */ |
| 102 | public function url(string $path = '/', string $host = '127.0.0.1'): string { |
| 103 | return "http://{$host}:{$this->port}{$path}"; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * サーバーを止める |
| 108 | * |
| 109 | * @return void |
| 110 | */ |
| 111 | public function stop(): void { |
| 112 | if (!is_resource($this->proc)) return; |
| 113 | // 終了のリクエストを送る(Windowsではproc_terminateで子プロセスが残る事がある) |
| 114 | $c = @stream_socket_client("tcp://127.0.0.1:{$this->port}", $errno, $errstr, 1); |
| 115 | if ($c) { |
| 116 | fwrite($c, "QUIT /__quit HTTP/1.0\r\n\r\n"); |
| 117 | fclose($c); |
| 118 | } |
| 119 | foreach ($this->pipes as $p) if (is_resource($p)) fclose($p); |
| 120 | proc_terminate($this->proc); |
| 121 | proc_close($this->proc); |
| 122 | } |
| 123 | |
| 124 | public function __destruct() { |
| 125 | $this->stop(); |
| 126 | } |
| 127 | } |