From 1b320c83c3bf127e04e9dba02f753417553bfe77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?W=C5=82odzimierz=20Gajda?= Date: Wed, 18 Apr 2012 13:59:47 +0200 Subject: [PATCH] [Finder][Comparator] not equal operator --- .../Finder/Comparator/Comparator.php | 4 +- .../Finder/Comparator/DateComparator.php | 2 +- .../Finder/Comparator/NumberComparator.php | 5 +- .../Tests/Comparator/DateComparatorTest.php | 1 + .../Tests/Comparator/NumberComparatorTest.php | 50 ++++++++++++++++--- 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Finder/Comparator/Comparator.php b/src/Symfony/Component/Finder/Comparator/Comparator.php index 33e9ef98d249..5d19d5d905d4 100644 --- a/src/Symfony/Component/Finder/Comparator/Comparator.php +++ b/src/Symfony/Component/Finder/Comparator/Comparator.php @@ -62,7 +62,7 @@ public function setOperator($operator) $operator = '=='; } - if (!in_array($operator, array('>', '<', '>=', '<=', '=='))) { + if (!in_array($operator, array('>', '<', '>=', '<=', '==', '!='))) { throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator)); } @@ -85,6 +85,8 @@ public function test($test) return $test < $this->target; case '<=': return $test <= $this->target; + case '!=': + return $test != $this->target; } return $test == $this->target; diff --git a/src/Symfony/Component/Finder/Comparator/DateComparator.php b/src/Symfony/Component/Finder/Comparator/DateComparator.php index f5ad8f39cacd..9de444f0521d 100644 --- a/src/Symfony/Component/Finder/Comparator/DateComparator.php +++ b/src/Symfony/Component/Finder/Comparator/DateComparator.php @@ -28,7 +28,7 @@ class DateComparator extends Comparator */ public function __construct($test) { - if (!preg_match('#^\s*([<>=]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) { + if (!preg_match('#^\s*(==|!=|[<>]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) { throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a date test.', $test)); } diff --git a/src/Symfony/Component/Finder/Comparator/NumberComparator.php b/src/Symfony/Component/Finder/Comparator/NumberComparator.php index 7b14238cde40..955cc6755b2f 100644 --- a/src/Symfony/Component/Finder/Comparator/NumberComparator.php +++ b/src/Symfony/Component/Finder/Comparator/NumberComparator.php @@ -44,11 +44,14 @@ class NumberComparator extends Comparator */ public function __construct($test) { - if (!preg_match('#^\s*([<>=]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) { + if (!preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) { throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test)); } $target = $matches[2]; + if (!is_numeric($target)) { + throw new \InvalidArgumentException(sprintf('Invalid number "%s".', $target)); + } if (isset($matches[3])) { // magnitude switch (strtolower($matches[3])) { diff --git a/src/Symfony/Component/Finder/Tests/Comparator/DateComparatorTest.php b/src/Symfony/Component/Finder/Tests/Comparator/DateComparatorTest.php index ebb0cc139a30..ac8256ab8601 100644 --- a/src/Symfony/Component/Finder/Tests/Comparator/DateComparatorTest.php +++ b/src/Symfony/Component/Finder/Tests/Comparator/DateComparatorTest.php @@ -57,6 +57,7 @@ public function getTestData() array('> 2005-10-10', array(strtotime('2005-10-15')), array(strtotime('2005-10-09'))), array('after 2005-10-10', array(strtotime('2005-10-15')), array(strtotime('2005-10-09'))), array('since 2005-10-10', array(strtotime('2005-10-15')), array(strtotime('2005-10-09'))), + array('!= 2005-10-10', array(strtotime('2005-10-11')), array(strtotime('2005-10-10'))), ); } diff --git a/src/Symfony/Component/Finder/Tests/Comparator/NumberComparatorTest.php b/src/Symfony/Component/Finder/Tests/Comparator/NumberComparatorTest.php index 3f8ba6eb1919..b07870ba81aa 100644 --- a/src/Symfony/Component/Finder/Tests/Comparator/NumberComparatorTest.php +++ b/src/Symfony/Component/Finder/Tests/Comparator/NumberComparatorTest.php @@ -15,13 +15,23 @@ class NumberComparatorTest extends \PHPUnit_Framework_TestCase { - public function testConstructor() + + /** + * @dataProvider getConstructorTestData + */ + public function testConstructor($successes, $failures) { - try { - new NumberComparator('foobar'); - $this->fail('__construct() throws an \InvalidArgumentException if the test expression is not valid.'); - } catch (\Exception $e) { - $this->assertInstanceOf('InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the test expression is not valid.'); + foreach ($successes as $s) { + new NumberComparator($s); + } + + foreach ($failures as $f) { + try { + new NumberComparator($f); + $this->fail('__construct() throws an \InvalidArgumentException if the test expression is not valid.'); + } catch (\Exception $e) { + $this->assertInstanceOf('InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the test expression is not valid.'); + } } } @@ -66,6 +76,34 @@ public function getTestData() array('==1g', array('1000000000'), array('999999999', '1000000001')), array('==1gi', array(1024*1024*1024), array(1024*1024*1024-1, 1024*1024*1024+1)), + + array('!= 1000', array('500', '999'), array('1000')), ); } + + public function getConstructorTestData() + { + return array( + array( + array( + '1', '0', + '3.5', '33.55', '123.456', '123456.78', + '.1', '.123', + '.0', '0.0', + '1.', '0.', '123.', + '==1', '!=1', '<1', '>1', '<=1', '>=1', + '==1k', '==1ki', '==1m', '==1mi', '==1g', '==1gi', + '1k', '1ki', '1m', '1mi', '1g', '1gi', + ), + array( + false, null, '', + ' ', 'foobar', + '=1', '===1', + '0 . 1', '123 .45', '234. 567', + '..', '.0.', '0.1.2', + ) + ), + ); + } + }