Skip to content

Commit

Permalink
Merge pull request #7 from PrestaShop/simplify-git-diff-parser
Browse files Browse the repository at this point in the history
Added tests
  • Loading branch information
mickaelandrieu committed Oct 27, 2016
2 parents ed60230 + 04cdf12 commit d89f319
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
10 changes: 2 additions & 8 deletions src/AppBundle/Diff/Iterator/FilesIterator.php
Expand Up @@ -2,17 +2,13 @@

namespace AppBundle\Diff\Iterator;

use AppBundle\Diff\Iterator\Filter\AdditionsFilterIterator;
use AppBundle\Diff\Iterator\Filter\DeletionsFilterIterator;
use AppBundle\Diff\Iterator\Filter\ContentFilterIterator;
use AppBundle\Diff\Iterator\Filter\PathFilterIterator;

class FilesIterator implements \IteratorAggregate, \Countable
{
private $contains;
private $files;
private $iterator;
private $names;

public function __construct($files)
{
Expand All @@ -32,16 +28,14 @@ public function count()

public function path($regexp)
{
$this->names[] = $regexp;
$this->iterator = new PathFilterIterator($this->iterator, $this->names);
$this->iterator = new PathFilterIterator($this->iterator, $regexp);

return $this;
}

public function contains($regexp)
{
$this->contains[] = $regexp;
$this->iterator = new ContentFilterIterator($this->iterator, $this->contains);
$this->iterator = new ContentFilterIterator($this->iterator, $regexp);

return $this;
}
Expand Down
12 changes: 5 additions & 7 deletions src/AppBundle/Diff/Iterator/Filter/ContentFilterIterator.php
Expand Up @@ -6,22 +6,20 @@

class ContentFilterIterator extends \FilterIterator
{
private $matchRegexps;
private $matchRegexp;

public function __construct(Iterator $iterator, $regexps)
public function __construct(Iterator $iterator, $regexp)
{
parent::__construct($iterator);
$this->matchRegexps = $regexps;
$this->matchRegexp = $regexp;
}

public function accept()
{
$file = $this->getInnerIterator()->current();

foreach ($this->matchRegexps as $regexp) {
if (preg_match($regexp, $file->content())) {
return true;
}
if (preg_match($this->matchRegexp, $file->content())) {
return true;
}

return false;
Expand Down
10 changes: 4 additions & 6 deletions src/AppBundle/Diff/Iterator/Filter/PathFilterIterator.php
Expand Up @@ -8,20 +8,18 @@ class PathFilterIterator extends \FilterIterator
{
private $matchRegexps;

public function __construct(Iterator $iterator, $matchRegexps)
public function __construct(Iterator $iterator, $matchRegexp)
{
parent::__construct($iterator);
$this->matchRegexps = $matchRegexps;
$this->matchRegexp = $matchRegexp;
}

public function accept()
{
$file = $this->getInnerIterator()->current();

foreach ($this->matchRegexps as $regexp) {
if (preg_match($regexp, $file->name())) {
return true;
}
if (preg_match($this->matchRegexp, $file->name())) {
return true;
}

return false;
Expand Down
34 changes: 34 additions & 0 deletions tests/AppBundle/Diff/LineTest.php
@@ -0,0 +1,34 @@
<?php

namespace tests\AppBundle\Diff;

use AppBundle\Diff\Line;

/**
* @author Mickaël Andrieu <andrieu.travail@gmail.com>
*/
class LineTest extends \PHPUnit_Framework_TestCase
{
const TRANS_PATTERN = '#(trans\(|->l\()#';

/**
* @dataProvider testCases
*/
public function testMatch($content, $expected)
{
$line = new Line($content);
$this->assertSame($expected, $line->match(self::TRANS_PATTERN));
}

public function testCases()
{
return [
['value.call()', false],
["{{ 'foo'|trans() }}", true],
['this->trans(', true],
['this->translator->trans(', true],
['this->translator', false],
["object->l['foo']", false],
];
}
}

0 comments on commit d89f319

Please sign in to comment.