refs #758. Testing command API

feature/actions
Manuel Aranda Rosales 2024-09-16 09:18:17 +02:00
parent babd702f85
commit 1883478bae
4 changed files with 183 additions and 1 deletions

View File

@ -18,6 +18,7 @@ final class CommandInput
)]
public ?string $name = null;
#[Assert\NotBlank(message: 'validators.command.script.not_blank')]
#[Groups(['command:write'])]
#[ApiProperty(
description: 'El script del comando',

View File

@ -6,7 +6,7 @@ use ApiPlatform\Metadata\Get;
use App\Entity\Command;
use Symfony\Component\Serializer\Annotation\Groups;
#[Get(shortName: 'Client')]
#[Get(shortName: 'Command')]
final class CommandOutput extends AbstractOutput
{
#[Groups(['command:read'])]

View File

@ -0,0 +1,59 @@
<?php
namespace App\Factory;
use App\Entity\Command;
use App\Repository\CommandRepository;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\Persistence\ProxyRepositoryDecorator;
/**
* @extends PersistentProxyObjectFactory<Command>
*/
final class CommandFactory extends ModelFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
*
* @todo inject services if required
*/
public function __construct()
{
parent::__construct();
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
*
* @todo add your default values here
*/
protected function getDefaults(): array
{
return [
'createdAt' => self::faker()->dateTime(),
'enabled' => self::faker()->boolean(),
'name' => self::faker()->text(255),
'readOnly' => self::faker()->boolean(),
'script' => self::faker()->text(255),
'updatedAt' => self::faker()->dateTime()
];
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
*/
protected function initialize(): self
{
return $this
// ->afterInstantiate(function(Command $command): void {})
;
}
public static function getClass(): string
{
return Command::class;
}
}

View File

@ -0,0 +1,122 @@
<?php
namespace Functional;
use App\Entity\Command;
use App\Factory\CommandFactory;
use App\Factory\UserFactory;
use App\Model\UserGroupPermissions;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class CommandTest extends AbstractTest
{
CONST string USER_ADMIN = 'ogadmin';
CONST string CMD_CREATE = 'test-cmd-create';
CONST string CMD_UPDATE = 'test-cmd-update';
CONST string CMD_DELETE = 'test-cmd-delete';
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testGetCollectionCommand(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
CommandFactory::createMany(10);
$this->createClientWithCredentials()->request('GET', '/commands');
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/contexts/Command',
'@id' => '/commands',
'@type' => 'hydra:Collection',
'hydra:totalItems' => 10,
]);
}
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testCreateCommand(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
$this->createClientWithCredentials()->request('POST', '/commands',['json' => [
'name' => self::CMD_CREATE,
'script' => 'echo "Hello World!"',
]]);
$this->assertResponseStatusCodeSame(201);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/contexts/CommandOutput',
'@type' => 'Command',
'name' => self::CMD_CREATE,
'script' => 'echo "Hello World!"'
]);
}
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testUpdateCommand(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
CommandFactory::createOne(['name' => self::CMD_CREATE]);
$iri = $this->findIriBy(Command::class, ['name' => self::CMD_CREATE]);
$this->createClientWithCredentials()->request('PUT', $iri, ['json' => [
'name' => self::CMD_UPDATE,
'readOnly' => true
]]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'@id' => $iri,
'name' => self::CMD_UPDATE,
'readOnly' => true
]);
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
*/
public function testDeleteCommand(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
CommandFactory::createOne(['name' => self::CMD_DELETE]);
$iri = $this->findIriBy(Command::class, ['name' => self::CMD_DELETE]);
$this->createClientWithCredentials()->request('DELETE', $iri);
$this->assertResponseStatusCodeSame(204);
$this->assertNull(
static::getContainer()->get('doctrine')->getRepository(Command::class)->findOneBy(['name' => self::CMD_DELETE])
);
}
}