From cbac8617464214f024c12bb996e206f81ae55e91 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Sat, 17 Jun 2017 10:49:54 +0200 Subject: [PATCH] Update to PHPUnit 6, update code style --- .gitignore | 1 + .php_cs | 15 ------------- .php_cs.dist | 39 ++++++++++++++++++++++++++++++++ .travis.yml | 24 +++++++------------- Makefile | 45 +++++++++++++++++++++++++++++++++++++ appveyor.yml | 13 +++-------- composer.json | 10 +++++---- lib/DefaultResolver.php | 40 ++++++++++++++++++++------------- lib/NoRecordException.php | 3 ++- lib/Record.php | 3 ++- lib/ResolutionException.php | 3 ++- lib/Resolver.php | 2 +- lib/TimeoutException.php | 3 ++- lib/functions.php | 6 ++--- phpunit.xml.dist | 12 +++++----- test/IntegrationTest.php | 3 ++- test/ResolvConfTest.php | 5 +++-- 17 files changed, 150 insertions(+), 77 deletions(-) delete mode 100644 .php_cs create mode 100644 .php_cs.dist create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index d92a4ec..ae40550 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ build composer.lock phpunit.xml vendor +.php_cs.cache diff --git a/.php_cs b/.php_cs deleted file mode 100644 index ab38568..0000000 --- a/.php_cs +++ /dev/null @@ -1,15 +0,0 @@ -level(Symfony\CS\FixerInterface::NONE_LEVEL) - ->fixers([ - "psr2", - "-braces", - "-psr0", - ]) - ->finder( - Symfony\CS\Finder\DefaultFinder::create() - ->in(__DIR__ . "/lib") - ->in(__DIR__ . "/test") - ) -; diff --git a/.php_cs.dist b/.php_cs.dist new file mode 100644 index 0000000..3fe01f5 --- /dev/null +++ b/.php_cs.dist @@ -0,0 +1,39 @@ +setRiskyAllowed(true) + ->setRules([ + "@PSR1" => true, + "@PSR2" => true, + "braces" => [ + "allow_single_line_closure" => true, + "position_after_functions_and_oop_constructs" => "same", + ], + "array_syntax" => ["syntax" => "short"], + "cast_spaces" => true, + "combine_consecutive_unsets" => true, + "function_to_constant" => true, + "no_multiline_whitespace_before_semicolons" => true, + "no_unused_imports" => true, + "no_useless_else" => true, + "no_useless_return" => true, + "no_whitespace_before_comma_in_array" => true, + "no_whitespace_in_blank_line" => true, + "non_printable_character" => true, + "normalize_index_brace" => true, + "ordered_imports" => true, + "php_unit_construct" => true, + "php_unit_dedicate_assert" => true, + "php_unit_fqcn_annotation" => true, + "phpdoc_summary" => true, + "phpdoc_types" => true, + "psr4" => true, + "return_type_declaration" => ["space_before" => "none"], + "short_scalar_cast" => true, + "single_blank_line_before_namespace" => true, + ]) + ->setFinder( + PhpCsFixer\Finder::create() + ->in(__DIR__ . "/lib") + ->in(__DIR__ . "/test") + ); diff --git a/.travis.yml b/.travis.yml index 6b00fc2..0d5ea8e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,26 +12,18 @@ matrix: - php: nightly fast_finish: true -env: - - DEPS=lowest - - DEPS=highest - -install: - - if [ "$DEPS" = "lowest" ]; then - composer update -n --prefer-source --prefer-lowest; - else - composer update -n --prefer-source; - fi; - - composer show +before_script: + # --ignore-platform-reqs, because https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/2722 + - composer update -n --prefer-dist --ignore-platform-reqs + - composer require satooshi/php-coveralls dev-master --ignore-platform-reqs script: - phpdbg -qrr vendor/bin/phpunit --coverage-text --coverage-clover build/logs/clover.xml - - php vendor/bin/php-cs-fixer --diff --dry-run -v fix + - PHP_CS_FIXER_IGNORE_ENV=1 php vendor/bin/php-cs-fixer --diff --dry-run -v fix -after_success: - - composer require satooshi/php-coveralls dev-master - - travis_retry php vendor/bin/coveralls -v +after_script: + - php vendor/bin/coveralls -v cache: directories: - - $HOME/.composer/cache + - $HOME/.composer/cache/files diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6c3291b --- /dev/null +++ b/Makefile @@ -0,0 +1,45 @@ +PHP_BIN := php +COMPOSER_BIN := composer + +COVERAGE = coverage +SRCS = lib test + +find_php_files = $(shell find $(1) -type f -name "*.php") +src = $(foreach d,$(SRCS),$(call find_php_files,$(d))) + +.PHONY: test +test: setup phpunit code-style + +.PHONY: clean +clean: clean-coverage clean-vendor + +.PHONY: clean-coverage +clean-coverage: + test ! -e coverage || rm -r coverage + +.PHONY: clean-vendor +clean-vendor: + test ! -e vendor || rm -r vendor + +.PHONY: setup +setup: vendor/autoload.php + +.PHONY: deps-update +deps-update: + $(COMPOSER_BIN) update + +.PHONY: phpunit +phpunit: setup + $(PHP_BIN) vendor/bin/phpunit + +.PHONY: code-style +code-style: setup + PHP_CS_FIXER_IGNORE_ENV=1 $(PHP_BIN) vendor/bin/php-cs-fixer --diff -v fix + +composer.lock: composer.json + $(COMPOSER_BIN) install + touch $@ + +vendor/autoload.php: composer.lock + $(COMPOSER_BIN) install + touch $@ diff --git a/appveyor.yml b/appveyor.yml index bd9808f..9538c85 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -5,14 +5,8 @@ clone_folder: c:\projects\php-project-workspace environment: matrix: - - dependencies: lowest - php_ver_target: 7.0 - - dependencies: lowest - php_ver_target: 7.1 - - dependencies: highest - php_ver_target: 7.0 - - dependencies: highest - php_ver_target: 7.1 + - php_ver_target: 7.0 + - php_ver_target: 7.1 ## Cache composer bits cache: @@ -39,8 +33,7 @@ install: - IF %PHP%==1 echo @php %%~dp0composer.phar %%* > composer.bat - appveyor DownloadFile https://getcomposer.org/composer.phar - cd c:\projects\php-project-workspace - - IF %dependencies%==lowest appveyor-retry composer update --prefer-lowest --no-progress --profile -n - - IF %dependencies%==highest appveyor-retry composer update --no-progress --profile -n + - appveyor-retry composer update --no-progress -n - composer show ## Run the actual test diff --git a/composer.json b/composer.json index 0e58223..c6f9cd1 100644 --- a/composer.json +++ b/composer.json @@ -1,13 +1,14 @@ { "name": "amphp/dns", "homepage": "https://github.com/amphp/dns", - "description": "Async DNS resolution built on the amp concurrency framework", + "description": "Async DNS resolution for Amp.", "keywords": [ "dns", "resolve", "client", "async", - "amp" + "amp", + "amphp" ], "license": "MIT", "authors": [ @@ -39,8 +40,9 @@ "minimum-stability": "dev", "prefer-stable": true, "require-dev": { - "phpunit/phpunit": "^5.2.8", - "friendsofphp/php-cs-fixer": "^1.9" + "amphp/phpunit-util": "^1", + "phpunit/phpunit": "^6", + "friendsofphp/php-cs-fixer": "^2.3" }, "autoload": { "psr-4": { diff --git a/lib/DefaultResolver.php b/lib/DefaultResolver.php index 4a3a7e3..24656f3 100644 --- a/lib/DefaultResolver.php +++ b/lib/DefaultResolver.php @@ -2,12 +2,22 @@ namespace Amp\Dns; -use Amp\{ CallableMaker, Deferred, Failure, Loop, MultiReasonException, Promise, Success, TimeoutException, function call }; use Amp\Cache\ArrayCache; +use Amp\CallableMaker; +use Amp\Deferred; +use Amp\Failure; use Amp\File\FilesystemException; -use Amp\WindowsRegistry\{ KeyNotFoundException, WindowsRegistry }; -use LibDNS\{ Decoder\DecoderFactory, Encoder\EncoderFactory }; -use LibDNS\Messages\{ MessageFactory, MessageTypes }; +use Amp\Loop; +use Amp\MultiReasonException; +use Amp\Promise; +use Amp\Success; +use Amp\TimeoutException; +use Amp\WindowsRegistry\KeyNotFoundException; +use Amp\WindowsRegistry\WindowsRegistry; +use LibDNS\Decoder\DecoderFactory; +use LibDNS\Encoder\EncoderFactory; +use LibDNS\Messages\MessageFactory; +use LibDNS\Messages\MessageTypes; use LibDNS\Records\QuestionFactory; class DefaultResolver implements Resolver { @@ -243,9 +253,8 @@ class DefaultResolver implements Resolver { if (empty($types)) { if (empty(array_filter($result))) { throw new NoRecordException("No records returned for {$name} (cached result)"); - } else { - return $result; } + return $result; } } @@ -267,7 +276,7 @@ class DefaultResolver implements Resolver { } try { - list( , $resultArr) = yield Promise\timeout(Promise\some($promises), $timeout); + list(, $resultArr) = yield Promise\timeout(Promise\some($promises), $timeout); foreach ($resultArr as $value) { $result += $value; } @@ -276,10 +285,9 @@ class DefaultResolver implements Resolver { throw new TimeoutException( "Name resolution timed out for {$name}" ); - } else { - $options["server"] = \preg_replace("#[a-z.]+://#", "tcp://", $uri); - return yield from $this->doResolve($name, $types, $options); } + $options["server"] = \preg_replace("#[a-z.]+://#", "tcp://", $uri); + return yield from $this->doResolve($name, $types, $options); } catch (ResolutionException $e) { if (empty($result)) { // if we have no cached results throw $e; @@ -365,7 +373,8 @@ class DefaultResolver implements Resolver { while ($nameserver === "" && ($key = \array_shift($keys))) { try { $nameserver = yield $reader->read($key); - } catch (KeyNotFoundException $e) { } + } catch (KeyNotFoundException $e) { + } } if ($nameserver === "") { @@ -379,7 +388,8 @@ class DefaultResolver implements Resolver { if ($nameserver !== "") { break 2; } - } catch (KeyNotFoundException $e) { } + } catch (KeyNotFoundException $e) { + } } } } @@ -514,13 +524,13 @@ class DefaultResolver implements Resolver { if (\substr($uri, 0, 6) == "tcp://") { $deferred = new Deferred; $server->connect = $deferred->promise(); - $watcher = Loop::onWritable($server->socket, static function($watcher) use ($server, $deferred, &$timer) { + $watcher = Loop::onWritable($server->socket, static function ($watcher) use ($server, $deferred, &$timer) { Loop::cancel($watcher); Loop::cancel($timer); unset($server->connect); $deferred->resolve(); }); - $timer = Loop::delay(5000, function() use ($id, $deferred, $watcher, $uri) { + $timer = Loop::delay(5000, function () use ($id, $deferred, $watcher, $uri) { Loop::cancel($watcher); $this->unloadServer($id); $deferred->fail(new TimeoutException("Name resolution timed out, could not connect to server at $uri")); @@ -660,7 +670,7 @@ class DefaultResolver implements Resolver { } else { foreach ($result as $type => $records) { $minttl = \PHP_INT_MAX; - foreach ($records as list( , , $ttl)) { + foreach ($records as list(, , $ttl)) { if ($ttl < $minttl) { $minttl = $ttl; } diff --git a/lib/NoRecordException.php b/lib/NoRecordException.php index e20426e..19f7c3f 100644 --- a/lib/NoRecordException.php +++ b/lib/NoRecordException.php @@ -2,4 +2,5 @@ namespace Amp\Dns; -class NoRecordException extends ResolutionException {} +class NoRecordException extends ResolutionException { +} diff --git a/lib/Record.php b/lib/Record.php index 9f60a18..d1048a7 100644 --- a/lib/Record.php +++ b/lib/Record.php @@ -2,4 +2,5 @@ namespace Amp\Dns; -class Record extends \LibDNS\Records\ResourceQTypes {} +class Record extends \LibDNS\Records\ResourceQTypes { +} diff --git a/lib/ResolutionException.php b/lib/ResolutionException.php index e11b880..09d00c3 100644 --- a/lib/ResolutionException.php +++ b/lib/ResolutionException.php @@ -2,4 +2,5 @@ namespace Amp\Dns; -class ResolutionException extends \Exception {} +class ResolutionException extends \Exception { +} diff --git a/lib/Resolver.php b/lib/Resolver.php index 6ad5dd4..2ad8001 100644 --- a/lib/Resolver.php +++ b/lib/Resolver.php @@ -14,4 +14,4 @@ interface Resolver { * @see \Amp\Dns\query */ public function query(string $name, $type, array $options = []): Promise; -} \ No newline at end of file +} diff --git a/lib/TimeoutException.php b/lib/TimeoutException.php index a013a8a..a6d6dcf 100644 --- a/lib/TimeoutException.php +++ b/lib/TimeoutException.php @@ -2,4 +2,5 @@ namespace Amp\Dns; -class TimeoutException extends ResolutionException {} +class TimeoutException extends ResolutionException { +} diff --git a/lib/functions.php b/lib/functions.php index a0bf85a..7540cde 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -8,7 +8,7 @@ use Amp\Promise; const LOOP_STATE_IDENTIFIER = Resolver::class; /** - * Retrieve the application-wide dns resolver instance + * Retrieve the application-wide dns resolver instance. * * @param \Amp\Dns\Resolver $resolver Optionally specify a new default dns resolver instance * @return \Amp\Dns\Resolver Returns the application-wide dns resolver instance @@ -30,7 +30,7 @@ function resolver(Resolver $resolver = null): Resolver { } /** - * Create a new dns resolver best-suited for the current environment + * Create a new dns resolver best-suited for the current environment. * * @return \Amp\Dns\Resolver */ @@ -40,7 +40,7 @@ function driver(): Resolver { /** * Resolve a hostname name to an IP address - * [hostname as defined by RFC 3986] + * [hostname as defined by RFC 3986]. * * Upon success the returned promise resolves to an indexed array of the form: * diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 65f10fb..ce62c46 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,7 @@ - + test @@ -22,7 +22,7 @@ lib - - - - \ No newline at end of file + + + + diff --git a/test/IntegrationTest.php b/test/IntegrationTest.php index 43589a7..3b9955d 100644 --- a/test/IntegrationTest.php +++ b/test/IntegrationTest.php @@ -3,8 +3,9 @@ namespace Amp\Dns\Test; use Amp\Loop; +use Amp\PHPUnit\TestCase; -class IntegrationTest extends \PHPUnit_Framework_TestCase { +class IntegrationTest extends TestCase { /** * @group internet * @dataProvider provideHostnames diff --git a/test/ResolvConfTest.php b/test/ResolvConfTest.php index 59901d8..b20126d 100644 --- a/test/ResolvConfTest.php +++ b/test/ResolvConfTest.php @@ -3,9 +3,10 @@ namespace Amp\Dns\Test; use Amp\Coroutine; +use Amp\PHPUnit\TestCase; use ReflectionObject; -class ResolvConfTest extends \PHPUnit_Framework_TestCase { +class ResolvConfTest extends TestCase { public function test() { $reflector = new ReflectionObject(\Amp\Dns\resolver()); $method = $reflector->getMethod("loadResolvConf"); @@ -39,4 +40,4 @@ class ResolvConfTest extends \PHPUnit_Framework_TestCase { "attempts" => 2, ], $result); } -} \ No newline at end of file +}