merged branch gajdaw/finder_comparator_not_equal (PR #3974)

Commits
-------

1b320c8 [Finder][Comparator] not equal operator

Discussion
----------

[Finder][Comparator] not equal operator

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -
This commit is contained in:
Fabien Potencier 2012-04-18 17:20:26 +02:00
commit 065632b059
5 changed files with 53 additions and 9 deletions

View File

@ -62,7 +62,7 @@ class Comparator
$operator = '==';
}
if (!in_array($operator, array('>', '<', '>=', '<=', '=='))) {
if (!in_array($operator, array('>', '<', '>=', '<=', '==', '!='))) {
throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
}
@ -85,6 +85,8 @@ class Comparator
return $test < $this->target;
case '<=':
return $test <= $this->target;
case '!=':
return $test != $this->target;
}
return $test == $this->target;

View File

@ -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));
}

View File

@ -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])) {

View File

@ -57,6 +57,7 @@ class DateComparatorTest extends \PHPUnit_Framework_TestCase
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'))),
);
}

View File

@ -15,13 +15,23 @@ use Symfony\Component\Finder\Comparator\NumberComparator;
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 @@ class NumberComparatorTest extends \PHPUnit_Framework_TestCase
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',
)
),
);
}
}