|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @link https://www.yiiframework.com/ |
| 4 | + * @copyright Copyright (c) 2008 Yii Software LLC |
| 5 | + * @license https://www.yiiframework.com/license/ |
| 6 | + */ |
| 7 | + |
| 8 | +namespace yiiunit\framework\console\controllers; |
| 9 | + |
| 10 | +use Yii; |
| 11 | +use yii\console\controllers\ServeController; |
| 12 | +use yiiunit\TestCase; |
| 13 | + |
| 14 | +/** |
| 15 | + * Unit test for [[\yii\console\controllers\ServeController]]. |
| 16 | + * @see ServeController |
| 17 | + * |
| 18 | + * @group console |
| 19 | + */ |
| 20 | +class ServeControllerTest extends TestCase |
| 21 | +{ |
| 22 | + public function setUp() |
| 23 | + { |
| 24 | + $this->mockApplication(); |
| 25 | + } |
| 26 | + |
| 27 | + public function testActionIndex() |
| 28 | + { |
| 29 | + if (!\function_exists('pcntl_fork')) { |
| 30 | + $this->markTestSkipped('pcntl_fork() is not available'); |
| 31 | + } |
| 32 | + |
| 33 | + if (!\function_exists('posix_kill')) { |
| 34 | + $this->markTestSkipped('posix_kill() is not available'); |
| 35 | + } |
| 36 | + |
| 37 | + if (!\function_exists('pcntl_waitpid')) { |
| 38 | + $this->markTestSkipped('pcntl_waitpid() is not available'); |
| 39 | + } |
| 40 | + |
| 41 | + $controller = new ServeController('serve', Yii::$app); |
| 42 | + $controller->docroot = __DIR__ . '/stub'; |
| 43 | + $controller->port = 8080; |
| 44 | + |
| 45 | + $pid = \pcntl_fork(); |
| 46 | + |
| 47 | + if ($pid == 0) { |
| 48 | + \ob_start(); |
| 49 | + $controller->actionIndex('localhost'); |
| 50 | + \ob_get_clean(); |
| 51 | + exit(); |
| 52 | + } |
| 53 | + |
| 54 | + \sleep(1); |
| 55 | + |
| 56 | + $response = \file_get_contents('http://localhost:8080'); |
| 57 | + |
| 58 | + $this->assertEquals('Hello!', $response); |
| 59 | + |
| 60 | + \posix_kill($pid, \SIGTERM); |
| 61 | + \pcntl_waitpid($pid, $status); |
| 62 | + } |
| 63 | + |
| 64 | + public function testActionIndexWithRouter() |
| 65 | + { |
| 66 | + if (!\function_exists('pcntl_fork')) { |
| 67 | + $this->markTestSkipped('pcntl_fork() is not available'); |
| 68 | + } |
| 69 | + |
| 70 | + if (!\function_exists('posix_kill')) { |
| 71 | + $this->markTestSkipped('posix_kill() is not available'); |
| 72 | + } |
| 73 | + |
| 74 | + if (!\function_exists('pcntl_waitpid')) { |
| 75 | + $this->markTestSkipped('pcntl_waitpid() is not available'); |
| 76 | + } |
| 77 | + |
| 78 | + $controller = new ServeController('serve', Yii::$app); |
| 79 | + $controller->docroot = __DIR__ . '/stub'; |
| 80 | + $controller->port = 8081; |
| 81 | + $controller->router = __DIR__ . '/stub/index.php'; |
| 82 | + |
| 83 | + $pid = \pcntl_fork(); |
| 84 | + |
| 85 | + if ($pid == 0) { |
| 86 | + \ob_start(); |
| 87 | + $controller->actionIndex('localhost'); |
| 88 | + \ob_get_clean(); |
| 89 | + exit(); |
| 90 | + } |
| 91 | + |
| 92 | + \sleep(1); |
| 93 | + |
| 94 | + $response = \file_get_contents('http://localhost:8081'); |
| 95 | + |
| 96 | + $this->assertEquals('Hello!', $response); |
| 97 | + |
| 98 | + \posix_kill($pid, \SIGTERM); |
| 99 | + \pcntl_waitpid($pid, $status); |
| 100 | + } |
| 101 | +} |
0 commit comments