Skip to content

Commit

Permalink
🚨 getRevision test
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptiste authored and lenybernard committed Jan 24, 2017
1 parent 07e5162 commit 27615c6
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions Tests/GitRevision/GitRevsionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Troopers\MetricsBundle\Tests\GitRevsion;

use Troopers\MetricsBundle\Monolog\Processor\GitProcessor;

class GitRevsionTest extends \PHPUnit_Framework_TestCase
{
static $defaultPath = '/tmp';

/**
* @before
*/
public function before(){
chdir(self::$defaultPath);
if (file_exists('REVISION')) {
unlink('REVISION');
}
}

/**
* @after
*/
public function after() {
GitProcessor::clearCache();
}

public function testGitFieldIsAdded()
{
$rootPath = self::$defaultPath;

$gitProcessor = new GitProcessor($rootPath);
$record = $gitProcessor([]);

$this->assertArrayHasKey('extra', $record);
$this->assertArrayHasKey('@git', $record['extra']);

$this->assertEquals(null, $record['extra']['@git']);
}

public function testGitRevisionWithGit()
{
$rootPath = __DIR__;
chdir($rootPath);
$gitProcessor = new GitProcessor($rootPath);
$record = $gitProcessor([]);

$this->assertRegExp("/^[0-9a-f]{40}|[0-9a-f]{6,8}$/", $record['extra']['@git']);
}

public function testGitRevisionWithBadRevisionFile()
{
$rootPath = self::$defaultPath;

$revisionFile = fopen('REVISION', 'w');
fputs($revisionFile, 'This is not a git commit sha');
fclose($revisionFile);

$gitProcessor = new GitProcessor($rootPath);
$record = $gitProcessor([]);

$this->assertEquals(null, $record['extra']['@git']);
}

public function testGitRevisionWithGoodRevisionFile()
{
$rootPath = self::$defaultPath;

$revisionFile = fopen('REVISION', 'w');
fputs($revisionFile, sha1(time()));
fclose($revisionFile);

$gitProcessor = new GitProcessor($rootPath);
$record = $gitProcessor([]);

$this->assertRegExp("/^[0-9a-f]{40}|[0-9a-f]{6,8}$/", $record['extra']['@git']);
}
}

0 comments on commit 27615c6

Please sign in to comment.