From 345dcf43fff287a06eec3a58d30c167c348d2e2a Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sat, 16 Oct 2021 09:39:07 +0300 Subject: [PATCH] Fix tests, Psalm level 1, add BC GitHub workflow and cleanup other workflows (#39) --- .editorconfig | 3 +++ .github/workflows/bc.yml | 15 +++++++++++++++ .github/workflows/bc.yml_ | 15 --------------- .github/workflows/build.yml | 13 ++++--------- .github/workflows/mutation.yml | 8 ++++---- .github/workflows/static.yml | 7 ++++--- psalm.xml | 3 +-- src/IpHelper.php | 9 +++++++-- tests/DnsHelperTest.php | 11 +++++++---- 9 files changed, 45 insertions(+), 39 deletions(-) create mode 100644 .github/workflows/bc.yml delete mode 100644 .github/workflows/bc.yml_ diff --git a/.editorconfig b/.editorconfig index 257221d..5e9a93e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -12,3 +12,6 @@ trim_trailing_whitespace = true [*.md] trim_trailing_whitespace = false + +[*.yml] +indent_size = 2 diff --git a/.github/workflows/bc.yml b/.github/workflows/bc.yml new file mode 100644 index 0000000..75f97a7 --- /dev/null +++ b/.github/workflows/bc.yml @@ -0,0 +1,15 @@ +on: + - pull_request + - push + +name: backwards compatibility +jobs: + roave_bc_check: + name: Roave BC Check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + - name: fetch tags + run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* + - name: Roave BC Check + uses: docker://nyholm/roave-bc-check-ga diff --git a/.github/workflows/bc.yml_ b/.github/workflows/bc.yml_ deleted file mode 100644 index 35b3a86..0000000 --- a/.github/workflows/bc.yml_ +++ /dev/null @@ -1,15 +0,0 @@ -on: - - pull_request - - push - -name: backwards compatibility -jobs: - roave_bc_check: - name: Roave BC Check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@master - - name: fetch tags - run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* - - name: Roave BC Check - uses: docker://nyholm/roave-bc-check-ga diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index df20a19..7d03f1e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -22,12 +22,12 @@ jobs: - windows-latest php: - - "7.4" - - "8.0" + - 7.4 + - 8.0 steps: - name: Checkout - uses: actions/checkout@v2.3.4 + uses: actions/checkout@v2 - name: Install PHP uses: shivammathur/setup-php@v2 @@ -56,13 +56,8 @@ jobs: - name: Update composer run: composer self-update - - name: Install dependencies with composer php 7.4 - if: matrix.php == '7.4' + - name: Install dependencies with composer run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi - - name: Install dependencies with composer php 8.0 - if: matrix.php == '8.0' - run: composer update --ignore-platform-reqs --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi - - name: Run tests with phpunit run: vendor/bin/phpunit --colors=always diff --git a/.github/workflows/mutation.yml b/.github/workflows/mutation.yml index 58e67d3..1f597ef 100644 --- a/.github/workflows/mutation.yml +++ b/.github/workflows/mutation.yml @@ -18,18 +18,18 @@ jobs: - ubuntu-latest php: - - "7.4" + - 7.4 steps: - name: Checkout - uses: actions/checkout@v2.3.4 + uses: actions/checkout@v2 - name: Install PHP uses: shivammathur/setup-php@v2 with: - php-version: "${{ matrix.php }}" + php-version: ${{ matrix.php }} ini-values: memory_limit=-1 - coverage: "pcov" + coverage: pcov tools: composer:v2 - name: Determine composer cache directory diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index ab28b1d..2584c66 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -18,16 +18,17 @@ jobs: - ubuntu-latest php: - - "7.4" + - 7.4 + - 8.0 steps: - name: Checkout - uses: actions/checkout@v2.3.4 + uses: actions/checkout@v2 - name: Install PHP uses: shivammathur/setup-php@v2 with: - php-version: "${{ matrix.php }}" + php-version: ${{ matrix.php }} tools: composer:v2, cs2pr coverage: none diff --git a/psalm.xml b/psalm.xml index 4fa7581..752a2ca 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,7 +1,6 @@ = $netMask; } @@ -143,7 +144,10 @@ public static function expandIPv6(string $ip): string } throw new InvalidArgumentException("Unrecognized address $ip."); } + + /** @psalm-var array{hex:string} $hex */ $hex = unpack('H*hex', $ipRaw); + return substr(preg_replace('/([a-f0-9]{4})/i', '$1:', $hex['hex']), 0, -1); } @@ -172,6 +176,7 @@ public static function ip2bin(string $ip): string if (!is_string($data)) { throw new RuntimeException('An error occurred while converting IP address to bits representation.'); } + /** @psalm-suppress MixedArgument */ $result .= str_pad(decbin(unpack('N', $data)[1]), 32, '0', STR_PAD_LEFT); } return $result; @@ -195,7 +200,7 @@ public static function getCidrBits(string $ip): int if ($bits === null) { return $maxBits; } - $bits = (int)$bits; + $bits = (int) $bits; if ($bits < 0) { throw new InvalidArgumentException('The number of CIDR bits cannot be negative.', 2); } diff --git a/tests/DnsHelperTest.php b/tests/DnsHelperTest.php index 97e760a..7018949 100644 --- a/tests/DnsHelperTest.php +++ b/tests/DnsHelperTest.php @@ -5,6 +5,7 @@ namespace Yiisoft\NetworkUtilities\Tests; use PHPUnit\Framework\TestCase; +use RuntimeException; use Yiisoft\NetworkUtilities\DnsHelper; /** @@ -23,8 +24,9 @@ public function testMx(): void public function testMxWithWrongDomain(): void { - $this->expectException(\RuntimeException::class); - DnsHelper::existsMx('ya2.ru'); + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Failed to get DNS record ".wrongdomain".'); + DnsHelper::existsMx('.wrongdomain'); } public function testA(): void @@ -35,8 +37,9 @@ public function testA(): void public function testAWithWrongDomain(): void { - $this->expectException(\RuntimeException::class); - DnsHelper::existsA('ya2.ru'); + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Failed to get DNS record ".wrongdomain".'); + DnsHelper::existsA('.wrongdomain'); } public function testAcceptsEmail(): void