Skip to content

Commit 5b6816f

Browse files
committed
fixed unit tests
1 parent 9eac1f3 commit 5b6816f

File tree

9 files changed

+55
-234
lines changed

9 files changed

+55
-234
lines changed

src/Source/GitDiff/Fetcher/Source/AbstractGitSource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
abstract readonly class AbstractGitSource implements GitSourceInterface
1616
{
1717
public function __construct(
18-
protected CommandsExecutorInterface $git,
18+
protected CommandsExecutorInterface $commandsExecutor,
1919
private FilesInterface $files,
2020
protected ?LoggerInterface $logger = null,
2121
) {}
@@ -112,7 +112,7 @@ protected function executeGitCommand(string $repository, string $command): array
112112
protected function executeGitCommandString(string $repository, string $command): string
113113
{
114114
try {
115-
return $this->git->executeString(new Command(repository: $repository, command: $command));
115+
return $this->commandsExecutor->executeString(new Command(repository: $repository, command: $command));
116116
} catch (GitCommandException $e) {
117117
$this->logger?->warning('Git command failed, returning empty result', [
118118
'command' => $command,

tests/src/Source/GitDiff/Fetcher/Source/AbstractGitSourceTest.php

Lines changed: 0 additions & 186 deletions
This file was deleted.

tests/src/Source/GitDiff/Fetcher/Source/CommitGitSourceTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Butschster\ContextGenerator\Source\GitDiff\Fetcher\Source\CommitGitSource;
88
use PHPUnit\Framework\Attributes\CoversClass;
99
use PHPUnit\Framework\Attributes\Test;
10+
use Spiral\Files\Files;
1011

1112
#[CoversClass(CommitGitSource::class)]
1213
final class CommitGitSourceTest extends GitSourceTestCase
@@ -44,7 +45,7 @@ public function it_should_not_support_other_formats(): void
4445
public function it_should_get_changed_files_in_commit_range(): void
4546
{
4647
// Mock the git command for getting changed files
47-
$expectedCommand = 'git diff --name-only \'HEAD~1..HEAD\'';
48+
$expectedCommand = 'diff --name-only HEAD~1..HEAD';
4849
$expectedFiles = ['test2.txt'];
4950
$this->mockChangedFiles($expectedCommand, $expectedFiles);
5051

@@ -60,7 +61,7 @@ public function it_should_get_changed_files_in_commit_range(): void
6061
public function it_should_get_file_diff_for_specific_file(): void
6162
{
6263
// Mock the git command for getting file diff
63-
$expectedCommand = 'git diff \'HEAD~1..HEAD\' -- \'test.txt\'';
64+
$expectedCommand = 'diff HEAD~1..HEAD -- test.txt';
6465
$expectedDiff = "diff --git a/test.txt b/test.txt\nindex 1234567..abcdef 100644\n--- a/test.txt\n+++ b/test.txt\n@@ -1 +1 @@\n-Original content\n+Modified content\n";
6566
$this->mockFileDiff($expectedCommand, $expectedDiff);
6667

@@ -105,7 +106,7 @@ public function it_should_get_changes_for_specific_commit_hash(): void
105106
$commitHash = 'abc1234567890abcdef1234567890abcdef123456';
106107

107108
// Mock the git command for getting changed files
108-
$expectedCommand = "git diff --name-only '{$commitHash}~1..{$commitHash}'";
109+
$expectedCommand = "diff --name-only {$commitHash}~1..{$commitHash}";
109110
$expectedFiles = ['specific.txt'];
110111
$this->mockChangedFiles($expectedCommand, $expectedFiles);
111112

@@ -124,6 +125,6 @@ public function it_should_get_changes_for_specific_commit_hash(): void
124125
protected function setUp(): void
125126
{
126127
parent::setUp();
127-
$this->commitGitSource = new CommitGitSource($this->gitClientMock, $this->logger);
128+
$this->commitGitSource = new CommitGitSource($this->commandExecutorMock, new Files(), $this->logger);
128129
}
129130
}

tests/src/Source/GitDiff/Fetcher/Source/FileAtCommitGitSourceTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Butschster\ContextGenerator\Source\GitDiff\Fetcher\Source\FileAtCommitGitSource;
88
use PHPUnit\Framework\Attributes\CoversClass;
99
use PHPUnit\Framework\Attributes\Test;
10+
use Spiral\Files\Files;
1011

1112
#[CoversClass(FileAtCommitGitSource::class)]
1213
final class FileAtCommitGitSourceTest extends GitSourceTestCase
@@ -39,7 +40,7 @@ public function it_should_get_file_at_specific_commit(): void
3940
$commitHash = 'abc1234567890abcdef1234567890abcdef123456';
4041

4142
// Mock the git command for getting files at commit with path filter
42-
$expectedCommand = "git show --name-only '{$commitHash}' -- 'src/'";
43+
$expectedCommand = "show --name-only {$commitHash} -- src/";
4344
$expectedFiles = ['src/test1.php'];
4445
$this->mockChangedFiles($expectedCommand, $expectedFiles);
4546

@@ -59,7 +60,7 @@ public function it_should_get_multiple_files_matching_pattern(): void
5960
$commitHash = 'abc1234567890abcdef1234567890abcdef123456';
6061

6162
// Mock the git command for getting multiple files matching pattern
62-
$expectedCommand = "git show --name-only '{$commitHash}' -- 'src/models/'";
63+
$expectedCommand = "show --name-only {$commitHash} -- src/models/";
6364
$expectedFiles = [
6465
'src/models/User.php',
6566
'src/models/Post.php',
@@ -84,7 +85,7 @@ public function it_should_get_file_content_at_commit(): void
8485
$commitHash = 'abc1234567890abcdef1234567890abcdef123456';
8586

8687
// Mock the git command for getting file content at commit
87-
$expectedCommand = "git show '{$commitHash}':'test.php'";
88+
$expectedCommand = "show {$commitHash}:test.php";
8889
$expectedContent = '<?php echo "Original";';
8990
$this->mockFileDiff($expectedCommand, $expectedContent);
9091

@@ -125,6 +126,6 @@ public function it_should_format_reference_for_display(): void
125126
protected function setUp(): void
126127
{
127128
parent::setUp();
128-
$this->fileAtCommitGitSource = new FileAtCommitGitSource($this->gitClientMock, $this->logger);
129+
$this->fileAtCommitGitSource = new FileAtCommitGitSource($this->commandExecutorMock, new Files(), $this->logger);
129130
}
130131
}

tests/src/Source/GitDiff/Fetcher/Source/GitSourceTestCase.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
namespace Tests\Source\GitDiff\Fetcher\Source;
66

7-
use Butschster\ContextGenerator\Lib\Git\GitClientInterface;
7+
use Butschster\ContextGenerator\Lib\Git\Command;
8+
use Butschster\ContextGenerator\Lib\Git\CommandsExecutorInterface;
89
use PHPUnit\Framework\MockObject\MockObject;
910
use PHPUnit\Framework\TestCase;
1011
use Psr\Log\LoggerInterface;
@@ -17,21 +18,21 @@
1718
abstract class GitSourceTestCase extends TestCase
1819
{
1920
protected string $repoDir = '/mocked/repo/path';
20-
protected MockObject&GitClientInterface $gitClientMock;
21+
protected MockObject&CommandsExecutorInterface $commandExecutorMock;
2122
protected LoggerInterface $logger;
2223

2324
protected function setUp(): void
2425
{
2526
parent::setUp();
2627

2728
// Create a mock for GitClientInterface
28-
$this->gitClientMock = $this->createMock(GitClientInterface::class);
29+
$this->commandExecutorMock = $this->createMock(CommandsExecutorInterface::class);
2930

3031
// Create a logger instance
3132
$this->logger = new NullLogger();
3233

3334
// Set default mock behavior for isValidRepository
34-
$this->gitClientMock
35+
$this->commandExecutorMock
3536
->method('isValidRepository')
3637
->with($this->repoDir)
3738
->willReturn(true);
@@ -45,11 +46,11 @@ protected function setUp(): void
4546
*/
4647
protected function mockChangedFiles(string $command, array $files): void
4748
{
48-
$this->gitClientMock
49+
$this->commandExecutorMock
4950
->expects($this->atLeastOnce())
50-
->method('execute')
51-
->with($this->repoDir, $command)
52-
->willReturn($files);
51+
->method('executeString')
52+
->with(new Command($this->repoDir, $command))
53+
->willReturn(\implode("\n", $files));
5354
}
5455

5556
/**
@@ -60,10 +61,10 @@ protected function mockChangedFiles(string $command, array $files): void
6061
*/
6162
protected function mockFileDiff(string $command, string $diff): void
6263
{
63-
$this->gitClientMock
64+
$this->commandExecutorMock
6465
->expects($this->atLeastOnce())
6566
->method('executeString')
66-
->with($this->repoDir, $command)
67+
->with(new Command($this->repoDir, $command))
6768
->willReturn($diff);
6869
}
6970

@@ -74,9 +75,9 @@ protected function mockFileDiff(string $command, string $diff): void
7475
*/
7576
protected function mockCommitHash(string $hash): string
7677
{
77-
$this->gitClientMock
78+
$this->commandExecutorMock
7879
->method('executeString')
79-
->with($this->repoDir, 'git rev-parse HEAD')
80+
->with(new Command($this->repoDir, 'git rev-parse HEAD'))
8081
->willReturn($hash);
8182

8283
return $hash;
@@ -91,9 +92,9 @@ protected function mockCommitHash(string $hash): string
9192
*/
9293
protected function mockGitCommand(string $repository, string $command, array $result): void
9394
{
94-
$this->gitClientMock
95-
->method('execute')
96-
->with($repository, $command)
95+
$this->commandExecutorMock
96+
->method('executeString')
97+
->with(new Command($this->repoDir, $command))
9798
->willReturn($result);
9899
}
99100
}

0 commit comments

Comments
 (0)