Skip to content

Commit

Permalink
Test the retriever and update the namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
tvbeek committed Sep 7, 2021
1 parent b96d011 commit e623b31
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/examples/basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use TJVB\GitHash\Exceptions\GitHashException;
use TJVB\GitHash\Factories\GitHashFinderFactory;
use TJVB\GitHash\Retriever\Retriever;
use TJVB\GitHash\Retrievers\Retriever;

require_once 'vendor/autoload.php';

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/without_git.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use TJVB\GitHash\Exceptions\GitHashException;
use TJVB\GitHash\Factories\GitHashFinderFactory;
use TJVB\GitHash\HashFinders\GitFileSystemHashFinder;
use TJVB\GitHash\Retriever\Retriever;
use TJVB\GitHash\Retrievers\Retriever;

require_once 'vendor/autoload.php';

Expand Down
2 changes: 1 addition & 1 deletion src/Retrievers/Retriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace TJVB\GitHash\Retriever;
namespace TJVB\GitHash\Retrievers;

use TJVB\GitHash\Contracts\FinderFactory;
use TJVB\GitHash\Contracts\GitHashRetriever;
Expand Down
34 changes: 34 additions & 0 deletions tests/Fixtures/GitHashFinderFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace TJVB\GitHash\Tests\Fixtures;

use TJVB\GitHash\Contracts\GitHashFinder;
use TJVB\GitHash\Exceptions\GitHashException;
use TJVB\GitHash\Values\GitHash;

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

public bool $available = true;

public ?GitHashException $exception = null;

public function findHash(string $path): GitHash
{
if ($this->exception !== null) {
throw $this->exception;
}
if ($this->gitHash === null) {
$this->gitHash = new GitHash('invalidhash');
}
return $this->gitHash;
}

public function isAvailable(): bool
{
return $this->available;
}
}
230 changes: 230 additions & 0 deletions tests/Retrievers/RetrieverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<?php

declare(strict_types=1);

namespace TJVB\GitHash\Tests\Retrievers;

use TJVB\GitHash\Exceptions\GitHashException;
use TJVB\GitHash\Factories\GitHashFinderFactory;
use TJVB\GitHash\Retrievers\Retriever;
use TJVB\GitHash\Tests\Fixtures\GitHashFinderFixture;
use TJVB\GitHash\Tests\TestCase;
use TJVB\GitHash\Values\GitHash;

class RetrieverTest extends TestCase
{

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

// run
$retriever = new Retriever();

// verify/assert
$this->assertNull($retriever->getFinderFactory());
}

/**
* @test
*/
public function weCanSetTheFinderFactory(): void
{
// setup / mock
$factory = new GitHashFinderFactory();

// run
$retriever = new Retriever();
$retriever->setFinderFactory($factory);

// verify/assert
$this->assertEquals($factory, $retriever->getFinderFactory());
}

/**
* @test
*
* @dataProvider hashMethodsProvider
*/
public function weCanNotGetAHashWithoutAFinderFactory(string $method): void
{
// setup / mock
$this->expectException(GitHashException::class);
$this->expectExceptionMessage('We can\'t find a hash if we didn\'t got a finder factory');

// run
$retriever = new Retriever();
$retriever->$method(self::PROJECT_ROOT);

// verify/assert
// no assertion because we expect the exception
}

/**
* @test
*
* @dataProvider hashMethodsProvider
*/
public function weCanGetTheHashIfWeHaveAnAvailableFinder(string $method): void
{
// setup / mock
$gitHash = new GitHash(sha1('test' . rand()));
$finder = new GitHashFinderFixture();
$finder->gitHash = $gitHash;

$factory = new GitHashFinderFactory();
$factory->register($finder);

// run
$retriever = new Retriever();
$retriever->setFinderFactory($factory);

$result = $retriever->$method(self::PROJECT_ROOT);

// verify/assert
$this->assertEquals($gitHash, $result);
}

/**
* @test
*
* @dataProvider hashMethodsProvider
*/
public function weGetAnExceptionIfNoneOfTheFindersIsAvailable(string $method): void
{
// setup / mock
$this->expectException(GitHashException::class);
if ($method === 'getHash') {
$this->expectExceptionMessage('No finder available');
}
if ($method === 'GitHashException') {
$this->expectExceptionMessage('Unable to find hash');
}

$finder = new GitHashFinderFixture();
$finder->available = false;

// run
$factory = new GitHashFinderFactory();
$factory->register($finder);

// run
$retriever = new Retriever();
$retriever->setFinderFactory($factory);

$retriever->$method(self::PROJECT_ROOT);
// verify/assert
// no assertion because we expect the exception
}

/**
* @test
*
* @dataProvider hashMethodsProvider
*/
public function weGetTheHashIfThereAreFindersAvailableWhileOthersAreNotAvailable(string $method): void
{
// setup / mock
$gitHash = new GitHash(sha1('test' . rand()));
$finder1 = new GitHashFinderFixture();
$finder1->available = false;
$finder2 = new GitHashFinderFixture();
$finder2->gitHash = $gitHash;

$factory = new GitHashFinderFactory();
$factory->register($finder1);
$factory->register($finder2);

// run
$retriever = new Retriever();
$retriever->setFinderFactory($factory);

$result = $retriever->$method(self::PROJECT_ROOT);

// verify/assert
$this->assertEquals($gitHash, $result);
}

/**
* @test
*/
public function weGetAnExceptionIfAFinderThrowsAnExceptionBeforeWeHaveTheHash(): void
{
// setup / mock
$exception = new GitHashException('test exception');
$this->expectExceptionObject($exception);

$gitHash = new GitHash(sha1('test' . rand()));
$finder1 = new GitHashFinderFixture();
$finder1->exception = $exception;

$finder2 = new GitHashFinderFixture();
$finder2->gitHash = $gitHash;

$factory = new GitHashFinderFactory();
$factory->register($finder1);
$factory->register($finder2);

// run
$retriever = new Retriever();
$retriever->setFinderFactory($factory);

$retriever->getHash(self::PROJECT_ROOT);
// verify/assert
// no assertion because we expect the exception
}

/**
* @test
*/
public function weDontGetAnExceptionIfAFinderThrowsAnExceptionBeforeWeHaveTheHashAndIgnoreFailures(): void
{
// setup / mock
$exception = new GitHashException('test exception');

$gitHash = new GitHash(sha1('test' . rand()));
$finder1 = new GitHashFinderFixture();
$finder1->exception = $exception;

$finder2 = new GitHashFinderFixture();
$finder2->gitHash = $gitHash;

$factory = new GitHashFinderFactory();
$factory->register($finder1);
$factory->register($finder2);

// run
$retriever = new Retriever();
$retriever->setFinderFactory($factory);

$result = $retriever->getHashAndIgnoreFailures(self::PROJECT_ROOT);
// verify/assert
$this->assertEquals($gitHash, $result);
}

/**
* @test
*/
public function weGetTheRetrieverWithTheFactoryIfWeInstantiateWithIt(): void
{
// setup / mock
$factory = new GitHashFinderFactory();

// run
$retriever = Retriever::getWithFactory($factory);

// verify/assert
$this->assertEquals($factory, $retriever->getFinderFactory());
}

public function hashMethodsProvider(): array
{
return [
'with failures' => ['getHash'],
'ignore failures' => ['getHashAndIgnoreFailures'],
];
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
class TestCase extends BaseTestCase
{


public const PROJECT_ROOT = __DIR__ . DIRECTORY_SEPARATOR . '..';
}

0 comments on commit e623b31

Please sign in to comment.