Skip to content

Commit

Permalink
Merge branch 'improve-test-quality' into 'master'
Browse files Browse the repository at this point in the history
Improve the tests

See merge request tjvb/githash!9
  • Loading branch information
tvbeek committed Aug 15, 2024
2 parents d41255f + 00329c0 commit 1b9d0a8
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 63 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
},
"require-dev": {
"fakerphp/faker": "^1.15",
"infection/infection": "^0.25.0",
"infection/infection": "^0.26.10 || ^0.27.0",
"mikey179/vfsstream": "^1.6.7",
"phpmd/phpmd": "^2.10",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.5",
Expand Down
4 changes: 2 additions & 2 deletions src/HashFinders/GitFileSystemHashFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function findHash(string $path): GitHash

$branchRefContent = $this->getBranchRefContentFromHeadContent($headContent, $path);

return new GitHash(trim($branchRefContent));
return new GitHash($branchRefContent);
}

public function isAvailable(): bool
Expand Down Expand Up @@ -67,7 +67,7 @@ private function getHeadContent(string $path): string
*/
private function getBranchRefContentFromHeadContent(string $headContent, string $path): string
{
$headContentParts = explode(':', $headContent, 2);
$headContentParts = explode(':', $headContent);
if (!isset($headContentParts[1])) {
throw new GitHashException(
'HEAD File isn\'t complete'
Expand Down
9 changes: 2 additions & 7 deletions tests/Factories/GitHashFinderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
use TJVB\GitHash\Tests\Fixtures\GitHashFinderFixture;
use TJVB\GitHash\Tests\TestCase;

class GitHashFinderFactoryTest extends TestCase
final class GitHashFinderFactoryTest extends TestCase
{
/**
* @test
*/
public function theFactoryDoesNotHaveAnyFinderByDefault(): void
{
// setup / mock

// run
$factory = new GitHashFinderFactory();
$finders = $factory->getRegisteredFinders();
Expand Down Expand Up @@ -49,24 +47,21 @@ public function weCanRegisterAFinder(): void
*/
public function weCanRegisterDefaultFindersOnAFactory(): void
{
// setup / mock

// run
$factory = new GitHashFinderFactory();
$factory->registerDefaultFinders();
$finders = $factory->getRegisteredFinders();

// verify/assert
$this->assertNotEmpty($finders);
$this->assertCount(3, $finders);
}

/**
* @test
*/
public function weCanInstantiateWithTheDefaultFinders(): void
{
// setup / mock

// run
$factory = GitHashFinderFactory::withDefaultFinders();
$finders = $factory->getRegisteredFinders();
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/GitHashFinderFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use TJVB\GitHash\Exceptions\GitHashException;
use TJVB\GitHash\Values\GitHash;

class GitHashFinderFixture implements GitHashFinder
final class GitHashFinderFixture implements GitHashFinder
{
public ?GitHash $gitHash = null;

Expand Down
140 changes: 125 additions & 15 deletions tests/HashFinders/GitFileSystemHashFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,18 @@

namespace TJVB\GitHash\Tests\HashFinders;

use org\bovigo\vfs\vfsStream;
use TJVB\GitHash\Exceptions\GitHashException;
use TJVB\GitHash\HashFinders\GitFileSystemHashFinder;
use TJVB\GitHash\HashFinders\GitProcessCommandHashFinder;
use TJVB\GitHash\Tests\TestCase;
use TJVB\GitHash\Values\GitHash;

/**
* @group branch-needed
*/
class GitFileSystemHashFinderTest extends TestCase
final class GitFileSystemHashFinderTest extends TestCase
{
/**
* @test
*/
public function weSeeThatTheFinderIsAvailable(): void
{
// setup / mock

// run
$finder = new GitFileSystemHashFinder();

Expand All @@ -35,43 +29,159 @@ public function weSeeThatTheFinderIsAvailable(): void
public function weCanFindAHashFromTheRepositoryRoot(): void
{
// setup / mock
$structure = $this->getFileSystemStructure();
$structure['.git']['refs']['heads']['testbranch'] = ' ' . self::TEST_HASH . ' ';
$vfStream = vfsStream::setup(structure: $structure);

// run
$finder = new GitFileSystemHashFinder();
$result = $finder->findHash(self::PROJECT_ROOT);
$result = $finder->findHash($vfStream->url());

// verify/assert
// @TODO work with a fixed git directory to get a fixed hash
$this->assertInstanceOf(GitHash::class, $result);
$this->assertEquals(self::TEST_HASH, $result->hash());
}

/**
* @test
*/
public function weCantFindAHashIfTheDirectoryDoesntContainAGitDirectory(): void
{
// setup / mock
// verify/assert
$this->expectException(GitHashException::class);
$this->expectExceptionMessage('Unable to find .git directory.');

// run
$finder = new GitFileSystemHashFinder();
$finder->findHash(__DIR__);
}

/**
* @test
*/
public function weCantFindAHashIfTheDirectoryIsNotPartOfAGitRepository(): void
{
// setup / mock
$structure = [
'test' => 'test',
];
$vfStream = vfsStream::setup(structure: $structure);

// verify/assert
$this->expectException(GitHashException::class);

// run
$finder = new GitFileSystemHashFinder();
$finder->findHash($vfStream->url());
}

/**
* @test
*/
public function weCantFindAHashIfTheDirectoryIsNotPartOfAGitRepository(): void
public function weCantFindAHashIfTheHeadFileIsMissing(): void
{
// setup / mock
$structure = $this->getFileSystemStructure();
unset($structure['.git']['HEAD']);
$vfStream = vfsStream::setup(structure: $structure);

// verify/assert
$this->expectException(GitHashException::class);
$this->expectExceptionMessage('Unable to find the HEAD in the .git directory');

// run
$finder = new GitProcessCommandHashFinder();
$finder->findHash(sys_get_temp_dir());
$finder = new GitFileSystemHashFinder();
$finder->findHash($vfStream->url());
}

/**
* @test
*/
public function weCantFindAHashIfTheHeadFileIsNotReadable(): void
{
// setup / mock
$structure = $this->getFileSystemStructure();
unset($structure['.git']['HEAD']);
$vfStream = vfsStream::setup(structure: $structure);
vfsStream::newFile('.git/HEAD', 0000)->at($vfStream);

// verify/assert
$this->expectException(GitHashException::class);
$this->expectExceptionMessage('Unable to read the HEAD in the .git directory');

// run
$finder = new GitFileSystemHashFinder();
$finder->findHash($vfStream->url());
}

/**
* @test
*/
public function weCantFindAHashIfTheHeadFileIsIncorrect(): void
{
// setup / mock
$structure = $this->getFileSystemStructure();
$structure['.git']['HEAD'] = 'invallid';
$vfStream = vfsStream::setup(structure: $structure);

// verify/assert
$this->expectException(GitHashException::class);
$this->expectExceptionMessage('HEAD File isn\'t complete');

// run
$finder = new GitFileSystemHashFinder();
$finder->findHash($vfStream->url());
}

/**
* @test
*/
public function weCantFindAHashIfTheBranchRefDoesNotExist(): void
{
// setup / mock
$structure = $this->getFileSystemStructure();
unset($structure['.git']['refs']['heads']['testbranch']);
$vfStream = vfsStream::setup(structure: $structure);

// verify/assert
$this->expectException(GitHashException::class);
$this->expectExceptionMessage('Unable to find the branch ref file.');

// run
$finder = new GitFileSystemHashFinder();
$finder->findHash($vfStream->url());
}

/**
* @test
*/
public function weCantFindAHashIfTheBranchRefIsNotReadable(): void
{
// setup / mock
$structure = $this->getFileSystemStructure();
unset($structure['.git']['refs']['heads']['testbranch']);
$vfStream = vfsStream::setup(structure: $structure);
vfsStream::newFile('.git/refs/heads/testbranch', 0000)->at($vfStream);

// verify/assert
$this->expectException(GitHashException::class);
$this->expectExceptionMessage('Unable to read the branch ref file.');

// run
$finder = new GitFileSystemHashFinder();
$finder->findHash($vfStream->url());
}

private function getFileSystemStructure(): array
{
return [
'.git' => [
'HEAD' => 'ref: refs/heads/testbranch',
'refs' => [
'heads' => [
'testbranch' => self::TEST_HASH,
],
],
],
];
}
}
12 changes: 2 additions & 10 deletions tests/HashFinders/GitProcessCommandHashFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
use TJVB\GitHash\Tests\TestCase;
use TJVB\GitHash\Values\GitHash;

class GitProcessCommandHashFinderTest extends TestCase
final class GitProcessCommandHashFinderTest extends TestCase
{
/**
* @test
*/
public function weSeeThatTheFinderIsAvailable(): void
{
// setup / mock

// run
$finder = new GitProcessCommandHashFinder();

Expand All @@ -31,8 +29,6 @@ public function weSeeThatTheFinderIsAvailable(): void
*/
public function weCanFindAHashFromTheRepositoryRoot(): void
{
// setup / mock

// run
$finder = new GitProcessCommandHashFinder();
$result = $finder->findHash(self::PROJECT_ROOT);
Expand All @@ -47,8 +43,6 @@ public function weCanFindAHashFromTheRepositoryRoot(): void
*/
public function weCanFindAHashFromASubDirectory(): void
{
// setup / mock

// run
$finder = new GitProcessCommandHashFinder();
$result = $finder->findHash(self::PROJECT_ROOT);
Expand All @@ -63,13 +57,11 @@ public function weCanFindAHashFromASubDirectory(): void
*/
public function weCantFindAHashIfTheDirectoryIsNotPartOfAGitRepository(): void
{
// setup / mock
// verify/assert
$this->expectException(GitHashException::class);

// run
$finder = new GitProcessCommandHashFinder();
$finder->findHash(sys_get_temp_dir());

// verify/assert
}
}
25 changes: 15 additions & 10 deletions tests/HashFinders/GitShellExecCommandHashFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@

namespace Tjvb\GitHash\Tests\HashFinders;

use TJVB\GitHash\Exceptions\GitHashException;
use TJVB\GitHash\HashFinders\GitFileSystemHashFinder;
use TJVB\GitHash\HashFinders\GitProcessCommandHashFinder;
use TJVB\GitHash\HashFinders\GitShellExecCommandHashFinder;
use TJVB\GitHash\Tests\TestCase;
use TJVB\GitHash\Values\GitHash;

class GitShellExecCommandHashFinderTest extends TestCase
final class GitShellExecCommandHashFinderTest extends TestCase
{
/**
* @test
*/
public function weSeeThatTheFinderIsAvailable(): void
{
// setup / mock

// run
$finder = new GitShellExecCommandHashFinder();

Expand All @@ -32,8 +27,6 @@ public function weSeeThatTheFinderIsAvailable(): void
*/
public function weCanFindAHashFromTheRepositoryRoot(): void
{
// setup / mock

// run
$finder = new GitShellExecCommandHashFinder();
$result = $finder->findHash(self::PROJECT_ROOT);
Expand All @@ -46,10 +39,22 @@ public function weCanFindAHashFromTheRepositoryRoot(): void
/**
* @test
*/
public function weCanFindAHashFromASubDirectory(): void
public function weCanHandleASeparatorAtTheEndOfThePath(): void
{
// setup / mock
// run
$finder = new GitShellExecCommandHashFinder();
$result = $finder->findHash(self::PROJECT_ROOT . DIRECTORY_SEPARATOR);

// verify/assert
// @TODO work with a fixed git directory to get a fixed hash
$this->assertInstanceOf(GitHash::class, $result);
}

/**
* @test
*/
public function weCanFindAHashFromASubDirectory(): void
{
// run
$finder = new GitShellExecCommandHashFinder();
$result = $finder->findHash(self::PROJECT_ROOT);
Expand Down
Loading

0 comments on commit 1b9d0a8

Please sign in to comment.