Skip to content

Commit

Permalink
adding basic cakephp test, fixing some test tools warnings (#263)
Browse files Browse the repository at this point in the history
this adds some basic test coverage for CakePHP. I couldn't get cake's testing
framework running, as I couldn't find a phpunit version it is compatible with,
so I just created a controller and fumbled my way to running it something like
how cake would. We have some coverage now, and a todo for a future improvement.
  • Loading branch information
brettmc authored May 5, 2024
1 parent 1acecf1 commit 8b14e88
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 9 deletions.
6 changes: 3 additions & 3 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
'blank_line_before_statement' => true,
'cast_spaces' => true,
'declare_strict_types' => true,
'function_typehint_space' => true,
'type_declaration_spaces' => true,
'include' => true,
'lowercase_cast' => true,
'new_with_braces' => true,
'new_with_parentheses' => true,
'no_extra_blank_lines' => true,
'no_leading_import_slash' => true,
'echo_tag_syntax' => true,
Expand All @@ -34,7 +34,7 @@
'phpdoc_scalar' => true,
'phpdoc_types' => true,
'short_scalar_cast' => true,
'single_blank_line_before_namespace' => true,
'blank_lines_before_namespace' => true,
'single_quote' => true,
'trailing_comma_in_multiline' => true,
])
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

This is a read-only subtree split of https://github.com/open-telemetry/opentelemetry-php-contrib.

# OpenTelemetry Slim Framework auto-instrumentation
# OpenTelemetry CakePHP auto-instrumentation
Please read https://opentelemetry.io/docs/instrumentation/php/automatic/ for instructions on how to
install and configure the extension and SDK.

## Overview
Auto-instrumentation hooks are registered via composer, and spans will automatically be created for:
- `::handle()` - root span
- Controller invoke

## Configuration

Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"require": {
"php": "^8.0",
"ext-opentelemetry": "*",
"cakephp/cakephp": ">=4.0",
"cakephp/cakephp": "^4 || ^5",
"open-telemetry/api": "^1.0",
"open-telemetry/sem-conv": "^1.23"
},
Expand All @@ -20,14 +20,14 @@
"mockery/mockery": "^1.5",
"nyholm/psr7": "*",
"phan/phan": "^5.0",
"php-http/mock-client": "*",
"phpstan/phpstan-mockery": "^1.1.0",
"phpstan/phpstan": "^1.1",
"phpstan/phpstan-phpunit": "^1.0",
"psalm/plugin-phpunit": "^0.18.4",
"open-telemetry/sdk": "^1.0",
"phpunit/phpunit": "^9.5",
"vimeo/psalm": "^5.24"
"vimeo/psalm": "^5.24",
"symfony/http-client": "^6 || ^7"
},
"autoload": {
"psr-4": {
Expand All @@ -39,7 +39,8 @@
},
"autoload-dev": {
"psr-4": {
"OpenTelemetry\\Tests\\Instrumentation\\CakePHP\\": "tests/"
"OpenTelemetry\\Tests\\Instrumentation\\CakePHP\\": "tests/",
"OpenTelemetry\\Tests\\Instrumentation\\CakePHP\\Integration\\App\\": "tests/Integration/App"
}
},
"config": {
Expand Down
5 changes: 5 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
<psalm
errorLevel="3"
cacheDirectory="var/cache/psalm"
findUnusedBaselineEntry="false"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd">
<projectFiles>
<directory name="src"/>
<directory name="tests"/>
<ignoreFiles>
<directory name="tests/Integration/App" />
</ignoreFiles>
</projectFiles>
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
Expand Down
20 changes: 20 additions & 0 deletions tests/Integration/App/ArticleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Tests\Instrumentation\CakePHP\Integration\App;

use Cake\Controller\Controller;

/**
* @psalm-suppress RedundantPropertyInitializationCheck
*/
class ArticleController extends Controller
{
public function index(): void
{
}
public function update(): void
{
}
}
107 changes: 107 additions & 0 deletions tests/Integration/CakePHPInstrumentationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Tests\Instrumentation\CakePHP\Integration;

use ArrayObject;
use Cake\Core\Configure;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use OpenTelemetry\API\Instrumentation\Configurator;
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Context\ScopeInterface;
use OpenTelemetry\SDK\Trace\ImmutableSpan;
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
use OpenTelemetry\Tests\Instrumentation\CakePHP\Integration\App\ArticleController;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;

/**
* @covers \OpenTelemetry\Contrib\Instrumentation\CakePHP\CakePHPInstrumentation
* @todo Use CakePHP's test framework, https://book.cakephp.org/4/en/development/testing.html
*/
class CakePHPInstrumentationTest extends TestCase
{
private const TRACE_ID = 'ff000000000000000000000000000041';
private const SPAN_ID = 'ff00000000000041';
private const TRACEPARENT_HEADER = '00-' . self::TRACE_ID . '-' . self::SPAN_ID . '-01';
private ScopeInterface $scope;
/** @var ArrayObject<int, ImmutableSpan> $storage */
private ArrayObject $storage;

public function setUp(): void
{
$this->storage = new ArrayObject();
$tracerProvider = new TracerProvider(
new SimpleSpanProcessor(
new InMemoryExporter($this->storage)
)
);

$this->scope = Configurator::create()
->withTracerProvider($tracerProvider)
->withPropagator(TraceContextPropagator::getInstance())
->activate();

Configure::write('App.encoding', 'utf8');
}

public function tearDown(): void
{
$this->scope->detach();
}

public function test_index(): void
{
$this->assertCount(0, $this->storage);

$request = new ServerRequest(['environment' => ['HTTP_TRACEPARENT' => self::TRACEPARENT_HEADER]]);
$controller = new ArticleController($request);

$controller->invokeAction(function (): ResponseInterface {
return new Response(['body' => 'hello world']);
}, []);

$this->assertCount(1, $this->storage);
/** @var ImmutableSpan $span */
$span = $this->storage[0];
$this->assertSame(StatusCode::STATUS_UNSET, $span->getStatus()->getCode());
$this->assertSame('GET', $span->getName());
$this->assertSame(SpanKind::KIND_SERVER, $span->getKind());
$this->assertGreaterThan(0, $span->getAttributes()->count());
$attributes = $span->getAttributes()->toArray();
$this->assertSame('invokeAction', $attributes['code.function']);
$this->assertSame('GET', $attributes['http.request.method']);
$this->assertSame(200, $attributes['http.response.status_code']);
$this->assertSame(self::TRACE_ID, $span->getParentContext()->getTraceId());
$this->assertSame(self::SPAN_ID, $span->getParentContext()->getSpanId());
}

public function test_exception(): void
{
$this->assertCount(0, $this->storage);

$request = new ServerRequest();
$controller = new ArticleController($request);

try {
$controller->invokeAction(function (): ResponseInterface {
throw new \Exception('kaboom');
}, []);
} catch (\Exception $e) {
}

$this->assertCount(1, $this->storage);
/** @var ImmutableSpan $span */
$span = $this->storage[0];
$this->assertSame(StatusCode::STATUS_ERROR, $span->getStatus()->getCode());
$event = $span->getEvents()[0];
$this->assertSame('exception', $event->getName());
$this->assertSame('kaboom', $event->getAttributes()->get('exception.message'));
}
}

0 comments on commit 8b14e88

Please sign in to comment.