-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #639 from bugsnag/support-enums-in-metadata
Support enums in metadata
- Loading branch information
Showing
5 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
parameters: | ||
ignoreErrors: [] | ||
ignoreErrors: | ||
- '#Class UnitEnum not found\.#' | ||
- '#.*invalid type UnitEnum\.#' | ||
- '#.*unknown class UnitEnum\.#' | ||
- '#Class BackedEnum not found\.#' | ||
- '#.*unknown class BackedEnum\.#' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
tests/phpt/php8.1/backed_enums_can_be_added_as_metadata.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--TEST-- | ||
Backed enums can be added as metadata | ||
--FILE-- | ||
<?php | ||
namespace Some\Namespace; | ||
|
||
enum StringBackedEnum: string { | ||
case Admin = 'admin'; | ||
case User = 'user'; | ||
} | ||
|
||
enum IntBackedEnum: int { | ||
case Square = 1; | ||
case Circle = 2; | ||
} | ||
|
||
$client = require __DIR__ . '/../_prelude.php'; | ||
|
||
$client->setMetaData([ | ||
'data' => [ | ||
'admin' => StringBackedEnum::Admin, | ||
'user' => StringBackedEnum::User, | ||
'square' => IntBackedEnum::Square, | ||
'circle' => IntBackedEnum::Circle, | ||
], | ||
]); | ||
|
||
echo "Backed enums should be stored as objects\n"; | ||
var_dump($client->getMetaData()['data']); | ||
echo "\n"; | ||
|
||
$client->notifyException(new \Exception('hello'), function (\Bugsnag\Report $report): void { | ||
echo "Backed enums should be converted to string representation when serialised\n"; | ||
var_dump($report->toArray()['metaData']['data']); | ||
echo "\n"; | ||
}); | ||
?> | ||
--SKIPIF-- | ||
<?php | ||
if (version_compare(PHP_VERSION, '8.1.0', '<')) { | ||
echo 'SKIP — this test requires PHP 8.1+ for enum support'; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Backed enums should be stored as objects | ||
array(4) { | ||
["admin"]=> | ||
enum(Some\Namespace\StringBackedEnum::Admin) | ||
["user"]=> | ||
enum(Some\Namespace\StringBackedEnum::User) | ||
["square"]=> | ||
enum(Some\Namespace\IntBackedEnum::Square) | ||
["circle"]=> | ||
enum(Some\Namespace\IntBackedEnum::Circle) | ||
} | ||
|
||
Backed enums should be converted to string representation when serialised | ||
array(4) { | ||
["admin"]=> | ||
string(46) "Some\Namespace\StringBackedEnum::Admin (admin)" | ||
["user"]=> | ||
string(44) "Some\Namespace\StringBackedEnum::User (user)" | ||
["square"]=> | ||
string(40) "Some\Namespace\IntBackedEnum::Square (1)" | ||
["circle"]=> | ||
string(40) "Some\Namespace\IntBackedEnum::Circle (2)" | ||
} | ||
|
||
Guzzle request made (1 event)! | ||
* Method: 'POST' | ||
* URI: 'http://localhost/notify' | ||
* Events: | ||
- hello |
86 changes: 86 additions & 0 deletions
86
tests/phpt/php8.1/pure_enums_can_be_added_as_metadata.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
--TEST-- | ||
Pure enums can be added as metadata | ||
--FILE-- | ||
<?php | ||
namespace Some\Namespace; | ||
|
||
enum MyPureEnum { | ||
case TheFirstCase; | ||
case SecondCase; | ||
case CaseNumberThree; | ||
} | ||
|
||
namespace Another\Namespace; | ||
|
||
enum AnotherPureEnum { | ||
case One; | ||
case Two; | ||
} | ||
|
||
$client = require __DIR__ . '/../_prelude.php'; | ||
|
||
$client->setMetaData([ | ||
'data' => [ | ||
'first case' => \Some\Namespace\MyPureEnum::TheFirstCase, | ||
'second case' => \Some\Namespace\MyPureEnum::SecondCase, | ||
'third case' => \Some\Namespace\MyPureEnum::CaseNumberThree, | ||
'unrelated thing' => 'yes', | ||
'case one' => \Another\Namespace\AnotherPureEnum::One, | ||
'case two' => \Another\Namespace\AnotherPureEnum::Two, | ||
], | ||
]); | ||
|
||
echo "Pure enums should be stored as objects\n"; | ||
var_dump($client->getMetaData()['data']); | ||
echo "\n"; | ||
|
||
$client->notifyException(new \Exception('hello'), function (\Bugsnag\Report $report): void { | ||
echo "Pure enums should be converted to a string representation when serialised\n"; | ||
var_dump($report->toArray()['metaData']['data']); | ||
echo "\n"; | ||
}); | ||
?> | ||
--SKIPIF-- | ||
<?php | ||
if (version_compare(PHP_VERSION, '8.1.0', '<')) { | ||
echo 'SKIP — this test requires PHP 8.1+ for enum support'; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Pure enums should be stored as objects | ||
array(6) { | ||
["first case"]=> | ||
enum(Some\Namespace\MyPureEnum::TheFirstCase) | ||
["second case"]=> | ||
enum(Some\Namespace\MyPureEnum::SecondCase) | ||
["third case"]=> | ||
enum(Some\Namespace\MyPureEnum::CaseNumberThree) | ||
["unrelated thing"]=> | ||
string(3) "yes" | ||
["case one"]=> | ||
enum(Another\Namespace\AnotherPureEnum::One) | ||
["case two"]=> | ||
enum(Another\Namespace\AnotherPureEnum::Two) | ||
} | ||
|
||
Pure enums should be converted to a string representation when serialised | ||
array(6) { | ||
["first case"]=> | ||
string(39) "Some\Namespace\MyPureEnum::TheFirstCase" | ||
["second case"]=> | ||
string(37) "Some\Namespace\MyPureEnum::SecondCase" | ||
["third case"]=> | ||
string(42) "Some\Namespace\MyPureEnum::CaseNumberThree" | ||
["unrelated thing"]=> | ||
string(3) "yes" | ||
["case one"]=> | ||
string(38) "Another\Namespace\AnotherPureEnum::One" | ||
["case two"]=> | ||
string(38) "Another\Namespace\AnotherPureEnum::Two" | ||
} | ||
|
||
Guzzle request made (1 event)! | ||
* Method: 'POST' | ||
* URI: 'http://localhost/notify' | ||
* Events: | ||
- hello |