-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add fastly:snippet:deploy command
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopware\Deployment\Command; | ||
|
||
use Shopware\Deployment\Event\PostDeploy; | ||
use Shopware\Deployment\Helper\EnvironmentHelper; | ||
use Shopware\Deployment\Integration\Fastly\FastlyServiceUpdater; | ||
use Shopware\Deployment\Struct\RunConfiguration; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
#[AsCommand( | ||
name: 'fastly:snippet:deploy', | ||
description: 'Deploy all Fastly snippets' | ||
)] | ||
class FastlySnippetDeployCommand extends Command | ||
{ | ||
public function __construct(private readonly FastlyServiceUpdater $fastlyServiceUpdater) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$apiToken = EnvironmentHelper::getVariable('FASTLY_API_TOKEN', ''); | ||
$serviceId = EnvironmentHelper::getVariable('FASTLY_SERVICE_ID', ''); | ||
|
||
if ($apiToken === '' || $serviceId === '') { | ||
$output->writeln('FASTLY_API_TOKEN or FASTLY_SERVICE_ID is not set.'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
$this->fastlyServiceUpdater->__invoke(new PostDeploy(new RunConfiguration(), $output)); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopware\Deployment\Tests\Command; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
use Shopware\Deployment\Command\FastlySnippetDeployCommand; | ||
use Shopware\Deployment\Integration\Fastly\FastlyServiceUpdater; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Zalas\PHPUnit\Globals\Attribute\Env; | ||
|
||
#[CoversClass(FastlySnippetDeployCommand::class)] | ||
class FastlySnippetDeployCommandTest extends TestCase | ||
{ | ||
public function testRunCommandWithoutEnv(): void | ||
{ | ||
$updater = $this->createMock(FastlyServiceUpdater::class); | ||
$updater | ||
->expects($this->never()) | ||
->method('__invoke'); | ||
|
||
$command = new FastlySnippetDeployCommand($updater); | ||
$tester = new CommandTester($command); | ||
|
||
$tester->execute([]); | ||
|
||
static::assertEquals(Command::FAILURE, $tester->getStatusCode()); | ||
static::assertStringContainsString('FASTLY_API_TOKEN or FASTLY_SERVICE_ID is not set.', $tester->getDisplay()); | ||
} | ||
|
||
#[Env('FASTLY_API_TOKEN', 'apiToken')] | ||
#[Env('FASTLY_SERVICE_ID', 'serviceId')] | ||
public function testRunCommandWithEnv(): void | ||
{ | ||
$updater = $this->createMock(FastlyServiceUpdater::class); | ||
$updater | ||
->expects($this->once()) | ||
->method('__invoke'); | ||
|
||
$command = new FastlySnippetDeployCommand($updater); | ||
$tester = new CommandTester($command); | ||
|
||
$tester->execute([]); | ||
|
||
static::assertEquals(Command::SUCCESS, $tester->getStatusCode()); | ||
} | ||
} |