This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
808a505
commit 08cd7ae
Showing
4 changed files
with
739 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace AppBundle\PullRequests; | ||
|
||
/** | ||
* Extract human readable data from Pull diff. | ||
*/ | ||
class Diff | ||
{ | ||
/* string */ | ||
public static $content; | ||
/* array */ | ||
public static $lines; | ||
|
||
const TOKEN_PLUS = '+'; | ||
|
||
/** | ||
* Parse diff content and look for regexp pattern. | ||
* | ||
* @param string a regex expression | ||
* @param string the diff content | ||
* | ||
* @return bool | ||
*/ | ||
public static function match($pattern, $diffContent) | ||
{ | ||
self::$content = $diffContent; | ||
|
||
$token = strtok($diffContent, PHP_EOL); | ||
|
||
while ($token !== false) { | ||
if ( | ||
0 === strpos($token, self::TOKEN_PLUS) && | ||
1 === preg_match($pattern, $token) | ||
) { | ||
return true; | ||
} | ||
$token = strtok(PHP_EOL); | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace tests\AppBundle\PullRequests; | ||
|
||
use AppBundle\PullRequests\Diff; | ||
|
||
/** | ||
* @author Mickaël Andrieu <andrieu.travail@gmail.com> | ||
*/ | ||
class DiffTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $gitDiff; | ||
const TRANS_PATTERN = '#(trans\(|l\()#'; | ||
|
||
public function testMatch() | ||
{ | ||
$matched = Diff::match(self::TRANS_PATTERN, $this->getExpectedDiff(true)); | ||
$this->assertTrue($matched); | ||
|
||
$unMatched = Diff::match(self::TRANS_PATTERN, $this->getExpectedDiff(false)); | ||
$this->assertFalse($unMatched); | ||
} | ||
|
||
private function getExpectedDiff($matched = true) | ||
{ | ||
$filename = $matched ? 'matched' : 'not_matched'; | ||
|
||
return file_get_contents(__DIR__.'/../webhook_examples/git_diff_'.$filename.'.diff'); | ||
} | ||
} |
Oops, something went wrong.