mockConnection = $this->getMockBuilder(ReplicationInterface::class)->getMock(); $this->mockCommand = $this->getMockBuilder(CommandInterface::class)->getMock(); } /** * @return void */ public function testExecuteCommand(): void { $this->mockConnection ->expects($this->once()) ->method('executeCommand') ->with($this->mockCommand) ->willReturn('OK'); $this->mockConnection ->expects($this->any()) ->method('getParameters') ->willReturn(new Parameters()); $strategy = new ReplicationConnectionStrategy($this->mockConnection, new MultiExecState()); $this->assertEquals('OK', $strategy->executeCommand($this->mockCommand)); } /** * @return void */ public function testCommandsAfterMultiGoOnlyToTheConnectionHoldingTheTransaction(): void { $master = $this->getMockBuilder(NodeConnectionInterface::class)->getMock(); $master ->expects($this->exactly(2)) ->method('executeCommand') ->willReturnCallback(static function (CommandInterface $command) { return $command->getId() === 'EXEC' ? ['OK'] : 'QUEUED'; }); $master ->method('isConnected') ->willReturn(true); $this->mockConnection ->expects($this->once()) ->method('executeCommand') ->with($this->callback(static function (CommandInterface $command) { return $command->getId() === 'MULTI'; })) ->willReturn('OK'); // The node that has just run MULTI. $this->mockConnection ->expects($this->once()) ->method('getCurrent') ->willReturn($master); $this->mockConnection ->expects($this->any()) ->method('getParameters') ->willReturn(new Parameters()); $strategy = new ReplicationConnectionStrategy($this->mockConnection, new MultiExecState()); $this->assertTrue($strategy->initializeTransaction()); $this->assertSame('QUEUED', $strategy->executeCommand(new SET())); $this->assertSame(['OK'], $strategy->executeTransaction()); } }