-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* UserFrosting Core Sprinkle (http://www.userfrosting.com) | ||
* | ||
* @link https://github.com/userfrosting/sprinkle-core | ||
* @copyright Copyright (c) 2021 Alexander Weissman & Louis Charette | ||
* @license https://github.com/userfrosting/sprinkle-core/blob/master/LICENSE.md (MIT License) | ||
*/ | ||
|
||
namespace UserFrosting\Sprinkle\Core\Bakery; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use UserFrosting\Bakery\WithSymfonyStyle; | ||
use UserFrosting\Sprinkle\Core\Bakery\Helper\ShellCommandHelper; | ||
|
||
/** | ||
* Alias for `php serve` command. | ||
*/ | ||
final class ServeCommand extends Command | ||
{ | ||
use WithSymfonyStyle; | ||
use ShellCommandHelper; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$help = [ | ||
'Run the php built-in web server to test your application.', | ||
'This is a simple way to test your application without having to configure a full web server.', | ||
'Hit `<info>ctrl+c</info>` to quit.' | ||
]; | ||
|
||
$this->setName('serve') | ||
->addOption('port', 'p', InputOption::VALUE_REQUIRED, 'The port to serve the application on', '8080') | ||
->setDescription('Alias for `php -S` command') | ||
->setHelp(implode(' ', $help)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$this->io->title('PHP built-in web server'); | ||
$this->io->info('Press `ctrl+c` to quit'); | ||
|
||
$port = $input->getOption('port'); | ||
$this->executeCommand("php -S localhost:$port -t public"); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* UserFrosting Core Sprinkle (http://www.userfrosting.com) | ||
* | ||
* @link https://github.com/userfrosting/sprinkle-core | ||
* @copyright Copyright (c) 2021 Alexander Weissman & Louis Charette | ||
* @license https://github.com/userfrosting/sprinkle-core/blob/master/LICENSE.md (MIT License) | ||
*/ | ||
|
||
namespace UserFrosting\Sprinkle\Core\Tests\Unit\Bakery; | ||
|
||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; | ||
use phpmock\mockery\PHPMockery; | ||
use PHPUnit\Framework\TestCase; | ||
use ReflectionClass; | ||
use UserFrosting\Sprinkle\Core\Bakery\Helper\ShellCommandHelper; | ||
use UserFrosting\Sprinkle\Core\Bakery\ServeCommand; | ||
use UserFrosting\Testing\BakeryTester; | ||
use UserFrosting\Testing\ContainerStub; | ||
|
||
class ServeCommandTest extends TestCase | ||
{ | ||
use MockeryPHPUnitIntegration; | ||
|
||
public function testCommand(): void | ||
{ | ||
// Mock passthru, from ShellCommandHelper | ||
$reflection_class = new ReflectionClass(ShellCommandHelper::class); | ||
$namespace = $reflection_class->getNamespaceName(); | ||
PHPMockery::mock($namespace, 'passthru')->andReturn(null); | ||
|
||
$ci = ContainerStub::create(); | ||
/** @var ServeCommand */ | ||
$command = $ci->get(ServeCommand::class); | ||
$result = BakeryTester::runCommand($command); | ||
|
||
// Assert some output | ||
$this->assertSame(0, $result->getStatusCode()); | ||
$this->assertStringContainsString('php -S localhost:8080 -t public', $result->getDisplay()); | ||
} | ||
|
||
public function testCommandWithPort(): void | ||
{ | ||
// Mock passthru, from ShellCommandHelper | ||
$reflection_class = new ReflectionClass(ShellCommandHelper::class); | ||
$namespace = $reflection_class->getNamespaceName(); | ||
PHPMockery::mock($namespace, 'passthru')->andReturn(null); | ||
|
||
$ci = ContainerStub::create(); | ||
/** @var ServeCommand */ | ||
$command = $ci->get(ServeCommand::class); | ||
$result = BakeryTester::runCommand($command, ['--port' => '1234']); | ||
|
||
// Assert some output | ||
$this->assertSame(0, $result->getStatusCode()); | ||
$this->assertStringContainsString('php -S localhost:1234 -t public', $result->getDisplay()); | ||
} | ||
} |