Skip to content

Commit

Permalink
Try mutation testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo FOUGERES committed Mar 8, 2023
1 parent cf66e01 commit 579ed49
Show file tree
Hide file tree
Showing 23 changed files with 217 additions and 38 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Run tests
on: [ push ]
jobs:
run-mutation-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
tools: composer:v2
coverage: xdebug

- name: Composer install
run: composer install

- name: Run mutation tests
run: vendor/bin/infection --show-mutations
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ env:
- XDEBUG_MODE=coverage

php:
- 7.4
- 8.0
- 8.1

before_script:
- travis_retry composer self-update
Expand Down
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
],
"minimum-stability": "stable",
"require": {
"php": "^7.4|^8.0",
"php": ">=8.1",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"infection/infection": "^0.26.16"
},
"autoload": {
"psr-4": {
Expand All @@ -26,5 +27,10 @@
"psr-4": {
"hunomina\\DataValidator\\Test\\": "tests/"
}
},
"config": {
"allow-plugins": {
"infection/extension-installer": true
}
}
}
13 changes: 13 additions & 0 deletions infection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "vendor/infection/infection/resources/schema.json",
"source": {
"directories": [
"src"
]
},
"mutators": {
"@default": true,
"Concat": false,
"ConcatOperandRemoval": false
}
}
2 changes: 2 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
forceCoversAnnotation="true"
stderr="true"
verbose="true"
executionOrder="random"
resolveDependencies="true"
>
<coverage processUncoveredFiles="true">
<include>
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Json/JsonData.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function offsetExists($offset): bool
* @return mixed|null
* @codeCoverageIgnore
*/
public function offsetGet($offset)
public function offsetGet($offset): mixed
{
if ($this->offsetExists($offset)) {
return $this->data[$offset];
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Json/Check/DateFormatCheckTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function setDateFormat(?string $dateFormat): void
* @return bool
* Return true if the string data match the date format $this->dateFormat
*/
public function validateDateFormat(string $data): bool
private function validateDateFormat(string $data): bool
{
if ($this->dateFormat === null) {
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/Rule/Json/Check/EmptyCheckTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ public function setEmpty(bool $empty): void
* Return true if the value is "empty" (string, list) and can
* Or if the value is not empty
*/
abstract public function validateEmptiness($data): bool;
}
abstract private function validateEmptiness($data): bool;
}
2 changes: 1 addition & 1 deletion src/Rule/Json/Check/EnumCheckTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function setEnum(?array $enum): void
* @return bool
* Return true if the data can be found in $this->enum
*/
public function validateEnum($data): bool
private function validateEnum($data): bool
{
if ($this->enum === null) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Json/Check/LengthCheckTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ public function setLength(?int $length): void
* @return bool
* Return true if the data length (regardless of the type, could be a string, an array, ...) equals $this->length
*/
abstract public function validateLength($data): bool;
abstract private function validateLength($data): bool;
}
2 changes: 1 addition & 1 deletion src/Rule/Json/Check/MaximumCheckTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ public function setMaximum(?float $maximum): void
* Return true if the value is lower or equals to $this->maximum
* For integer check => Cast the parameter
*/
abstract public function validateMaximum($data): bool;
abstract private function validateMaximum($data): bool;
}
2 changes: 1 addition & 1 deletion src/Rule/Json/Check/MinimumCheckTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ public function setMinimum(?float $minimum): void
* Return true if the value is greater or equals to $this->minimum
* For integer check => Cast the parameter
*/
abstract public function validateMinimum($data): bool;
abstract private function validateMinimum($data): bool;
}
2 changes: 1 addition & 1 deletion src/Rule/Json/Check/PatternCheckTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function setPattern(?string $pattern): void
* Return true if the string data match the pattern
* Regular expressions only apply to string that's why this method is not abstract
*/
public function validatePattern(string $data): bool
private function validatePattern(string $data): bool
{
if ($this->pattern === null) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Json/Factory/JsonRuleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function create(string $type, array $options): JsonRule
{
try {
$rule = self::getRuleObjectFromType($type);
} catch (InvalidRuleException $e) {
} catch (InvalidRuleException) {
throw new InvalidRuleException('Invalid rule type : `' . $type . '`', InvalidRuleException::INVALID_RULE_TYPE);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Rule/Json/FloatRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function validate($data): bool
* @param $data
* @return bool
*/
public function validateMaximum($data): bool
private function validateMaximum($data): bool
{
if ($this->maximum === null) {
return true;
Expand All @@ -65,7 +65,7 @@ public function validateMaximum($data): bool
* @param $data
* @return bool
*/
public function validateMinimum($data): bool
private function validateMinimum($data): bool
{
if ($this->minimum === null) {
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/Rule/Json/IntegerRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function validate($data): bool
* @param $data
* @return bool
*/
public function validateMaximum($data): bool
private function validateMaximum($data): bool
{
if ($this->maximum === null) {
return true;
Expand All @@ -65,7 +65,7 @@ public function validateMaximum($data): bool
* @param $data
* @return bool
*/
public function validateMinimum($data): bool
private function validateMinimum($data): bool
{
if ($this->minimum === null) {
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/Rule/Json/NumericRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function validate($data): bool
* @param float $data
* @return bool
*/
public function validateMaximum($data): bool
private function validateMaximum($data): bool
{
if ($this->maximum === null) {
return true;
Expand All @@ -65,7 +65,7 @@ public function validateMaximum($data): bool
* @param float $data
* @return bool
*/
public function validateMinimum($data): bool
private function validateMinimum($data): bool
{
if ($this->minimum === null) {
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/Json/StringRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function validate($data): bool
* @param $data
* @return bool
*/
public function validateEmptiness($data): bool
private function validateEmptiness($data): bool
{
if ($this->empty === false) { // can not be an empty value
return $data !== '';
Expand All @@ -77,7 +77,7 @@ public function validateEmptiness($data): bool
* @param $data
* @return bool
*/
public function validateLength($data): bool
private function validateLength($data): bool
{
if ($this->length === null) {
return true;
Expand All @@ -93,4 +93,4 @@ public function getType(): string
{
return self::STRING_TYPE;
}
}
}
15 changes: 6 additions & 9 deletions src/Rule/Json/TypedListRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ class TypedListRule extends JsonRule
use EnumCheckTrait;
use EmptyCheckTrait;

private JsonRule $childRule;

public function __construct(JsonRule $childRule)
public function __construct(private JsonRule $childRule)
{
$this->childRule = $childRule;
}

/**
Expand Down Expand Up @@ -81,7 +78,7 @@ public function validate($data): bool
/**
* @inheritDoc
*/
public function validateEmptiness($data): bool
private function validateEmptiness($data): bool
{
if (($this->empty === false) && $this->minimum !== 0) {
return count($data) !== 0;
Expand All @@ -106,7 +103,7 @@ public function setLength(?int $length): void
/**
* @inheritDoc
*/
public function validateLength($data): bool
private function validateLength($data): bool
{
if ($this->length === null) {
return true;
Expand All @@ -131,7 +128,7 @@ public function setMaximum(?float $maximum): void
* @param array $data
* @return bool
*/
public function validateMaximum($data): bool
private function validateMaximum($data): bool
{
if ($this->maximum === null) {
return true;
Expand All @@ -156,7 +153,7 @@ public function setMinimum(?float $minimum): void
* @param array $data
* @return bool
*/
public function validateMinimum($data): bool
private function validateMinimum($data): bool
{
if ($this->minimum === null) {
return true;
Expand All @@ -180,4 +177,4 @@ public function getType(): string
{
return $this->childRule->getType() . self::LIST_TYPE_SUFFIX;
}
}
}
1 change: 1 addition & 0 deletions src/Schema/Json/JsonSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ private function validateObject(JsonData $dataType): bool
} catch (InvalidDataException $e) {
throw new InvalidDataException('`' . $field . '` does not validate the schema. ' . $e->getMessage(), $e->getCode(), $e);
}
break;
}
}

Expand Down
Loading

0 comments on commit 579ed49

Please sign in to comment.