Skip to content

Commit

Permalink
Laravel: test refactoring (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLightfootWild authored Sep 24, 2023
1 parent 34d218f commit d47851a
Show file tree
Hide file tree
Showing 65 changed files with 49 additions and 2,627 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"laravel/tinker": "*",
"nunomaduro/collision": "*",
"open-telemetry/sdk": "^1.0",
"orchestra/testbench": ">=4.0",
"phan/phan": "^5.0",
"php-http/mock-client": "*",
"phpstan/phpstan": "^1.1",
Expand All @@ -42,11 +43,11 @@
},
"autoload-dev": {
"psr-4": {
"App\\": "tests/app/",
"OpenTelemetry\\Tests\\Instrumentation\\Laravel\\": "tests/"
"OpenTelemetry\\Tests\\Contrib\\Instrumentation\\Laravel\\": "tests/"
}
},
"config": {
"lock": false,
"sort-packages": true,
"allow-plugins": {
"php-http/discovery": false
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="file"/>
<env name="DB_CONNECTION" value="sqlite_testing"/>
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
Expand Down
9 changes: 1 addition & 8 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd">
<projectFiles>
<ignoreFiles>
<directory name="vendor"/>
<directory name="tests/app"/>
<directory name="tests/bootstrap"/>
<directory name="tests/routes"/>
<directory name="tests/storage"/>
</ignoreFiles>
<directory name="src"/>
<directory name="src"/>
<directory name="tests"/>
</projectFiles>
<plugins>
Expand Down
5 changes: 0 additions & 5 deletions src/Watchers/ExceptionWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ public function recordException(MessageLogged $log): void

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

$attributes = [
TraceAttributes::CODE_NAMESPACE => get_class($exception),
TraceAttributes::CODE_FILEPATH => $exception->getFile(),
TraceAttributes::CODE_LINENO => $exception->getLine(),
];
$scope = Context::storage()->scope();
if (!$scope) {
return;
Expand Down
58 changes: 0 additions & 58 deletions tests/.env

This file was deleted.

23 changes: 0 additions & 23 deletions tests/CreatesApplication.php

This file was deleted.

13 changes: 8 additions & 5 deletions tests/Integration/ConsoleInstrumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace OpenTelemetry\Tests\Instrumentation\Laravel\Integration;
namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Integration;

use ArrayObject;
use Illuminate\Console\Command;
Expand All @@ -13,7 +13,6 @@
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
use OpenTelemetry\Tests\Instrumentation\Laravel\TestCase;

class ConsoleInstrumentationTest extends TestCase
{
Expand Down Expand Up @@ -46,9 +45,7 @@ public function test_command_tracing(): void
{
$this->assertCount(0, $this->storage);

/** @var Kernel $kernel */
$kernel = $this->app[Kernel::class];
$exitCode = $kernel->handle(
$exitCode = $this->kernel()->handle(
new \Symfony\Component\Console\Input\ArrayInput(['optimize:clear']),
new \Symfony\Component\Console\Output\NullOutput(),
);
Expand All @@ -72,4 +69,10 @@ public function test_command_tracing(): void
$span = $this->storage->offsetGet(--$count);
$this->assertSame('Command optimize:clear', $span->getName());
}

private function kernel(): Kernel
{
/** @psalm-suppress PossiblyNullReference */
return $this->app->make(Kernel::class);
}
}
36 changes: 24 additions & 12 deletions tests/Integration/LaravelInstrumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,19 @@

declare(strict_types=1);

namespace OpenTelemetry\Tests\Instrumentation\Laravel\Integration;
namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Integration;

use ArrayObject;
use Illuminate\Foundation\Http\Kernel;
use Illuminate\Http\Response;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use OpenTelemetry\API\Instrumentation\Configurator;
use OpenTelemetry\Context\ScopeInterface;
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
use OpenTelemetry\SemConv\TraceAttributes;
use OpenTelemetry\Tests\Instrumentation\Laravel\TestCase;

class ByPassRouterKernel extends Kernel
{
protected function sendRequestThroughRouter($request)
{
return new Response();
}
}

class LaravelInstrumentationTest extends TestCase
{
Expand Down Expand Up @@ -56,6 +48,8 @@ public function tearDown(): void

public function test_request_response(): void
{
$this->router()->get('/', fn () => null);

$this->assertCount(0, $this->storage);
$response = $this->call('GET', '/');
$this->assertEquals(200, $response->status());
Expand All @@ -70,6 +64,18 @@ public function test_request_response(): void
}
public function test_cache_log_db(): void
{
$this->router()->get('/hello', function () {
$text = 'Hello Cruel World';
cache()->forever('opentelemetry', 'opentelemetry');
Log::info('Log info');
cache()->get('opentelemetry.io', 'php');
cache()->get('opentelemetry', 'php');
cache()->forget('opentelemetry');
DB::select('select 1');

return view('welcome', ['text' => $text]);
});

$this->assertCount(0, $this->storage);
$response = $this->call('GET', '/hello');
$this->assertEquals(200, $response->status());
Expand All @@ -91,4 +97,10 @@ public function test_cache_log_db(): void
$this->assertSame('select 1', $span->getAttributes()->get('db.statement'));
$this->assertSame('sqlite', $span->getAttributes()->get('db.system'));
}

private function router(): Router
{
/** @psalm-suppress PossiblyNullReference */
return $this->app->make(Router::class);
}
}
11 changes: 11 additions & 0 deletions tests/Integration/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Integration;

use Orchestra\Testbench\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
}
12 changes: 0 additions & 12 deletions tests/TestCase.php

This file was deleted.

29 changes: 0 additions & 29 deletions tests/app/Console/Kernel.php

This file was deleted.

50 changes: 0 additions & 50 deletions tests/app/Exceptions/Handler.php

This file was deleted.

14 changes: 0 additions & 14 deletions tests/app/Http/Controllers/Controller.php

This file was deleted.

Loading

0 comments on commit d47851a

Please sign in to comment.