/** * Build a pending version * * @param Version $pending * @param Version|null $previousVersion * * @return Version */ public function buildPendingVersion(Version $pending, Version $previousVersion = null) { $versionNumber = $previousVersion ? $previousVersion->getVersion() + 1 : 1; $oldSnapshot = $previousVersion ? $previousVersion->getSnapshot() : []; $modification = $pending->getChangeset(); $snapshot = $modification + $oldSnapshot; $changeset = $this->buildChangeset($oldSnapshot, $snapshot); $pending->setVersion($versionNumber)->setSnapshot($snapshot)->setChangeset($changeset); return $pending; }
function it_builds_pending_versions_for_a_given_entity(ProductInterface $product, $builder, $versionRepository) { $product->getId()->willReturn(1); $pending1 = new Version('Product', 1, 'julia'); $pending1->setChangeset(['foo' => 'bar']); $pending2 = new Version('Product', 1, 'julia'); $pending2->setChangeset(['foo' => 'fubar']); $versionRepository->findBy(Argument::cetera())->willReturn([$pending1, $pending2]); $builder->buildPendingVersion($pending1, null)->willReturn($pending1)->shouldBeCalled(); $builder->buildPendingVersion($pending2, $pending1)->willReturn($pending2)->shouldBeCalled(); $versions = $this->buildPendingVersions($product); $versions->shouldHaveCount(2); }
function it_applies_on_timestampable_versioned_object_with_a_document_manager($registry, LifecycleEventArgs $args, DocumentManager $om, ODMUnitOfWork $uow, Version $version, TimestampableInterface $object, ODMClassMetadata $metadata) { $registry->getManagerForClass('bar')->willReturn($om); $om->getClassMetadata('bar')->willReturn($metadata); $metadata->getReflectionClass()->willReturn(new \ReflectionClass('Pim\\Component\\Catalog\\Model\\TimestampableInterface')); $version->getResourceId()->willReturn('foo'); $version->getResourceName()->willReturn('bar'); $version->getLoggedAt()->willReturn('foobar'); $args->getObject()->willReturn($version); $om->getUnitOfWork()->willReturn($uow); $om->find('bar', 'foo')->willReturn($object); $uow->computeChangeSet($metadata, $object)->shouldBeCalled(); $object->setUpdated('foobar')->shouldBeCalled(); $this->prePersist($args); }
/** * Compute version change set * * @param Version $version */ protected function computeChangeSet(Version $version) { $om = $this->container->get('pim_versioning.manager.version')->getObjectManager(); if ($version->getChangeset()) { $om->persist($version); $om->getUnitOfWork()->computeChangeSet($om->getClassMetadata(ClassUtils::getClass($version)), $version); } else { $om->remove($version); } }
/** * Build a pending version * * @param Version $pending * @param Version|null $previousVersion * * @return Version */ public function buildPendingVersion(Version $pending, Version $previousVersion = null) { if (null === $previousVersion) { $previousVersion = $this->getVersionRepository()->getNewestLogEntry($pending->getResourceName(), $pending->getResourceId(), false); } return $this->versionBuilder->buildPendingVersion($pending, $previousVersion); }
function it_normalize_versions_with_deleted_user($userManager, $translator, $datetimePresenter, Version $version) { $versionTime = new \DateTime(); $version->getId()->willReturn(12); $version->getResourceId()->willReturn(112); $version->getSnapshot()->willReturn('a nice snapshot'); $version->getChangeset()->willReturn(['text' => 'the changeset']); $version->getContext()->willReturn(['locale' => 'en_US', 'channel' => 'mobile']); $version->getVersion()->willReturn(12); $version->getLoggedAt()->willReturn($versionTime); $translator->getLocale()->willReturn('en_US'); $datetimePresenter->present($versionTime, Argument::any())->willReturn('01/01/1985 09:41 AM'); $version->isPending()->willReturn(false); $version->getAuthor()->willReturn('steve'); $userManager->findUserByUsername('steve')->willReturn(null); $translator->trans('Removed user')->willReturn('Utilisateur supprimé'); $this->normalize($version, 'internal_api')->shouldReturn(['id' => 12, 'author' => 'steve - Utilisateur supprimé', 'resource_id' => '112', 'snapshot' => 'a nice snapshot', 'changeset' => ['text' => 'the changeset'], 'context' => ['locale' => 'en_US', 'channel' => 'mobile'], 'version' => 12, 'logged_at' => '01/01/1985 09:41 AM', 'pending' => false]); }
function it_normalize_versions_with_deleted_user($userManager, $translator, Version $version, \DateTime $versionTime) { $version->getId()->willReturn(12); $version->getResourceId()->willReturn(112); $version->getSnapshot()->willReturn('a nice snapshot'); $version->getChangeset()->willReturn('the changeset'); $version->getContext()->willReturn(['locale' => 'en_US', 'channel' => 'mobile']); $version->getVersion()->willReturn(12); $version->getLoggedAt()->willReturn($versionTime); $versionTime->format('Y-m-d H:i:s')->willReturn('1985-10-1 09:41:00'); $version->isPending()->willReturn(false); $version->getAuthor()->willReturn('steve'); $userManager->findUserByUsername('steve')->willReturn(null); $translator->trans('Removed user')->willReturn('Utilisateur supprimé'); $this->normalize($version, 'internal_api')->shouldReturn(['id' => 12, 'author' => 'steve - Utilisateur supprimé', 'resource_id' => '112', 'snapshot' => 'a nice snapshot', 'changeset' => 'the changeset', 'context' => ['locale' => 'en_US', 'channel' => 'mobile'], 'version' => 12, 'logged_at' => '1985-10-1 09:41:00', 'pending' => false]); }
/** * Compute version change set * * @param Version $version */ protected function applyChangeSet(Version $version) { $om = $this->versionManager->getObjectManager(); if ($version->getChangeset()) { $om->persist($version); $om->getUnitOfWork()->computeChangeSet($om->getClassMetadata(get_class($version)), $version); $om->flush($version); } else { $om->remove($version); } }