From 2815d8f6945416c69ee0d95b8f3046d1f8154556 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Thu, 22 Jun 2017 23:39:13 +0200 Subject: [PATCH] Add config test --- .gitignore | 1 + lib/Config.php | 4 +-- test/ConfigTest.php | 70 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 test/ConfigTest.php diff --git a/.gitignore b/.gitignore index ae40550..210bf24 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ composer.lock phpunit.xml vendor .php_cs.cache +coverage diff --git a/lib/Config.php b/lib/Config.php index 94157de..9db9d25 100644 --- a/lib/Config.php +++ b/lib/Config.php @@ -31,8 +31,8 @@ class Config { $this->attempts = $attempts; } - private function validateNameserver(string $nameserver) { - if (!$nameserver) { + private function validateNameserver($nameserver) { + if (!$nameserver || !\is_string($nameserver)) { throw new ConfigException("Invalid nameserver: {$nameserver}"); } diff --git a/test/ConfigTest.php b/test/ConfigTest.php new file mode 100644 index 0000000..c58bda2 --- /dev/null +++ b/test/ConfigTest.php @@ -0,0 +1,70 @@ +assertInstanceOf(Config::class, new Config($nameservers)); + } + + public function provideValidServers() { + return [ + [["127.1.1.1"]], + [["127.1.1.1:1"]], + [["[::1]:52"]], + [["[::1]"]], + ]; + } + + /** + * @param string[] $nameservers Invalid server array. + * + * @dataProvider provideInvalidServers + */ + public function testRejectsInvalidServers(array $nameservers) { + $this->expectException(ConfigException::class); + new Config($nameservers); + } + + public function provideInvalidServers() { + return [ + [[]], + [[42]], + [[null]], + [[true]], + [["foobar"]], + [["foobar.com"]], + [["127.1.1"]], + [["127.1.1.1.1"]], + [["126.0.0.5", "foobar"]], + [["42"]], + [["::1"]], + [["::1:53"]], + [["[::1]:"]], + [["[::1]:76235"]], + [["[::1]:0"]], + [["[::1]:-1"]], + [["[::1:51"]], + [["[::1]:abc"]], + ]; + } + + public function testInvalidTimeout() { + $this->expectException(ConfigException::class); + new Config(["127.0.0.1"], [], -1); + } + + public function testInvalidAttempts() { + $this->expectException(ConfigException::class); + new Config(["127.0.0.1"], [], 500, 0); + } +}