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

Commit 6d56714

Browse files
finalizing track error improvements (#48)
* 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
1 parent 62e0556 commit 6d56714

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 0.4.3
44

5+
- Support tracking Throwable and Error, not only Exceptions.
56
- Support for internal context and override of SDK version.
67
- Fix duration serialization for `trackPageView` call.
78
- Do not send `User-Agent` when uploading telemetry to avoid misclassification of server telemetry as client telemetry.

Tests/Telemetry_Client_Test.php

+42-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,47 @@ public function testCompleteException()
6060
}
6161
}
6262

63+
/**
64+
* Tests a completely filled error.
65+
*
66+
* Ensure this method doesn't move in the source, if it does, the test will fail and needs to have a line number adjusted.
67+
*/
68+
public function testCompleteError()
69+
{
70+
$errorsSupported = false;
71+
72+
try
73+
{
74+
Utils::throwError();
75+
}
76+
catch (\Error $err)
77+
{
78+
$errorsSupported = true;
79+
$this->_telemetryClient->trackException($err, ['InlineProperty' => 'test_value'], ['duration_inner' => 42.0]);
80+
}
81+
82+
if (!$errorsSupported)
83+
{
84+
return;
85+
}
86+
87+
$queue = json_decode($this->_telemetryClient->getChannel()->getSerializedQueue(), true);
88+
$queue = $this->adjustDataInQueue($queue);
89+
90+
$searchStrings = array("\\");
91+
$replaceStrings = array("\\\\");
92+
93+
$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"}}]');
94+
$expectedValue = json_decode($expectedString, true);
95+
96+
$this->assertEquals($this->removeMachineSpecificExceptionData($expectedValue, 1), $this->removeMachineSpecificExceptionData($queue, 1));
97+
98+
if (Utils::sendDataToServer())
99+
{
100+
$this->_telemetryClient->flush();
101+
}
102+
}
103+
63104
/**
64105
* Verifies the object is constructed properly.
65106
*/
@@ -72,7 +113,7 @@ public function testConstructor()
72113

73114

74115
/**
75-
* Verifies the guzzle client is properly overriden.
116+
* Verifies the guzzle client is properly overridden.
76117
*/
77118
public function testGuzzleClientOverrideConstructor()
78119
{

Tests/Utils.php

+8
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ public static function throwNestedException($depth = 0)
133133
Utils::throwNestedException($depth - 1);
134134
}
135135

136+
/**
137+
* Used for testing error related code
138+
*/
139+
public static function throwError()
140+
{
141+
eval('sdklafjha asdlkja asdaksd al');
142+
}
143+
136144
/**
137145
* Creates user cookie for testing.
138146
*/

0 commit comments

Comments
 (0)