Skip to content

Commit

Permalink
New command : Serve
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Mar 2, 2024
1 parent 6bf3b6b commit 67530bb
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
60 changes: 60 additions & 0 deletions app/src/Bakery/ServeCommand.php
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;
}
}
2 changes: 2 additions & 0 deletions app/src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use UserFrosting\Sprinkle\Core\Bakery\RouteListCommand;
use UserFrosting\Sprinkle\Core\Bakery\SeedCommand;
use UserFrosting\Sprinkle\Core\Bakery\SeedListCommand;
use UserFrosting\Sprinkle\Core\Bakery\ServeCommand;
use UserFrosting\Sprinkle\Core\Bakery\SetupCommand;
use UserFrosting\Sprinkle\Core\Bakery\SetupDbCommand;
use UserFrosting\Sprinkle\Core\Bakery\SetupEnvCommand;
Expand Down Expand Up @@ -157,6 +158,7 @@ public function getBakeryCommands(): array
RouteListCommand::class,
SeedCommand::class,
SeedListCommand::class,
ServeCommand::class,
SetupCommand::class,
SetupDbCommand::class,
SetupEnvCommand::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use UserFrosting\Config\Config;
use UserFrosting\Sprinkle\Core\Log\QueryLogger;
use UserFrosting\Sprinkle\Core\Log\QueryLoggerInterface;
use UserFrosting\Sprinkle\Core\Testing\RefreshDatabase;
use UserFrosting\Sprinkle\Core\Tests\CoreTestCase as TestCase;

/**
Expand Down
61 changes: 61 additions & 0 deletions app/tests/Unit/Bakery/ServeCommandTest.php
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());
}
}

0 comments on commit 67530bb

Please sign in to comment.