From 1883478baeecaa4e07f565f07638f2486568092b Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Mon, 16 Sep 2024 09:18:17 +0200 Subject: [PATCH] refs #758. Testing command API --- src/Dto/Input/CommandInput.php | 1 + src/Dto/Output/CommandOutput.php | 2 +- src/Factory/CommandFactory.php | 59 +++++++++++++++ tests/Functional/CommandTest.php | 122 +++++++++++++++++++++++++++++++ 4 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 src/Factory/CommandFactory.php create mode 100644 tests/Functional/CommandTest.php diff --git a/src/Dto/Input/CommandInput.php b/src/Dto/Input/CommandInput.php index a9d36af..7ddf219 100644 --- a/src/Dto/Input/CommandInput.php +++ b/src/Dto/Input/CommandInput.php @@ -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', diff --git a/src/Dto/Output/CommandOutput.php b/src/Dto/Output/CommandOutput.php index d8598f5..75f7d5e 100644 --- a/src/Dto/Output/CommandOutput.php +++ b/src/Dto/Output/CommandOutput.php @@ -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'])] diff --git a/src/Factory/CommandFactory.php b/src/Factory/CommandFactory.php new file mode 100644 index 0000000..2e318ed --- /dev/null +++ b/src/Factory/CommandFactory.php @@ -0,0 +1,59 @@ + + */ +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; + } +} diff --git a/tests/Functional/CommandTest.php b/tests/Functional/CommandTest.php new file mode 100644 index 0000000..68a55ad --- /dev/null +++ b/tests/Functional/CommandTest.php @@ -0,0 +1,122 @@ + 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]) + ); + } +} \ No newline at end of file