Skip to content

Commit

Permalink
SONARPHP-1343 Improve code coverage (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstachniuk authored Jan 11, 2023
1 parent 8bbcb99 commit 7edf35b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 21 deletions.
4 changes: 4 additions & 0 deletions php-frontend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
8 changes: 4 additions & 4 deletions php-frontend/src/test/java/org/sonar/php/cache/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

public class CacheTest {
Expand Down Expand Up @@ -69,7 +69,7 @@ public void shouldNotWriteToCacheIfItsDisabled() {

cache.write(DEFAULT_INPUT_FILE, data);

verifyZeroInteractions(writeCache);
verifyNoInteractions(writeCache);
}

@Test
Expand Down Expand Up @@ -138,7 +138,7 @@ public void shouldNotWriteToCacheWhenNoContent() {

cache.write(notExistingFile(), data);

verifyZeroInteractions(writeCache);
verifyNoInteractions(writeCache);
}

@Test
Expand All @@ -151,7 +151,7 @@ public void shouldNotReadFromCacheWhenNoContent() {
SymbolTableImpl actual = cache.read(notExistingFile());

assertThat(actual).isNull();
verifyZeroInteractions(readCache);
verifyNoInteractions(readCache);
}

void warmupReadCache(SymbolTableImpl data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,24 @@

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.php.compat.PhpFileImpl;
import org.sonar.plugins.php.api.visitors.PhpFile;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mockStatic;

public class HashProviderTest {

@Test
public void shouldProvideHashForInputFile() {
File file = new File("src/test/resources/symbols/symbolTable.php");
DefaultInputFile inputFile = new TestInputFileBuilder("", file.getPath())
.setCharset(StandardCharsets.UTF_8)
.build();
DefaultInputFile inputFile = exampleInputFile();

String hash = HashProvider.hash(inputFile);

Expand All @@ -45,10 +47,7 @@ public void shouldProvideHashForInputFile() {

@Test
public void shouldProvideHashForPhpFile() {
File file = new File("src/test/resources/symbols/symbolTable.php");
DefaultInputFile inputFile = new TestInputFileBuilder("", file.getPath())
.setCharset(StandardCharsets.UTF_8)
.build();
DefaultInputFile inputFile = exampleInputFile();
PhpFile phpFile = PhpFileImpl.create(inputFile);

String hash = HashProvider.hash(phpFile);
Expand All @@ -58,10 +57,7 @@ public void shouldProvideHashForPhpFile() {

@Test
public void shouldReturnsNullWhenFileDoesntExist() {
File file = new File("do_not_exist_file.php");
DefaultInputFile inputFile = new TestInputFileBuilder("", file.getPath())
.setCharset(StandardCharsets.UTF_8)
.build();
DefaultInputFile inputFile = notExistingFile();

String hash = HashProvider.hash(inputFile);

Expand All @@ -70,10 +66,7 @@ public void shouldReturnsNullWhenFileDoesntExist() {

@Test
public void shouldReturnsNullWhenPhpFileDoesntExist() {
File file = new File("do_not_exist_file.php");
DefaultInputFile inputFile = new TestInputFileBuilder("", file.getPath())
.setCharset(StandardCharsets.UTF_8)
.build();
DefaultInputFile inputFile = notExistingFile();

String hash = HashProvider.hash(PhpFileImpl.create(inputFile));

Expand All @@ -92,4 +85,34 @@ public void shouldReturnHashForEmptyFile() {

assertThat(hash).isEqualTo("-12804752987762098394035772686106585063470084017442529046078187006797464553387");
}

@Test
public void shouldReturnWhenHashAlgorithmNotAvailable() {
try (MockedStatic<MessageDigest> messageDigest = mockStatic(MessageDigest.class)) {
messageDigest.when(() -> MessageDigest.getInstance(any()))
.thenThrow(new NoSuchAlgorithmException());

DefaultInputFile inputFile = exampleInputFile();

String hash = HashProvider.hash(inputFile);

assertThat(hash).isNull();
}
}

private static DefaultInputFile exampleInputFile() {
File file = new File("src/test/resources/symbols/symbolTable.php");
DefaultInputFile inputFile = new TestInputFileBuilder("", file.getPath())
.setCharset(StandardCharsets.UTF_8)
.build();
return inputFile;
}

private static DefaultInputFile notExistingFile() {
File file = new File("do_not_exist_file.php");
DefaultInputFile inputFile = new TestInputFileBuilder("", file.getPath())
.setCharset(StandardCharsets.UTF_8)
.build();
return inputFile;
}
}
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<version.commons-lang>3.10</version.commons-lang>
<version.junit>4.13.1</version.junit>
<version.maven-project>2.0.6</version.maven-project>
<version.mockito>2.21.0</version.mockito>
<version.mockito>4.11.0</version.mockito>
<version.sonar>9.7.1.62043</version.sonar>
<version.staxmate>2.0.1</version.staxmate>
<version.sonar-orchestrator>3.40.0.183</version.sonar-orchestrator>
Expand Down Expand Up @@ -210,6 +210,12 @@
<version>${version.mockito}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${version.mockito}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down

0 comments on commit 7edf35b

Please sign in to comment.