Skip to content

Commit

Permalink
feat: add fastly:snippet:deploy command
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Oct 21, 2024
1 parent fa8b9a0 commit 60e267a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Command/FastlySnippetDeployCommand.php
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;
}
}
50 changes: 50 additions & 0 deletions tests/Command/FastlySnippetDeployCommandTest.php
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());
}
}

0 comments on commit 60e267a

Please sign in to comment.