diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ce9b1d5..c6b5b8b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -9,27 +9,26 @@ jobs: strategy: fail-fast: true matrix: - php: [ "8.2", "8.3", "8.4" ] - symfony: [ "6.0", "7.0" ] + php: [ "8.2", "8.3", "8.4", "8.5" ] - name: PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony }} ${{ matrix.prefer }} + name: PHP ${{ matrix.php }} steps: - - name: Checkout code - uses: actions/checkout@v6 + - uses: actions/checkout@v6 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} - extensions: curl, mbstring, zip, pcntl, pdo, pdo_sqlite, iconv coverage: none - name: Install dependencies run: | - composer require --no-interaction \ - symfony/console:^${{ matrix.symfony }} \ - symfony/var-dumper:^${{ matrix.symfony }} + composer update \ + --no-audit \ + --no-suggest \ + --no-interaction + - name: Execute tests run: composer test diff --git a/UPGRADING.md b/UPGRADING.md index 69bc66e..c01ff02 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -6,3 +6,4 @@ - Метод `prepare` переименован в `beforeEach` - Метод `start` переименован в `make` - Исправлена типизация входящих колбэков с супер-класса `callable` на `Closure` +- Класс `ValueIsNotCallableException` теперь принимает объект вместо строки типа. diff --git a/composer.json b/composer.json index e0f77cf..95977db 100644 --- a/composer.json +++ b/composer.json @@ -38,12 +38,11 @@ ], "require": { "php": "^8.2", - "dragon-code/support": "^6.10", - "symfony/console": "^6.0 || ^7.0" + "dragon-code/support": "^6.10" }, "require-dev": { "pestphp/pest": "^4.3", - "symfony/var-dumper": "^6.0 || ^7.0" + "symfony/var-dumper": "^7.0 || ^8.0" }, "minimum-stability": "stable", "prefer-stable": true, diff --git a/src/Benchmark.php b/src/Benchmark.php index 2c88014..29fc0b9 100644 --- a/src/Benchmark.php +++ b/src/Benchmark.php @@ -6,13 +6,11 @@ use Closure; use DragonCode\Benchmark\Exceptions\ValueIsNotCallableException; +use DragonCode\Benchmark\Services\Callbacks; use DragonCode\Benchmark\Services\Runner; use DragonCode\Benchmark\Services\View; use DragonCode\Benchmark\Transformers\Transformer; -use Symfony\Component\Console\Helper\ProgressBar as ProgressBarService; -use Symfony\Component\Console\Input\ArgvInput; -use Symfony\Component\Console\Output\ConsoleOutput; -use Symfony\Component\Console\Style\SymfonyStyle; +use DragonCode\Benchmark\View\ProgressBarView; use function count; use function func_get_args; @@ -23,14 +21,8 @@ class Benchmark { - protected View $view; - protected int $iterations = 100; - protected ?Closure $beforeEach = null; - - protected ?Closure $afterEach = null; - protected array $result = [ 'each' => [], 'total' => [], @@ -38,15 +30,10 @@ class Benchmark public function __construct( protected Runner $runner = new Runner, - protected Transformer $transformer = new Transformer - ) { - $this->view = new View( - new SymfonyStyle( - new ArgvInput, - new ConsoleOutput - ) - ); - } + protected Transformer $transformer = new Transformer, + protected View $view = new View, + protected Callbacks $callbacks = new Callbacks, + ) {} public static function make(): static { @@ -55,14 +42,14 @@ public static function make(): static public function beforeEach(Closure $callback): self { - $this->beforeEach = $callback; + $this->callbacks->beforeEach = $callback; return $this; } public function afterEach(Closure $callback): self { - $this->afterEach = $callback; + $this->callbacks->afterEach = $callback; return $this; } @@ -81,14 +68,23 @@ public function round(?int $precision): self return $this; } - public function compare(array|Closure ...$callbacks): void + public function compare(array|Closure ...$callbacks): static { $values = is_array($callbacks[0]) ? $callbacks[0] : func_get_args(); $this->withProgress($values, $this->stepsCount($values)); $this->show(); + + return $this; } + /** + * @return \DragonCode\Benchmark\Data\ResultData[] + */ + public function toData(): array {} + + public function toConsole(): void {} + protected function withProgress(array $callbacks, int $count): void { $bar = $this->view->progressBar()->create($count); @@ -104,7 +100,7 @@ protected function stepsCount(array $callbacks): int return count($callbacks) * $this->iterations; } - protected function chunks(array $callbacks, ProgressBarService $progressBar): void + protected function chunks(array $callbacks, ProgressBarView $progressBar): void { foreach ($callbacks as $name => $callback) { $this->validate($callback); @@ -113,21 +109,21 @@ protected function chunks(array $callbacks, ProgressBarService $progressBar): vo } } - protected function each(mixed $name, Closure $callback, ProgressBarService $progressBar): void + protected function each(mixed $name, Closure $callback, ProgressBarView $progressBar): void { $this->result['total'][$name] = $this->call( fn () => $this->run($name, $callback, $progressBar) ); } - protected function run(mixed $name, Closure $callback, ProgressBarService $progressBar): void + protected function run(mixed $name, Closure $callback, ProgressBarView $progressBar): void { for ($i = 1; $i <= $this->iterations; ++$i) { - $result = $this->runCallback($this->beforeEach, $name, $i); + $result = $this->callbacks->performBeforeEach($name, $i); [$time, $ram] = $this->call($callback, [$i, $result]); - $this->runCallback($this->afterEach, $name, $i, $time, $ram); + $this->callbacks->performAfterEach($name, $i, $time, $ram); $this->push($name, $i, $time, $ram); @@ -135,15 +131,6 @@ protected function run(mixed $name, Closure $callback, ProgressBarService $progr } } - protected function runCallback(?Closure $callback, mixed ...$arguments): mixed - { - if (! $callback) { - return null; - } - - return $callback(...$arguments); - } - protected function call(Closure $callback, array $parameters = []): array { return $this->runner->call($callback, $parameters); diff --git a/src/Exceptions/ValueIsNotCallableException.php b/src/Exceptions/ValueIsNotCallableException.php index 6ef349f..b75ce51 100644 --- a/src/Exceptions/ValueIsNotCallableException.php +++ b/src/Exceptions/ValueIsNotCallableException.php @@ -6,10 +6,12 @@ use TypeError; +use function gettype; + class ValueIsNotCallableException extends TypeError { - public function __construct(string $actualType) + public function __construct(mixed $value) { - parent::__construct(sprintf('The array value must be of type callable, %s given.', $actualType), 500); + parent::__construct(sprintf('The array value must be of type Closure, %s given.', gettype($value)), 500); } } diff --git a/src/Services/Callbacks.php b/src/Services/Callbacks.php new file mode 100644 index 0000000..3e1fe1f --- /dev/null +++ b/src/Services/Callbacks.php @@ -0,0 +1,47 @@ +perform($this->before, $name); + } + + public function performBeforeEach(string|int $name, int $iteration): mixed + { + return $this->perform($this->beforeEach, $name, $iteration); + } + + public function performAfter(string|int $name): mixed + { + return $this->perform($this->after, $name); + } + + public function performAfterEach(string|int $name, int $iteration, float $time, float $ram): mixed + { + return $this->perform($this->afterEach, $name, $iteration, $time, $ram); + } + + protected function perform(?Closure $callback, mixed ...$args): mixed + { + if ($callback === null) { + return null; + } + + return $callback(...$args); + } +} diff --git a/src/Services/View.php b/src/Services/View.php index 9f77901..11a3d28 100644 --- a/src/Services/View.php +++ b/src/Services/View.php @@ -4,9 +4,8 @@ namespace DragonCode\Benchmark\Services; -use DragonCode\Benchmark\View\ProgressBar; -use DragonCode\Benchmark\View\Table; -use Symfony\Component\Console\Style\SymfonyStyle; +use DragonCode\Benchmark\View\ProgressBarView; +use DragonCode\Benchmark\View\TableView; use function is_array; use function is_numeric; @@ -15,19 +14,13 @@ class View { - protected Table $table; - - protected ProgressBar $progressBar; - protected ?int $roundPrecision = null; public function __construct( - protected SymfonyStyle $io, - protected Memory $memory = new Memory - ) { - $this->table = new Table($this->io); - $this->progressBar = new ProgressBar($this->io); - } + protected Memory $memory = new Memory, + protected TableView $table = new TableView, + protected ProgressBarView $progressBar = new ProgressBarView, + ) {} public function setRound(?int $precision): void { @@ -41,14 +34,14 @@ public function table(array $data): void ); } - public function progressBar(): ProgressBar + public function progressBar(): ProgressBarView { return $this->progressBar; } public function emptyLine(int $count = 1): void { - $this->io->newLine($count); + echo "\r"; } protected function appendMs(array $data): array diff --git a/src/Transformers/Separator.php b/src/Transformers/Separator.php index 9584951..06ec53b 100644 --- a/src/Transformers/Separator.php +++ b/src/Transformers/Separator.php @@ -4,8 +4,6 @@ namespace DragonCode\Benchmark\Transformers; -use Symfony\Component\Console\Helper\TableSeparator; - class Separator extends Base { public function transform(array $data): array diff --git a/src/View/ProgressBar.php b/src/View/ProgressBar.php deleted file mode 100644 index a178446..0000000 --- a/src/View/ProgressBar.php +++ /dev/null @@ -1,20 +0,0 @@ -io->createProgressBar($count); - } -} diff --git a/src/View/ProgressBarView.php b/src/View/ProgressBarView.php new file mode 100644 index 0000000..b7e30b8 --- /dev/null +++ b/src/View/ProgressBarView.php @@ -0,0 +1,69 @@ +total = max(1, $total); + + $this->display(); + + return $this; + } + + public function advance(int $step = 1): void + { + $this->current += $step; + + $this->display(); + } + + public function finish(): void + { + $this->current = $this->total; + + $this->display(); + + $this->write(PHP_EOL); + } + + protected function display(): void + { + $percent = $this->current / $this->total; + $filled = (int) floor($percent * $this->barWidth); + $empty = $this->barWidth - $filled; + $percText = (int) floor($percent * 100); + + $bar = str_repeat('▓', $filled) . str_repeat('░', $empty); + + $line = " {$this->current}/{$this->total} [{$bar}] {$percText}%"; + + $this->write("\r" . $line); + } + + /** + * @return resource + */ + protected function stream() + { + if (static::$stream === null) { + static::$stream = fopen('php://stderr', 'w'); + } + + return static::$stream; + } +} diff --git a/src/View/Table.php b/src/View/Table.php deleted file mode 100644 index c25b14d..0000000 --- a/src/View/Table.php +++ /dev/null @@ -1,27 +0,0 @@ -io->table($this->headers($data), $data); - } - - protected function headers(array $data): array - { - return array_keys(array_values($data)[0]); - } -} diff --git a/src/View/TableView.php b/src/View/TableView.php new file mode 100644 index 0000000..fb545db --- /dev/null +++ b/src/View/TableView.php @@ -0,0 +1,80 @@ +headers($data); + $widths = $this->columnWidths($headers, $data); + + $this->writeLine($this->separator($widths)); + $this->writeLine($this->formatRow($headers, $widths)); + $this->writeLine($this->separator($widths)); + + foreach ($data as $row) { + $this->writeLine($this->formatRow(array_values($row), $widths)); + } + + $this->writeLine($this->separator($widths)); + } + + protected function headers(array $data): array + { + return array_keys(array_values($data)[0]); + } + + protected function columnWidths(array $headers, array $data): array + { + $widths = array_map(fn ($header) => mb_strlen((string) $header), $headers); + + foreach ($data as $row) { + foreach (array_values($row) as $i => $value) { + $widths[$i] = max($widths[$i], mb_strlen((string) $value)); + } + } + + return $widths; + } + + protected function separator(array $widths): string + { + $parts = array_map(fn (int $w) => str_repeat('-', $w + 2), $widths); + + return '+' . implode('+', $parts) . '+'; + } + + protected function formatRow(array $values, array $widths): string + { + $cells = []; + + foreach ($values as $i => $value) { + $cells[] = ' ' . mb_str_pad((string) $value, $widths[$i]) . ' '; + } + + return '|' . implode('|', $cells) . '|'; + } + + /** + * @return resource + */ + protected function stream() + { + if (static::$stream === null) { + static::$stream = fopen('php://stdout', 'w'); + } + + return static::$stream; + } +} diff --git a/src/View/View.php b/src/View/View.php new file mode 100644 index 0000000..fdf23b7 --- /dev/null +++ b/src/View/View.php @@ -0,0 +1,25 @@ +stream(), $line . PHP_EOL); + } + + protected function write(string $line): void + { + fwrite($this->stream(), $line); + } + + /** + * @return resource + */ + abstract protected function stream(); +} diff --git a/tests/Helpers/benchmark.php b/tests/Helpers/benchmark.php index cf4bdc8..c780756 100644 --- a/tests/Helpers/benchmark.php +++ b/tests/Helpers/benchmark.php @@ -6,5 +6,5 @@ function benchmark(): Benchmark { - return new Benchmark; + return (new Benchmark)->iterations(3); } diff --git a/tests/Pest.php b/tests/Pest.php index 2eaebdd..29ad8da 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -2,4 +2,4 @@ pest() ->extend(Tests\TestCase::class) - ->in('Feature', 'Unit'); + ->in('Unit'); diff --git a/tests/TestCase.php b/tests/TestCase.php index f29a091..ad62f2f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,10 +6,4 @@ use PHPUnit\Framework\TestCase as BaseTestCase; -abstract class TestCase extends BaseTestCase -{ - protected function work(): void - { - usleep(10); - } -} +abstract class TestCase extends BaseTestCase {} diff --git a/tests/Unit/Architecture/CreateTest.php b/tests/Unit/Architecture/CreateTest.php new file mode 100644 index 0000000..b781170 --- /dev/null +++ b/tests/Unit/Architecture/CreateTest.php @@ -0,0 +1,19 @@ +toBeInstanceOf(Benchmark::class); +}); + +test('static', function () { + expect(Benchmark::make())->toBeInstanceOf(Benchmark::class); +}); + +test('helper', function () { + expect(benchmark())->toBeInstanceOf(Benchmark::class); +}); diff --git a/tests/Unit/AsArrayTest.php b/tests/Unit/AsArrayTest.php deleted file mode 100644 index 5c220f7..0000000 --- a/tests/Unit/AsArrayTest.php +++ /dev/null @@ -1,46 +0,0 @@ -compare([ - 'foo' => fn () => $this->work(), - 'bar' => fn () => $this->work(), - ]); - - expect(true)->toBeTrue(); -}); - -test('iterations', function () { - benchmark()->iterations(5)->compare([ - 'foo' => fn () => $this->work(), - 'bar' => fn () => $this->work(), - ]); - - benchmark()->iterations(500)->compare([ - 'foo' => fn () => $this->work(), - 'bar' => fn () => $this->work(), - ]); - - expect(true)->toBeTrue(); -}); - -test('without data', function () { - benchmark()->compare([ - 'foo' => fn () => $this->work(), - 'bar' => fn () => $this->work(), - ]); - - expect(true)->toBeTrue(); -}); - -test('round', function () { - benchmark()->round(2)->compare([ - 'foo' => fn () => $this->work(), - 'bar' => fn () => $this->work(), - ]); - - expect(true)->toBeTrue(); -}); diff --git a/tests/Unit/AsCallbackTest.php b/tests/Unit/AsCallbackTest.php deleted file mode 100644 index 5cca9ba..0000000 --- a/tests/Unit/AsCallbackTest.php +++ /dev/null @@ -1,46 +0,0 @@ -compare( - fn () => $this->work(), - fn () => $this->work(), - ); - - expect(true)->toBeTrue(); -}); - -test('iterations', function () { - benchmark()->iterations(5)->compare( - fn () => $this->work(), - fn () => $this->work(), - ); - - benchmark()->iterations(500)->compare( - fn () => $this->work(), - fn () => $this->work(), - ); - - expect(true)->toBeTrue(); -}); - -test('without data', function () { - benchmark()->compare( - fn () => $this->work(), - fn () => $this->work(), - ); - - expect(true)->toBeTrue(); -}); - -test('round', function () { - benchmark()->round(2)->iterations(5)->compare( - fn () => $this->work(), - fn () => $this->work(), - ); - - expect(true)->toBeTrue(); -}); diff --git a/tests/Unit/Callbacks/AfterEachTest.php b/tests/Unit/Callbacks/AfterEachTest.php new file mode 100644 index 0000000..61265d3 --- /dev/null +++ b/tests/Unit/Callbacks/AfterEachTest.php @@ -0,0 +1,108 @@ +afterEach(function () use (&$result) { + $result[] = 1; + }) + ->compare([ + fn () => true, + fn () => true, + ]); + + expect($result)->toHaveCount(6); +}); + +test('names only', function () { + $result = []; + + benchmark() + ->afterEach(function (mixed $name) use (&$result) { + $result[] = $name; + }) + ->compare([ + 'foo' => fn () => true, + 'bar' => fn () => true, + ]); + + expect($result)->toBe([ + 'foo', + 'foo', + 'foo', + 'bar', + 'bar', + 'bar', + ]); +}); + +test('iterations', function () { + $result = []; + + benchmark() + ->afterEach(function (mixed $name, int $iteration) use (&$result) { + $result[] = $name . ':' . $iteration; + }) + ->compare([ + 'foo' => fn () => true, + 'bar' => fn () => true, + ]); + + expect($result)->toBe([ + 'foo:1', + 'foo:2', + 'foo:3', + 'bar:1', + 'bar:2', + 'bar:3', + ]); +}); + +test('array without names', function () { + $result = []; + + benchmark() + ->afterEach(function (mixed $name, int $iteration) use (&$result) { + $result[] = $name . ':' . $iteration; + }) + ->compare([ + fn () => true, + fn () => true, + ]); + + expect($result)->toBe([ + '0:1', + '0:2', + '0:3', + '1:1', + '1:2', + '1:3', + ]); +}); + +test('callback without names', function () { + $result = []; + + benchmark() + ->afterEach(function (mixed $name, int $iteration) use (&$result) { + $result[] = $name . ':' . $iteration; + }) + ->compare( + fn () => true, + fn () => true, + ); + + expect($result)->toBe([ + '0:1', + '0:2', + '0:3', + '1:1', + '1:2', + '1:3', + ]); +}); diff --git a/tests/Unit/Callbacks/BeforeEachTest.php b/tests/Unit/Callbacks/BeforeEachTest.php new file mode 100644 index 0000000..9e366d4 --- /dev/null +++ b/tests/Unit/Callbacks/BeforeEachTest.php @@ -0,0 +1,108 @@ +beforeEach(function () use (&$result) { + $result[] = 1; + }) + ->compare([ + fn () => true, + fn () => true, + ]); + + expect($result)->toHaveCount(6); +}); + +test('names only', function () { + $result = []; + + benchmark() + ->beforeEach(function (mixed $name) use (&$result) { + $result[] = $name; + }) + ->compare([ + 'foo' => fn () => true, + 'bar' => fn () => true, + ]); + + expect($result)->toBe([ + 'foo', + 'foo', + 'foo', + 'bar', + 'bar', + 'bar', + ]); +}); + +test('iterations', function () { + $result = []; + + benchmark() + ->beforeEach(function (mixed $name, int $iteration) use (&$result) { + $result[] = $name . ':' . $iteration; + }) + ->compare([ + 'foo' => fn () => true, + 'bar' => fn () => true, + ]); + + expect($result)->toBe([ + 'foo:1', + 'foo:2', + 'foo:3', + 'bar:1', + 'bar:2', + 'bar:3', + ]); +}); + +test('array without names', function () { + $result = []; + + benchmark() + ->beforeEach(function (mixed $name, int $iteration) use (&$result) { + $result[] = $name . ':' . $iteration; + }) + ->compare([ + fn () => true, + fn () => true, + ]); + + expect($result)->toBe([ + '0:1', + '0:2', + '0:3', + '1:1', + '1:2', + '1:3', + ]); +}); + +test('callback without names', function () { + $result = []; + + benchmark() + ->beforeEach(function (mixed $name, int $iteration) use (&$result) { + $result[] = $name . ':' . $iteration; + }) + ->compare( + fn () => true, + fn () => true, + ); + + expect($result)->toBe([ + '0:1', + '0:2', + '0:3', + '1:1', + '1:2', + '1:3', + ]); +}); diff --git a/tests/Unit/Compares/AsArrayTest.php b/tests/Unit/Compares/AsArrayTest.php new file mode 100644 index 0000000..93b7d5a --- /dev/null +++ b/tests/Unit/Compares/AsArrayTest.php @@ -0,0 +1,29 @@ +compare([ + 'foo' => fn () => true, + 'bar' => fn () => true, + ])->toData(); + + expect($results)->toHaveKeys([ + 'foo', + 'bar', + ]); +}); + +test('without names', function () { + $results = benchmark()->compare([ + fn () => true, + fn () => true, + ])->toData(); + + expect($results)->toHaveKeys([ + 0, + 1, + ]); +}); diff --git a/tests/Unit/Compares/AsCallbackTest.php b/tests/Unit/Compares/AsCallbackTest.php new file mode 100644 index 0000000..f1b06c8 --- /dev/null +++ b/tests/Unit/Compares/AsCallbackTest.php @@ -0,0 +1,29 @@ +compare( + foo: fn () => true, + bar: fn () => true, + )->toData(); + + expect($results)->toHaveKeys([ + 'foo', + 'bar', + ]); +}); + +test('without names', function () { + $results = benchmark()->compare( + fn () => true, + fn () => true, + )->toData(); + + expect($results)->toHaveKeys([ + 0, + 1, + ]); +}); diff --git a/tests/Unit/CoverageTest.php b/tests/Unit/CoverageTest.php deleted file mode 100644 index 8a8a4bf..0000000 --- a/tests/Unit/CoverageTest.php +++ /dev/null @@ -1,17 +0,0 @@ -iterations(2)->compare( - fn () => $this->work(), - fn () => $this->work(), - fn () => $this->work(), - fn () => $this->work(), - fn () => $this->work(), - ); - - expect(true)->toBeTrue(); -}); diff --git a/tests/Unit/CreateTest.php b/tests/Unit/CreateTest.php deleted file mode 100644 index 83b8203..0000000 --- a/tests/Unit/CreateTest.php +++ /dev/null @@ -1,15 +0,0 @@ -toBeInstanceOf(Benchmark::class); -}); - -test('as static', function () { - expect(Benchmark::make())->toBeInstanceOf(Benchmark::class); -}); diff --git a/tests/Unit/Fails/FailureTest.php b/tests/Unit/Fails/FailureTest.php new file mode 100644 index 0000000..6907f0d --- /dev/null +++ b/tests/Unit/Fails/FailureTest.php @@ -0,0 +1,30 @@ +compare(123); +})->throws(TypeError::class, 'must be of type Closure|array, int given'); + +test('named array', function () { + benchmark()->compare([ + 'foo' => 123, + ]); +})->throws(ValueIsNotCallableException::class, 'The array value must be of type Closure, integer given.'); + +test('unnamed array', function () { + benchmark()->compare([ + 123, + ]); +})->throws(ValueIsNotCallableException::class, 'The array value must be of type Closure, integer given.'); + +test('callback', function () { + benchmark()->compare( + fn () => 123, + ); +})->throws(ValueIsNotCallableException::class, 'The array value must be of type Closure, integer given.'); diff --git a/tests/Unit/FailureTest.php b/tests/Unit/FailureTest.php deleted file mode 100644 index 349f9eb..0000000 --- a/tests/Unit/FailureTest.php +++ /dev/null @@ -1,41 +0,0 @@ -compare(123); -})->throws(TypeError::class, 'must be of type Closure|array, int given'); - -test('as array', function () { - benchmark()->compare([ - 'first' => 123, - 'second' => 123, - ]); -})->throws(ValueIsNotCallableException::class, 'The array value must be of type callable, integer given.'); - -test('as properties with iterations', function () { - benchmark()->iterations(5)->compare(123); -})->throws(TypeError::class, 'must be of type Closure|array, int given'); - -test('as array with iterations', function () { - benchmark()->iterations(5)->compare([ - 'first' => 123, - 'second' => 123, - ]); -})->throws(ValueIsNotCallableException::class, 'The array value must be of type callable, integer given.'); - -test('as properties without data', function () { - benchmark()->compare(123); -})->throws(TypeError::class, 'must be of type Closure|array, int given'); - -test('as array without data', function () { - benchmark()->compare([ - 'first' => 123, - 'second' => 123, - ]); -})->throws(ValueIsNotCallableException::class, 'The array value must be of type callable, integer given.'); diff --git a/tests/Unit/HardTest.php b/tests/Unit/HardTest.php deleted file mode 100644 index f9d4ef2..0000000 --- a/tests/Unit/HardTest.php +++ /dev/null @@ -1,27 +0,0 @@ -iterations(10)->compare( - fn () => $process(), - fn () => $process() - ); - - expect(true)->toBeTrue(); -}); diff --git a/tests/Unit/PrepareTest.php b/tests/Unit/PrepareTest.php deleted file mode 100644 index 4f8d525..0000000 --- a/tests/Unit/PrepareTest.php +++ /dev/null @@ -1,83 +0,0 @@ -iterations(3) - ->beforeEach(function () use (&$result) { - $result[] = 1; - }) - ->compare([ - 'foo' => fn () => $this->work(), - 'bar' => fn () => $this->work(), - ]); - - expect(count($result))->toBe(6); -}); - -test('parameters', function () { - $result = []; - - benchmark() - ->iterations(3) - ->beforeEach(function (mixed $name, int $iteration) use (&$result) { - $result[] = sprintf('%s:%d', $name, $iteration); - }) - ->compare([ - 'foo' => fn () => $this->work(), - 'bar' => fn () => $this->work(), - ]); - - expect(count($result))->toBe(6); - - expect($result)->toBe([ - 'foo:1', - 'foo:2', - 'foo:3', - 'bar:1', - 'bar:2', - 'bar:3', - ]); -}); - -test('name', function () { - $result = []; - - benchmark() - ->iterations(3) - ->beforeEach(function (mixed $name) use (&$result) { - $result[] = $name; - }) - ->compare([ - 'foo' => fn () => $this->work(), - 'bar' => fn () => $this->work(), - ]); - - expect(count($result))->toBe(6); - - expect($result)->toBe([ - 'foo', - 'foo', - 'foo', - 'bar', - 'bar', - 'bar', - ]); -}); - -test('prepare result', function () { - benchmark() - ->iterations(3) - ->beforeEach( - fn (mixed $name, int $iteration) => sprintf('%s:%d', $name, $iteration) - ) - ->compare([ - 'foo' => fn (int $iteration, string $result) => expect($result)->toBe('foo:' . $iteration), - 'bar' => fn (int $iteration, string $result) => expect($result)->toBe('bar:' . $iteration), - ]); -});