Skip to content

Commit

Permalink
New version: 1.1.0
Browse files Browse the repository at this point in the history
Code improvements
Better report data
New method `assert()` to write dynamic tests
  • Loading branch information
rawsrc committed Oct 22, 2021
1 parent bb3dc3f commit a96979a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
36 changes: 30 additions & 6 deletions Pilot.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Closure;
use Exception;

use function array_key_last;
use function count;
use function is_file;

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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}'");
}
Expand All @@ -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
Expand All @@ -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;
}

Expand Down
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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**

Expand All @@ -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.<br>
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.
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function create(int $max_str_length = 500): void
$html[] = <<<html
<tr>
<td rowspan="2">&nbsp;</td>
<td class="failed" rowspan="2">FAILED</td>
<td class="failed" rowspan="2">FAILED: {$this((string)$test_name)}</td>
<td class="failed">Expected</td>
<td class="failed">{$this($short_string(print_r($expected, true)))}</td>
</tr>
Expand Down
5 changes: 3 additions & 2 deletions Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use Exception;

use function count;
use function hrtime;

/**
Expand Down Expand Up @@ -105,10 +106,10 @@ public function getDescription(): string

/**
* @param bool $result
* @param string|null $expected
* @param array|int|float|string|null $expected
* @param string|null $test_name
*/
public function addAssertResult(bool $result, string|null $expected = null, string|null $test_name = null): void
public function addAssertResult(bool $result, array|int|float|string|null $expected = null, string|null $test_name = null): void
{
$this->assert_results[] = ['result' => $result, 'expected' => $expected, 'test_name' => $test_name];
}
Expand Down

0 comments on commit a96979a

Please sign in to comment.