diff --git a/composer.json b/composer.json index 7037dcf3..e72035b1 100644 --- a/composer.json +++ b/composer.json @@ -38,11 +38,11 @@ }, "require-dev": { - "phpunit/phpunit": "^9.6", + "phpunit/phpunit": "^9.6 || ^10.1", "mockery/mockery": "^1.5.1", - "squizlabs/php_codesniffer": "^3.6 || 3.x-dev", - "phpmd/phpmd" : "^2", - "vimeo/psalm": "^5" + "squizlabs/php_codesniffer": "^3.7", + "phpmd/phpmd" : "^2.13", + "vimeo/psalm": "^5.9" }, "suggest": { @@ -55,7 +55,7 @@ "test": [ "phpcs --standard=PSR2 src tests", "psalm --long-progress", - "phpunit --coverage-clover build/logs/clover.xml --testsuite 'Rollbar Test Suite'" + "phpunit --coverage-clover build/logs/clover.xml" ], "fix": "phpcbf --standard=PSR2 src tests", "get-js-snippet": "ROLLBAR_JS_TAG=$(curl -s https://api.github.com/repos/rollbar/rollbar.js/releases/latest | sed -n 's/\"tag_name\":.*\"\\(.*\\)\",/\\1/p' | sed 's/ *//'); curl -X GET https://raw.githubusercontent.com/rollbar/rollbar.js/$ROLLBAR_JS_TAG/dist/rollbar.snippet.js > data/rollbar.snippet.js", diff --git a/phpunit.xml b/phpunit.xml index 51e56e66..254b6617 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,30 +1,36 @@ - + ./tests/ ./tests/Performance/ + ./tests/TestHelpers/ + ./tests/FakeDataBuilder.php + ./tests/bootstrap.php + ./tests/BaseRollbarTest.php + ./tests/Performance/ - + + + + + + + + ./src - - - - + + diff --git a/tests/BaseRollbarTest.php b/tests/BaseRollbarTest.php index 62f0f0f3..2bbafbe2 100644 --- a/tests/BaseRollbarTest.php +++ b/tests/BaseRollbarTest.php @@ -4,7 +4,6 @@ abstract class BaseRollbarTest extends TestCase { - const DEFAULT_ACCESS_TOKEN = 'ad865e76e7fb496fab096ac07b1dbabb'; public function tearDown(): void diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index be99c342..e090f336 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -202,7 +202,7 @@ public function testVerboseInfo(): void )); $handlerMock = $this->getMockBuilder(ErrorLogHandler::class) - ->setMethods(array('handle')) + ->onlyMethods(array('handle')) ->getMock(); $handlerMock->expects($this->once())->method('handle'); @@ -721,7 +721,7 @@ public function testUseErrorReporting($use_error_reporting, $error_reporting, $e } } - public function useErrorReportingProvider(): array + public static function useErrorReportingProvider(): array { return array( "use_error_reporting off" => array( @@ -760,7 +760,7 @@ public function testExceptionSampleRate($exception, $expected): void $this->assertEquals($expected, $sampleRate); } - public function providerExceptionSampleRate(): array + public static function providerExceptionSampleRate(): array { return array( array( diff --git a/tests/DataBuilderTest.php b/tests/DataBuilderTest.php index 75a6a60e..ea2b7a12 100644 --- a/tests/DataBuilderTest.php +++ b/tests/DataBuilderTest.php @@ -78,11 +78,11 @@ public function testGetUrl($protoData, $hostData, $portData): void $this->assertEquals($expected, $result); } - public function getUrlProvider(): array + public static function getUrlProvider(): array { - $protoData = $this->getUrlProtoProvider(); - $hostData = $this->getUrlHostProvider(); - $portData = $this->getUrlPortProvider(); + $protoData = self::getUrlProtoProvider(); + $hostData = self::getUrlHostProvider(); + $portData = self::getUrlPortProvider(); $testData = array(); @@ -118,7 +118,7 @@ public function testParseForwardedString($forwaded, $expected): void $this->assertEquals($expected, $output); } - public function parseForwardedStringProvider(): array + public static function parseForwardedStringProvider(): array { return array( array( // test 1 @@ -182,7 +182,7 @@ public function testGetUrlProto($data, $expected): void $_SERVER = $pre_SERVER; } - public function getUrlProtoProvider(): array + public static function getUrlProtoProvider(): array { return array( array( // test 1: HTTP_FORWARDED @@ -237,7 +237,7 @@ public function testGetUrlHost($data, $expected): void $this->assertEquals($expected, $output); } - public function getUrlHostProvider(): array + public static function getUrlHostProvider(): array { return array( array( // test 1: HTTP_FORWARDED @@ -311,7 +311,7 @@ public function testGetUrlPort($data, $expected): void $this->assertEquals($expected, $output); } - public function getUrlPortProvider(): array + public static function getUrlPortProvider(): array { return array( array( // test 1: HTTP_X_FORWARDED @@ -952,7 +952,7 @@ public function testCaptureErrorStacktracesError( $this->assertEquals($expected, count($frames) == 0); } - public function captureErrorStacktracesProvider(): array + public static function captureErrorStacktracesProvider(): array { return array( array(false,true), @@ -1002,7 +1002,7 @@ public function testGetUserIp($ipAddress, $expected, $captureIP): void unset($_SERVER['HTTP_X_REAL_IP']); } - public function getUserIpProvider(): array + public static function getUserIpProvider(): array { return array( array('127.0.0.1', '127.0.0.1', null), diff --git a/tests/FakeLog.php b/tests/FakeLog.php deleted file mode 100644 index d90e49cf..00000000 --- a/tests/FakeLog.php +++ /dev/null @@ -1,11 +0,0 @@ -getMockBuilder(ErrorHandler::class) ->setConstructorArgs(array(new RollbarLogger(self::$simpleConfig))) - ->setMethods(array('handle')) + ->onlyMethods(array('handle')) ->getMock(); $handler->expects($this->once()) @@ -57,7 +57,7 @@ public function testHandle(): void { $logger = $this->getMockBuilder(RollbarLogger::class) ->setConstructorArgs(array(self::$simpleConfig)) - ->setMethods(array('report')) + ->onlyMethods(array('report')) ->getMock(); $logger->expects($this->once()) diff --git a/tests/Handlers/ExceptionHandlerTest.php b/tests/Handlers/ExceptionHandlerTest.php index 90f40724..2e9379e0 100644 --- a/tests/Handlers/ExceptionHandlerTest.php +++ b/tests/Handlers/ExceptionHandlerTest.php @@ -49,7 +49,7 @@ public function testSetup(): void { $handler = $this->getMockBuilder(ExceptionHandler::class) ->setConstructorArgs(array(new RollbarLogger(self::$simpleConfig))) - ->setMethods(array('handle')) + ->onlyMethods(array('handle')) ->getMock(); $handler->expects($this->once()) @@ -73,7 +73,7 @@ public function testHandle(): void $logger = $this->getMockBuilder(RollbarLogger::class) ->setConstructorArgs(array(self::$simpleConfig)) - ->setMethods(array('report')) + ->onlyMethods(array('report')) ->getMock(); $logger->expects($this->once()) diff --git a/tests/JsHelperTest.php b/tests/JsHelperTest.php index 11a768af..69aa4459 100644 --- a/tests/JsHelperTest.php +++ b/tests/JsHelperTest.php @@ -2,20 +2,27 @@ class JsHelperTest extends BaseRollbarTest { - protected RollbarJsHelper $jsHelper; - protected string|false $testSnippetPath; + protected static RollbarJsHelper $jsHelper; + protected static string|false $testSnippetPath = ""; public function setUp(): void { - $this->jsHelper = new RollbarJsHelper(array()); - $this->testSnippetPath = realpath(__DIR__ . "/../data/rollbar.snippet.js"); + self::$jsHelper = new RollbarJsHelper(array()); + self::$testSnippetPath = realpath(__DIR__ . "/../data/rollbar.snippet.js"); } - + + # TODO this is needed for phpUnit 10 as all Generators needs to be static + public static function init(): void + { + self::$jsHelper = new RollbarJsHelper(array()); + self::$testSnippetPath = realpath(__DIR__ . "/../data/rollbar.snippet.js"); + } + public function testSnippetPath(): void { $this->assertEquals( - $this->testSnippetPath, - $this->jsHelper->snippetPath() + self::$testSnippetPath, + self::$jsHelper->snippetPath() ); } @@ -40,7 +47,7 @@ public function testShouldAddJs($setup, $expected): void $this->assertEquals($expected, $mock->shouldAddJs($status, array())); } - public function shouldAddJsProvider(): array + public static function shouldAddJsProvider(): array { return array( array( @@ -85,11 +92,11 @@ public function testIsHtml($headers, $expected): void { $this->assertEquals( $expected, - $this->jsHelper->isHtml($headers) + self::$jsHelper->isHtml($headers) ); } - public function isHtmlProvider(): array + public static function isHtmlProvider(): array { return array( array( @@ -114,11 +121,11 @@ public function testHasAttachment($headers, $expected): void { $this->assertEquals( $expected, - $this->jsHelper->hasAttachment($headers) + self::$jsHelper->hasAttachment($headers) ); } - public function hasAttachmentProvider(): array + public static function hasAttachmentProvider(): array { return array( array( @@ -137,9 +144,9 @@ public function hasAttachmentProvider(): array public function testJsSnippet(): void { - $expected = file_get_contents($this->testSnippetPath); + $expected = file_get_contents(self::$testSnippetPath); - $this->assertEquals($expected, $this->jsHelper->jsSnippet()); + $this->assertEquals($expected, self::$jsHelper->jsSnippet()); } /** @@ -149,11 +156,11 @@ public function testShouldAppendNonce($headers, $expected): void { $this->assertEquals( $expected, - $this->jsHelper->shouldAppendNonce($headers) + self::$jsHelper->shouldAppendNonce($headers) ); } - public function shouldAppendNonceProvider(): array + public static function shouldAppendNonceProvider(): array { return array( array( @@ -184,7 +191,7 @@ public function testScriptTag($content, $headers, $nonce, $expected): void { if ($expected === 'Exception') { try { - $result = $this->jsHelper->scriptTag($content, $headers, $nonce); + $result = self::$jsHelper->scriptTag($content, $headers, $nonce); $this->fail(); } catch (\Exception $e) { @@ -192,13 +199,13 @@ public function testScriptTag($content, $headers, $nonce, $expected): void return; } } else { - $result = $this->jsHelper->scriptTag($content, $headers, $nonce); + $result = self::$jsHelper->scriptTag($content, $headers, $nonce); $this->assertEquals($expected, $result); } } - public function scriptTagProvider(): array + public static function scriptTagProvider(): array { return array( 'nonce script' => array( @@ -239,7 +246,7 @@ public function testConfigJsTag($config, $expectedJson): void $this->assertEquals($expected, $result); } - public function configJsTagProvider() + public static function configJsTagProvider(): array { return array( array(array(), '{}'), @@ -254,7 +261,7 @@ public function configJsTagProvider() /** * @dataProvider addJsProvider */ - public function testBuildJs($config, $headers, $nonce, $expected): void + public static function testBuildJs($config, $headers, $nonce, $expected): void { $result = RollbarJsHelper::buildJs( $config, @@ -263,7 +270,7 @@ public function testBuildJs($config, $headers, $nonce, $expected): void "var customJs = true;" ); - $this->assertEquals($expected, $result); + self::assertEquals($expected, $result); } /** @@ -281,11 +288,13 @@ public function testAddJs($config, $headers, $nonce, $expected): void $this->assertEquals($expected, $result); } - - public function addJsProvider(): array + + # TODO this is needed for phpUnit 10 as all Generators needs to be static + public static function addJsProvider(): array { - $this->setUp(); - $expectedJs = file_get_contents($this->testSnippetPath); + self::init(); + $expectedJs = file_get_contents(self::$testSnippetPath); + return array( array( array(), // 'config' diff --git a/tests/LevelFactoryTest.php b/tests/LevelFactoryTest.php index 19d27e41..c242f14b 100644 --- a/tests/LevelFactoryTest.php +++ b/tests/LevelFactoryTest.php @@ -15,9 +15,9 @@ public function testIsValidLevelProvider(string $level, bool $expected): void ); } - public function isValidLevelProvider(): array + public static function isValidLevelProvider(): array { - $data = $this->fromNameProvider(); + $data = self::fromNameProvider(); foreach ($data as &$testParams) { $testParams[] = true; } @@ -41,7 +41,7 @@ public function testFromName(string $level): void ); } - public function fromNameProvider(): array + public static function fromNameProvider(): array { return [ [Level::EMERGENCY], @@ -66,7 +66,7 @@ public function testFromNameOrInstance(Level|string $level): void ); } - public function fromNameOrInstanceProvider(): array + public static function fromNameOrInstanceProvider(): array { return [ [Level::EMERGENCY], diff --git a/tests/NotifierTest.php b/tests/NotifierTest.php index fd3ed455..da5a880a 100644 --- a/tests/NotifierTest.php +++ b/tests/NotifierTest.php @@ -30,8 +30,16 @@ public function testDefaultNotifierIsRepresentableAsJson(): void $notifier = Notifier::defaultNotifier()->serialize(); $encoding = json_encode($notifier, flags: JSON_THROW_ON_ERROR|JSON_FORCE_OBJECT); $decoding = json_decode($encoding, flags: JSON_THROW_ON_ERROR); - $this->assertObjectHasAttribute('name', $decoding); - $this->assertObjectHasAttribute('version', $decoding); + + if (method_exists($this, 'assertObjectHasProperty')) { + # phpUnit 10 + $this->assertObjectHasProperty('name', $decoding); + $this->assertObjectHasProperty('version', $decoding); + } else { + # phpUnit 9 + $this->assertObjectHasAttribute('name', $decoding); + $this->assertObjectHasAttribute('version', $decoding); + } } public function testDefaultNotifierVersionIsSemVerCompliant(): void diff --git a/tests/RollbarLoggerTest.php b/tests/RollbarLoggerTest.php index 6440642f..04d26032 100644 --- a/tests/RollbarLoggerTest.php +++ b/tests/RollbarLoggerTest.php @@ -222,7 +222,7 @@ public function testVerbose(): void $originalHandler = $originalHandler[0]; $handlerMock = $this->getMockBuilder(ErrorLogHandler::class) - ->setMethods(array('handle')) + ->onlyMethods(array('handle')) ->getMock(); $handlerMock->setLevel($originalHandler->getLevel()); @@ -493,7 +493,7 @@ private function scrubTestAssert( } } - public function scrubDataProvider(): array + public static function scrubDataProvider(): array { return array( array( // test 1 @@ -652,9 +652,9 @@ public function testGetRequestScrubBodyContext($testData): void ); } - public function scrubQueryStringDataProvider(): array + public static function scrubQueryStringDataProvider(): array { - $data = $this->scrubDataProvider(); + $data = self::scrubDataProvider(); foreach ($data as &$test) { $test[0] = http_build_query($test[0]); @@ -757,7 +757,7 @@ public function testMaxItems($maxItemsConfig): void ); } - public function maxItemsProvider(): array + public static function maxItemsProvider(): array { return array( 'use default max_items' => array(null), diff --git a/tests/RollbarTest.php b/tests/RollbarTest.php index 619cac00..b4ad8c2a 100644 --- a/tests/RollbarTest.php +++ b/tests/RollbarTest.php @@ -223,7 +223,7 @@ public function testEnable(): void public function testLogUncaughtUnsetLogger(): void { $sut = new Rollbar; - $result = $sut->logUncaught('level', new \Exception); + $result = $sut::logUncaught('level', new \Exception); $expected = new Response(0, "Rollbar Not Initialized"); $this->assertEquals($expected, $result); } diff --git a/tests/ScrubberTest.php b/tests/ScrubberTest.php index 5d19e617..af8b9c4a 100644 --- a/tests/ScrubberTest.php +++ b/tests/ScrubberTest.php @@ -7,7 +7,7 @@ class ScrubberTest extends BaseRollbarTest { - public function scrubUrlDataProvider(): array + public static function scrubUrlDataProvider(): array { return array( 'nothing to scrub' => array( @@ -40,7 +40,7 @@ public function testScrubSafelist($testData, $scrubFields, $safelist, $expected) ); } - public function scrubSafelistProvider(): array + public static function scrubSafelistProvider(): array { return array( array( @@ -96,23 +96,23 @@ public function testScrub(array $testData, array $scrubFields, array $expected): $this->assertEquals($expected, $result, "Looks like some fields did not get scrubbed correctly."); } - public function scrubDataProvider(): array + public static function scrubDataProvider(): array { return array_merge(array( 'flat data array' => - $this->scrubFlatDataProvider(), + self::scrubFlatDataProvider(), 'recursive data array' => - $this->scrubRecursiveDataProvider(), + self::scrubRecursiveDataProvider(), 'string encoded values' => - $this->scrubFlatStringDataProvider(), + self::scrubFlatStringDataProvider(), 'string encoded recursive values' => - $this->scrubRecursiveStringDataProvider(), + self::scrubRecursiveStringDataProvider(), 'string encoded recursive values in recursive array' => - $this->scrubRecursiveStringRecursiveDataProvider() - ), $this->scrubUrlDataProvider(), $this->scrubJSONNumbersProvider()); + self::scrubRecursiveStringRecursiveDataProvider() + ), self::scrubUrlDataProvider(), self::scrubJSONNumbersProvider()); } - private function scrubJSONNumbersProvider(): array + private static function scrubJSONNumbersProvider(): array { return array( 'plain array' => array( @@ -132,7 +132,7 @@ private function scrubJSONNumbersProvider(): array ); } - private function scrubFlatDataProvider(): array + private static function scrubFlatDataProvider(): array { return array( array( // $testData @@ -149,7 +149,7 @@ private function scrubFlatDataProvider(): array ); } - private function scrubRecursiveDataProvider(): array + private static function scrubRecursiveDataProvider(): array { return array( array( // $testData @@ -196,7 +196,7 @@ private function scrubRecursiveDataProvider(): array ); } - private function scrubFlatStringDataProvider(): array + private static function scrubFlatStringDataProvider(): array { return array( // $testData @@ -225,7 +225,7 @@ private function scrubFlatStringDataProvider(): array ); } - private function scrubRecursiveStringDataProvider(): array + private static function scrubRecursiveStringDataProvider(): array { return array( // $testData @@ -260,7 +260,7 @@ private function scrubRecursiveStringDataProvider(): array ); } - private function scrubRecursiveStringRecursiveDataProvider(): array + private static function scrubRecursiveStringRecursiveDataProvider(): array { return array( array( // $testData @@ -332,7 +332,7 @@ public function testScrubArray($testData, $scrubFields, $expected): void $this->assertEquals($expected, $result, "Looks like some fields did not get scrubbed correctly."); } - public function scrubArrayDataProvider(): array + public static function scrubArrayDataProvider(): array { return array( 'flat data array' => array( diff --git a/tests/Truncation/FramesStrategyTest.php b/tests/Truncation/FramesStrategyTest.php index 96ad41b5..a99f819b 100644 --- a/tests/Truncation/FramesStrategyTest.php +++ b/tests/Truncation/FramesStrategyTest.php @@ -32,7 +32,7 @@ public function testExecute(array $data, array $expected): void * * @return array */ - public function executeProvider(): array + public static function executeProvider(): array { $data = array( 'nothing to truncate using trace key' => array( diff --git a/tests/Truncation/MinBodyStrategyTest.php b/tests/Truncation/MinBodyStrategyTest.php index 6b881154..30041e7d 100644 --- a/tests/Truncation/MinBodyStrategyTest.php +++ b/tests/Truncation/MinBodyStrategyTest.php @@ -27,7 +27,7 @@ public function testExecute($data, $expected): void $this->assertEquals($expected, $result->data()); } - public function executeProvider(): array + public static function executeProvider(): array { $data = array(); @@ -52,18 +52,18 @@ public function executeProvider(): array $data['trace data set'] = array( - $this->payloadStructureProvider(array('trace' => $traceData)), - $this->payloadStructureProvider(array('trace' => $expected)) + self::payloadStructureProvider(array('trace' => $traceData)), + self::payloadStructureProvider(array('trace' => $expected)) ); $data['trace_chain data set'] = array( - $this->payloadStructureProvider(array('trace_chain' => $traceData)), - $this->payloadStructureProvider(array('trace_chain' => $expected)) + self::payloadStructureProvider(array('trace_chain' => $traceData)), + self::payloadStructureProvider(array('trace_chain' => $expected)) ); return $data; } - protected function payloadStructureProvider($body): array + protected static function payloadStructureProvider($body): array { return array( "data" => array( diff --git a/tests/Truncation/StringsStrategyTest.php b/tests/Truncation/StringsStrategyTest.php index 79ceb92a..004fb14b 100644 --- a/tests/Truncation/StringsStrategyTest.php +++ b/tests/Truncation/StringsStrategyTest.php @@ -8,7 +8,6 @@ class StringsStrategyTest extends BaseRollbarTest { - protected function execute($data): array { $config = new Config(array('access_token' => $this->getTestAccessToken())); @@ -36,13 +35,13 @@ public function testExecuteTruncateNothing($data, $expected): void * * @return array */ - public function executeTruncateNothingProvider(): array + public static function executeTruncateNothingProvider(): array { $data = array(); $data["truncate nothing"] = array( - $this->payloadStructureProvider(str_repeat("A", 10)), - $this->payloadStructureProvider(str_repeat("A", 10)) + self::payloadStructureProvider(str_repeat("A", 10)), + self::payloadStructureProvider(str_repeat("A", 10)) ); return $data; @@ -69,23 +68,23 @@ public function testExecuteArray($data, $expectedThreshold): void * * @return array */ - public function executeArrayProvider(): array + public static function executeArrayProvider(): array { $data = array(); $thresholds = StringsStrategy::getThresholds(); foreach ($thresholds as $threshold) { - $data['truncate strings to ' . $threshold] = $this->thresholdTestProvider($threshold); + $data['truncate strings to ' . $threshold] = self::thresholdTestProvider($threshold); } return $data; } - public function thresholdTestProvider($threshold): array + public static function thresholdTestProvider($threshold): array { $stringLengthToTrim = $threshold*2; - $payload = $this->payloadStructureProvider(array()); + $payload = self::payloadStructureProvider(array()); $payload['data']['body']['message']['body']['value2'] = array(); while (strlen(json_encode($payload)) <= Truncation::MAX_PAYLOAD_SIZE) { @@ -103,19 +102,19 @@ public function executeProvider(): array $data = array(); $data["truncate nothing"] = array( - $this->payloadStructureProvider(str_repeat("A", 10)), - $this->payloadStructureProvider(str_repeat("A", 10)) + self::payloadStructureProvider(str_repeat("A", 10)), + self::payloadStructureProvider(str_repeat("A", 10)) ); $thresholds = StringsStrategy::getThresholds(); foreach ($thresholds as $threshold) { - $data['truncate strings to ' . $threshold] = $this->thresholdTestProvider($threshold); + $data['truncate strings to ' . $threshold] = self::thresholdTestProvider($threshold); } return $data; } - public function payloadStructureProvider($message): array + public static function payloadStructureProvider($message): array { return array( "data" => array( diff --git a/tests/Truncation/TruncationTest.php b/tests/Truncation/TruncationTest.php index 987bb8aa..602a9fe6 100644 --- a/tests/Truncation/TruncationTest.php +++ b/tests/Truncation/TruncationTest.php @@ -48,7 +48,6 @@ public function testCustomTruncation(): void */ public function testTruncateNoPerformance($data): void { - $data = new EncodedPayload($data); $data->encode(); @@ -62,13 +61,12 @@ public function testTruncateNoPerformance($data): void ); } - public function truncateProvider(): array + public static function truncateProvider(): array { - - $stringsTest = new StringsStrategyTest(); - $framesTest = new FramesStrategyTest(); + $stringsTest = new StringsStrategyTest("StringsStrategyTest"); + $framesTest = new FramesStrategyTest("FramesStrategyTest"); - $framesTestData = $framesTest->executeProvider(); + $framesTestData = $framesTest::executeProvider(); // Fill up frames with data to go over the allowed payload size limit $frames = &$framesTestData['truncate middle using trace key'][0]['data']['body']['trace']['frames']; @@ -84,8 +82,8 @@ public function truncateProvider(): array } $data = array_merge( - $stringsTest->executeTruncateNothingProvider(), - $stringsTest->executeArrayProvider(), + $stringsTest::executeTruncateNothingProvider(), + $stringsTest::executeArrayProvider(), $framesTestData );