Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Added pull request diff matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelandrieu committed Sep 19, 2016
1 parent 808a505 commit 08cd7ae
Show file tree
Hide file tree
Showing 4 changed files with 739 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/AppBundle/PullRequests/Diff.php
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;
}
}
30 changes: 30 additions & 0 deletions tests/AppBundle/PullRequests/DiffTest.php
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');
}
}
Loading

0 comments on commit 08cd7ae

Please sign in to comment.