ogcore/tests/Functional/ClientTest.php

115 lines
4.3 KiB
PHP

<?php
namespace Functional;
use App\Entity\Client;
use App\Entity\HardwareProfile;
use App\Entity\OrganizationalUnit;
use App\Entity\UserGroup;
use App\Factory\ClientFactory;
use App\Factory\HardwareProfileFactory;
use App\Factory\OrganizationalUnitFactory;
use App\Factory\UserFactory;
use App\Factory\UserGroupFactory;
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 ClientTest extends AbstractTest
{
CONST string USER_ADMIN = 'ogadmin';
CONST string CLIENT_CREATE = 'test-client-create';
CONST string CLIENT_UPDATE = 'test-client-update';
CONST string CLIENT_DELETE = 'test-client-delete';
const string HW_PROFILE = 'HW Test';
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testGetCollectionClients(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
ClientFactory::createMany(10);
$this->createClientWithCredentials()->request('GET', '/clients');
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/contexts/Client',
'@id' => '/clients',
'@type' => 'hydra:Collection',
'hydra:totalItems' => 10,
]);
}
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testCreateClient(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
OrganizationalUnitFactory::createOne(['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]);
$ouIri = $this->findIriBy(OrganizationalUnit::class, ['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]);
HardwareProfileFactory::createOne(['description' => self::HW_PROFILE]);
$hpIri = $this->findIriBy(HardwareProfile::class, ['description' => self::HW_PROFILE]);
$this->createClientWithCredentials()->request('POST', '/clients',['json' => [
'name' => self::CLIENT_CREATE,
'organizationalUnit' => $ouIri,
'hardwareProfile' => $hpIri,
'serialNumber' => '123abc',
]]);
$this->assertResponseStatusCodeSame(201);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/contexts/ClientOutput',
'@type' => 'Client',
'name' => self::CLIENT_CREATE,
'serialNumber' => '123abc',
]);
}
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testUpdateUserGroup(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
ClientFactory::createOne(['name' => self::CLIENT_UPDATE, 'serialNumber' => '123abc']);
$iri = $this->findIriBy(Client::class, ['name' => self::CLIENT_UPDATE]);
$this->createClientWithCredentials()->request('PUT', $iri, ['json' => [
'name' => self::CLIENT_UPDATE,
'serialNumber' => '987zyx'
]]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'@id' => $iri,
'name' => self::CLIENT_UPDATE,
'serialNumber' => '987zyx'
]);
}
}