function it_gets_identity_from_entity(Definition\Repository $definitions, Definition\IdentificationStrategy $identificationStrategy) { $entity = new EntityFake(1); $identificationStrategy->getIdentity($entity)->willReturn(1); $definitions->hasDefinition($entity)->willReturn(true); $definitions->getDefinition($entity)->willReturn(new Definition(new ClassName(EntityFake::getClassName()), new Definition\Identity("id"), $identificationStrategy->getWrappedObject())); $this->getIdentity($entity)->shouldReturn(1); }
function it_dispatch_handler_for_remove_command_if_exists(Definition\Repository $definitions, RemoveCommandHandler $handler) { $definition = $this->createEntityDefinition(); $definition->setRemoveCommandHandler($handler->getWrappedObject()); $entity = new EntityFake(1); $command = new RemoveCommand($entity); $definitions->getDefinition($entity)->willReturn($definition); $handler->handle($command)->willReturn(true); $this->dispatch($command)->shouldReturn(true); }
function let(Definition\Repository $definitions, Identifier $identifier) { $definitions->getDefinition(Argument::type(EntityFake::getClassName()))->willReturn($this->createEntityDefinition()); $definitions->getDefinition(Argument::type(AssociatedEntityFake::getClassName()))->willReturn($this->createAssociatedEntityDefinition()); $identifier->isPersisted(Argument::any())->will(function ($args) { return !is_null($args[0]->getId()); }); $identifier->getIdentity(Argument::any())->will(function ($args) { return $args[0]->getId(); }); $this->beConstructedWith($definitions, $identifier); }
/** * @param $oldEntity * @param $newEntity * @return ChangeSet * @throws RuntimeException * * @api */ public function buildChanges($oldEntity, $newEntity) { $changes = []; $entityDefinition = $this->definitions->getDefinition($oldEntity); foreach ($entityDefinition->getObservedProperties() as $property) { if ($this->isDifferent($property, $oldEntity, $newEntity)) { $oldValue = $this->propertyAccessor->getValue($oldEntity, $property->getName()); $newValue = $this->propertyAccessor->getValue($newEntity, $property->getName()); $changes[] = $this->buildChange($property, $oldValue, $newValue); } } return new ChangeSet($changes); }
/** * @param $firstEntity * @param $secondEntity * @return bool * @throws InvalidArgumentException */ public function areEqual($firstEntity, $secondEntity) { $entityDefinition = $this->definitions->getDefinition($firstEntity); if (!$entityDefinition->fitsFor($secondEntity)) { throw new InvalidArgumentException("You can't compare entities of different type."); } foreach ($entityDefinition->getObservedProperties() as $property) { if ($this->propertyValueComparer->hasDifferentValue($property, $firstEntity, $secondEntity)) { return false; } } return true; }
/** * @param Command $command */ public function dispatch(Command $command) { $definition = $this->definitions->getDefinition($command->getEntity()); if ($command instanceof NewCommand && $definition->hasNewCommandHandler()) { return $definition->getNewCommandHandler()->handle($command); } if ($command instanceof EditCommand && $definition->hasEditCommandHandler()) { return $definition->getEditCommandHandler()->handle($command); } if ($command instanceof RemoveCommand && $definition->hasRemoveCommandHandler()) { return $definition->getRemoveCommandHandler()->handle($command); } }
function let(Registry $registry, Identifier $identifier, Definition\Repository $definitions, Comparer $comparer, CommandBus $commandBus) { $this->beConstructedWith($registry, $identifier, new ChangeBuilder($definitions->getWrappedObject(), $identifier->getWrappedObject()), $comparer, $commandBus); }
/** * @param $entity * @throws RuntimeException */ private function validateEntity($entity) { if (!$this->definitions->hasDefinition($entity)) { throw new RuntimeException(sprintf("Class \"%s\" does not have definition.", get_class($entity))); } }
function let(Definition\Repository $definitions) { $definitions->getDefinition(Argument::type(EntityFake::getClassName()))->willReturn($this->createEntityDefinition()); $this->beConstructedWith($definitions); }