8000 merged branch gajdaw/finder_comparator_not_equal (PR #3974) · vicb/symfony@065632b · GitHub
[go: up one dir, main page]

Skip to content

Commit 065632b

Browse files
committed
merged branch gajdaw/finder_comparator_not_equal (PR symfony#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: -
2 parents 85bb439 + 1b320c8 commit 065632b

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

src/Symfony/Component/Finder/Comparator/Comparator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function setOperator($operator)
6262
$operator = '==';
6363
}
6464

65-
if (!in_array($operator, array('>', '<', '>=', '<=', '=='))) {
65+
if (!in_array($operator, array('>', '<', '>=', '<=', '==', '!='))) {
6666
throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
6767
}
6868

@@ -85,6 +85,8 @@ public function test($test)
8585
return $test < $this->target;
8686
case '<=':
8787
return $test <= $this->target;
88+
case '!=':
89+
return $test != $this->target;
8890
}
8991

9092
return $test == $this->target;

src/Symfony/Component/Finder/Comparator/DateComparator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class DateComparator extends Comparator
2828
*/
2929
public function __construct($test)
3030
{
31-
if (!preg_match('#^\s*([<>=]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) {
31+
if (!preg_match('#^\s*(==|!=|[<>]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) {
3232
throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a date test.', $test));
3333
}
3434

src/Symfony/Component/Finder/Comparator/NumberComparator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ class NumberComparator extends Comparator
4444
*/
4545
public function __construct($test)
4646
{
47-
if (!preg_match('#^\s*([<>=]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) {
47+
if (!preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) {
4848
throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test));
4949
}
5050

5151
$target = $matches[2];
52+
if (!is_numeric($target)) {
53+
throw new \InvalidArgumentException(sprintf('Invalid number "%s".', $target));
54+
}
5255
if (isset($matches[3])) {
5356
// magnitude
5457
switch (strtolower($matches[3])) {

src/Symfony/Component/Finder/Tests/Comparator/DateComparatorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function getTestData()
5757
array('> 2005-10-10', array(strtotime('2005-10-15')), array(strtotime('2005-10-09'))),
5858
array('after 2005-10-10', array(strtotime('2005-10-15')), array(strtotime('2005-10-09'))),
5959
array('since 2005-10-10', array(strtotime('2005-10-15')), array(strtotime('2005-10-09'))),
60+
array('!= 2005-10-10', array(strtotime('2005-10-11')), array(strtotime('2005-10-10'))),
6061
);
6162

6263
}

src/Symfony/Component/Finder/Tests/Comparator/NumberComparatorTest.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@
1515

1616
class NumberComparatorTest extends \PHPUnit_Framework_TestCase
1717
{
18-
public function testConstructor()
18+
19+
/**
20+
* @dataProvider getConstructorTestData
21+
*/
22+
public function testConstructor($successes, $failures)
1923
{
20-
try {
21-
new NumberComparator('foobar');
22-
$this->fail('__construct() throws an \InvalidArgumentException if the test expression is not valid.');
23-
} catch (\Exception $e) {
24-
$this->assertInstanceOf('InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the test expression is not valid.');
24+
foreach ($successes as $s) {
25+
new NumberComparator($s);
26+
}
27+
28+
foreach ($failures as $f) {
29+
try {
30+
new NumberComparator($f);
31+
$this->fail('__construct() throws an \InvalidArgumentException if the test expression is not valid.');
32+
} catch (\Exception $e) {
33+
$this->assertInstanceOf('InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the test expression is not valid.');
34+
}
2535
}
2636
}
2737

@@ -66,6 +76,34 @@ public function getTestData()
6676

6777
array('==1g', array('1000000000'), array('999999999', '1000000001')),
6878
array('==1gi', array(1024*1024*1024), array(1024*1024*1024-1, 1024*1024*1024+1)),
79+
80+
array('!= 1000', array('500', '999'), array('1000')),
6981
);
7082
}
83+
84+
public function getConstructorTestData()
85+
{
86+
return array(
87+
array(
88+
array(
89+
'1', '0',
90+
'3.5', '33.55', '123.456', '123456.78',
91+
'.1', '.123',
92+
'.0', '0.0',
93+
'1.', '0.', '123.',
94+
'==1', '!=1', '<1', '>1', '<=1', '>=1',
95+
'==1k', '==1ki', '==1m', '==1mi', '==1g', '==1gi',
96+
'1k', '1ki', '1m', '1mi', '1g', '1gi',
97+
),
98+
array(
99+
false, null, '',
100+
' ', 'foobar',
101+
'=1', '===1',
102+
'0 . 1', '123 .45', '234. 567',
103+
'..', '.0.', '0.1.2',
104+
)
105+
),
106+
);
107+
}
108+
71109
}

0 commit comments

Comments
 (0)
0