From 362988833480d67660bcc46517675c26719a7ff2 Mon Sep 17 00:00:00 2001 From: Ben Batschelet Date: Thu, 17 Aug 2023 14:34:58 -0500 Subject: [PATCH 1/4] Added passCallback() assertion --- phpstan.neon.dist | 1 + src/API/Base.php | 12 ++++++++++++ src/Constraint/Factory.php | 6 ++---- test/FileFunctionalTest.php | 12 ++++++++++++ test/ValueFunctionalTest.php | 17 +++++++++++++++++ 5 files changed, 44 insertions(+), 4 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index d456a35..a702f64 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -9,6 +9,7 @@ parameters: checkMissingIterableValueType: true ignoreErrors: - '/Call to function is_object\(\) with object will always evaluate to true/' + - '/Parameter \#1 \$callback of method BeBat\\Verify\\API\\Base::passCallback\(\) expects callable\(mixed\): bool/' - message: '/Call to an undefined method BeBat\\Verify\\API\\Base::equalTo/' path: %currentWorkingDirectory%/test/BaseUnitTest.php diff --git a/src/API/Base.php b/src/API/Base.php index 829a1ee..ca9313e 100644 --- a/src/API/Base.php +++ b/src/API/Base.php @@ -151,6 +151,18 @@ final public function constraint(Constraint $constraint): self return $this->performAssertion($constraint, $this->getActualValue()); } + /** + * Assert SUT will or will not pass a given callback. + * + * @param callable(mixed $actual): bool $callback + * + * @return static + */ + final public function passCallback(callable $callback): self + { + return $this->constraint($this->constraintFactory()->callback($callback)); + } + /** * Inject a custom Assert instance. * diff --git a/src/Constraint/Factory.php b/src/Constraint/Factory.php index 78695d0..ef68cc5 100644 --- a/src/Constraint/Factory.php +++ b/src/Constraint/Factory.php @@ -27,11 +27,9 @@ public function anything(): PHPUnitConstraint\IsAnything } /** - * @template CallbackInput of mixed + * @param callable(mixed $callback): bool $callback * - * @param callable(CallbackInput $callback): bool $callback - * - * @return PHPUnitConstraint\Callback + * @return PHPUnitConstraint\Callback */ public function callback(callable $callback): PHPUnitConstraint\Callback { diff --git a/test/FileFunctionalTest.php b/test/FileFunctionalTest.php index d5d63db..bafd2fa 100644 --- a/test/FileFunctionalTest.php +++ b/test/FileFunctionalTest.php @@ -22,6 +22,18 @@ final class FileFunctionalTest extends TestCase /** @var string */ private $fixtureDir = __DIR__ . '/_fixtures'; + public function testCallbackAssertion(): void + { + verify_file($this->fixtureDir . '/File1')->will() + ->passCallback(static function (string $actual): bool { + return metaphone($actual) === '0SSMFL'; + }) + ->and()->willNot() + ->passCallback(static function (string $actual): bool { + return metaphone($actual) === 'FOOBAR'; + }); + } + public function testContainsAssertion(): void { verify_file($this->fixtureDir . '/File1')->will()->contain('My File') diff --git a/test/ValueFunctionalTest.php b/test/ValueFunctionalTest.php index 063cabb..2a7cbd4 100644 --- a/test/ValueFunctionalTest.php +++ b/test/ValueFunctionalTest.php @@ -24,6 +24,17 @@ final class ValueFunctionalTest extends TestCase /** @var string */ private $fixtureDir = __DIR__ . '/_fixtures'; + public function isPrime(int $num): bool + { + for ($i = 2; $i < $num / ($i - 1); ++$i) { + if ($num % $i === 0) { + return false; + } + } + + return true; + } + public function testArrayAssertions(): void { $object1 = new ExampleChild(); @@ -63,6 +74,12 @@ public function testBooleanAssertions(): void ->and()->isNot()->null(); } + public function testCallableAssertion(): void + { + verify(7)->will()->passCallback([$this, 'isPrime']); + verify(169)->willNot()->passCallback([$this, 'isPrime']); + } + /** * @requires PHPUnit < 10 */ From 22c57a8f22102c1e1de41c312ce6d7db63fbd08d Mon Sep 17 00:00:00 2001 From: Ben Batschelet Date: Thu, 17 Aug 2023 14:45:29 -0500 Subject: [PATCH 2/4] Place PR template in correct directory, use correct slash --- .github/ISSUE_TEMPLATE/bug_report.yml | 2 +- .github/ISSUE_TEMPLATE/feature_request.yml | 2 +- .github/{ISSUE_TEMPLATE => }/PULL_REQUEST_TEMPLATE.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename .github/{ISSUE_TEMPLATE => }/PULL_REQUEST_TEMPLATE.md (96%) diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 3632c06..35c91aa 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -1,5 +1,5 @@ name: Bug Report -description: Report an issue with BeBat\Verify +description: Report an issue with BeBat/Verify title: "[Bug]: " labels: ["bug"] body: diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml index 2da761c..cff4428 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.yml +++ b/.github/ISSUE_TEMPLATE/feature_request.yml @@ -1,5 +1,5 @@ name: Feature Request -description: Propose a new feature for BeBat\Verify +description: Propose a new feature for BeBat/Verify title: "[Feature]: " labels: ["feature"] body: diff --git a/.github/ISSUE_TEMPLATE/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md similarity index 96% rename from .github/ISSUE_TEMPLATE/PULL_REQUEST_TEMPLATE.md rename to .github/PULL_REQUEST_TEMPLATE.md index 463a472..b87ee97 100644 --- a/.github/ISSUE_TEMPLATE/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,5 +1,5 @@