Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ jobs:
# friendsofphp/php-cs-fixer: No need for this package to run phpunit and it conflicts with older Laravel versions
# livewire/livewire: Only supported on Laravel 7.0 and above
# laravel/folio: Only supported on PHP 8.1 + Laravel 10.0 and above
composer remove friendsofphp/php-cs-fixer livewire/livewire laravel/folio --dev --no-interaction --no-update
# laravel/pennant: Only supported on PHP 8.1 + Laravel 10.0 and above
composer remove friendsofphp/php-cs-fixer livewire/livewire laravel/folio laravel/pennant --dev --no-interaction --no-update

# Require the correct versions we want to run phpunit for
composer require \
Expand Down
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"require": {
"php": "^7.2 | ^8.0",
"illuminate/support": "^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0 | ^11.0 | ^12.0",
"sentry/sentry": "^4.16.0",
"sentry/sentry": "^4.18.0",
"symfony/psr-http-message-bridge": "^1.0 | ^2.0 | ^6.0 | ^7.0",
"nyholm/psr7": "^1.0"
},
Expand All @@ -35,15 +35,16 @@
}
},
"require-dev": {
"phpunit/phpunit": "^8.4 | ^9.3 | ^10.4 | ^11.5",
"friendsofphp/php-cs-fixer": "^3.11",
"guzzlehttp/guzzle": "^7.2",
"laravel/folio": "^1.1",
"laravel/framework": "^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0 | ^11.0 | ^12.0",
"laravel/pennant": "^1.0",
"livewire/livewire": "^2.0 | ^3.0",
"orchestra/testbench": "^4.7 | ^5.1 | ^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0",
"friendsofphp/php-cs-fixer": "^3.11",
"mockery/mockery": "^1.3",
"orchestra/testbench": "^4.7 | ^5.1 | ^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0",
"phpstan/phpstan": "^1.10",
"laravel/folio": "^1.1",
"guzzlehttp/guzzle": "^7.2"
"phpunit/phpunit": "^8.4 | ^9.3 | ^10.4 | ^11.5"
},
"autoload-dev": {
"psr-4": {
Expand Down
34 changes: 34 additions & 0 deletions src/Sentry/Laravel/Features/PennantPackageIntegration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Sentry\Laravel\Features;

use Sentry\State\Scope;
use Illuminate\Contracts\Events\Dispatcher;
use Sentry\SentrySdk;
use Laravel\Pennant\Feature as Pennant;
use Laravel\Pennant\Events\FeatureResolved;
use Laravel\Pennant\Events\FeatureRetrieved;

class PennantPackageIntegration extends Feature
{
private const FEATURE_KEY = 'pennant';

public function isApplicable(): bool
{
return class_exists(Pennant::class);
}

public function onBoot(Dispatcher $events): void
{
$events->listen(FeatureRetrieved::class, [$this, 'handleFeatureRetrieved']);
}

public function handleFeatureRetrieved($feature): void
{
SentrySdk::getCurrentHub()->configureScope(function (Scope $scope) use ($feature) {
// The value of the feature is not always a bool (Rich Feature Values) but only bools are supported.
// The feature is considered "active" if its value is not explicitly false following Pennant's logic.
$scope->addFeatureFlag($feature->feature, $feature->value !== false);
});
}
}
1 change: 1 addition & 0 deletions src/Sentry/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class ServiceProvider extends BaseServiceProvider
Features\HttpClientIntegration::class,
Features\FolioPackageIntegration::class,
Features\NotificationsIntegration::class,
Features\PennantPackageIntegration::class,
Features\LivewirePackageIntegration::class,
Features\ConsoleSchedulingIntegration::class,
];
Expand Down
107 changes: 107 additions & 0 deletions test/Sentry/Features/PennantPackageIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Sentry\Laravel\Tests\Features;

use Laravel\Folio\Folio;
use Laravel\Pennant\Feature;
use Sentry\Event;
use Sentry\EventType;
use Sentry\Laravel\Integration;
use Illuminate\Config\Repository;
use Sentry\Laravel\Tests\TestCase;
use Illuminate\Database\Eloquent\Model;

class PennantPackageIntegrationTest extends TestCase
{
protected function setUp(): void
{
if (!class_exists(Feature::class)) {
$this->markTestSkipped('Laravel Pennant package is not installed.');
}

parent::setUp();
}

protected function defineEnvironment($app): void
{
parent::defineEnvironment($app);

tap($app['config'], static function (Repository $config) {
// Force Pennant to use the array driver instead of the database which is the default
$config->set('pennant.default', 'array');
$config->set('pennant.stores.array', ['driver' => 'array']);
});
}

public function testPennantFeatureIsRecorded(): void
{
Feature::define('new-dashboard', static function () {
return true;
});

Feature::active('new-dashboard');

$scope = $this->getCurrentSentryScope();

$event = $scope->applyToEvent(Event::createEvent());

$this->assertArrayHasKey('flags', $event->getContexts());

$flags = $event->getContexts()['flags']['values'];

$this->assertEquals([
[
'flag' => 'new-dashboard',
'result' => true,
]
], $flags);
}

public function testPennantRichFeatureIsRecordedAsActive(): void
{
Feature::define('dashboard-version', static function () {
return 'dark';
});

Feature::value('dashboard-version');

$scope = $this->getCurrentSentryScope();

$event = $scope->applyToEvent(Event::createEvent());

$this->assertArrayHasKey('flags', $event->getContexts());

$flags = $event->getContexts()['flags']['values'];

$this->assertEquals([
[
'flag' => 'dashboard-version',
'result' => true,
]
], $flags);
}

public function testPennantRichFeatureIsRecordedAsInactive(): void
{
Feature::define('dashboard-version', static function () {
return false;
});

Feature::value('dashboard-version');

$scope = $this->getCurrentSentryScope();

$event = $scope->applyToEvent(Event::createEvent());

$this->assertArrayHasKey('flags', $event->getContexts());

$flags = $event->getContexts()['flags']['values'];

$this->assertEquals([
[
'flag' => 'dashboard-version',
'result' => false,
]
], $flags);
}
}