Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LikePredicate only supports specific Expression types. Add NotLikePre…
…dicate
- Loading branch information
Tom Kay
committed
Jan 6, 2015
1 parent
636d908
commit 28bcbfd
Showing
11 changed files
with
168 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
namespace Packaged\QueryBuilder\Expression\Like; | ||
|
||
class ContainsExpression extends CustomLikeExpression | ||
{ | ||
public function getValue() | ||
{ | ||
return '%' . parent::getValue() . '%'; | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
namespace Packaged\QueryBuilder\Expression\Like; | ||
|
||
use Packaged\QueryBuilder\Expression\StringExpression; | ||
|
||
class CustomLikeExpression extends StringExpression | ||
{ | ||
|
||
} |
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,10 @@ | ||
<?php | ||
namespace Packaged\QueryBuilder\Expression\Like; | ||
|
||
class EndsWithExpression extends CustomLikeExpression | ||
{ | ||
public function getValue() | ||
{ | ||
return parent::getValue() . '%'; | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
namespace Packaged\QueryBuilder\Expression\Like; | ||
|
||
class StartsWithExpression extends CustomLikeExpression | ||
{ | ||
public function getValue() | ||
{ | ||
return '%' . parent::getValue(); | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
namespace Packaged\QueryBuilder\Predicate; | ||
|
||
class NotLikePredicate extends LikePredicate | ||
{ | ||
public function getOperator() | ||
{ | ||
return 'NOT LIKE'; | ||
} | ||
} |
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,69 @@ | ||
<?php | ||
namespace Packaged\Tests\QueryBuilder\Predicate; | ||
|
||
use Packaged\QueryBuilder\Assembler\QueryAssembler; | ||
use Packaged\QueryBuilder\Expression\Like\ContainsExpression; | ||
use Packaged\QueryBuilder\Expression\Like\CustomLikeExpression; | ||
use Packaged\QueryBuilder\Expression\Like\EndsWithExpression; | ||
use Packaged\QueryBuilder\Expression\Like\StartsWithExpression; | ||
use Packaged\QueryBuilder\Expression\StringExpression; | ||
use Packaged\QueryBuilder\Predicate\NotLikePredicate; | ||
|
||
class NotLikePredicateTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testAssemble() | ||
{ | ||
$predicate = new NotLikePredicate(); | ||
$predicate->setField('field'); | ||
$this->assertEquals( | ||
'field NOT LIKE NULL', | ||
QueryAssembler::stringify($predicate) | ||
); | ||
$predicate->setExpression(CustomLikeExpression::create(1)); | ||
$this->assertEquals( | ||
'field NOT LIKE "1"', | ||
QueryAssembler::stringify($predicate) | ||
); | ||
$predicate->setExpression(CustomLikeExpression::create('1')); | ||
$this->assertEquals( | ||
'field NOT LIKE "1"', | ||
QueryAssembler::stringify($predicate) | ||
); | ||
$predicate->setExpression(CustomLikeExpression::create('abc')); | ||
$this->assertEquals( | ||
'field NOT LIKE "abc"', | ||
QueryAssembler::stringify($predicate) | ||
); | ||
$predicate->setExpression(EndsWithExpression::create('abc')); | ||
$this->assertEquals( | ||
'field NOT LIKE "abc%"', | ||
QueryAssembler::stringify($predicate) | ||
); | ||
$predicate->setExpression(ContainsExpression::create('abc')); | ||
$this->assertEquals( | ||
'field NOT LIKE "%abc%"', | ||
QueryAssembler::stringify($predicate) | ||
); | ||
$predicate->setExpression(StartsWithExpression::create('abc')); | ||
$this->assertEquals( | ||
'field NOT LIKE "%abc"', | ||
QueryAssembler::stringify($predicate) | ||
); | ||
$predicate->setExpression(CustomLikeExpression::create('a%bc')); | ||
$this->assertEquals( | ||
'field NOT LIKE "a%bc"', | ||
QueryAssembler::stringify($predicate) | ||
); | ||
} | ||
|
||
/** | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage Invalid Expression Type | ||
*/ | ||
public function testInvalidExpression() | ||
{ | ||
$predicate = new NotLikePredicate(); | ||
$predicate->setField('field'); | ||
$predicate->setExpression(StringExpression::create('a%bc')); | ||
} | ||
} |
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