diff --git a/Pilot.php b/Pilot.php
index 6cda75f..1bc9a91 100644
--- a/Pilot.php
+++ b/Pilot.php
@@ -7,7 +7,6 @@
use Closure;
use Exception;
-use function array_key_last;
use function count;
use function is_file;
@@ -233,7 +232,7 @@ public function run(int|string|null $id, Closure $test, string $description = ''
*/
public function createReport(int $max_str_length = 500): void
{
- $this->report->create($max_str_length);
+ $this->report->create($max_str_length);
}
/**
@@ -282,7 +281,7 @@ private function getValidRunnerId(int|string|null $id = null): int|string
{
if (isset($id)) {
if (isset($this->runners[$id])) {
- return $this->runners[$id];
+ return $this->runners[$id]->getId();
} else {
throw new Exception("Unknown runner's id: '{$id}'");
}
@@ -293,6 +292,30 @@ private function getValidRunnerId(int|string|null $id = null): int|string
}
}
+ /**
+ * @param Closure $test
+ * @param string|null $test_name
+ * @param mixed $expected
+ */
+ public function assert(Closure $test, ?string $test_name = null, mixed $expected = null)
+ {
+ $runner_id = $this->getValidRunnerId();
+ if ($test() === true) {
+ $this->runners[$runner_id]->addAssertResult(
+ result: true,
+ test_name: $test_name
+ );
+ $this->stats['passed_assertions'] += 1;
+ } else {
+ $this->runners[$runner_id]->addAssertResult(
+ result: false,
+ test_name: $test_name,
+ expected: $expected
+ );
+ $this->stats['failed_assertions'] += 1;
+ }
+ }
+
/**
* @param string|null $test_name
* @throws Exception
@@ -305,13 +328,14 @@ public function addSuccess(string|null $test_name = null): void
}
/**
- * @param array|string $expected
+ * @param array|int|float|string $expected
+ * @param string|null $test_name
* @throws Exception
*/
- public function addFailure(array|string $expected): void
+ public function addFailure(array|int|float|string $expected, string|null $test_name = null): void
{
$runner_id = $this->getValidRunnerId();
- $this->runners[$runner_id]->addAssertResult(result: false, expected: $expected);
+ $this->runners[$runner_id]->addAssertResult(result: false, expected: $expected, test_name: $test_name);
$this->stats['failed_assertions'] += 1;
}
diff --git a/README.md b/README.md
index 3a9b28b..79bfc8c 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# **Exacodis**
-`2021-09-06` `PHP 8.0+` `v.1.0.3`
+`2021-10-22` `PHP 8.0+` `v.1.1.0`
## **A PHP TEST ENGINE**
@@ -22,6 +22,12 @@ Please note that your source code for tests must be perfectly clean: you can't
override a test run or a result nor a resource.
If you do, then the code will fail with an `Exception` until you fix the code.
+**CHANGELOG**
+1. Code improvements
+2. Better report data
+3. Does not break the compatibility with the previous version
+4. Add a new method `assert()` to the `Pilot` class that simplifies the writing of totally dynamic tests
+
**HOW TO USE**
I will directly use `Exacodis` to test itself as a learning support.
@@ -108,7 +114,18 @@ If you want to change the current runner, then you can ask for it:
$pilot->setCurrentRunnerTo('001');
```
Then the assertions will apply to this one (see below: NESTED RUNS)
-
+
+- DYNAMIC ASSERTION
+
+Here's the way to write a dynamic test:
+```php
+$pilot->assert(
+ test: fn() => count($pilot->getRunner('select_001')->getResult()) === 2,
+ test_name: 'Count the records',
+ expected: 2,
+);
+```
+
- COMPLEX TEST CODE
You can write your test code as raw code, especially for complex test code.
diff --git a/Report.php b/Report.php
index cb13066..faff84b 100644
--- a/Report.php
+++ b/Report.php
@@ -169,7 +169,7 @@ public function create(int $max_str_length = 500): void
$html[] = <<