|
| 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 testAddressTaken() |
| 28 | + { |
| 29 | + $docroot = __DIR__ . '/stub'; |
| 30 | + |
| 31 | + /** @var ServeController $serveController */ |
| 32 | + $serveController = $this->getMockBuilder(ServeControllerMocK::className()) |
| 33 | + ->setConstructorArgs(['serve', Yii::$app]) |
| 34 | + ->setMethods(['isAddressTaken', 'runCommand']) |
| 35 | + ->getMock(); |
| 36 | + |
| 37 | + $serveController->expects($this->once())->method('isAddressTaken')->willReturn(true); |
| 38 | + $serveController->expects($this->never())->method('runCommand'); |
| 39 | + |
| 40 | + $serveController->docroot = $docroot; |
| 41 | + $serveController->port = 8080; |
| 42 | + |
| 43 | + ob_start(); |
| 44 | + $serveController->actionIndex('localhost:8080'); |
| 45 | + ob_end_clean(); |
| 46 | + |
| 47 | + $result = $serveController->flushStdOutBuffer(); |
| 48 | + |
| 49 | + $this->assertContains('http://localhost:8080 is taken by another process.', $result); |
| 50 | + } |
| 51 | + |
| 52 | + public function testDefaultValues() |
| 53 | + { |
| 54 | + $docroot = __DIR__ . '/stub'; |
| 55 | + |
| 56 | + /** @var ServeController $serveController */ |
| 57 | + $serveController = $this->getMockBuilder(ServeControllerMock::className()) |
| 58 | + ->setConstructorArgs(['serve', Yii::$app]) |
| 59 | + ->setMethods(['runCommand']) |
| 60 | + ->getMock(); |
| 61 | + |
| 62 | + $serveController->docroot = $docroot; |
| 63 | + $serveController->port = 8080; |
| 64 | + |
| 65 | + $serveController->expects($this->once())->method('runCommand')->willReturn(true); |
| 66 | + |
| 67 | + ob_start(); |
| 68 | + $serveController->actionIndex(); |
| 69 | + ob_end_clean(); |
| 70 | + |
| 71 | + $result = $serveController->flushStdOutBuffer(); |
| 72 | + |
| 73 | + $this->assertContains('Server started on http://localhost:8080', $result); |
| 74 | + $this->assertContains("Document root is \"{$docroot}\"", $result); |
| 75 | + $this->assertContains('Quit the server with CTRL-C or COMMAND-C.', $result); |
| 76 | + } |
| 77 | + |
| 78 | + public function testDoocRootWithNoExistValue() |
| 79 | + { |
| 80 | + $docroot = '/not/exist/path'; |
| 81 | + |
| 82 | + /** @var ServeController $serveController */ |
| 83 | + $serveController = $this->getMockBuilder(ServeControllerMock::className()) |
| 84 | + ->setConstructorArgs(['serve', Yii::$app]) |
| 85 | + ->setMethods(['runCommand']) |
| 86 | + ->getMock(); |
| 87 | + |
| 88 | + $serveController->docroot = $docroot; |
| 89 | + |
| 90 | + $serveController->expects($this->any())->method('runCommand')->willReturn(true); |
| 91 | + |
| 92 | + ob_start(); |
| 93 | + $serveController->actionIndex(); |
| 94 | + ob_end_clean(); |
| 95 | + |
| 96 | + $result = $serveController->flushStdOutBuffer(); |
| 97 | + |
| 98 | + $this->assertContains("Document root \"{$docroot}\" does not exist.", $result); |
| 99 | + } |
| 100 | + |
| 101 | + public function testWithRouterNoExistValue() |
| 102 | + { |
| 103 | + $docroot = __DIR__ . '/stub'; |
| 104 | + $router = '/not/exist/path'; |
| 105 | + |
| 106 | + /** @var ServeController $serveController */ |
| 107 | + $serveController = $this->getMockBuilder(ServeControllerMock::className()) |
| 108 | + ->setConstructorArgs(['serve', Yii::$app]) |
| 109 | + ->setMethods(['runCommand']) |
| 110 | + ->getMock(); |
| 111 | + |
| 112 | + $serveController->docroot = $docroot; |
| 113 | + $serveController->port = 8081; |
| 114 | + $serveController->router = $router; |
| 115 | + |
| 116 | + $serveController->expects($this->any())->method('runCommand')->willReturn(true); |
| 117 | + |
| 118 | + ob_start(); |
| 119 | + $serveController->actionIndex(); |
| 120 | + ob_end_clean(); |
| 121 | + |
| 122 | + $result = $serveController->flushStdOutBuffer(); |
| 123 | + |
| 124 | + $this->assertContains("Routing file \"$router\" does not exist.", $result); |
| 125 | + } |
| 126 | + |
| 127 | + public function testWithRouterValue() |
| 128 | + { |
| 129 | + $docroot = __DIR__ . '/stub'; |
| 130 | + $router = __DIR__ . '/stub/index.php'; |
| 131 | + |
| 132 | + /** @var ServeController $serveController */ |
| 133 | + $serveController = $this->getMockBuilder(ServeControllerMock::className()) |
| 134 | + ->setConstructorArgs(['serve', Yii::$app]) |
| 135 | + ->setMethods(['runCommand']) |
| 136 | + ->getMock(); |
| 137 | + |
| 138 | + $serveController->docroot = $docroot; |
| 139 | + $serveController->port = 8081; |
| 140 | + $serveController->router = $router; |
| 141 | + |
| 142 | + $serveController->expects($this->once())->method('runCommand')->willReturn(true); |
| 143 | + |
| 144 | + ob_start(); |
| 145 | + $serveController->actionIndex(); |
| 146 | + ob_end_clean(); |
| 147 | + |
| 148 | + $result = $serveController->flushStdOutBuffer(); |
| 149 | + |
| 150 | + $this->assertContains('Server started on http://localhost:8081', $result); |
| 151 | + $this->assertContains("Document root is \"{$docroot}\"", $result); |
| 152 | + $this->assertContains("Routing file is \"{$router}\"", $result); |
| 153 | + $this->assertContains('Quit the server with CTRL-C or COMMAND-C.', $result); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * Mock class for [[\yii\console\controllers\ServeController]]. |
| 159 | + */ |
| 160 | +class ServeControllerMock extends ServeController |
| 161 | +{ |
| 162 | + use StdOutBufferControllerTrait; |
| 163 | +} |
0 commit comments