Skip to content

Commit

Permalink
test(mutation): add mutation tests (#2)
Browse files Browse the repository at this point in the history
Configure Infection and add mutation tests.
  • Loading branch information
donatorsky committed May 9, 2021
1 parent 6b9cebe commit 9d9203e
Show file tree
Hide file tree
Showing 62 changed files with 1,186 additions and 398 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ jobs:
run: composer lint:stan:analyse

- name: Run Unit tests
run: composer lint:test:unit
run: composer tests:unit

- name: Run Mutation tests
run: composer tests:mutation:ci

coveralls:

Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@

# PHP Unit
/.phpunit.result.cache

# Infection
/infection.json
2 changes: 2 additions & 0 deletions build/infection/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
24 changes: 17 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
"ext-simplexml": "*",
"ext-xml": "*",
"beberlei/assert": "^3.3",
"symfony/event-dispatcher": "^5.2"
"symfony/event-dispatcher": "^5.2",
"thecodingmachine/safe": "^1.3"
},
"suggest": {
"symfony/event-dispatcher": "To use Symfony's evend dispatcher"
},
"require-dev": {
"fakerphp/faker": "^1.14",
"friendsofphp/php-cs-fixer": "^3.0",
"infection/infection": "^0.22.1",
"jangregor/phpstan-prophecy": "^0.8.1",
"jetbrains/phpstorm-attributes": "^1.0",
"phpspec/prophecy-phpunit": "^2.0",
Expand Down Expand Up @@ -62,15 +64,23 @@
"phpstan analyse -l max src --memory-limit=256M --ansi --verbose",
"phpstan analyse -l 6 tests -c phpstan.tests.neon --memory-limit=256M --ansi --verbose"
],
"lint": [
"@lint:cs:fix",
"@lint:stan:analyse"
],

"lint:test:unit": [
"tests:unit": [
"phpunit --colors=auto"
],

"lint": [
"@lint:cs:fix",
"@lint:stan:analyse",
"@lint:test:unit"
"tests:mutation": [
"infection --threads=8 --log-verbosity=default"
],
"tests:mutation:ci": [
"infection --min-msi=95 --min-covered-msi=95 --threads=8 --log-verbosity=none"
],
"test": [
"@tests:unit",
"@tests:mutation"
]
},
"config": {
Expand Down
40 changes: 40 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"source": {
"directories": [
"src"
],
"excludes": [
"Events",
"Exceptions"
]
},
"timeout": 5,
"logs": {
"text": "build/infection/infection.log",
"summary": "build/infection/summary.log",
"perMutator": "build/infection/per-mutator.md"
},
"mutators": {
"global-ignore": [
"Donatorsky\\XmlTemplate\\Reader\\XmlTemplateReader::deinitializeParser"
],
"global-ignoreSourceCodeByRegex": [
"\\$this->deinitializeParser\\(\\).*",
"xml_parse\\(\\$this->xmlParser, '', true\\).*",
"xml_parser_free\\(\\$this->xmlParser\\).*",
"xml_parser_set_option\\(\\$this->xmlParser,.*",
"xml_set_character_data_handler\\(\\$this->xmlParser,.*",
"xml_set_element_handler\\(\\$this->xmlParser,.*",
"xml_set_object\\(\\$this->xmlParser, \\$this\\).*"
],
"@default": true,
"ArrayItemRemoval": {
"ignoreSourceCodeByRegex": [
"'attributesRules'\\s*=>\\s*\\[\\],"
]
}
},
"testFramework": "phpunit",
"testFrameworkOptions": "--do-not-cache-result",
"initialTestsPhpOptions": "-d xdebug.mode=coverage"
}
7 changes: 6 additions & 1 deletion phpstan.tests.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ parameters:
ignoreErrors:
# False positives
- '#^Parameter \x232 \$value of method Donatorsky\\XmlTemplate\\Reader\\Models\\Map<Donatorsky\\XmlTemplate\\Reader\\Models\\Collection<Donatorsky\\XmlTemplate\\Reader\\Models\\Contracts\\NodeInterface>>::set\(\) expects Donatorsky\\XmlTemplate\\Reader\\Models\\Collection<Donatorsky\\XmlTemplate\\Reader\\Models\\Contracts\\NodeInterface>, Donatorsky\\XmlTemplate\\Reader\\Models\\Collection<Donatorsky\\XmlTemplate\\Reader\\Models\\Node> given\.$#'

# Test Cases
-
message: '#^Parameter \x231 \$offset of method Donatorsky\\XmlTemplate\\Reader\\Models\\Map<mixed>::offset[GS]et\(\) expects string, int given\.$#'
path: tests/Models/MapTest.php
-
message: '#^Parameter \x232 \$ruleClassFqn of method Donatorsky\\XmlTemplate\\Reader\\XmlTemplateReader::registerRuleFilter\(\) expects class-string<Donatorsky\\XmlTemplate\\Reader\\Rules\\Contracts\\RuleInterface>, string given\.$#'
path: tests/Feature/XmlTemplateReader/CustomFilterTest.php
path: tests/XmlTemplateReader/CustomFilterTest.php
7 changes: 2 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
</exclude>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
<testsuite name="Tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 2 additions & 0 deletions src/Models/Contracts/NodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public function getContents(): ?string;

public function setContents(?string $contents): self;

public function appendContents(string $contents): self;

public function hasContents(): bool;

public function getParent(): ?self;
Expand Down
10 changes: 5 additions & 5 deletions src/Models/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use ArrayAccess;
use ArrayIterator;
use Assert\Assertion;
use Countable;
use IteratorAggregate;
use OutOfBoundsException;
Expand Down Expand Up @@ -99,6 +98,11 @@ public function offsetExists($offset): bool
return $this->has($offset);
}

/**
* @param string $offset
*
* @return TValue
*/
public function offsetGet($offset)
{
return $this->get($offset);
Expand All @@ -107,13 +111,9 @@ public function offsetGet($offset)
/**
* @param string $offset
* @param TValue $value
*
* @throws \Assert\AssertionFailedException
*/
public function offsetSet($offset, $value): void
{
Assertion::string($offset);

$this->set($offset, $value);
}

Expand Down
11 changes: 11 additions & 0 deletions src/Models/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ public function setContents(?string $contents): self
return $this;
}

public function appendContents(string $contents): self
{
if (null === $this->contents) {
$this->contents = $contents;
} else {
$this->contents .= $contents;
}

return $this;
}

public function hasContents(): bool
{
return null !== $this->contents;
Expand Down
5 changes: 4 additions & 1 deletion src/Rules/FloatNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

class FloatNumber extends Numeric
{
/**
* @param numeric-string|float|int $value
*/
public function process($value): float
{
return (float) parent::process($value);
return parent::process($value);
}
}
3 changes: 3 additions & 0 deletions src/Rules/IntegerNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public function passes($value): bool
return parent::passes($value) && false !== filter_var($value, FILTER_VALIDATE_INT);
}

/**
* @param numeric-string|int $value
*/
public function process($value): int
{
return (int) parent::process($value);
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Numeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public function passes($value): bool
}

/**
* @param mixed $value
* @param numeric-string|float|int $value
*
* @return float|int
*/
public function process($value)
{
return $value + 0;
return +$value;
}
}
5 changes: 4 additions & 1 deletion src/Rules/Trim.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ public function passes($value): bool
return \is_string($value);
}

/**
* @param string $value
*/
public function process($value): string
{
return trim((string) $value);
return trim($value);
}
}
Loading

0 comments on commit 9d9203e

Please sign in to comment.