Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Report::getFeatureFlags to allow accessing feature flags in callbacks #653

Merged
merged 2 commits into from
Oct 19, 2022
Merged
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: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Changelog
* Raised the default maximum number of breadcrumbs to 50
[#652](https://github.com/bugsnag/bugsnag-php/pull/652)

* Add a `Report::getFeatureFlags` method to allow accessing feature flags in callbacks
[#653](https://github.com/bugsnag/bugsnag-php/pull/653)

## 3.28.0 (2022-05-18)

### Enhancements
Expand Down
15 changes: 3 additions & 12 deletions src/Internal/FeatureFlagDelegate.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,12 @@ public function clear()
}

/**
* Convert the list of stored feature flags into the format used by the
* Bugsnag Event API.
* Get the list of stored feature flags as an array.
*
* For example: [{ "featureFlag": "name", "variant": "variant" }, ...]
*
* @return array[]
* @phpstan-return list<array{featureFlag: string, variant?: string}>
* @return \Bugsnag\FeatureFlag[]
*/
public function toArray()
{
return array_map(
function (FeatureFlag $flag) {
return $flag->toArray();
},
$this->storage
);
return $this->storage;
}
}
17 changes: 16 additions & 1 deletion src/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,16 @@ public function clearFeatureFlags()
$this->featureFlags->clear();
}

/**
* Get the list of feature flags for this report.
*
* @return \Bugsnag\FeatureFlag[]
*/
public function getFeatureFlags()
{
return $this->featureFlags->toArray();
}

/**
* Set the current user.
*
Expand Down Expand Up @@ -763,7 +773,12 @@ public function toArray()
'metaData' => $this->cleanupObj($this->getMetaData(), true),
'unhandled' => $this->getUnhandled(),
'severityReason' => $this->getSeverityReason(),
'featureFlags' => $this->featureFlags->toArray(),
'featureFlags' => array_map(
function (FeatureFlag $flag) {
return $flag->toArray();
},
$this->featureFlags->toArray()
),
];

if ($hash = $this->getGroupingHash()) {
Expand Down
75 changes: 65 additions & 10 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1261,13 +1261,13 @@ public function testFeatureFlagsCanBeAddedToClient()
$this->client->addFeatureFlag('another name', 'with variant');

$expected = [
['featureFlag' => 'a name'],
['featureFlag' => 'another name', 'variant' => 'with variant'],
new FeatureFlag('a name'),
new FeatureFlag('another name', 'with variant'),
];

$actual = $this->client->getConfig()->getFeatureFlagsCopy()->toArray();

$this->assertSame($expected, $actual);
$this->assertEquals($expected, $actual);
}

public function testMultipleFeatureFlagsCanBeAddedToClientAtOnce()
Expand All @@ -1280,15 +1280,15 @@ public function testMultipleFeatureFlagsCanBeAddedToClientAtOnce()
]);

$expected = [
['featureFlag' => 'a name'],
['featureFlag' => 'another name', 'variant' => 'with variant'],
['featureFlag' => 'name3'],
['featureFlag' => 'four', 'variant' => 'yes'],
new FeatureFlag('a name'),
new FeatureFlag('another name', 'with variant'),
new FeatureFlag('name3'),
new FeatureFlag('four', 'yes'),
];

$actual = $this->client->getConfig()->getFeatureFlagsCopy()->toArray();

$this->assertSame($expected, $actual);
$this->assertEquals($expected, $actual);
}

public function testAFeatureFlagCanBeRemovedFromClient()
Expand All @@ -1299,12 +1299,12 @@ public function testAFeatureFlagCanBeRemovedFromClient()
$this->client->clearFeatureFlag('another name');

$expected = [
['featureFlag' => 'a name'],
new FeatureFlag('a name'),
];

$actual = $this->client->getConfig()->getFeatureFlagsCopy()->toArray();

$this->assertSame($expected, $actual);
$this->assertEquals($expected, $actual);
}

public function testAllFeatureFlagsCanBeRemovedFromClient()
Expand Down Expand Up @@ -1357,4 +1357,59 @@ function ($options) {

$this->client->flush();
}

public function testFeatureFlagsCanBeAccessedInCallbacks()
{
$this->client->registerCallback(function (Report $report) {
$report->addFeatureFlag('hi');

$report->addFeatureFlags([
new FeatureFlag('added from report 1', 'yes'),
new FeatureFlag('added from report 2'),
]);

$report->clearFeatureFlag('hello');
$report->clearFeatureFlag('hi');
});

$called = false;
$this->client->registerCallback(function (Report $report) use (&$called) {
$expected = [
new FeatureFlag('added from client'),
new FeatureFlag('added from report 1', 'yes'),
new FeatureFlag('added from report 2'),
];

$this->assertEquals($expected, $report->getFeatureFlags());

$called = true;
});

$this->client->addFeatureFlag('added from client');
$this->client->addFeatureFlag('hello');

$this->client->notifyError('SomeError', 'Some message');

$this->expectGuzzlePostWithCallback(
$this->client->getNotifyEndpoint(),
function ($options) use ($called) {
$payload = $this->getPayloadFromGuzzleOptions($options);

$this->assertTrue(isset($payload['events'][0]['featureFlags']));
$this->assertTrue($called);

$expected = [
['featureFlag' => 'added from client'],
['featureFlag' => 'added from report 1', 'variant' => 'yes'],
['featureFlag' => 'added from report 2'],
];

$this->assertSame($expected, $payload['events'][0]['featureFlags']);

return true;
}
);

$this->client->flush();
}
}
20 changes: 10 additions & 10 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,13 @@ public function testFeatureFlagsCanBeAddedToConfiguration()
$this->config->addFeatureFlag('another name', 'with variant');

$expected = [
['featureFlag' => 'a name'],
['featureFlag' => 'another name', 'variant' => 'with variant'],
new FeatureFlag('a name'),
new FeatureFlag('another name', 'with variant'),
];

$actual = $this->config->getFeatureFlagsCopy()->toArray();

$this->assertSame($expected, $actual);
$this->assertEquals($expected, $actual);
}

public function testMultipleFeatureFlagsCanBeAddedToConfigurationAtOnce()
Expand All @@ -385,15 +385,15 @@ public function testMultipleFeatureFlagsCanBeAddedToConfigurationAtOnce()
]);

$expected = [
['featureFlag' => 'a name'],
['featureFlag' => 'another name', 'variant' => 'with variant'],
['featureFlag' => 'name3'],
['featureFlag' => 'four', 'variant' => 'yes'],
new FeatureFlag('a name'),
new FeatureFlag('another name', 'with variant'),
new FeatureFlag('name3'),
new FeatureFlag('four', 'yes'),
];

$actual = $this->config->getFeatureFlagsCopy()->toArray();

$this->assertSame($expected, $actual);
$this->assertEquals($expected, $actual);
}

public function testAFeatureFlagCanBeRemovedFromConfiguration()
Expand All @@ -404,12 +404,12 @@ public function testAFeatureFlagCanBeRemovedFromConfiguration()
$this->config->clearFeatureFlag('another name');

$expected = [
['featureFlag' => 'a name'],
new FeatureFlag('a name'),
];

$actual = $this->config->getFeatureFlagsCopy()->toArray();

$this->assertSame($expected, $actual);
$this->assertEquals($expected, $actual);
}

public function testAllFeatureFlagsCanBeRemovedFromConfiguration()
Expand Down
34 changes: 17 additions & 17 deletions tests/Internal/FeatureFlagDelegateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public function testAdd()
$delegate->add('another name', 'a variant');

$expected = [
['featureFlag' => 'a name'],
['featureFlag' => 'another name', 'variant' => 'a variant'],
new FeatureFlag('a name'),
new FeatureFlag('another name', 'a variant'),
];

$this->assertSame($expected, $delegate->toArray());
$this->assertEquals($expected, $delegate->toArray());
}

public function testMerge()
Expand All @@ -33,12 +33,12 @@ public function testMerge()
]);

$expected = [
['featureFlag' => 'name'],
['featureFlag' => '2flag', 'variant' => '2variant'],
['featureFlag' => 'flag', 'variant' => 'abc'],
new FeatureFlag('name'),
new FeatureFlag('2flag', '2variant'),
new FeatureFlag('flag', 'abc'),
];

$this->assertSame($expected, $delegate->toArray());
$this->assertEquals($expected, $delegate->toArray());

$delegate->merge([
// replace the 'name' flag with one that has a variant
Expand All @@ -47,13 +47,13 @@ public function testMerge()
]);

$expected = [
['featureFlag' => '2flag', 'variant' => '2variant'],
['featureFlag' => 'flag', 'variant' => 'abc'],
['featureFlag' => 'name', 'variant' => 'with variant'],
['featureFlag' => 'final flag'],
new FeatureFlag('2flag', '2variant'),
new FeatureFlag('flag', 'abc'),
new FeatureFlag('name', 'with variant'),
new FeatureFlag('final flag'),
];

$this->assertSame($expected, $delegate->toArray());
$this->assertEquals($expected, $delegate->toArray());
}

public function testMergeIgnoresIncorrectTypes()
Expand All @@ -69,11 +69,11 @@ public function testMergeIgnoresIncorrectTypes()
]);

$expected = [
['featureFlag' => '2flag', 'variant' => '2variant'],
['featureFlag' => '3flag', 'variant' => '3variant'],
new FeatureFlag('2flag', '2variant'),
new FeatureFlag('3flag', '3variant'),
];

$this->assertSame($expected, $delegate->toArray());
$this->assertEquals($expected, $delegate->toArray());
}

public function testRemove()
Expand All @@ -86,10 +86,10 @@ public function testRemove()
$delegate->remove('a name');

$expected = [
['featureFlag' => 'another name', 'variant' => 'a variant'],
new FeatureFlag('another name', 'a variant'),
];

$this->assertSame($expected, $delegate->toArray());
$this->assertEquals($expected, $delegate->toArray());

$delegate->remove('another name');

Expand Down
Loading