From 9318473a4a48d9e9267fce02fc5e97a288af2c53 Mon Sep 17 00:00:00 2001 From: Olha Livitchuk <77281282+olhalivitchuk@users.noreply.github.com> Date: Tue, 4 Jun 2024 16:48:29 +0200 Subject: [PATCH] SUPESC-849 Fixed PHPStan issue (#319) --- .../FileErrorsFetcher/FileErrorsFetcher.php | 6 ++-- .../FileErrorsFetcherTest.php | 36 +++++++++++++++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/DynamicEvaluator/Application/Checker/BrokenPhpFilesChecker/FileErrorsFetcher/FileErrorsFetcher.php b/src/DynamicEvaluator/Application/Checker/BrokenPhpFilesChecker/FileErrorsFetcher/FileErrorsFetcher.php index 2b9baf8f..afd416b6 100644 --- a/src/DynamicEvaluator/Application/Checker/BrokenPhpFilesChecker/FileErrorsFetcher/FileErrorsFetcher.php +++ b/src/DynamicEvaluator/Application/Checker/BrokenPhpFilesChecker/FileErrorsFetcher/FileErrorsFetcher.php @@ -138,7 +138,7 @@ protected function fetchNewFileError(string $file, array $message): ?FileErrorDt $this->assertArrayKeyExists($message, 'line'); $this->assertArrayKeyExists($message, 'message'); - $fileError = new FileErrorDto($file, $message['line'], $message['message']); + $fileError = new FileErrorDto($file, (int)$message['line'], (string)$message['message']); if ($this->baselineStorage->hasFileError($fileError)) { return null; @@ -146,7 +146,7 @@ protected function fetchNewFileError(string $file, array $message): ?FileErrorDt $this->baselineStorage->addFileError($fileError); - return new FileErrorDto($file, $message['line'], $message['message']); + return new FileErrorDto($file, (int)$message['line'], (string)$message['message']); } /** @@ -212,7 +212,7 @@ public function reset(): void */ protected function assertArrayKeyExists(array $data, string $key, bool $isArray = false): void { - if (!isset($data[$key]) || ($isArray && !is_array($data[$key]))) { + if (!array_key_exists($key, $data) || ($isArray && !is_array($data[$key]))) { throw new InvalidArgumentException(sprintf( 'Unable to find %s key or it\'s not an array in %s. Tooling export format is changes.', $key, diff --git a/tests/DynamicEvaluatorTest/Application/Checker/BrokenPhpFilesChecker/FileErrorsFetcher/FileErrorsFetcherTest.php b/tests/DynamicEvaluatorTest/Application/Checker/BrokenPhpFilesChecker/FileErrorsFetcher/FileErrorsFetcherTest.php index 84eab909..7791de44 100644 --- a/tests/DynamicEvaluatorTest/Application/Checker/BrokenPhpFilesChecker/FileErrorsFetcher/FileErrorsFetcherTest.php +++ b/tests/DynamicEvaluatorTest/Application/Checker/BrokenPhpFilesChecker/FileErrorsFetcher/FileErrorsFetcherTest.php @@ -74,13 +74,16 @@ public function testFetchProjectFileErrorsAndSaveInBaseLineShouldReturnEmptyWhen } /** + * @dataProvider projectFileErrorsDataProvider + * + * @param array $toolOutput + * @param array $expectedResult + * * @return void */ - public function testFetchProjectFileErrorsAndSaveInBaseLineShouldFetchFileErrors(): void + public function testFetchProjectFileErrorsAndSaveInBaseLineShouldFetchFileErrors(array $toolOutput, array $expectedResult): void { // Arrange - $toolOutput = ['files' => ['src/someClass.php' => ['messages' => [['line' => 1, 'message' => 'test message']]]]]; - $baseLineStorage = new BaselineStorage(); $fileErrorsFetcher = new FileErrorsFetcher('', '', $this->createProcessRunnerServiceMock($toolOutput), $baseLineStorage, $this->createLoggerMock()); @@ -89,9 +92,30 @@ public function testFetchProjectFileErrorsAndSaveInBaseLineShouldFetchFileErrors // Assert $this->assertCount(1, $fileErrors); - $this->assertSame('src/someClass.php', $fileErrors[0]->getFilename()); - $this->assertSame(1, $fileErrors[0]->getLine()); - $this->assertSame('test message', $fileErrors[0]->getMessage()); + $this->assertSame($expectedResult['expectedClass'], $fileErrors[0]->getFilename()); + $this->assertSame($expectedResult['expectedLine'], $fileErrors[0]->getLine()); + $this->assertSame($expectedResult['expectedMessage'], $fileErrors[0]->getMessage()); + } + + /** + * @return array + */ + public function projectFileErrorsDataProvider(): array + { + return [ + [ + ['files' => ['src/someClass.php' => ['messages' => [['line' => 1, 'message' => 'test message']]]]], + ['expectedClass' => 'src/someClass.php', 'expectedLine' => 1, 'expectedMessage' => 'test message'], + ], + [ + ['files' => ['src/someClass.php' => ['messages' => [['line' => null, 'message' => 'test message']]]]], + ['expectedClass' => 'src/someClass.php', 'expectedLine' => 0, 'expectedMessage' => 'test message'], + ], + [ + ['files' => ['src/someClass.php' => ['messages' => [['line' => 1, 'message' => null]]]]], + ['expectedClass' => 'src/someClass.php', 'expectedLine' => 1, 'expectedMessage' => ''], + ], + ]; } /**