Skip to content

Commit

Permalink
Added mutation testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sspat committed Dec 26, 2020
1 parent bc6d31f commit 36a794c
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 17 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/mutation-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: "Mutation tests"
on:
pull_request:
push:
branches:
- "master"
jobs:
mutation-tests:
name: "Mutation tests"
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
dependencies:
- "locked"
php-version:
- "7.4"
operating-system:
- "ubuntu-latest"
steps:
- name: "Checkout"
uses: "actions/checkout@v2"
- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "pcov"
php-version: "${{ matrix.php-version }}"
ini-values: memory_limit=-1
- name: "Cache dependencies"
uses: "actions/cache@v2"
with:
path: |
~/.composer/cache
vendor
key: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}"
restore-keys: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}"
- name: "Install lowest dependencies"
if: ${{ matrix.dependencies == 'lowest' }}
run: "composer update --prefer-lowest --no-interaction --no-progress --no-suggest"
- name: "Install highest dependencies"
if: ${{ matrix.dependencies == 'highest' }}
run: "composer update --no-interaction --no-progress --no-suggest"
- name: "Install locked dependencies"
if: ${{ matrix.dependencies == 'locked' }}
run: "composer install --no-interaction --no-progress --no-suggest"
- name: "Infection"
run: "vendor/bin/roave-infection-static-analysis-plugin"
env:
INFECTION_BADGE_API_KEY: ${{ secrets.INFECTION_BADGE_API_KEY }}
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

[![Latest Version](https://img.shields.io/github/v/release/sspat/reserved-words)](https://github.com/sspat/reserved-words/releases)
[![Build](https://img.shields.io/travis/sspat/reserved-words/master)](https://travis-ci.org/sspat/reserved-words)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fsspat%2Freserved-words%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/sspat/reserved-words/master)
[![Test Coverage](https://coveralls.io/repos/github/sspat/reserved-words/badge.svg?branch=master)](https://coveralls.io/github/sspat/reserved-words?branch=master)
[![Type Coverage](https://shepherd.dev/github/sspat/reserved-words/coverage.svg)](https://shepherd.dev/github/sspat/reserved-words)
[![License](https://img.shields.io/github/license/sspat/reserved-words)](https://github.com/sspat/reserved-words/blob/master/LICENSE)
[![Email](https://img.shields.io/badge/email-studio22@mail.ru-blue.svg?style=flat-square)](mailto:studio22@mail.ru)

## About

Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
},
"require-dev": {
"doctrine/coding-standard": "^8.2",
"infection/infection": "^0.20.2",
"phpstan/phpstan": "^0.12.64",
"phpstan/phpstan-phpunit": "^0.12.17",
"phpstan/phpstan-strict-rules": "^0.12.7",
"phpunit/phpunit": "^8",
"roave/infection-static-analysis-plugin": "^1.6",
"squizlabs/php_codesniffer": "^3.5",
"thecodingmachine/phpstan-safe-rule": "^1.0",
"vimeo/psalm": "^4.3"
Expand All @@ -49,6 +51,7 @@
"psalm": "psalm",
"stan": "phpstan analyze",
"test": "phpunit",
"all": "composer psalm && composer stan && composer test && composer cs"
"mutation": "roave-infection-static-analysis-plugin",
"all": "composer psalm && composer stan && composer test && composer mutation && composer cs"
}
}
21 changes: 21 additions & 0 deletions infection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"source": {
"directories": [
"src"
]
},
"timeout": 3,
"logs": {
"text": "php://stderr",
"badge": {
"branch": "master"
}
},
"mutators": {
"@default": true,
"IdenticalEqual": false,
"NotIdenticalNotEqual": false
},
"minMsi": 100,
"minCoveredMsi": 100
}
3 changes: 3 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
<file>src/</file>
<file>tests/</file>

<rule ref="Doctrine">
<exclude name="SlevomatCodingStandard.PHP.RequireExplicitAssertion.RequiredExplicitAssertion" />
</rule>
</ruleset>
11 changes: 5 additions & 6 deletions src/ReservedWords.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use function strtolower;
use function version_compare;

class ReservedWords
final class ReservedWords
{
public const PHP_VERSION_REGEXP = '/^\d\.\d\.?\d*$/';

Expand Down Expand Up @@ -83,10 +83,6 @@ private function isReservedAs(string $string, string $checkKey, ?string $phpVers
$targetPhpVersion = $this->getPhpVersion($phpVersion);
$reservedVersion = $this->reservedWords[strtolower($string)][$checkKey];

if ($reservedVersion === false) {
return false;
}

if (is_string($reservedVersion)) {
return $this->firstVersionEqualsOrHigherThanSecond($targetPhpVersion, $reservedVersion);
}
Expand Down Expand Up @@ -114,7 +110,10 @@ private function firstVersionLessThanSecond(string $firstVersion, string $second
private function getPhpVersion(?string $phpVersion = null): string
{
if ($phpVersion === null) {
return (string) phpversion();
/** @var string $version */
$version = phpversion();

return $version;
}

if (preg_match(self::PHP_VERSION_REGEXP, $phpVersion) === 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/ReservedWordsLookupError.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use function Safe\sprintf;

class ReservedWordsLookupError extends RuntimeException
final class ReservedWordsLookupError extends RuntimeException
{
public static function invalidPhpVersion(string $phpVersion, string $correctFormat): self
{
Expand Down
5 changes: 4 additions & 1 deletion tests/ReservedWordsListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
use function Safe\preg_match;
use function Safe\sprintf;

class ReservedWordsListTest extends TestCase
/**
* @covers \sspat\ReservedWords\ReservedWordsList
*/
final class ReservedWordsListTest extends TestCase
{
public function testReservedWordsList(): void
{
Expand Down
21 changes: 21 additions & 0 deletions tests/ReservedWordsLookupErrorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace sspat\ReservedWords\Tests;

use PHPUnit\Framework\TestCase;
use sspat\ReservedWords\ReservedWordsLookupError;

/**
* @covers \sspat\ReservedWords\ReservedWordsLookupError
*/
final class ReservedWordsLookupErrorTest extends TestCase
{
public function testInvalidPhpVersion(): void
{
$this->expectExceptionMessage('Invalid PHP version: 7, the correct format is: /\d+/');

throw ReservedWordsLookupError::invalidPhpVersion('7', '/\d+/');
}
}
41 changes: 34 additions & 7 deletions tests/ReservedWordsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use sspat\ReservedWords\ReservedWords;
use sspat\ReservedWords\ReservedWordsLookupError;

use function strtoupper;

class ReservedWordsTest extends TestCase
/**
* @covers \sspat\ReservedWords\ReservedWords
*/
final class ReservedWordsTest extends TestCase
{
public function testDefaultReservedWordsLoaded(): void
{
Expand All @@ -22,20 +26,33 @@ public function testDefaultReservedWordsLoaded(): void

public function testIsReserved(): void
{
$reservedWord = 'reserved-word';
$notReservedWord = 'not-reserved-word';
$reservedWords = new ReservedWords([$reservedWord => []]);
$reservedWord = 'reserved-word';
$reservedWords = new ReservedWords([$reservedWord => []]);

Assert::assertTrue($reservedWords->isReserved($reservedWord));
Assert::assertFalse($reservedWords->isReserved($notReservedWord));
}

public function testIsNotReserved(): void
{
$notReservedWord = 'not-reserved-word';
$reservedWords = new ReservedWords([]);

Assert::assertFalse($reservedWords->isReservedMethodName($notReservedWord));
}

public function testIsReservedCaseInsensitive(): void
{
$reservedWord = 'reserved-word';
$reservedWords = new ReservedWords([$reservedWord => []]);
$reservedWords = new ReservedWords([
$reservedWord => [
'constant' => '7.0',
'namespace' => '7.0',
'function' => '7.0',
'method' => '7.0',
],
]);

Assert::assertTrue($reservedWords->isReserved(strtoupper($reservedWord)));
Assert::assertTrue($reservedWords->isReservedMethodName(strtoupper($reservedWord), '7.0'));
}

/**
Expand Down Expand Up @@ -102,6 +119,16 @@ public function testMethodName(
Assert::assertEquals($isReserved, $reservedWords->isReservedMethodName($reservedWord, $phpVersion));
}

public function testInvalidPhpVersion(): void
{
$reservedWord = 'reserved-word';
$reservedWords = new ReservedWords([$reservedWord => []]);

$this->expectException(ReservedWordsLookupError::class);
$this->expectErrorMessage('Invalid PHP version: 7, the correct format is: /^\d\.\d\.?\d*$/');
$reservedWords->isReservedMethodName($reservedWord, '7');
}

/**
* @return array<array-key, array<string, string|bool|array<string, string|bool|array<array-key, string>>>>
*/
Expand Down

0 comments on commit 36a794c

Please sign in to comment.