ogcore/tests/Functional/HardwareProfileTest.php

156 lines
6.3 KiB
PHP

<?php
namespace Functional;
use App\Entity\Client;
use App\Entity\HardwareProfile;
use App\Entity\OrganizationalUnit;
use App\Factory\ClientFactory;
use App\Factory\HardwareProfileFactory;
use App\Factory\OrganizationalUnitFactory;
use App\Factory\UserFactory;
use App\Model\OrganizationalUnitTypes;
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 HardwareProfileTest extends AbstractTest
{
CONST string USER_ADMIN = 'ogadmin';
CONST string HW_PROFILE_CREATE = 'test-hw-profile-create';
CONST string HW_PROFILE_UPDATE = 'test-hw-profile-update';
CONST string HW_PROFILE_DELETE = 'test-hw-profile-delete';
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testGetCollectionHardwareProfile(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
HardwareProfileFactory::createMany(10);
$this->createClientWithCredentials()->request('GET', '/hardware-profiles');
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/contexts/HardwareProfile',
'@id' => '/hardware-profiles',
'@type' => 'hydra:Collection',
'hydra:totalItems' => 10,
]);
}
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testCreateHardwareProfile(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
$this->createClientWithCredentials()->request('POST', '/hardware-profiles',['json' => [
'description' => self::HW_PROFILE_CREATE,
]]);
$this->assertResponseStatusCodeSame(201);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/contexts/HardwareProfileOutput',
'@type' => 'HardwareProfile',
'description' => self::HW_PROFILE_CREATE
]);
}
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testUpdateHardwareProfile(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
HardwareProfileFactory::createOne(['description' => self::HW_PROFILE_CREATE]);
$iri = $this->findIriBy(HardwareProfile::class, ['description' => self::HW_PROFILE_CREATE]);
$this->createClientWithCredentials()->request('PUT', $iri, ['json' => [
'description' => self::HW_PROFILE_UPDATE,
]]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'@id' => $iri,
'description' => self::HW_PROFILE_UPDATE,
]);
}
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testHardwareProfileSharedByMultipleClients(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
$ou = OrganizationalUnitFactory::createOne(['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]);
ClientFactory::createOne(['name' => 'test-client-a', 'organizationalUnit' => $ou]);
ClientFactory::createOne(['name' => 'test-client-b', 'organizationalUnit' => $ou]);
HardwareProfileFactory::createOne(['description' => self::HW_PROFILE_CREATE]);
$clientAIri = $this->findIriBy(Client::class, ['name' => 'test-client-a']);
$clientBIri = $this->findIriBy(Client::class, ['name' => 'test-client-b']);
$hardwareProfileIri = $this->findIriBy(HardwareProfile::class, ['description' => self::HW_PROFILE_CREATE]);
$client = $this->createClientWithCredentials();
$client->request('PUT', $clientAIri, ['json' => ['name' => 'test-client-a', 'hardwareProfile' => $hardwareProfileIri]]);
$this->assertResponseIsSuccessful();
$client->request('PUT', $clientBIri, ['json' => ['name' => 'test-client-b', 'hardwareProfile' => $hardwareProfileIri]]);
$this->assertResponseIsSuccessful();
$client->request('GET', $hardwareProfileIri);
$this->assertResponseIsSuccessful();
$data = $client->getResponse()->toArray();
$this->assertCount(2, $data['clients']);
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
*/
public function testDeleteHardwareProfile(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
HardwareProfileFactory::createOne(['description' => self::HW_PROFILE_DELETE]);
$iri = $this->findIriBy(HardwareProfile::class, ['description' => self::HW_PROFILE_DELETE]);
$this->createClientWithCredentials()->request('DELETE', $iri);
$this->assertResponseStatusCodeSame(204);
$this->assertNull(
static::getContainer()->get('doctrine')->getRepository(HardwareProfile::class)->findOneBy(['description' => self::HW_PROFILE_DELETE])
);
}
}