Add tests for the progress value object

This commit is contained in:
Andrea Marco Sartori 2023-06-08 17:47:56 +02:00
parent 18cde0f63d
commit 5a65884255
2 changed files with 36 additions and 0 deletions

View File

@ -21,3 +21,15 @@ it('parses JSON when calling the helper', function (string $json, array $parsed)
it('eager loads JSON into an array', function (string $json, array $parsed) {
expect(JsonParser::parse($json)->toArray())->toBe($parsed);
})->with(Dataset::forParsing());
it('shows the progress while parsing', function () {
$parser = new JsonParser(fixture('json/simple_array.json'));
expect($parser->progress()->percentage())->toBe($percentage = 0.0);
foreach ($parser as $value) {
expect($percentage)->toBeLessThan($percentage = $parser->progress()->percentage());
}
expect($parser->progress()->percentage())->toBe(100.0);
});

View File

@ -0,0 +1,24 @@
<?php
use Cerbero\JsonParser\ValueObjects\Progress;
it('tracks the progress', function () {
$progress = new Progress();
expect($progress)
->current()->toBe(0)
->total()->toBeNull()
->format()->toBeNull()
->percentage()->toBeNull()
->fraction()->toBeNull();
$progress->setTotal(200)->setCurrent(33);
expect($progress)
->current()->toBe(33)
->total()->toBe(200)
->format()->toBe('16.5%')
->percentage()->toBe(16.5)
->fraction()->toBe(0.165);
});