Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
finalizing track error improvements (#48)
Browse files Browse the repository at this point in the history
* finalizing track error improvements

* only compare a single stack frame as all other are PHPUnit version-specific

* extra tracing for tests

* only test errors when they are supported

* make unit tests totally php unit version independant
  • Loading branch information
SergeyKanzhelev authored May 22, 2018
1 parent 62e0556 commit 6d56714
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.4.3

- Support tracking Throwable and Error, not only Exceptions.
- Support for internal context and override of SDK version.
- Fix duration serialization for `trackPageView` call.
- Do not send `User-Agent` when uploading telemetry to avoid misclassification of server telemetry as client telemetry.
Expand Down
43 changes: 42 additions & 1 deletion Tests/Telemetry_Client_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,47 @@ public function testCompleteException()
}
}

/**
* Tests a completely filled error.
*
* Ensure this method doesn't move in the source, if it does, the test will fail and needs to have a line number adjusted.
*/
public function testCompleteError()
{
$errorsSupported = false;

try
{
Utils::throwError();
}
catch (\Error $err)
{
$errorsSupported = true;
$this->_telemetryClient->trackException($err, ['InlineProperty' => 'test_value'], ['duration_inner' => 42.0]);
}

if (!$errorsSupported)
{
return;
}

$queue = json_decode($this->_telemetryClient->getChannel()->getSerializedQueue(), true);
$queue = $this->adjustDataInQueue($queue);

$searchStrings = array("\\");
$replaceStrings = array("\\\\");

$expectedString = str_replace($searchStrings, $replaceStrings, '[{"ver":1,"name":"Microsoft.ApplicationInsights.Exception","time":"TIME_PLACEHOLDER","sampleRate":100,"iKey":"11111111-1111-1111-1111-111111111111","tags":{"ai.application.ver":"1.0.0.0","ai.device.id":"my_device_id","ai.device.ip":"127.0.0.1","ai.device.language":"EN","ai.device.locale":"EN","ai.device.model":"my_device_model","ai.device.network":5,"ai.device.oemName":"my_device_oem_name","ai.device.os":"Window","ai.device.osVersion":"8","ai.device.roleInstance":"device role instance","ai.device.roleName":"device role name","ai.device.screenResolution":"1920x1080","ai.device.type":"PC","ai.device.vmName":"device vm name","ai.location.ip":"127.0.0.0","ai.operation.id":"my_operation_id","ai.operation.name":"my_operation_name","ai.operation.parentId":"my_operation_parent_id","ai.operation.rootId":"my_operation_rood","ai.session.id":"my_session_id","ai.session.isFirst":"false","ai.session.isNew":"false","ai.user.id":"my_user_id","ai.user.accountAcquisitionDate":"1/1/2014","ai.user.accountId":"my_account_id","ai.user.userAgent":"my_user_agent","ai.internal.sdkVersion":"SDK_VERSION_STRING"},"data":{"baseData":{"ver":2,"handledAt":"UserCode","exceptions":[{"typeName":"ParseError","message":"syntax error, unexpected \'asdlkja\' (T_STRING) in \/Users\/sergeykanzhelev\/src\/ApplicationInsights\/php\/Tests\/Utils.php(141) : eval()\'d code on line 1","hasFullStack":true,"id":1,"parsedStack":[{"level":"10","method":"main","assembly":"PHPUnit\\TextUI\\Command","fileName":"\/usr\/local\/Cellar\/phpunit\/7.1.5\/bin\/phpunit","line":588},{"level":"9","method":"run","assembly":"PHPUnit\\TextUI\\Command","fileName":"phar:\/\/\/usr\/local\/Cellar\/phpunit\/7.1.5\/bin\/phpunit\/phpunit\/TextUI\/Command.php","line":151},{"level":"8","method":"doRun","assembly":"PHPUnit\\TextUI\\TestRunner","fileName":"phar:\/\/\/usr\/local\/Cellar\/phpunit\/7.1.5\/bin\/phpunit\/phpunit\/TextUI\/Command.php","line":198},{"level":"7","method":"run","assembly":"PHPUnit\\Framework\\TestSuite","fileName":"phar:\/\/\/usr\/local\/Cellar\/phpunit\/7.1.5\/bin\/phpunit\/phpunit\/TextUI\/TestRunner.php","line":529},{"level":"6","method":"run","assembly":"PHPUnit\\Framework\\TestSuite","fileName":"phar:\/\/\/usr\/local\/Cellar\/phpunit\/7.1.5\/bin\/phpunit\/phpunit\/Framework\/TestSuite.php","line":776},{"level":"5","method":"run","assembly":"PHPUnit\\Framework\\TestCase","fileName":"phar:\/\/\/usr\/local\/Cellar\/phpunit\/7.1.5\/bin\/phpunit\/phpunit\/Framework\/TestSuite.php","line":776},{"level":"4","method":"run","assembly":"PHPUnit\\Framework\\TestResult","fileName":"phar:\/\/\/usr\/local\/Cellar\/phpunit\/7.1.5\/bin\/phpunit\/phpunit\/Framework\/TestCase.php","line":798},{"level":"3","method":"runBare","assembly":"PHPUnit\\Framework\\TestCase","fileName":"phar:\/\/\/usr\/local\/Cellar\/phpunit\/7.1.5\/bin\/phpunit\/phpunit\/Framework\/TestResult.php","line":645},{"level":"2","method":"runTest","assembly":"PHPUnit\\Framework\\TestCase","fileName":"phar:\/\/\/usr\/local\/Cellar\/phpunit\/7.1.5\/bin\/phpunit\/phpunit\/Framework\/TestCase.php","line":840},{"level":"1","method":"testCompleteError","assembly":"ApplicationInsights\\Tests\\Telemetry_Client_Test","fileName":"phar:\/\/\/usr\/local\/Cellar\/phpunit\/7.1.5\/bin\/phpunit\/phpunit\/Framework\/TestCase.php","line":1145},{"level":"0","method":"throwError","assembly":"ApplicationInsights\\Tests\\Utils","fileName":"\/Users\/sergeykanzhelev\/src\/ApplicationInsights\/php\/Tests\/Telemetry_Client_Test.php","line":74}]}],"properties":{"InlineProperty":"test_value","MyCustomProperty":42,"MyCustomProperty2":"test"},"measurements":{"duration_inner":42}},"baseType":"ExceptionData"}}]');
$expectedValue = json_decode($expectedString, true);

$this->assertEquals($this->removeMachineSpecificExceptionData($expectedValue, 1), $this->removeMachineSpecificExceptionData($queue, 1));

if (Utils::sendDataToServer())
{
$this->_telemetryClient->flush();
}
}

/**
* Verifies the object is constructed properly.
*/
Expand All @@ -72,7 +113,7 @@ public function testConstructor()


/**
* Verifies the guzzle client is properly overriden.
* Verifies the guzzle client is properly overridden.
*/
public function testGuzzleClientOverrideConstructor()
{
Expand Down
8 changes: 8 additions & 0 deletions Tests/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ public static function throwNestedException($depth = 0)
Utils::throwNestedException($depth - 1);
}

/**
* Used for testing error related code
*/
public static function throwError()
{
eval('sdklafjha asdlkja asdaksd al');
}

/**
* Creates user cookie for testing.
*/
Expand Down

0 comments on commit 6d56714

Please sign in to comment.