ogcore/tests/Functional/SoftwareProfileTest.php

164 lines
6.4 KiB
PHP

<?php
namespace Functional;
use App\Entity\Software;
use App\Entity\SoftwareProfile;
use App\Factory\SoftwareFactory;
use App\Factory\SoftwareProfileFactory;
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 SoftwareProfileTest extends AbstractTest
{
CONST string USER_ADMIN = 'ogadmin';
CONST string SW_PROFILE_CREATE = 'test-sw-profile-create';
CONST string SW_PROFILE_UPDATE = 'test-sw-profile-update';
CONST string SW_PROFILE_DELETE = 'test-sw-profile-delete';
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testGetCollectionSoftwareProfile(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
SoftwareProfileFactory::createMany(10);
$this->createClientWithCredentials()->request('GET', '/software-profiles');
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/contexts/SoftwareProfile',
'@id' => '/software-profiles',
'@type' => 'hydra:Collection',
'hydra:totalItems' => 10,
]);
}
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testCreateSoftwareProfile(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
$this->createClientWithCredentials()->request('POST', '/software-profiles',['json' => [
'description' => self::SW_PROFILE_CREATE,
]]);
$this->assertResponseStatusCodeSame(201);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/contexts/SoftwareProfileOutput',
'@type' => 'SoftwareProfile',
'description' => self::SW_PROFILE_CREATE
]);
}
/**
* Verifica que el software seleccionado en el formulario se persiste
* (softwareCollection) y que se puede añadir y quitar software después.
*
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testSoftwareProfileSoftwareCollectionIsPersisted(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
SoftwareFactory::createOne(['name' => 'test-software-a']);
SoftwareFactory::createOne(['name' => 'test-software-b']);
$softwareAIri = $this->findIriBy(Software::class, ['name' => 'test-software-a']);
$softwareBIri = $this->findIriBy(Software::class, ['name' => 'test-software-b']);
$client = $this->createClientWithCredentials();
$client->request('POST', '/software-profiles', ['json' => [
'description' => 'test-sw-profile-collection',
'softwareCollection' => [$softwareAIri, $softwareBIri],
]]);
$this->assertResponseStatusCodeSame(201);
$iri = $client->getResponse()->toArray()['@id'];
$client->request('GET', $iri);
$this->assertResponseIsSuccessful();
$data = $client->getResponse()->toArray();
$this->assertCount(2, $data['softwareCollection']);
$client->request('PUT', $iri, ['json' => [
'description' => 'test-sw-profile-collection',
'softwareCollection' => [$softwareAIri],
]]);
$this->assertResponseIsSuccessful();
$client->request('GET', $iri);
$data = $client->getResponse()->toArray();
$this->assertCount(1, $data['softwareCollection']);
$this->assertSame($softwareAIri, $data['softwareCollection'][0]['@id']);
}
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testUpdateSoftwareProfile(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
SoftwareProfileFactory::createOne(['description' => self::SW_PROFILE_CREATE]);
$iri = $this->findIriBy(SoftwareProfile::class, ['description' => self::SW_PROFILE_CREATE]);
$this->createClientWithCredentials()->request('PUT', $iri, ['json' => [
'description' => self::SW_PROFILE_UPDATE,
]]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'@id' => $iri,
'description' => self::SW_PROFILE_UPDATE,
]);
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
*/
public function testDeleteSoftwareProfile(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
SoftwareProfileFactory::createOne(['description' => self::SW_PROFILE_DELETE]);
$iri = $this->findIriBy(SoftwareProfile::class, ['description' => self::SW_PROFILE_DELETE]);
$this->createClientWithCredentials()->request('DELETE', $iri);
$this->assertResponseStatusCodeSame(204);
$this->assertNull(
static::getContainer()->get('doctrine')->getRepository(SoftwareProfile::class)->findOneBy(['description' => self::SW_PROFILE_DELETE])
);
}
}