Skip to content

Commit

Permalink
upgrade to phpunit 11 (open-telemetry#1308)
Browse files Browse the repository at this point in the history
* run unit tests in random order
running in random order highlights some interference between tests (mostly logging being enabled), and is a requirement for
mutation testing (ie, all tests must pass when executed in a random order).
* adding infection mutation testing
this adds the ability to run infection. It generates a lot of output, which is an exercise for another PR...
* replace assertwell/phpunit-global-state
the package was not compatible with phpunit 11. create out own test Trait, which handles
env vars, as well as general cleanup of the usual suspects of state that our tests mess up
* remove redundant tearDown functions
there's an after annotation on the trait to handle this
* use rector to upgrade tests for phpunit 11
* fixing CoversFunction for namespaced functions
* use FQNs in test attributes
  • Loading branch information
brettmc authored May 15, 2024
1 parent 4f32817 commit fbdb103
Show file tree
Hide file tree
Showing 219 changed files with 1,043 additions and 1,752 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ bash: ## bash shell into container
style: ## Run style check/fix
$(DC_RUN_PHP) env XDEBUG_MODE=off env PHP_CS_FIXER_IGNORE_ENV=1 vendor-bin/php-cs-fixer/vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --using-cache=no -vvv
rector-write: ## Run rector
$(DC_RUN_PHP) env XDEBUG_MODE=off vendor-bin/rector/vendor/bin/rector process src
$(DC_RUN_PHP) env XDEBUG_MODE=off vendor-bin/rector/vendor/bin/rector process
rector: ## Run rector (dry-run)
$(DC_RUN_PHP) env XDEBUG_MODE=off vendor-bin/rector/vendor/bin/rector process src --dry-run
$(DC_RUN_PHP) env XDEBUG_MODE=off vendor-bin/rector/vendor/bin/rector process --dry-run
deptrac: ## Run deptrac
$(DC_RUN_PHP) env XDEBUG_MODE=off vendor-bin/deptrac/vendor/bin/deptrac --formatter=table --report-uncovered --no-cache
w3c-test-service:
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
"require-dev": {
"ext-grpc": "*",
"grpc/grpc": "^1.30",
"assertwell/phpunit-global-state": "^0.2.2",
"bamarni/composer-bin-plugin": "^1.8",
"dg/bypass-finals": "^1.4",
"guzzlehttp/guzzle": "^7.4",
Expand All @@ -100,7 +99,7 @@
"phpstan/phpstan": "^1.10.13",
"phpstan/phpstan-mockery": "^1.1",
"phpstan/phpstan-phpunit": "^1.3",
"phpunit/phpunit": "^10",
"phpunit/phpunit": "^10 || ^11",
"symfony/http-client": "^5.2",
"symfony/var-exporter": "^5.4 || ^6.4 || ^7.0",
"symfony/yaml": "^5.4 || ^6.4 || ^7.0"
Expand Down
1 change: 1 addition & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
],
ReadOnlyPropertyRector::class => [
__DIR__ . '/src/SDK/Metrics/Stream/SynchronousMetricStream.php',
__DIR__ . '/tests/Unit/Extension/Propagator',
],
DisallowedEmptyRuleFixerRector::class,
ExplicitBoolCompareRector::class,
Expand Down
6 changes: 3 additions & 3 deletions tests/Benchmark/OtlpBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
class OtlpBench
{
private TracerInterface $tracer;
private SamplerInterface $sampler;
private ResourceInfo $resource;
private readonly SamplerInterface $sampler;
private readonly ResourceInfo $resource;

private const PROTOBUF = 'application/x-protobuf';
private const JSON = 'application/json';
Expand Down Expand Up @@ -57,7 +57,7 @@ public function setUpNoExporter(): void
private function createTransport(string $contentType): TransportInterface
{
return new class($contentType) implements TransportInterface {
private string $contentType;
private readonly string $contentType;

public function __construct(string $contentType)
{
Expand Down
10 changes: 5 additions & 5 deletions tests/Integration/Config/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

declare(strict_types=1);

namespace Config;
namespace OpenTelemetry\Tests\Integration\Config;

use OpenTelemetry\Config\SDK\Configuration;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

#[CoversNothing]
final class ConfigurationTest extends TestCase
{

/**
* @dataProvider openTelemetryConfigurationDataProvider
* @coversNothing
*/
#[DataProvider('openTelemetryConfigurationDataProvider')]
public function test_open_telemetry_configuration(string $file): void
{
$this->expectNotToPerformAssertions();
Expand Down
5 changes: 2 additions & 3 deletions tests/Integration/SDK/AlwaysOffSamplerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOffSampler;
use OpenTelemetry\SDK\Trace\SamplingResult;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*/
#[CoversNothing]
class AlwaysOffSamplerTest extends TestCase
{
public function test_always_off_sampler(): void
Expand Down
5 changes: 2 additions & 3 deletions tests/Integration/SDK/AlwaysOnSamplerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\SamplingResult;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*/
#[CoversNothing]
class AlwaysOnSamplerTest extends TestCase
{
public function test_always_on_sampler_decision(): void
Expand Down
19 changes: 7 additions & 12 deletions tests/Integration/SDK/Context/SpanContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
use OpenTelemetry\API\Trace\SpanContextValidator;
use OpenTelemetry\API\Trace\TraceState;
use OpenTelemetry\SDK\Trace\RandomIdGenerator;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*/
#[CoversNothing]
class SpanContextTest extends TestCase
{
/**
* @dataProvider invalidSpanData
*/
#[DataProvider('invalidSpanData')]
public function test_invalid_span(string $traceId, string $spanId): void
{
$spanContext = SpanContext::create($traceId, $spanId);
Expand All @@ -40,18 +39,14 @@ public static function invalidSpanData(): array
];
}

/**
* @group trace-compliance
*/
#[Group('trace-compliance')]
public function test_valid_span(): void
{
$spanContext = SpanContext::create('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbb', API\TraceFlags::SAMPLED);
$this->assertTrue($spanContext->isValid());
}

/**
* @group trace-compliance
*/
#[Group('trace-compliance')]
public function test_context_is_remote_from_restore(): void
{
$spanContext = SpanContext::createFromRemoteParent('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbb', API\TraceFlags::SAMPLED);
Expand Down
14 changes: 4 additions & 10 deletions tests/Integration/SDK/MeterProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace OpenTelemetry\Tests\Integration\SDK;

use AssertWell\PHPUnitGlobalState\EnvironmentVariables;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\API\Common\Time\TestClock;
use OpenTelemetry\API\Metrics as API;
Expand All @@ -28,19 +27,14 @@
use OpenTelemetry\SDK\Metrics\StalenessHandler\ImmediateStalenessHandlerFactory;
use OpenTelemetry\SDK\Metrics\View\CriteriaViewRegistry;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\Tests\TestState;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*/
#[CoversNothing]
final class MeterProviderTest extends TestCase
{
use EnvironmentVariables;

public function tearDown(): void
{
self::restoreEnvironmentVariables();
}
use TestState;

public function test_weak_asynchronous_observer_is_released_when_instance_out_of_scope(): void
{
Expand Down
26 changes: 5 additions & 21 deletions tests/Integration/SDK/MeterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
use OpenTelemetry\API\Metrics\Noop\NoopObservableCounter;
use OpenTelemetry\API\Metrics\ObserverInterface;
use OpenTelemetry\SDK\Metrics\Data\Sum;
use OpenTelemetry\SDK\Metrics\Meter;
use OpenTelemetry\SDK\Metrics\MeterProviderBuilder;
use OpenTelemetry\SDK\Metrics\MetricExporter\InMemoryExporter;
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Psr\Log\LogLevel;

#[CoversClass(Meter::class)]
final class MeterTest extends TestCase
{
/**
* @covers \OpenTelemetry\SDK\Metrics\Meter::batchObserve
*/
public function test_batch_observe_observes_all_provided_instruments(): void
{
$exporter = new InMemoryExporter();
Expand Down Expand Up @@ -49,9 +50,6 @@ static function (ObserverInterface $a, ObserverInterface $b): void {
}
}

/**
* @covers \OpenTelemetry\SDK\Metrics\Meter::batchObserve
*/
public function test_batch_observe_calls_callback_only_once(): void
{
$reader = new ExportingReader(new InMemoryExporter());
Expand All @@ -78,9 +76,6 @@ static function () use (&$c): void {
}
}

/**
* @covers \OpenTelemetry\SDK\Metrics\Meter::batchObserve
*/
public function test_batch_observe_detach_detaches_callback(): void
{
$exporter = new InMemoryExporter();
Expand Down Expand Up @@ -111,9 +106,6 @@ static function (ObserverInterface $a, ObserverInterface $b): void {
}
}

/**
* @covers \OpenTelemetry\SDK\Metrics\Meter::batchObserve
*/
public function test_batch_observe_weakens_callback(): void
{
$exporter = new InMemoryExporter();
Expand Down Expand Up @@ -153,9 +145,6 @@ public function __invoke(ObserverInterface $a, ObserverInterface $b): void
}
}

/**
* @covers \OpenTelemetry\SDK\Metrics\Meter::batchObserve
*/
public function test_batch_observe_invalid_instrument(): void
{
$logWriter = $this->createMock(LogWriterInterface::class);
Expand Down Expand Up @@ -192,9 +181,6 @@ static function (ObserverInterface $a, ObserverInterface $b): void {
}
}

/**
* @covers \OpenTelemetry\SDK\Metrics\Meter::batchObserve
*/
public function test_batch_observe_invalid_instrument_different_meter(): void
{
$logWriter = $this->createMock(LogWriterInterface::class);
Expand Down Expand Up @@ -231,9 +217,7 @@ static function (ObserverInterface $a, ObserverInterface $b): void {
}
}

/**
* @coversNothing
*/
#[CoversNothing]
public function test_batch_observe_detach_with_repeated_instrument_does_not_trigger_undefined_offset_warning(): void
{
$this->expectNotToPerformAssertions();
Expand Down
5 changes: 2 additions & 3 deletions tests/Integration/SDK/ParentBasedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
use OpenTelemetry\SDK\Trace\Sampler\ParentBased;
use OpenTelemetry\SDK\Trace\SamplerInterface;
use OpenTelemetry\SDK\Trace\SamplingResult;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*/
#[CoversNothing]
class ParentBasedTest extends TestCase
{
public function test_parent_based_root_span(): void
Expand Down
14 changes: 4 additions & 10 deletions tests/Integration/SDK/Resource/ResourceInfoFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@

namespace OpenTelemetry\Tests\Integration\SDK\Resource;

use AssertWell\PHPUnitGlobalState\EnvironmentVariables;
use Composer\InstalledVersions;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SemConv\ResourceAttributes;
use OpenTelemetry\Tests\TestState;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*/
#[CoversNothing]
class ResourceInfoFactoryTest extends TestCase
{
use EnvironmentVariables;

public function tearDown(): void
{
$this->restoreEnvironmentVariables();
}
use TestState;

public function test_all_default_resources(): void
{
Expand Down
22 changes: 8 additions & 14 deletions tests/Integration/SDK/Resource/ResourceInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,22 @@

namespace OpenTelemetry\Tests\Integration\SDK\Resource;

use AssertWell\PHPUnitGlobalState\EnvironmentVariables;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Resource\Detectors;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\Tests\TestState;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*/
#[CoversNothing]
class ResourceInfoTest extends TestCase
{
use EnvironmentVariables;
use TestState;

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

/**
* @dataProvider environmentResourceProvider
* @group compliance
*/
#[DataProvider('environmentResourceProvider')]
#[Group('compliance')]
public function test_resource_from_environment(string $envAttributes, array $userAttributes, array $expected): void
{
$this->setEnvironmentVariable('OTEL_RESOURCE_ATTRIBUTES', $envAttributes);
Expand Down
Loading

0 comments on commit fbdb103

Please sign in to comment.