-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NotEqualComparison to allow simple NOT NULL conditions
- Loading branch information
1 parent
9c04d4c
commit 891f3e4
Showing
9 changed files
with
212 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Dataphant. | ||
* | ||
* (c) Laszlo Korte <me@laszlokorte.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* and AUTHORS files that was distributed with this source code. | ||
*/ | ||
|
||
namespace Dataphant\Query\Comparisons; | ||
|
||
class NotEqualToComparison extends ComparisonBase | ||
{ | ||
public function match($record) | ||
{ | ||
return $this->getGivenValueFor($record) != $this->getNeededValueFor($record); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
tests/Dataphant/Tests/Query/Comparisons/NotEqualToComparisonTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
namespace Dataphant\Tests\Query\Comparisons; | ||
|
||
use Dataphant\Tests\Query\QueryBaseTestCase; | ||
|
||
use Dataphant\Query\Comparisons\NotEqualToComparison; | ||
use Dataphant\Query\Operations; | ||
|
||
class NotEqualToComparisonTest extends QueryBaseTestCase | ||
{ | ||
public function testRecordsPropertyMatchesNumericValueIfEqual() | ||
{ | ||
$compareValue = 23; | ||
$realValue = 23; | ||
$this->property->expects($this->any()) | ||
->method('getValueFor') | ||
->will($this->returnValue($realValue)); | ||
|
||
$comparison = new NotEqualToComparison($this->property, $compareValue); | ||
|
||
$this->assertFalse($comparison->match($this->record)); | ||
} | ||
|
||
public function testRecordsPropertyMatchesStringValueIfEqual() | ||
{ | ||
$compareValue = 'FailWhale'; | ||
$realValue = 'FailWhale'; | ||
$this->property->expects($this->any()) | ||
->method('getValueFor') | ||
->will($this->returnValue($realValue)); | ||
|
||
$comparison = new NotEqualToComparison($this->property, $compareValue); | ||
|
||
$this->assertFalse($comparison->match($this->record)); | ||
} | ||
|
||
public function testRecordsPropertyDoesNotMatchNumericValueIfGreater() | ||
{ | ||
$compareValue = 23; | ||
$realValue = 42; | ||
$this->property->expects($this->any()) | ||
->method('getValueFor') | ||
->will($this->returnValue($realValue)); | ||
|
||
$comparison = new NotEqualToComparison($this->property, $compareValue); | ||
|
||
$this->assertTrue($comparison->match($this->record)); | ||
} | ||
|
||
public function testRecordsPropertyDoesNotMatchNumericValueIfLess() | ||
{ | ||
$compareValue = 8; | ||
$realValue = 4; | ||
$this->property->expects($this->any()) | ||
->method('getValueFor') | ||
->will($this->returnValue($realValue)); | ||
|
||
$comparison = new NotEqualToComparison($this->property, $compareValue); | ||
|
||
$this->assertTrue($comparison->match($this->record)); | ||
} | ||
|
||
public function testRecordsPropertyDoesNotMatchStringValueIfNotEqual() | ||
{ | ||
$compareValue = 'RapidRaptor'; | ||
$realValue = 'FailWhale'; | ||
$this->property->expects($this->any()) | ||
->method('getValueFor') | ||
->will($this->returnValue($realValue)); | ||
|
||
$comparison = new NotEqualToComparison($this->property, $compareValue); | ||
|
||
$this->assertTrue($comparison->match($this->record)); | ||
} | ||
|
||
|
||
public function testIsValidByDefault() | ||
{ | ||
$comparison = new NotEqualToComparison($this->property, 16); | ||
$this->assertTrue($comparison->isValid()); | ||
} | ||
|
||
public function testLogicalAndCompositionWithOtherConditionCanBeBuilt() | ||
{ | ||
$model = $this->getFakeModel(); | ||
$query = $this->adapter->getNewQuery($this->dataSource, $model); | ||
$comp = new NotEqualToComparison('3', '4'); | ||
$op = new Operations\AndOperation(array($comp, $comp)); | ||
$query->setConditions($op); | ||
|
||
$this->adapter->read($query); | ||
$sql = "SELECT "; | ||
$sql .= "\"users\".\"id\" AS \"id\", \"users\".\"nickname\" AS \"nickname\" FROM \"users\" "; | ||
$sql .= "WHERE (('3' <> '4') AND ('3' <> '4'))"; | ||
$this->assertSame($sql, $this->adapter->getLastStatement()); | ||
} | ||
|
||
public function testLogicalOrCompositionWithOtherConditionCanBeBuilt() | ||
{ | ||
$model = $this->getFakeModel(); | ||
$query = $this->adapter->getNewQuery($this->dataSource, $model); | ||
$comp = new NotEqualToComparison('3', '4'); | ||
$op = new Operations\OrOperation(array($comp, $comp)); | ||
$query->setConditions($op); | ||
|
||
$this->adapter->read($query); | ||
$sql = "SELECT "; | ||
$sql .= "\"users\".\"id\" AS \"id\", \"users\".\"nickname\" AS \"nickname\" FROM \"users\" "; | ||
$sql .= "WHERE (('3' <> '4') OR ('3' <> '4'))"; | ||
$this->assertSame($sql, $this->adapter->getLastStatement()); | ||
} | ||
|
||
public function testLogicalAndNotCompositionWithOtherConditionCanBeBuilt() | ||
{ | ||
$model = $this->getFakeModel(); | ||
$query = $this->adapter->getNewQuery($this->dataSource, $model); | ||
$comp = new NotEqualToComparison('3', '4'); | ||
$notop = new Operations\NotOperation($comp); | ||
$op = new Operations\AndOperation(array($comp, $notop)); | ||
$query->setConditions($op); | ||
|
||
$this->adapter->read($query); | ||
$sql = "SELECT "; | ||
$sql .= "\"users\".\"id\" AS \"id\", \"users\".\"nickname\" AS \"nickname\" FROM \"users\" "; | ||
$sql .= "WHERE (('3' <> '4') AND (NOT ('3' <> '4')))"; | ||
$this->assertSame($sql, $this->adapter->getLastStatement()); | ||
} | ||
} |