Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Fix: Test provided values individually #336

Merged
merged 1 commit into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/integrate.yaml
Expand Up @@ -10,8 +10,8 @@ on: # yamllint disable-line rule:truthy

env:
ERGEBNIS_BOT_NAME: "ergebnis-bot"
MIN_COVERED_MSI: 90
MIN_MSI: 88
MIN_COVERED_MSI: 94
MIN_MSI: 91
PHP_EXTENSIONS: "mbstring"

jobs:
Expand Down
4 changes: 2 additions & 2 deletions Makefile
@@ -1,5 +1,5 @@
MIN_COVERED_MSI:=90
MIN_MSI:=88
MIN_COVERED_MSI:=94
MIN_MSI:=91

.PHONY: it
it: coding-standards static-code-analysis tests ## Runs the coding-standards, static-code-analysis, and tests targets
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Expand Up @@ -665,6 +665,11 @@ parameters:
count: 2
path: test/Unit/DataProvider/AbstractProviderTestCase.php

-
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertIsArray\\(\\) with array will always evaluate to true\\.$#"
count: 1
path: test/Unit/DataProvider/AbstractProviderTestCase.php

-
message: "#^Method Ergebnis\\\\Test\\\\Util\\\\Test\\\\Unit\\\\Exception\\\\EmptyValuesTest\\:\\:faker\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
Expand Down
11 changes: 10 additions & 1 deletion psalm-baseline.xml
Expand Up @@ -54,10 +54,19 @@
<code>class-string</code>
</UnnecessaryVarAnnotation>
</file>
<file src="test/Unit/DataProvider/IntProviderTest.php">
<file src="test/Unit/DataProvider/AbstractProviderTestCase.php">
<MissingClosureParamType occurrences="1">
<code>$value</code>
</MissingClosureParamType>
<MixedAssignment occurrences="1">
<code>$value</code>
</MixedAssignment>
<RedundantConditionGivenDocblockType occurrences="1">
<code>assertIsArray</code>
</RedundantConditionGivenDocblockType>
<RedundantIdentityWithTrue occurrences="1">
<code>true === $test($value)</code>
</RedundantIdentityWithTrue>
</file>
<file src="test/Unit/Exception/InvalidExcludeClassNameTest.php">
<MixedInferredReturnType occurrences="1">
Expand Down
47 changes: 33 additions & 14 deletions test/Unit/DataProvider/AbstractProviderTestCase.php
Expand Up @@ -45,27 +45,46 @@ final protected static function assertProvidesDataForValues(array $values, \Gene
}

/**
* @param \Closure $test
* @param array<string, \Closure|mixed> $tests
* @param \Generator<string, array<mixed>> $provider
*/
final protected static function assertProvidesDataForValuesWhere(\Closure $test, \Generator $provider): void
final protected static function assertProvidesDataForValuesPassingTests(array $tests, \Generator $provider): void
{
$provided = \iterator_to_array($provider);

self::assertProvidedDataIsNotEmpty($provided);
self::assertProvidedDataContainsArraysWhereFirstElementPassesTest($test, $provided);
}
self::assertEquals(
\array_keys($tests),
\array_keys($provided),
'Failed asserting that the provided data has the same keys as the tests.'
);

/**
* @param \Closure $test
* @param \Generator<string, array<mixed>> $provider
*/
final protected static function assertProvidesDataForValuesWhereNot(\Closure $test, \Generator $provider): void
{
$provided = \iterator_to_array($provider);
$normalizedTests = \array_map(static function ($test): \Closure {
if (!$test instanceof \Closure) {
return static function ($value) use ($test): bool {
return $value === $test;
};
}

self::assertProvidedDataIsNotEmpty($provided);
self::assertProvidedDataContainsArraysWhereFirstElementDoesNotPassTest($test, $provided);
return $test;
}, $tests);

$keysWhereValueDoesNotPassTest = \array_filter(\array_keys($provided), static function (string $key) use ($provided, $normalizedTests): bool {
$set = $provided[$key];

self::assertIsArray($set);
self::assertCount(1, $set);

$value = \array_shift($set);
$test = $normalizedTests[$key];

return true !== $test($value);
});

self::assertEquals(
[],
$keysWhereValueDoesNotPassTest,
'Failed asserting that all values pass the corresponding tests.'
);
}

/**
Expand Down
60 changes: 40 additions & 20 deletions test/Unit/DataProvider/IntProviderTest.php
Expand Up @@ -34,13 +34,21 @@ public function testArbitraryProvidesInt($value): void

public function testArbitraryReturnsGeneratorThatProvidesIntValues(): void
{
$test = static function ($value): bool {
return \is_int($value);
};
$tests = [
'int-less-than-minus-one' => static function (int $value): bool {
return -1 > $value;
},
'int-minus-one' => -1,
'int-zero' => 0,
'int-plus-one' => 1,
'int-greater-than-plus-one' => static function (int $value): bool {
return 1 < $value;
},
];

$provider = IntProvider::arbitrary();

self::assertProvidesDataForValuesWhere($test, $provider);
self::assertProvidesDataForValuesPassingTests($tests, $provider);
}

/**
Expand All @@ -55,13 +63,16 @@ public function testLessThanZeroProvidesIntLessThanZero(int $value): void

public function testLessThanZeroReturnsGeneratorThatProvidesIntLessThanZero(): void
{
$test = static function (int $value): bool {
return 0 > $value;
};
$tests = [
'int-less-than-minus-one' => static function (int $value): bool {
return -1 > $value;
},
'int-minus-one' => -1,
];

$provider = IntProvider::lessThanZero();

self::assertProvidesDataForValuesWhere($test, $provider);
self::assertProvidesDataForValuesPassingTests($tests, $provider);
}

/**
Expand Down Expand Up @@ -97,13 +108,16 @@ public function testGreaterThanZeroProvidesIntGreaterThanZero(int $value): void

public function testGreaterThanZeroReturnsGeneratorThatProvidesIntGreaterThanZero(): void
{
$test = static function (int $value): bool {
return 0 < $value;
};
$tests = [
'int-plus-one' => 1,
'int-greater-than-plus-one' => static function (int $value): bool {
return 1 < $value;
},
];

$provider = IntProvider::greaterThanZero();

self::assertProvidesDataForValuesWhere($test, $provider);
self::assertProvidesDataForValuesPassingTests($tests, $provider);
}

/**
Expand All @@ -118,13 +132,17 @@ public function testLessThanOneProvidesIntLessThanOne(int $value): void

public function testLessThanOneReturnsGeneratorThatProvidesIntLessThanOne(): void
{
$test = static function (int $value): bool {
return 1 > $value;
};
$tests = [
'int-less-than-minus-one' => static function (int $value): bool {
return -1 > $value;
},
'int-minus-one' => -1,
'int-zero' => 0,
];

$provider = IntProvider::lessThanOne();

self::assertProvidesDataForValuesWhere($test, $provider);
self::assertProvidesDataForValuesPassingTests($tests, $provider);
}

public function testOneReturnsGeneratorThatProvidesOne(): void
Expand All @@ -150,12 +168,14 @@ public function testGreaterThanOneProvidesIntGreaterThanOne(int $value): void

public function testGreaterThanOneReturnsGeneratorThatProvidesIntGreaterThanOne(): void
{
$test = static function (int $value): bool {
return 1 < $value;
};
$tests = [
'int-greater-than-plus-one' => static function (int $value): bool {
return 1 < $value;
},
];

$provider = IntProvider::greaterThanOne();

self::assertProvidesDataForValuesWhere($test, $provider);
self::assertProvidesDataForValuesPassingTests($tests, $provider);
}
}
45 changes: 36 additions & 9 deletions test/Unit/DataProvider/StringProviderTest.php
Expand Up @@ -34,13 +34,30 @@ public function testArbitraryProvidesString(string $value): void

public function testArbitraryReturnsGeneratorThatProvidesStringsThatAreNeitherEmptyNorBlank(): void
{
$test = static function (string $value): bool {
return '' === \trim($value);
};
$tests = [
'string-arbitrary-sentence' => static function (string $value): bool {
return '' !== $value && '' !== \trim($value);
},
'string-arbitrary-word' => static function (string $value): bool {
return '' !== $value && '' !== \trim($value);
},
'string-untrimmed-carriage-return' => static function (string $value): bool {
return 1 === \preg_match('/^\r{1,5}\w+\r{1,5}$/', $value);
},
'string-untrimmed-line-feed' => static function (string $value): bool {
return 1 === \preg_match('/^\n{1,5}\w+\n{1,5}$/', $value);
},
'string-untrimmed-space' => static function (string $value): bool {
return 1 === \preg_match('/^\s{1,5}\w+\s{1,5}$/', $value);
},
'string-untrimmed-tab' => static function (string $value): bool {
return 1 === \preg_match('/^\t{1,5}\w+\t{1,5}$/', $value);
},
];

$provider = StringProvider::arbitrary();

self::assertProvidesDataForValuesWhereNot($test, $provider);
self::assertProvidesDataForValuesPassingTests($tests, $provider);
}

/**
Expand Down Expand Up @@ -103,13 +120,23 @@ public function testUntrimmedProvidesUntrimmedString(string $value): void

public function testUntrimmedReturnsGeneratorThatProvidesUntrimmedStrings(): void
{
$test = static function (string $value): bool {
return \trim($value) !== $value
&& '' !== \trim($value);
};
$tests = [
'string-untrimmed-carriage-return' => static function (string $value): bool {
return 1 === \preg_match('/^\r{1,5}\w+\r{1,5}$/', $value);
},
'string-untrimmed-line-feed' => static function (string $value): bool {
return 1 === \preg_match('/^\n{1,5}\w+\n{1,5}$/', $value);
},
'string-untrimmed-space' => static function (string $value): bool {
return 1 === \preg_match('/^\s{1,5}\w+\s{1,5}$/', $value);
},
'string-untrimmed-tab' => static function (string $value): bool {
return 1 === \preg_match('/^\t{1,5}\w+\t{1,5}$/', $value);
},
];

$provider = StringProvider::untrimmed();

self::assertProvidesDataForValuesWhere($test, $provider);
self::assertProvidesDataForValuesPassingTests($tests, $provider);
}
}