1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
<?php
namespace Illuminate\Tests\Foundation;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Console\DocsCommand;
use Illuminate\Support\Facades\Http;
use Orchestra\Testbench\TestCase;
use RuntimeException;
use Symfony\Component\Process\Exception\ProcessFailedException;
class FoundationDocsCommandTest extends TestCase
{
/**
* The URL opened by the command.
*
* @var string|null
*/
protected $openedUrl;
/**
* The command registered to the container.
*
* @var \Illuminate\Foundation\Console\DocsCommand
*/
protected $command;
protected function setUp(): void
{
parent::setUp();
Http::preventStrayRequests()->fake([
'https://laravel.com/docs/8.x/index.json' => Http::response(file_get_contents(__DIR__.'/fixtures/docs.json')),
]);
$this->app[Kernel::class]->registerCommand($this->command());
}
protected function tearDown(): void
{
parent::tearDown();
putenv('ARTISAN_DOCS_ASK_STRATEGY');
putenv('ARTISAN_DOCS_OPEN_STRATEGY');
}
public function testItCanOpenTheLaravelDocumentation(): void
{
$this->artisan('docs')
->expectsQuestion('Which page would you like to open?', '')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/installation')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/installation');
}
public function testItCanSpecifyAutocompleteInOriginalCasing(): void
{
$this->artisan('docs')
->expectsQuestion('Which page would you like to open?', 'Laravel Dusk')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/dusk')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/dusk');
}
public function testItCanSpecifyAutocompleteInLowerCasing(): void
{
$this->artisan('docs')
->expectsQuestion('Which page would you like to open?', 'laravel dusk')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/dusk')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/dusk');
}
public function testItMatchesSectionsThatStartWithInput()
{
$this->artisan('docs el-col uni')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/eloquent-collections#method-unique')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/eloquent-collections#method-unique');
}
public function testItMatchesSectionsWithFuzzyMatching()
{
$this->artisan('docs el-col qery')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/eloquent-collections#method-toquery')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/eloquent-collections#method-toquery');
}
public function testItCanProvidePageToVisit(): void
{
$this->artisan('docs eloquent\ collections')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/eloquent-collections')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/eloquent-collections');
}
public function testItCanUseHyphensInsteadOfEscapingSpaces(): void
{
$this->artisan('docs eloquent-collections')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/eloquent-collections')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/eloquent-collections');
}
public function testItHasMinimumScoreToMatch(): void
{
$this->artisan('docs zag')
->expectsOutputToContain('Unable to determine the page you are trying to visit.')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x');
}
public function testItMinimumScoreAccountsForInputLength(): void
{
$this->artisan('docs z')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/localization')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/localization');
}
public function testItCanUseCustomAskStrategy()
{
putenv('ARTISAN_DOCS_ASK_STRATEGY='.__DIR__.'/fixtures/always-dusk-ask-strategy.php');
$this->artisan('docs')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/dusk')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/dusk');
}
public function testItFallsbackToAutocompleteWhenAskStrategyContainsBadSyntax(): void
{
putenv('ARTISAN_DOCS_ASK_STRATEGY='.__DIR__.'/fixtures/bad-syntax-strategy.php');
$this->artisan('docs')
->expectsQuestion('Which page would you like to open?', 'laravel dusk')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/dusk')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/dusk');
}
public function testItFallsbackToAutocompleteWithBadAskStrategyReturnValue(): void
{
putenv('ARTISAN_DOCS_ASK_STRATEGY='.__DIR__.'/fixtures/bad-return-strategy.php');
$this->artisan('docs')
->expectsQuestion('Which page would you like to open?', 'laravel dusk')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/dusk')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/dusk');
}
public function testItCatchesAndHandlesProcessInterruptExceptionsInAskStrategies()
{
putenv('ARTISAN_DOCS_ASK_STRATEGY='.__DIR__.'/fixtures/process-interrupt-strategy.php');
$this->artisan('docs')->assertExitCode(130);
}
public function testItBubblesUpAskStrategyExceptions()
{
putenv('ARTISAN_DOCS_ASK_STRATEGY='.__DIR__.'/fixtures/exception-throwing-strategy.php');
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('strategy failed');
$this->artisan('docs');
}
public function testItBubblesUpNonProcessInterruptExceptionsInAskStrategies()
{
putenv('ARTISAN_DOCS_ASK_STRATEGY='.__DIR__.'/fixtures/process-failure-strategy.php');
$this->expectException(ProcessFailedException::class);
if (PHP_OS_FAMILY === 'Windows') {
$this->expectExceptionMessage('The command "expected-command" failed.
Exit Code: 1(General error)
Working directory: expected-working-directory');
} else {
$this->expectExceptionMessage('The command "\'expected-command\'" failed.
Exit Code: 1(General error)
Working directory: expected-working-directory');
}
$this->artisan('docs');
}
public function testItCanGuessTheRequestedPageWhenItIsTheStartOfAPageTitle()
{
$this->artisan('docs elo')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/eloquent')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/eloquent');
}
public function testItCanGuessTheRequestedPageWhenItIsContainedSomewhereInThePageTitle()
{
$this->artisan('docs quent')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/eloquent')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/eloquent');
}
public function testItCanGuessTheWithTopAndTailMatching()
{
$this->artisan('docs elo-col')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/eloquent-collections')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/eloquent-collections');
}
public function testItCanSpecifyCustomOpenCommandsViaEnvVariables()
{
$GLOBALS['open-strategy-output-path'] = __DIR__.'/output.txt';
putenv('ARTISAN_DOCS_OPEN_STRATEGY='.__DIR__.'/fixtures/open-strategy.php');
$this->app[Kernel::class]->registerCommand($this->command()->setUrlOpener(null));
@unlink($GLOBALS['open-strategy-output-path']);
$this->artisan('docs installation')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/installation')
->assertSuccessful();
if (PHP_OS_FAMILY === 'Windows') {
$this->assertSame('"https://laravel.com/docs/8.x/installation?expected-query=1"', trim(file_get_contents($GLOBALS['open-strategy-output-path'])));
} else {
$this->assertSame('https://laravel.com/docs/8.x/installation?expected-query=1', trim(file_get_contents($GLOBALS['open-strategy-output-path'])));
}
@unlink($GLOBALS['open-strategy-output-path']);
unset($GLOBALS['open-strategy-output-path']);
}
public function testItHandlesBadSyntaxInOpeners()
{
putenv('ARTISAN_DOCS_OPEN_STRATEGY='.__DIR__.'/fixtures/bad-syntax-strategy.php');
$this->app[Kernel::class]->registerCommand($this->command()->setUrlOpener(null));
$this->artisan('docs installation')
->expectsOutputToContain('Unable to open the URL with your custom strategy. You will need to open it yourself.')
->assertSuccessful();
}
public function testItHandlesBadReturnTypesInOpeners()
{
putenv('ARTISAN_DOCS_OPEN_STRATEGY='.__DIR__.'/fixtures/bad-return-strategy.php');
$this->app[Kernel::class]->registerCommand($this->command()->setUrlOpener(null));
$this->artisan('docs installation')
->expectsOutputToContain('Unable to open the URL with your custom strategy. You will need to open it yourself.')
->assertSuccessful();
}
public function testItCanPerformSearchAgainstLaravelDotCom()
{
$argCache = $_SERVER['argv'];
$_SERVER['argv'] = explode(' ', 'artisan docs -- here is my search term for the laravel website');
$this->app[Kernel::class]->registerCommand($this->command());
$this->artisan('docs -- here is my search term for the laravel website')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x?q=here%20is%20my%20search%20term%20for%20the%20laravel%20website')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x?q=here%20is%20my%20search%20term%20for%20the%20laravel%20website');
$_SERVER['argv'] = $argCache;
}
public function testUnknownSystemNotifiedToOpenManually()
{
$this->app[Kernel::class]->registerCommand($this->command()->setUrlOpener(null)->setSystemOsFamily('Laravel OS'));
$this->artisan('docs validation')
->expectsOutputToContain('Unable to open the URL on your system. You will need to open it yourself or create a custom opener for your system.')
->assertSuccessful();
}
public function testGuessedMatchesThatDirectlyContainTheGivenStringRankHigherThanArbitraryMatches()
{
$this->artisan('docs ora')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/filesystem')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/filesystem');
}
public function testItHandlesPoorSpelling()
{
$this->artisan('docs vewis')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x/views')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x/views');
}
public function testItHandlesNoInteractionOption()
{
$this->artisan('docs -n')
->expectsOutputToContain('Opening the docs to: https://laravel.com/docs/8.x')
->assertSuccessful();
$this->assertSame($this->openedUrl, 'https://laravel.com/docs/8.x');
}
public function testCanGetHelpWithoutInstantiatingDependencies()
{
$help = (new DocsCommand())->getHelp();
$this->stringContains('php artisan docs', $help);
}
protected function command()
{
$this->app->forgetInstance(DocsCommand::class);
return $this->app->make(DocsCommand::class)
->setVersion('8.30.12')
->setUrlOpener(function ($url) {
$this->openedUrl = $url;
});
}
}
|