From 67530bb0d60d328dad7bec70522240677e7f81b5 Mon Sep 17 00:00:00 2001 From: Louis Charette Date: Sat, 2 Mar 2024 15:46:40 -0500 Subject: [PATCH] New command : Serve --- app/src/Bakery/ServeCommand.php | 60 ++++++++++++++++++ app/src/Core.php | 2 + .../ServicesProvider/DatabaseServiceTest.php | 1 - app/tests/Unit/Bakery/ServeCommandTest.php | 61 +++++++++++++++++++ 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 app/src/Bakery/ServeCommand.php create mode 100644 app/tests/Unit/Bakery/ServeCommandTest.php diff --git a/app/src/Bakery/ServeCommand.php b/app/src/Bakery/ServeCommand.php new file mode 100644 index 00000000..88007ef0 --- /dev/null +++ b/app/src/Bakery/ServeCommand.php @@ -0,0 +1,60 @@ +ctrl+c` 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; + } +} diff --git a/app/src/Core.php b/app/src/Core.php index 4453c19d..9ed6e55f 100644 --- a/app/src/Core.php +++ b/app/src/Core.php @@ -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; @@ -157,6 +158,7 @@ public function getBakeryCommands(): array RouteListCommand::class, SeedCommand::class, SeedListCommand::class, + ServeCommand::class, SetupCommand::class, SetupDbCommand::class, SetupEnvCommand::class, diff --git a/app/tests/Integration/ServicesProvider/DatabaseServiceTest.php b/app/tests/Integration/ServicesProvider/DatabaseServiceTest.php index 479efad1..cb70b63f 100644 --- a/app/tests/Integration/ServicesProvider/DatabaseServiceTest.php +++ b/app/tests/Integration/ServicesProvider/DatabaseServiceTest.php @@ -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; /** diff --git a/app/tests/Unit/Bakery/ServeCommandTest.php b/app/tests/Unit/Bakery/ServeCommandTest.php new file mode 100644 index 00000000..fdb256a0 --- /dev/null +++ b/app/tests/Unit/Bakery/ServeCommandTest.php @@ -0,0 +1,61 @@ +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()); + } +}