Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Allow invokable classes as predicate (solves #270) #272

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Sql/Predicate/PredicateSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function addPredicates($predicates, $combination = self::OP_AND)
$this->addPredicate($predicates, $combination);
return $this;
}
if ($predicates instanceof \Closure) {
if ($predicates instanceof \Closure || is_callable($predicates)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified to \is_callable($predicates)

$predicates($this);
return $this;
}
Expand Down
20 changes: 20 additions & 0 deletions test/Sql/DeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Zend\Db\Sql\Predicate\IsNotNull;
use Zend\Db\Sql\TableIdentifier;
use Zend\Db\Sql\Where;
use ZendTest\Db\TestAsset\WhereInvokable;

class DeleteTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -105,6 +106,25 @@ public function testWhere()
});
}

/**
* @testdox unit test: test where will accept invokable classes
* @covers Zend\Db\Sql\Update::where
*/
public function testWhereArgumentCanBeInvokable()
{
$select = new Delete;
$select->where(new WhereInvokable('bar'));

/** @var Where $where */
$where = $select->getRawState('where');
$predicates = $where->getPredicates();
$expressionData = $predicates[0][1]->getExpressionData();

$this->assertEquals(1, count($predicates));
$this->assertEquals('foo', $expressionData[0][1][0]);
$this->assertEquals('bar', $expressionData[0][1][1]);
}

/**
* @covers Zend\Db\Sql\Delete::prepareStatement
*/
Expand Down
20 changes: 20 additions & 0 deletions test/Sql/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Zend\Db\Sql\TableIdentifier;
use Zend\Db\Sql\Where;
use ZendTest\Db\TestAsset\TrustingSql92Platform;
use ZendTest\Db\TestAsset\WhereInvokable;

class SelectTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -348,6 +349,25 @@ public function testWhereArgument1IsClosure()
});
}

/**
* @testdox unit test: test where will accept invokable classes
* @covers Zend\Db\Sql\Select::where
*/
public function testWhereArgumentCanBeInvokable()
{
$select = new Select;
$select->where(new WhereInvokable('bar'));

/** @var Where $where */
$where = $select->getRawState('where');
$predicates = $where->getPredicates();
$expressionData = $predicates[0][1]->getExpressionData();

$this->assertEquals(1, count($predicates));
$this->assertEquals('foo', $expressionData[0][1][0]);
$this->assertEquals('bar', $expressionData[0][1][1]);
}

/**
* @testdox unit test: Test where() will accept any Predicate object as-is
* @covers Zend\Db\Sql\Select::where
Expand Down
20 changes: 20 additions & 0 deletions test/Sql/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Zend\Db\Sql\Expression;
use Zend\Db\Sql\TableIdentifier;
use ZendTest\Db\TestAsset\TrustingSql92Platform;
use ZendTest\Db\TestAsset\WhereInvokable;

class UpdateTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -168,6 +169,25 @@ public function testGetRawState()
$this->assertInstanceOf('Zend\Db\Sql\Where', $this->update->getRawState('where'));
}

/**
* @testdox unit test: test where will accept invokable classes
* @covers Zend\Db\Sql\Update::where
*/
public function testWhereArgumentCanBeInvokable()
{
$select = new Update;
$select->where(new WhereInvokable('bar'));

/** @var Where $where */
$where = $select->getRawState('where');
$predicates = $where->getPredicates();
$expressionData = $predicates[0][1]->getExpressionData();

$this->assertEquals(1, count($predicates));
$this->assertEquals('foo', $expressionData[0][1][0]);
$this->assertEquals('bar', $expressionData[0][1][1]);
}

/**
* @covers Zend\Db\Sql\Update::prepareStatement
*/
Expand Down
27 changes: 27 additions & 0 deletions test/TestAsset/WhereInvokable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace ZendTest\Db\TestAsset;

use Zend\Db\Sql\Predicate\Like;
use Zend\Db\Sql\Select;

class WhereInvokable
{
private $value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docblock


/**
* WhereInvokable constructor.
* @param $value
*/
public function __construct($value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a type declaration to $value

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not add the type declaration because the composer.json says this module is still on php 5.5. If this is an outdated information, I'll add the type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, weird that it is still on 5.5. develop should be on 7.1

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, also travis runs with an old configuration

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@I3ekka we are going to support PHP 5.6+ with zend-db 2.9.0. We'll move to PHP 7.1 with zend-db 3.0.0

{
$this->value = $value;
}

public function __invoke($select)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a type declaration to $select

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class is also used for Delete and Update test cases. If I add the declaration the UnitTest will fail, because an instance of Update and Delete is injected into the function.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@I3ekka I've not read the code but I usually solve 'multi-type' by introducing an interface(with or without methods). If this commends totally misses the point, ignore it :P

{
/** @var Select $select */
$select->where->addPredicate(new Like('foo', $this->value));
}

}