public function test() { $readConnection = new Connection('dsn'); $writeConnection = new Connection('dsn'); $cacheManager = new CacheManager(); $connectionManager = new ConnectionManager(); $connectionManager->setReadConnection($readConnection); $connectionManager->setWriteConnection($writeConnection); $connectionManager->setCacheManager($cacheManager); $table = new Table('users', 'id'); $table->setConnectionManager($connectionManager); $select = $table->select('name'); $this->assertSame($readConnection, $select->getConnection()); $this->assertSame($cacheManager, $select->getCacheManager()); $this->assertSame("SELECT users.name FROM users", $select->toString()); $select = $table->selectById(1, 'name'); $this->assertSame("SELECT users.name FROM users WHERE (users.id = 1)", $select->toString()); $insert = $table->insert(['name' => 'foo']); $this->assertSame($writeConnection, $insert->getConnection()); $this->assertSame("INSERT INTO users SET name = 'foo'", $insert->toString()); $update = $table->update(['name' => 'foo']); $this->assertSame($writeConnection, $update->getConnection()); $this->assertSame("UPDATE users SET name = 'foo'", $update->toString()); $delete = $table->delete('id', 1); $this->assertSame($writeConnection, $delete->getConnection()); $this->assertSame("DELETE FROM users WHERE (id = 1)", $delete->toString()); }
public function testRunWithReadAndWriteConnection() { $readSql = $this->getMock('Sloths\\Db\\Sql\\SqlReadInterface'); $readSql->expects($this->once())->method('toString')->willReturn('read sql'); $writeSql = $this->getMock('Sloths\\Db\\Sql\\SqlWriteInterface'); $writeSql->expects($this->once())->method('toString')->willReturn('write sql'); $readConnection = $this->getMock('Sloths\\Db\\Connection', ['query'], ['dsn']); $readConnection->expects($this->once())->method('query')->with('read sql'); $writeConnection = $this->getMock('Sloths\\Db\\Connection', ['exec'], ['dsn']); $writeConnection->expects($this->once())->method('exec')->with('write sql'); $connectionManager = new ConnectionManager(); $connectionManager->setReadConnection($readConnection); $connectionManager->setWriteConnection($writeConnection); $connectionManager->run($readSql); $connectionManager->run($writeSql); }
public function testSaveInsert() { $connection = $this->getMock('Sloths\\Db\\Connection', ['getLastInsertId'], ['dsn']); $connection->expects($this->once())->method('getLastInsertId')->willReturn(1); $connectionManager = new ConnectionManager(); $connectionManager->setWriteConnection($connection); $insert = $this->getMock('insert', ['run']); $insert->expects($this->once())->method('run'); $table = $this->getMock('table', ['insert']); $table->expects($this->once())->method('insert')->with(['name' => 'foo'])->willReturn($insert); $model = $this->getMock('Sloths\\Db\\Model\\AbstractModel', ['getDataForSave', 'table']); $model->expects($this->once())->method('getDataForSave')->willReturn(['name' => 'foo']); $model->expects($this->once())->method('table')->willReturn($table); $model->setConnectionManager($connectionManager); $model->save(); $this->assertSame(1, $model->id()); }