Skip to content

Commit d47851a

Browse files
Laravel: test refactoring (#195)
1 parent 34d218f commit d47851a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+49
-2627
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"laravel/tinker": "*",
2424
"nunomaduro/collision": "*",
2525
"open-telemetry/sdk": "^1.0",
26+
"orchestra/testbench": ">=4.0",
2627
"phan/phan": "^5.0",
2728
"php-http/mock-client": "*",
2829
"phpstan/phpstan": "^1.1",
@@ -42,11 +43,11 @@
4243
},
4344
"autoload-dev": {
4445
"psr-4": {
45-
"App\\": "tests/app/",
46-
"OpenTelemetry\\Tests\\Instrumentation\\Laravel\\": "tests/"
46+
"OpenTelemetry\\Tests\\Contrib\\Instrumentation\\Laravel\\": "tests/"
4747
}
4848
},
4949
"config": {
50+
"lock": false,
5051
"sort-packages": true,
5152
"allow-plugins": {
5253
"php-http/discovery": false

phpunit.xml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
<env name="APP_ENV" value="testing"/>
3737
<env name="BCRYPT_ROUNDS" value="4"/>
3838
<env name="CACHE_DRIVER" value="file"/>
39-
<env name="DB_CONNECTION" value="sqlite_testing"/>
40-
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
39+
<env name="DB_CONNECTION" value="sqlite"/>
40+
<env name="DB_DATABASE" value=":memory:"/>
4141
<env name="MAIL_MAILER" value="array"/>
4242
<env name="QUEUE_CONNECTION" value="sync"/>
4343
<env name="SESSION_DRIVER" value="array"/>

psalm.xml.dist

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@
66
xmlns="https://getpsalm.org/schema/config"
77
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd">
88
<projectFiles>
9-
<ignoreFiles>
10-
<directory name="vendor"/>
11-
<directory name="tests/app"/>
12-
<directory name="tests/bootstrap"/>
13-
<directory name="tests/routes"/>
14-
<directory name="tests/storage"/>
15-
</ignoreFiles>
16-
<directory name="src"/>
9+
<directory name="src"/>
1710
<directory name="tests"/>
1811
</projectFiles>
1912
<plugins>

src/Watchers/ExceptionWatcher.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ public function recordException(MessageLogged $log): void
3131

3232
$exception = $log->context['exception'];
3333

34-
$attributes = [
35-
TraceAttributes::CODE_NAMESPACE => get_class($exception),
36-
TraceAttributes::CODE_FILEPATH => $exception->getFile(),
37-
TraceAttributes::CODE_LINENO => $exception->getLine(),
38-
];
3934
$scope = Context::storage()->scope();
4035
if (!$scope) {
4136
return;

tests/.env

Lines changed: 0 additions & 58 deletions
This file was deleted.

tests/CreatesApplication.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/Integration/ConsoleInstrumentationTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace OpenTelemetry\Tests\Instrumentation\Laravel\Integration;
5+
namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Integration;
66

77
use ArrayObject;
88
use Illuminate\Console\Command;
@@ -13,7 +13,6 @@
1313
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter;
1414
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
1515
use OpenTelemetry\SDK\Trace\TracerProvider;
16-
use OpenTelemetry\Tests\Instrumentation\Laravel\TestCase;
1716

1817
class ConsoleInstrumentationTest extends TestCase
1918
{
@@ -46,9 +45,7 @@ public function test_command_tracing(): void
4645
{
4746
$this->assertCount(0, $this->storage);
4847

49-
/** @var Kernel $kernel */
50-
$kernel = $this->app[Kernel::class];
51-
$exitCode = $kernel->handle(
48+
$exitCode = $this->kernel()->handle(
5249
new \Symfony\Component\Console\Input\ArrayInput(['optimize:clear']),
5350
new \Symfony\Component\Console\Output\NullOutput(),
5451
);
@@ -72,4 +69,10 @@ public function test_command_tracing(): void
7269
$span = $this->storage->offsetGet(--$count);
7370
$this->assertSame('Command optimize:clear', $span->getName());
7471
}
72+
73+
private function kernel(): Kernel
74+
{
75+
/** @psalm-suppress PossiblyNullReference */
76+
return $this->app->make(Kernel::class);
77+
}
7578
}

tests/Integration/LaravelInstrumentationTest.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,19 @@
22

33
declare(strict_types=1);
44

5-
namespace OpenTelemetry\Tests\Instrumentation\Laravel\Integration;
5+
namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Integration;
66

77
use ArrayObject;
8-
use Illuminate\Foundation\Http\Kernel;
9-
use Illuminate\Http\Response;
8+
use Illuminate\Routing\Router;
9+
use Illuminate\Support\Facades\DB;
1010
use Illuminate\Support\Facades\Http;
11+
use Illuminate\Support\Facades\Log;
1112
use OpenTelemetry\API\Instrumentation\Configurator;
1213
use OpenTelemetry\Context\ScopeInterface;
1314
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter;
1415
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
1516
use OpenTelemetry\SDK\Trace\TracerProvider;
1617
use OpenTelemetry\SemConv\TraceAttributes;
17-
use OpenTelemetry\Tests\Instrumentation\Laravel\TestCase;
18-
19-
class ByPassRouterKernel extends Kernel
20-
{
21-
protected function sendRequestThroughRouter($request)
22-
{
23-
return new Response();
24-
}
25-
}
2618

2719
class LaravelInstrumentationTest extends TestCase
2820
{
@@ -56,6 +48,8 @@ public function tearDown(): void
5648

5749
public function test_request_response(): void
5850
{
51+
$this->router()->get('/', fn () => null);
52+
5953
$this->assertCount(0, $this->storage);
6054
$response = $this->call('GET', '/');
6155
$this->assertEquals(200, $response->status());
@@ -70,6 +64,18 @@ public function test_request_response(): void
7064
}
7165
public function test_cache_log_db(): void
7266
{
67+
$this->router()->get('/hello', function () {
68+
$text = 'Hello Cruel World';
69+
cache()->forever('opentelemetry', 'opentelemetry');
70+
Log::info('Log info');
71+
cache()->get('opentelemetry.io', 'php');
72+
cache()->get('opentelemetry', 'php');
73+
cache()->forget('opentelemetry');
74+
DB::select('select 1');
75+
76+
return view('welcome', ['text' => $text]);
77+
});
78+
7379
$this->assertCount(0, $this->storage);
7480
$response = $this->call('GET', '/hello');
7581
$this->assertEquals(200, $response->status());
@@ -91,4 +97,10 @@ public function test_cache_log_db(): void
9197
$this->assertSame('select 1', $span->getAttributes()->get('db.statement'));
9298
$this->assertSame('sqlite', $span->getAttributes()->get('db.system'));
9399
}
100+
101+
private function router(): Router
102+
{
103+
/** @psalm-suppress PossiblyNullReference */
104+
return $this->app->make(Router::class);
105+
}
94106
}

tests/Integration/TestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Integration;
6+
7+
use Orchestra\Testbench\TestCase as BaseTestCase;
8+
9+
abstract class TestCase extends BaseTestCase
10+
{
11+
}

tests/TestCase.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)