Skip to content

Commit a1b6ecb

Browse files
committed
add tests
1 parent a06809e commit a1b6ecb

File tree

6 files changed

+177
-0
lines changed

6 files changed

+177
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
.idea
2+
.phpunit.result.cache
3+
.phpunit.xml.dist
4+
composer.lock
5+
vendor/

composer.json

+10
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@
1616
"Ilzrv\\LaravelSlowQueryDetector\\": "src"
1717
}
1818
},
19+
"autoload-dev": {
20+
"psr-4": {
21+
"Ilzrv\\LaravelSlowQueryDetector\\Tests\\": "tests"
22+
}
23+
},
1924
"extra": {
2025
"laravel": {
2126
"providers": [
2227
"Ilzrv\\LaravelSlowQueryDetector\\ServiceProvider"
2328
]
2429
}
30+
},
31+
"require-dev": {
32+
"phpunit/phpunit": "^9.1",
33+
"orchestra/testbench": "^5.1",
34+
"timacdonald/log-fake": "^1.6"
2535
}
2636
}

phpunit.xml.dist

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Unit Tests">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

tests/SlowCodeTest.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Ilzrv\LaravelSlowQueryDetector\Tests;
4+
5+
use Illuminate\Support\Facades\Log;
6+
7+
class SlowCodeTest extends TestCase
8+
{
9+
public function testBeNotifiedWhenCodeExceededMaxTime()
10+
{
11+
$this->request(function () {
12+
usleep(1000001); // 1 s
13+
});
14+
15+
Log::assertLogged('critical', function ($message, $context) {
16+
return \Str::contains($message, 'SQD');
17+
});
18+
}
19+
20+
public function testBeSilentWhenCodeRunsFine()
21+
{
22+
$this->request(function () {
23+
usleep(900000); // 0.9 s
24+
});
25+
26+
Log::assertNotLogged('critical');
27+
}
28+
29+
public function testBeNotifiedWhenCodeHasTooManyQueries()
30+
{
31+
$this->request(function () {
32+
$connection = $this->getConnectionWithSleepFunction();
33+
for ($i = 0; $i < 51; $i++) {
34+
$connection->select((\DB::raw('select sleep(1)')));
35+
}
36+
});
37+
38+
Log::assertLogged('critical', function ($message, $context) {
39+
return \Str::contains($message, 'SQD');
40+
});
41+
}
42+
43+
public function testBeSilentWhenCodeHasFineCountQueries()
44+
{
45+
$this->request(function () {
46+
$connection = $this->getConnectionWithSleepFunction();
47+
for ($i = 0; $i < 50; $i++) {
48+
$connection->select((\DB::raw('select sleep(1)')));
49+
}
50+
});
51+
52+
Log::assertNotLogged('critical');
53+
}
54+
}

tests/SlowQueryTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Ilzrv\LaravelSlowQueryDetector\Tests;
4+
5+
use Illuminate\Support\Facades\Log;
6+
7+
class SlowQueryTest extends TestCase
8+
{
9+
public function testBeNotifiedWhenQueryExceededMaxTime()
10+
{
11+
$this->request(function () {
12+
$connection = $this->getConnectionWithSleepFunction();
13+
$connection->select((\DB::raw('select sleep(100)')));
14+
15+
return response('slow');
16+
});
17+
18+
Log::assertLogged('critical', function ($message, $context) {
19+
return \Str::contains($message, 'SQD');
20+
});
21+
}
22+
23+
public function testBeSilentWhenQueryNotExceededMaxTime()
24+
{
25+
$this->request(function () {
26+
$connection = $this->getConnectionWithSleepFunction();
27+
$connection->select((\DB::raw('select sleep(1)')));
28+
29+
return response('fast');
30+
});
31+
32+
Log::assertNotLogged('critical');
33+
}
34+
}

tests/TestCase.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Ilzrv\LaravelSlowQueryDetector\Tests;
4+
5+
use Illuminate\Support\Str;
6+
use Ilzrv\LaravelSlowQueryDetector\Http\Middleware\SlowQueryDetectorMiddleware;
7+
use Ilzrv\LaravelSlowQueryDetector\ServiceProvider;
8+
use Orchestra\Testbench\TestCase as OrchestraTestCase;
9+
use Illuminate\Support\Facades\Log;
10+
use TiMacDonald\Log\LogFake;
11+
12+
class TestCase extends OrchestraTestCase
13+
{
14+
public function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
Log::swap(new LogFake);
19+
}
20+
21+
protected function getPackageProviders($app)
22+
{
23+
return [ServiceProvider::class];
24+
}
25+
26+
public function request(callable $callback)
27+
{
28+
$uri = Str::random();
29+
30+
$this->app['router']->get($uri, $callback)->middleware(SlowQueryDetectorMiddleware::class);
31+
32+
$this->get($uri);
33+
}
34+
35+
public function getConnectionWithSleepFunction($name = 'sqn')
36+
{
37+
app()['config']->set('database.connections.'.$name, [
38+
'driver' => 'sqlite',
39+
'database' => ':memory:',
40+
'prefix' => '',
41+
]);
42+
43+
$connection = \DB::connection($name);
44+
$pdo = $connection->getPdo();
45+
$pdo->sqliteCreateFunction(
46+
'sleep',
47+
function ($miliseconds) {
48+
return usleep($miliseconds * 1000);
49+
}
50+
);
51+
return $connection;
52+
}
53+
}

0 commit comments

Comments
 (0)