public function setUp() { while (CurrentUnitOfWork::isStarted()) { CurrentUnitOfWork::get()->rollback(); } $this->event1 = new GenericEventMessage(new TestMessage(1)); $this->event2 = new GenericEventMessage(new TestMessage(1)); $this->testSubject = new DefaultUnitOfWork(); $this->mockEventBus = \Phake::mock(EventBusInterface::class); $this->mockAggregateRoot = \Phake::mock(AggregateRootInterface::class); $this->listener1 = \Phake::mock(EventListenerInterface::class); $this->listener2 = \Phake::mock(EventListenerInterface::class); $this->callback1 = \Phake::mock(SaveAggregateCallbackInterface::class); $this->callback2 = \Phake::mock(SaveAggregateCallbackInterface::class); $self = $this; \Phake::when($this->callback1)->save($this->mockAggregateRoot)->thenGetReturnByLambda(function ($aggregate) use($self) { CurrentUnitOfWork::get()->publishEvent($self->event1, $self->mockEventBus); CurrentUnitOfWork::get()->publishEvent($self->event2, $self->mockEventBus); }); \Phake::when($this->mockEventBus)->publish(\Phake::anyParameters())->thenGetReturnByLambda(function ($event) use($self) { $self->listener1->handle($event[0]); $self->listener2->handle($event[0]); $self->listener1->handle($event[1]); $self->listener2->handle($event[1]); }); }
/** * @CommandHandler */ public function handleStrangeCommand(StrangeCommand $testCommand) { $aggregate = $this->repository->load($testCommand->getAggregateIdentifier(), null); $aggregate->doSomething(); $this->eventBus->publish(new GenericEventMessage(new MyApplicationEvent())); CurrentUnitOfWork::get()->publishEvent(new GenericEventMessage(new MyApplicationEvent()), $this->eventBus); throw new StrangeCommandReceivedException("Strange command received"); }
public function testSetSession() { $mockUnitOfWork = $this->getMock(UnitOfWorkInterface::class); CurrentUnitOfWork::set($mockUnitOfWork); $this->assertSame($mockUnitOfWork, CurrentUnitOfWork::get()); CurrentUnitOfWork::clear($mockUnitOfWork); $this->assertFalse(CurrentUnitOfWork::isStarted()); }
public function add(AggregateRootInterface $aggregateRoot) { if (null !== $aggregateRoot->getVersion()) { throw new \InvalidArgumentException("Only newly created (unpersisted) aggregates may be added."); } if (!$this->supportsClass(get_class($aggregateRoot))) { throw new \InvalidArgumentException(sprintf("This repository supports %s, but got %s", $this->className, get_class($aggregateRoot))); } CurrentUnitOfWork::get()->registerAggregate($aggregateRoot, $this->eventBus, $this->saveAggregateCallback); }
public function setUp() { $this->mockEventBus = $this->getMock(EventBusInterface::class); $this->lockManager = $this->getMock(NullLockManager::class); // new NullLockManager(); //spy(new OptimisticLockManager()); $this->lockManager->expects($this->any())->method('validateLock')->will($this->returnValue(true)); $this->testSubject = new InMemoryLockingRepository(StubAggregate::class, $this->mockEventBus, $this->lockManager); //testSubject = spy(testSubject); // some UoW is started somewhere, but not shutdown in the same test. while (CurrentUnitOfWork::isStarted()) { CurrentUnitOfWork::get()->rollback(); } }
public function publish(array $events) { if (null === $this->connection) { throw new \RuntimeException("The AMQPTerminal has no connection configured."); } $channel = $this->connection->channel(); if ($this->isTransactional) { $channel->tx_select(); } try { if ($this->waitForAck) { $channel->confirm_select(); } foreach ($events as $event) { $amqpMessage = $this->messageConverter->createAmqpMessage($event); $this->doSendMessage($channel, $amqpMessage); } if (CurrentUnitOfWork::isStarted()) { CurrentUnitOfWork::get()->registerListener(new ChannelTransactionUnitOfWorkListener($this->logger, $channel, $this)); } elseif ($this->isTransactional) { $channel->tx_commit(); } elseif ($this->waitForAck) { $channel->wait_for_pending_acks($this->publisherAckTimeout); } } catch (\Exception $ex) { if ($this->isTransactional) { $this->tryRollback($channel); } throw new EventPublicationFailedException("Failed to dispatch Events to the Message Broker.", 0, $ex); } finally { if (!CurrentUnitOfWork::isStarted()) { $this->tryClose($channel); } } }
public function testDispatchCommand_ImplicitUnitOfWorkIsRolledBackOnException() { $command = new TestCommand("Say hi!"); $test = $this; $this->handlerRegistry->subscribe(TestCommand::class, new CallbackCommandHandler(function (CommandMessageInterface $commandMessage, UnitOfWorkInterface $unitOfWork) use($test) { $test->assertTrue(CurrentUnitOfWork::isStarted()); $test->assertNotNull(CurrentUnitOfWork::get()); throw new \RuntimeException("exception"); })); $callback = new ClosureCommandCallback(function ($result) use($command) { $this->fail("Did not expect exception"); }, function ($exception) { $this->assertEquals(\RuntimeException::class, get_class($exception)); }); $this->commandBus->dispatch(GenericCommandMessage::asCommandMessage($command), $callback); $this->assertFalse(CurrentUnitOfWork::isStarted()); }
public function tearDown() { while (CurrentUnitOfWork::isStarted()) { CurrentUnitOfWork::get()->rollback(); } }
protected function doLoad($id, $expectedVersion) { try { $events = $this->eventStore->readEvents($this->getTypeIdentifier(), $id); } catch (EventStreamNotFoundException $ex) { throw new AggregateNotFoundException($id, "The aggregate was not found", $ex); } foreach ($this->eventStreamDecorators as $decorator) { $events = $decorator->decorateForRead($this->getTypeIdentifier(), $id, $events); } $aggregate = $this->factory->createAggregate($id, $events->peek()); $unseenEvents = array(); $aggregate->initializeState(new CapturingEventStream($events, $unseenEvents, $expectedVersion)); if ($aggregate->isDeleted()) { throw new AggregateDeletedException($id); } CurrentUnitOfWork::get()->registerListener(new ConflictResolvingListener($aggregate, $unseenEvents, $this->conflictResolver)); return $aggregate; }