Skip to content

Commit

Permalink
[Finder] Improve ComparatorTest
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander M. Turek <me@derrabus.de>
  • Loading branch information
derrabus committed Jul 16, 2021
1 parent cbf2e51 commit dcc7738
Showing 1 changed file with 47 additions and 18 deletions.
65 changes: 47 additions & 18 deletions Tests/Comparator/ComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ class ComparatorTest extends TestCase
{
public function testGetSetOperator()
{
$comparator = new Comparator();
try {
$comparator->setOperator('foo');
$this->fail('->setOperator() throws an \InvalidArgumentException if the operator is not valid.');
} catch (\Exception $e) {
$this->assertInstanceOf(\InvalidArgumentException::class, $e, '->setOperator() throws an \InvalidArgumentException if the operator is not valid.');
}

$comparator = new Comparator();
$comparator->setOperator('>');
$this->assertEquals('>', $comparator->getOperator(), '->getOperator() returns the current operator');
}

public function testInvalidOperator()
{
$comparator = new Comparator();

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid operator "foo".');
$comparator->setOperator('foo');
}

public function testGetSetTarget()
{
$comparator = new Comparator();
Expand All @@ -39,27 +40,55 @@ public function testGetSetTarget()
}

/**
* @dataProvider getTestData
* @dataProvider provideMatches
*/
public function testTest($operator, $target, $match, $noMatch)
public function testTestSucceeds(string $operator, string $target, string $testedValue)
{
$c = new Comparator();
$c->setOperator($operator);
$c->setTarget($target);

foreach ($match as $m) {
$this->assertTrue($c->test($m), '->test() tests a string against the expression');
}
$this->assertTrue($c->test($testedValue));
}

public function provideMatches(): array
{
return [
['<', '1000', '500'],
['<', '1000', '999'],
['<=', '1000', '999'],
['!=', '1000', '999'],
['<=', '1000', '1000'],
['==', '1000', '1000'],
['>=', '1000', '1000'],
['>=', '1000', '1001'],
['>', '1000', '1001'],
['>', '1000', '5000'],
];
}

/**
* @dataProvider provideNonMatches
*/
public function testTestFails(string $operator, string $target, string $testedValue)
{
$c = new Comparator();
$c->setOperator($operator);
$c->setTarget($target);

foreach ($noMatch as $m) {
$this->assertFalse($c->test($m), '->test() tests a string against the expression');
}
$this->assertFalse($c->test($testedValue));
}

public function getTestData()
public function provideNonMatches(): array
{
return [
['<', '1000', ['500', '999'], ['1000', '1500']],
['>', '1000', '500'],
['>=', '1000', '500'],
['>', '1000', '1000'],
['!=', '1000', '1000'],
['<', '1000', '1000'],
['<', '1000', '1500'],
['<=', '1000', '1500'],
];
}
}

0 comments on commit dcc7738

Please sign in to comment.