From 3c9ee41b3b2ee3a0864f61278d8ac9be706b35eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 4 Sep 2024 18:30:18 +0200 Subject: [PATCH] fix some tests --- .gitlab-ci.yml | 2 +- .../Context/Generator/GeneratorTest.php | 27 ++++++++---- .../Entity/StoredObject.php | 5 +++ .../Controller/AsyncUploadControllerTest.php | 4 ++ .../AsyncFileExistsValidatorTest.php | 44 ++++++++++++------- 5 files changed, 55 insertions(+), 27 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e493e6556..acd66a42e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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: diff --git a/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/Generator/GeneratorTest.php b/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/Generator/GeneratorTest.php index 0bb274228..ea21f09e5 100644 --- a/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/Generator/GeneratorTest.php +++ b/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/Generator/GeneratorTest.php @@ -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); diff --git a/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php b/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php index 271c7cd32..6efc3fb9c 100644 --- a/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php +++ b/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php @@ -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; diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Controller/AsyncUploadControllerTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Controller/AsyncUploadControllerTest.php index 489bf1185..c26d22d6a 100644 --- a/src/Bundle/ChillDocStoreBundle/Tests/Controller/AsyncUploadControllerTest.php +++ b/src/Bundle/ChillDocStoreBundle/Tests/Controller/AsyncUploadControllerTest.php @@ -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(), diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Validator/Constraints/AsyncFileExistsValidatorTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Validator/Constraints/AsyncFileExistsValidatorTest.php index ac0f4a6e7..77f6d872e 100644 --- a/src/Bundle/ChillDocStoreBundle/Tests/Validator/Constraints/AsyncFileExistsValidatorTest.php +++ b/src/Bundle/ChillDocStoreBundle/Tests/Validator/Constraints/AsyncFileExistsValidatorTest.php @@ -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(); } }