getMockBuilder(Config::class) ->getMock(); /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ $app = $this->getMockBuilder(Application::class) ->setMethods(['get']) ->getMock(); $app->expects($this->once()) ->method('get') ->with('config') ->willReturn($config); $config->expects($this->atLeastOnce()) ->method('get') ->with('database') ->willReturn([ 'host' => 'localhost', 'db' => 'database', 'user' => 'user', 'pw' => 'password', ]); $serviceProvider = new DatabaseServiceProvider($app); $this->expectException(Exception::class); $serviceProvider->register(); } }