mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-01 20:43:49 +00:00
Enhance behaviour of duplicating storedObject to keep only the last "kept before conversion" version if any
Enhance the duplication service to selectively handle versions tagged with "KEEP_BEFORE_CONVERSION". Modify StoredObject to support retrieval and checking of such versions. Add relevant test cases to validate this behavior.
This commit is contained in:
@@ -12,6 +12,8 @@ declare(strict_types=1);
|
||||
namespace Chill\DocStoreBundle\Tests\Entity;
|
||||
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\DocStoreBundle\Entity\StoredObjectPointInTime;
|
||||
use Chill\DocStoreBundle\Entity\StoredObjectPointInTimeReasonEnum;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
@@ -54,4 +56,27 @@ class StoredObjectTest extends KernelTestCase
|
||||
|
||||
self::assertNotSame($firstVersion, $version);
|
||||
}
|
||||
|
||||
public function testHasKeptBeforeConversionVersion(): void
|
||||
{
|
||||
$storedObject = new StoredObject();
|
||||
$version1 = $storedObject->registerVersion();
|
||||
|
||||
self::assertFalse($storedObject->hasKeptBeforeConversionVersion());
|
||||
|
||||
// add a point in time without the correct version
|
||||
new StoredObjectPointInTime($version1, StoredObjectPointInTimeReasonEnum::KEEP_BY_USER);
|
||||
|
||||
self::assertFalse($storedObject->hasKeptBeforeConversionVersion());
|
||||
self::assertNull($storedObject->getLastKeptBeforeConversionVersion());
|
||||
|
||||
new StoredObjectPointInTime($version1, StoredObjectPointInTimeReasonEnum::KEEP_BEFORE_CONVERSION);
|
||||
|
||||
self::assertTrue($storedObject->hasKeptBeforeConversionVersion());
|
||||
// add a second version
|
||||
$version2 = $storedObject->registerVersion();
|
||||
new StoredObjectPointInTime($version2, StoredObjectPointInTimeReasonEnum::KEEP_BEFORE_CONVERSION);
|
||||
|
||||
self::assertSame($version2, $storedObject->getLastKeptBeforeConversionVersion());
|
||||
}
|
||||
}
|
||||
|
@@ -12,9 +12,13 @@ declare(strict_types=1);
|
||||
namespace Chill\DocStoreBundle\Tests\Service;
|
||||
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\DocStoreBundle\Entity\StoredObjectPointInTime;
|
||||
use Chill\DocStoreBundle\Entity\StoredObjectPointInTimeReasonEnum;
|
||||
use Chill\DocStoreBundle\Service\StoredObjectDuplicate;
|
||||
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
/**
|
||||
@@ -24,9 +28,13 @@ use Psr\Log\NullLogger;
|
||||
*/
|
||||
class StoredObjectDuplicateTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
public function testDuplicateHappyScenario(): void
|
||||
{
|
||||
$storedObject = new StoredObject();
|
||||
// we create multiple version, we want the last to be duplicated
|
||||
$storedObject->registerVersion(type: 'application/test');
|
||||
$version = $storedObject->registerVersion(type: $type = 'application/test');
|
||||
|
||||
$manager = $this->createMock(StoredObjectManagerInterface::class);
|
||||
@@ -45,4 +53,78 @@ class StoredObjectDuplicateTest extends TestCase
|
||||
self::assertNotNull($actual->getCurrentVersion()->getCreatedFrom());
|
||||
self::assertSame($version, $actual->getCurrentVersion()->getCreatedFrom());
|
||||
}
|
||||
|
||||
public function testDuplicateWithKeptVersion(): void
|
||||
{
|
||||
$storedObject = new StoredObject();
|
||||
// we create two versions for stored object
|
||||
// the first one is "kept before conversion", and that one should
|
||||
// be duplicated, not the second one
|
||||
$version1 = $storedObject->registerVersion(type: $type = 'application/test');
|
||||
new StoredObjectPointInTime($version1, StoredObjectPointInTimeReasonEnum::KEEP_BEFORE_CONVERSION);
|
||||
$version2 = $storedObject->registerVersion(type: $type = 'application/test');
|
||||
|
||||
$manager = $this->prophesize(StoredObjectManagerInterface::class);
|
||||
|
||||
// we create both possibilities for the method "read"
|
||||
$manager->read($version1)->willReturn('1234');
|
||||
$manager->read($version2)->willReturn('4567');
|
||||
|
||||
// we create the write method, and check that it is called with the content from version1, not version2
|
||||
$manager->write(Argument::type(StoredObject::class), '1234', 'application/test')
|
||||
->shouldBeCalled()
|
||||
->will(function ($args) {
|
||||
/** @var StoredObject $storedObject */
|
||||
$storedObject = $args[0]; // args are ordered by key, so the first one is the stored object...
|
||||
$type = $args[2]; // and the last one is the string $type
|
||||
|
||||
return $storedObject->registerVersion(type: $type);
|
||||
});
|
||||
|
||||
// we create the service which will duplicate things
|
||||
$storedObjectDuplicate = new StoredObjectDuplicate($manager->reveal(), new NullLogger());
|
||||
|
||||
$actual = $storedObjectDuplicate->duplicate($storedObject);
|
||||
|
||||
self::assertNotNull($actual->getCurrentVersion());
|
||||
self::assertNotNull($actual->getCurrentVersion()->getCreatedFrom());
|
||||
self::assertSame($version1, $actual->getCurrentVersion()->getCreatedFrom());
|
||||
}
|
||||
|
||||
public function testDuplicateWithKeptVersionButWeWantToDuplicateTheLastOne(): void
|
||||
{
|
||||
$storedObject = new StoredObject();
|
||||
// we create two versions for stored object
|
||||
// the first one is "kept before conversion", and that one should
|
||||
// be duplicated, not the second one
|
||||
$version1 = $storedObject->registerVersion(type: $type = 'application/test');
|
||||
new StoredObjectPointInTime($version1, StoredObjectPointInTimeReasonEnum::KEEP_BEFORE_CONVERSION);
|
||||
$version2 = $storedObject->registerVersion(type: $type = 'application/test');
|
||||
|
||||
$manager = $this->prophesize(StoredObjectManagerInterface::class);
|
||||
|
||||
// we create both possibilities for the method "read"
|
||||
$manager->read($version1)->willReturn('1234');
|
||||
$manager->read($version2)->willReturn('4567');
|
||||
|
||||
// we create the write method, and check that it is called with the content from version1, not version2
|
||||
$manager->write(Argument::type(StoredObject::class), '4567', 'application/test')
|
||||
->shouldBeCalled()
|
||||
->will(function ($args) {
|
||||
/** @var StoredObject $storedObject */
|
||||
$storedObject = $args[0]; // args are ordered by key, so the first one is the stored object...
|
||||
$type = $args[2]; // and the last one is the string $type
|
||||
|
||||
return $storedObject->registerVersion(type: $type);
|
||||
});
|
||||
|
||||
// we create the service which will duplicate things
|
||||
$storedObjectDuplicate = new StoredObjectDuplicate($manager->reveal(), new NullLogger());
|
||||
|
||||
$actual = $storedObjectDuplicate->duplicate($storedObject, false);
|
||||
|
||||
self::assertNotNull($actual->getCurrentVersion());
|
||||
self::assertNotNull($actual->getCurrentVersion()->getCreatedFrom());
|
||||
self::assertSame($version2, $actual->getCurrentVersion()->getCreatedFrom());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user