fix some tests

This commit is contained in:
Julien Fastré 2024-09-04 18:30:18 +02:00
parent 2dd275a074
commit 3c9ee41b3b
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
5 changed files with 55 additions and 27 deletions

View File

@ -122,7 +122,7 @@ unit_tests:
- php tests/console chill:db:sync-views --env=test
- php -d memory_limit=2G tests/console cache:clear --env=test
- php -d memory_limit=3G tests/console doctrine:fixtures:load -n --env=test
- php -d memory_limit=4G bin/phpunit --colors=never --exclude-group dbIntensive --exclude-group openstack-integration
- php -d memory_limit=4G bin/phpunit --colors=never --exclude-group dbIntensive,openstack-integration
artifacts:
expire_in: 1 day
paths:

View File

@ -19,6 +19,7 @@ use Chill\DocGeneratorBundle\Service\Generator\Generator;
use Chill\DocGeneratorBundle\Service\Generator\ObjectReadyException;
use Chill\DocGeneratorBundle\Service\Generator\RelatedEntityNotFoundException;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Entity\StoredObjectVersion;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Chill\MainBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
@ -39,11 +40,11 @@ class GeneratorTest extends TestCase
public function testSuccessfulGeneration(): void
{
$template = (new DocGeneratorTemplate())->setFile($templateStoredObject = (new StoredObject())
->setType('application/test'));
$templateStoredObject = new StoredObject();
$templateStoredObject->registerVersion(type: 'application/test');
$template = (new DocGeneratorTemplate())->setFile($templateStoredObject);
$destinationStoredObject = (new StoredObject())->setStatus(StoredObject::STATUS_PENDING);
$reflection = new \ReflectionClass($destinationStoredObject);
$reflection->getProperty('id')->setAccessible(true);
$reflection->getProperty('id')->setValue($destinationStoredObject, 1);
$entity = new class () {};
$data = [];
@ -76,7 +77,14 @@ class GeneratorTest extends TestCase
$storedObjectManager = $this->prophesize(StoredObjectManagerInterface::class);
$storedObjectManager->read($templateStoredObject)->willReturn('template');
$storedObjectManager->write($destinationStoredObject, 'generated')->shouldBeCalled();
$storedObjectManager->write($destinationStoredObject, 'generated', 'application/test')
->will(function ($args): StoredObjectVersion {
/** @var StoredObject $storedObject */
$storedObject = $args[0];
return $storedObject->registerVersion(type: $args[2]);
})
->shouldBeCalled();
$generator = new Generator(
$contextManagerInterface->reveal(),
@ -107,8 +115,9 @@ class GeneratorTest extends TestCase
$this->prophesize(StoredObjectManagerInterface::class)->reveal()
);
$template = (new DocGeneratorTemplate())->setFile($templateStoredObject = (new StoredObject())
->setType('application/test'));
$templateStoredObject = new StoredObject();
$templateStoredObject->registerVersion(type: 'application/test');
$template = (new DocGeneratorTemplate())->setFile($templateStoredObject);
$destinationStoredObject = (new StoredObject())->setStatus(StoredObject::STATUS_READY);
$generator->generateDocFromTemplate(
@ -124,11 +133,11 @@ class GeneratorTest extends TestCase
{
$this->expectException(RelatedEntityNotFoundException::class);
$template = (new DocGeneratorTemplate())->setFile($templateStoredObject = (new StoredObject())
->setType('application/test'));
$templateStoredObject = new StoredObject();
$templateStoredObject->registerVersion(type: 'application/test');
$template = (new DocGeneratorTemplate())->setFile($templateStoredObject);
$destinationStoredObject = (new StoredObject())->setStatus(StoredObject::STATUS_PENDING);
$reflection = new \ReflectionClass($destinationStoredObject);
$reflection->getProperty('id')->setAccessible(true);
$reflection->getProperty('id')->setValue($destinationStoredObject, 1);
$context = $this->prophesize(DocGeneratorContextInterface::class);

View File

@ -261,6 +261,11 @@ class StoredObject implements Document, TrackCreationInterface
return $this->versions;
}
public function hasCurrentVersion(): bool
{
return null !== $this->getCurrentVersion();
}
public function hasTemplate(): bool
{
return null !== $this->template;

View File

@ -16,6 +16,7 @@ use Chill\DocStoreBundle\AsyncUpload\SignedUrlPost;
use Chill\DocStoreBundle\AsyncUpload\TempUrlGeneratorInterface;
use Chill\DocStoreBundle\Controller\AsyncUploadController;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\Entity\User;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
@ -61,6 +62,7 @@ class AsyncUploadControllerTest extends TestCase
$storedObject->registerVersion();
$security = $this->prophesize(Security::class);
$security->isGranted('SEE', $storedObject)->willReturn(true)->shouldBeCalled();
$security->getUser()->willReturn(new User());
$controller = new AsyncUploadController(
$this->buildTempUrlGenerator(),
@ -105,6 +107,7 @@ class AsyncUploadControllerTest extends TestCase
$security = $this->prophesize(Security::class);
$security->isGranted('SEE', $storedObject)->willReturn(true)->shouldBeCalled();
$security->getUser()->willReturn(new User());
$controller = new AsyncUploadController(
$this->buildTempUrlGenerator(),
@ -136,6 +139,7 @@ class AsyncUploadControllerTest extends TestCase
$security = $this->prophesize(Security::class);
$security->isGranted('SEE', $storedObject)->willReturn(true)->shouldBeCalled();
$security->getUser()->willReturn(new User());
$controller = new AsyncUploadController(
$this->buildTempUrlGenerator(),

View File

@ -11,15 +11,13 @@ declare(strict_types=1);
namespace Chill\DocStoreBundle\Tests\Validator\Constraints;
use Chill\DocStoreBundle\AsyncUpload\SignedUrl;
use Chill\DocStoreBundle\AsyncUpload\TempUrlGeneratorInterface;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Entity\StoredObjectVersion;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Chill\DocStoreBundle\Validator\Constraints\AsyncFileExists;
use Chill\DocStoreBundle\Validator\Constraints\AsyncFileExistsValidator;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
/**
@ -33,35 +31,47 @@ class AsyncFileExistsValidatorTest extends ConstraintValidatorTestCase
protected function createValidator()
{
$client = new MockHttpClient(function ($method, $url, $options): MockResponse {
if (str_contains((string) $url, '404')) {
return new MockResponse('', ['http_code' => 404]);
}
$storedObjectManager = $this->prophesize(StoredObjectManagerInterface::class);
$storedObjectManager->exists(Argument::any())
->will(function ($args): bool {
/** @var StoredObject|StoredObjectVersion $arg0 */
$arg0 = $args[0];
return new MockResponse('', ['http_code' => 200]);
});
if ($arg0 instanceof StoredObjectVersion) {
$storedObject = $arg0->getStoredObject();
} else {
$storedObject = $arg0;
}
$generator = $this->prophesize(TempUrlGeneratorInterface::class);
$generator->generate(Argument::in(['GET', 'HEAD']), Argument::type('string'), Argument::any())
->will(fn (array $args): SignedUrl => new SignedUrl($args[0], 'https://object.store.example/container/'.$args[1], new \DateTimeImmutable('1 hours'), $args[1]));
if ('present' === $storedObject->getCurrentVersion()->getFilename()) {
return true;
}
return new AsyncFileExistsValidator($generator->reveal(), $client);
return false;
});
return new AsyncFileExistsValidator($storedObjectManager->reveal());
}
public function testWhenFileExistsIsValid(): void
{
$this->validator->validate((new StoredObject())->setFilename('present'), new AsyncFileExists());
$object = new StoredObject();
$object->registerVersion(filename: 'present');
$this->validator->validate($object, new AsyncFileExists());
$this->assertNoViolation();
}
public function testWhenFileIsNotPresent(): void
{
$object = new StoredObject();
$object->registerVersion(filename: 'absent');
$this->validator->validate(
(new StoredObject())->setFilename('is_404'),
$object,
new AsyncFileExists(['message' => 'my_message'])
);
$this->buildViolation('my_message')->setParameter('{{ filename }}', 'is_404')->assertRaised();
$this->buildViolation('my_message')->setParameter('{{ filename }}', 'absent')->assertRaised();
}
}