ogcore/tests/Functional/ImageTest.php

141 lines
5.2 KiB
PHP

<?php
namespace Functional;
use App\Entity\Client;
use App\Entity\Image;
use App\Entity\OrganizationalUnit;
use App\Entity\SoftwareProfile;
use App\Factory\ClientFactory;
use App\Factory\ImageFactory;
use App\Factory\OrganizationalUnitFactory;
use App\Factory\SoftwareProfileFactory;
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 ImageTest extends AbstractTest
{
CONST string USER_ADMIN = 'ogadmin';
CONST string IMAGE_CREATE = 'test-image-create';
CONST string IMAGE_UPDATE = 'test-image-update';
CONST string IMAGE_DELETE = 'test-image-delete';
const string CLIENT = 'test-client';
const string SOFTWARE_PROFILE = 'test-software-profile';
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testGetCollectionImages(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
ImageFactory::createMany(10);
$this->createClientWithCredentials()->request('GET', '/images');
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/contexts/Image',
'@id' => '/images',
'@type' => 'hydra:Collection',
'hydra:totalItems' => 10,
]);
}
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testCreateImage(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
ClientFactory::createOne(['name' => self::CLIENT]);
$clientIri = $this->findIriBy(Client::class, ['name' => self::CLIENT]);
SoftwareProfileFactory::createOne(['description' => self::SOFTWARE_PROFILE]);
$swPIri = $this->findIriBy(SoftwareProfile::class, ['description' => self::SOFTWARE_PROFILE]);
OrganizationalUnitFactory::createOne(['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]);
$ouIri = $this->findIriBy(OrganizationalUnit::class, ['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]);
$this->createClientWithCredentials()->request('POST', '/images',['json' => [
'name' => self::IMAGE_CREATE,
'size' => 123,
'path' => '/path/to/image',
'client' => $clientIri,
'softwareProfile' => $swPIri,
'organizationalUnit' => $ouIri,
]]);
$this->assertResponseStatusCodeSame(201);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/contexts/ImageOutput',
'@type' => 'Image',
'name' => self::IMAGE_CREATE,
]);
}
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testUpdateImage(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
ImageFactory::createOne(['name' => self::IMAGE_CREATE]);
$iri = $this->findIriBy(Image::class, ['name' => self::IMAGE_CREATE]);
$this->createClientWithCredentials()->request('PUT', $iri, ['json' => [
'name' => self::IMAGE_UPDATE,
'size' => 123
]]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'@id' => $iri,
'name' => self::IMAGE_UPDATE,
'size' => 123
]);
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
*/
public function testDeleteImage(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
ImageFactory::createOne(['name' => self::IMAGE_DELETE]);
$iri = $this->findIriBy(Image::class, ['name' => self::IMAGE_DELETE]);
$this->createClientWithCredentials()->request('DELETE', $iri);
$this->assertResponseStatusCodeSame(204);
$this->assertNull(
static::getContainer()->get('doctrine')->getRepository(Client::class)->findOneBy(['name' => self::IMAGE_DELETE])
);
}
}