PHP code example of ralder / phpunit-query-scaling
1. Go to this page and download the library: Download ralder/phpunit-query-scaling library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ralder / phpunit-query-scaling example snippets
use PHPUnit\Framework\TestCase;
use Ralder\QueryScaling\Assertions\QueryScalingAssertions;
use Ralder\QueryScaling\QueryGrowth;
final class PostIndexQueryTest extends TestCase
{
use QueryScalingAssertions;
public function test_index_scales_constantly(): void
{
$this->assertQueriesScaleConstantly(
scales: [2, 5, 10],
// Populate $scale rows on EVERY scale run; runs against a clean dataset.
populate: function (int $scale): void {
Post::factory()->count($scale)->hasComments(2)->create();
},
// The measured scenario; exactly $run() queries are counted.
run: function (): void {
$posts = Post::with('comments')->get();
$this->assertTrue($posts->every->comments->isNotEmpty());
},
);
}
public function test_comment_counts_scale_linearly_at_most(): void
{
$this->assertQueryScaling(
scales: [2, 5, 10],
expectation: QueryGrowth::atMostSlope(1, 0),
populate: function (int $scale): void {
Post::factory()->count($scale)->create();
},
run: function (): void {
// one lookup query per post is acceptable; two per post is not
Post::all()->each->comments->count();
},
);
}
}
use Ralder\QueryScaling\Assertions\QueryScalingAssertions;
use Ralder\QueryScaling\QueryGrowth;
uses(Tests\TestCase::class, QueryScalingAssertions::class);
test('index scales constantly', function (): void {
$this->assertQueriesScaleConstantly(
scales: [2, 5, 10],
populate: fn (int $scale) => Post::factory()->count($scale)->create(),
run: function (): void {
$this->getJson('/api/posts')->assertSuccessful();
},
);
});