PHPSensor implements Sensor { @Override public void execute(SensorContext context) { FileSystem fileSystem = context.fileSystem(); FilePredicate phpFilePredicate = fileSystem.predicates().hasLanguage(Php.KEY); List<InputFile> inputFiles = new ArrayList<>(); fileSystem.inputFiles(phpFilePredicate).forEach(inputFiles::add); SymbolScanner symbolScanner = new SymbolScanner(context); try { symbolScanner.execute(inputFiles); ProjectSymbolData projectSymbolData = symbolScanner.getProjectSymbolData(); new AnalysisScanner(context, projectSymbolData).execute(inputFiles); if (!inSonarLint(context)) { processTestsAndCoverage(context); } } catch (CancellationException e) { LOG.info(e.getMessage()); } } PHPSensor(FileLinesContextFactory fileLinesContextFactory,
    CheckFactory checkFactory, NoSonarFilter noSonarFilter);  PHPSensor(FileLinesContextFactory fileLinesContextFactory,
    CheckFactory checkFactory, NoSonarFilter noSonarFilter, @Nullable PHPCustomRuleRepository[] customRuleRepositories);   PHPSensor(FileLinesContextFactory fileLinesContextFactory, PHPChecks checks, NoSonarFilter noSonarFilter); @Override void describe(SensorDescriptor descriptor); @Override void execute(SensorContext context); @Override String toString();  }
@Test public void test_no_cpd_on_test_files() { String fileName = "cpd.php"; String componentKey = "moduleKey:" + fileName; PHPSensor phpSensor = createSensor(); InputFile testFile = inputFile(fileName, Type.TEST); context.fileSystem().add(testFile); phpSensor.execute(context); List<TokensLine> tokensLines = context.cpdTokens(componentKey); assertThat(tokensLines).isNull(); }
@Test public void init_and_terminate_method_called_only_once() throws Exception { PHPCheck check = spy(new PHPVisitorCheck() {}); addInputFiles(ANALYZED_FILE, "cpd.php", "empty.php"); createSensor(check).execute(context); verify(check, times(1)).init(); verify(check, times(1)).terminate(); }
@Test public void exception_should_report_file_name() throws Exception { PHPCheck check = new ExceptionRaisingCheck(new IllegalStateException()); addInputFiles(ANALYZED_FILE); createSensor(check).execute(context); assertThat(logTester.logs(LoggerLevel.ERROR)).contains("Could not analyse PHPSquidSensor.php"); }
@Test public void exception_should_fail_analysis_if_configured_so() throws Exception { RuntimeException exception = new NumberFormatException(); PHPCheck check = new ExceptionRaisingCheck(exception); addInputFiles(ANALYZED_FILE); context.setSettings(new MapSettings().setProperty("sonar.internal.analysis.failFast", "true")); PHPSensor sensor = createSensor(check); assertThatThrownBy(() -> sensor.execute(context)) .isInstanceOf(IllegalStateException.class) .hasCause(exception) .hasMessageContaining("PHPSquidSensor.php"); }
@Test public void should_disable_unnecessary_features_for_sonarlint() throws Exception { context.settings().setProperty(PhpPlugin.PHPUNIT_TESTS_REPORT_PATH_KEY, PhpTestUtils.PHPUNIT_REPORT_NAME); context.settings().setProperty(PhpPlugin.PHPUNIT_COVERAGE_REPORT_PATHS_KEY, PhpTestUtils.PHPUNIT_COVERAGE_REPORT); DefaultInputFile inputFile = inputFile(ANALYZED_FILE); DefaultInputFile testFile = TestInputFileBuilder.create("moduleKey", "src/AppTest.php") .setModuleBaseDir(context.fileSystem().baseDirPath()) .setType(InputFile.Type.TEST) .setLanguage(Php.KEY).build(); String testFileKey = testFile.key(); String mainFileKey = inputFile.key(); context.fileSystem().add(inputFile); context.fileSystem().add(testFile); context.setRuntime(SONARLINT_RUNTIME); createSensor().execute(context); assertThat(context.cpdTokens(mainFileKey)).isNull(); assertThat(context.highlightingTypeAt(mainFileKey, 1, 0)).isEmpty(); assertThat(context.measure(testFileKey, CoreMetrics.TESTS)).isNull(); assertThat(context.measure(testFileKey, CoreMetrics.LINES_TO_COVER)).isNull(); assertThat(context.measure(mainFileKey, CoreMetrics.NCLOC)).isNull(); assertThat(context.referencesForSymbolAt(mainFileKey, 6, 7)).isNull(); context.setRuntime(NOT_SONARLINT_RUNTIME); createSensor().execute(context); assertThat(context.cpdTokens(mainFileKey)).isNotEmpty(); assertThat(context.highlightingTypeAt(mainFileKey, 2, 0)).isNotEmpty(); assertThat(context.measure(testFileKey, CoreMetrics.TESTS)).isNotNull(); assertThat(context.measure(mainFileKey, CoreMetrics.NCLOC)).isNotNull(); assertThat(context.referencesForSymbolAt(mainFileKey, 6, 7)).isNotNull(); }
@Test public void no_measures_for_test_files() { checkFactory = new CheckFactory(new ActiveRulesBuilder() .addRule(newActiveRule("S2187")) .build()); InputFile testFile = inputFile(TEST_FILE, Type.TEST); String testFileKey = testFile.key(); context.fileSystem().add(testFile); context.setRuntime(NOT_SONARLINT_RUNTIME); createSensor().execute(context); assertThat(context.allIssues()).isNotEmpty(); assertThat(context.measure(testFileKey, CoreMetrics.NCLOC)).isNull(); }
@Test public void should_use_multi_path_coverage() throws Exception { context.setRuntime(SONARQUBE_6_7); context.settings().setProperty(PhpPlugin.PHPUNIT_COVERAGE_REPORT_PATHS_KEY, String.join(",", PhpTestUtils.GENERATED_UT_COVERAGE_REPORT_RELATIVE_PATH, PhpTestUtils.GENERATED_IT_COVERAGE_REPORT_RELATIVE_PATH, " ", PhpTestUtils.GENERATED_OVERALL_COVERAGE_REPORT_RELATIVE_PATH)); DefaultInputFile inputFile = inputFile("src/App.php"); createReportWithAbsolutePath(PhpTestUtils.GENERATED_UT_COVERAGE_REPORT_RELATIVE_PATH, PhpTestUtils.UT_COVERAGE_REPORT_RELATIVE_PATH, inputFile); createReportWithAbsolutePath(PhpTestUtils.GENERATED_IT_COVERAGE_REPORT_RELATIVE_PATH, PhpTestUtils.IT_COVERAGE_REPORT_RELATIVE_PATH, inputFile); createReportWithAbsolutePath(PhpTestUtils.GENERATED_OVERALL_COVERAGE_REPORT_RELATIVE_PATH, PhpTestUtils.OVERALL_COVERAGE_REPORT_RELATIVE_PATH, inputFile); String mainFileKey = inputFile.key(); context.fileSystem().add(inputFile); createSensor().execute(context); assertThat(context.lineHits(mainFileKey, 3)).isEqualTo(3); assertThat(context.lineHits(mainFileKey, 6)).isEqualTo(2); assertThat(context.lineHits(mainFileKey, 7)).isEqualTo(1); }
@Test public void should_log_message_when_no_coverage_and_test_property() throws Exception { context.setSettings(new MapSettings()); context.setRuntime(SONARQUBE_6_7); createSensor().execute(context); assertThat(logTester.logs()).contains( "No PHPUnit test report provided (see '" + PhpPlugin.PHPUNIT_TESTS_REPORT_PATH_KEY + "' property)", "No PHPUnit coverage reports provided (see '" + PhpPlugin.PHPUNIT_COVERAGE_REPORT_PATHS_KEY + "' property)"); logTester.clear(); }
@Test public void should_use_test_file_checks() { TestFileCheck check = new TestFileCheck(); InputFile testFile = inputFile(ANALYZED_FILE, Type.TEST); context.fileSystem().add(testFile); createSensor(check).execute(context); assertThat(check.wasTriggered).isTrue(); }