/** * @test */ public function setMasterReplicaGetMasterReplica() { $config = new ConfigService(); $config->addMaster($this->_correct1); $config->addReplica($this->_correct2); $this->assertSame($this->_correct1, $config->getMaster()); $this->assertSame($this->_correct2, $config->getReplica()); }
/** * IMPORTANT: to write-then-read slave lag issues, when a master database * has already been selected, continue using it instead of creating a replica connection * * @return PDO */ public function getReplica() { if ($this->isUsingMaster()) { return $this->_master; } if (empty($this->_replica)) { $config = null; try { $config = $this->_config->getReplica(); } catch (ConfigMissingException $e) { return $this->getMaster(); } $this->_replica = $this->_buildAdapter($config); } // if !replica return $this->_replica; }
/** * @test */ public function closeOpenedConnectionMasterOverride() { $config = new ConfigService(); $config->addMaster($this->_db_config1); $config->addReplica($this->_db_config2); $connect = $this->getMockBuilder(ConnectionService::class)->setMethods(['_createConnection'])->setConstructorArgs([$config])->getMock(); $callback = function () { return new PPDO(); }; $connect->expects($this->any())->method('_createConnection')->will($this->returnCallback($callback)); // Order here is important, otherwise master will be returned in both cases $connect->getReplica(); $master = $connect->getMaster(); $replica = $connect->getReplica(); // Master override in effect $this->assertSame($master, $replica); $this->assertEquals(2, $connect->closeOpenedConnections()); $this->assertEquals(0, $connect->closeOpenedConnections()); }