Skip to content

Commit

Permalink
Merge pull request #3 from inovia-team/public-query-filter
Browse files Browse the repository at this point in the history
Update visibility of 'applyQueryFilters' method
  • Loading branch information
louiscelier authored Mar 5, 2018
2 parents efb035b + 8feeae0 commit 7c291af
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"zendframework/zend-hydrator": "^2.3"
},
"require-dev": {
"phpunit/phpunit": "^7.0"
"phpunit/phpunit": "^7.0",
"mockery/mockery": "~0.9"
},
"autoload": {
"psr-4": {"Matters\\": "src/"}
Expand Down
5 changes: 4 additions & 1 deletion src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ protected function fetchCount(Select $select)
/**
* @param \Zend\Db\Sql\Where $where
* @param array $filters
* @return \Zend\Db\Sql\Where
*/
private function applyQueryFilters(Where $where, array $filters)
public function applyQueryFilters(Where $where, array $filters)
{
/** @var \Matters\ValueObjects\QueryFilter $filter */
foreach ($filters as $filter) {
Expand All @@ -151,5 +152,7 @@ private function applyQueryFilters(Where $where, array $filters)
break;
}
}

return $where;
}
}
181 changes: 177 additions & 4 deletions tests/RepositoryTest.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,201 @@
<?php
/**
* @author Matters Studio (https://matters.tech)
*/

namespace Matters;

use Matters\Enums\FilterType;
use Matters\ValueObjects\QueryFilter;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Zend\Db\Sql\Where;
use Zend\Db\TableGateway\TableGateway;


/**
* Class RepositoryTest
* @package Matters
*/
class RepositoryTest extends TestCase
{
/**
* @var Repository
*/
private $testedInstance;

/**
* @var m\MockInterface
*/
private $tableGateway;

protected function setUp()
{
$this->tableGateway = $this->prophesize(TableGateway::class);
$this->tableGateway = m::mock(TableGateway::class);

$this->testedInstance = new class($this->tableGateway->reveal()) extends Repository {};
$this->testedInstance = new class($this->tableGateway) extends Repository {};
}

public function testInstance()
{
self::assertInstanceOf(Repository::class, $this->testedInstance);
}
}

public function applyQueryFiltersProvider()
{
return [
[
[],
[
'equal-to-nb-calls' => 0,
'greater-than-nb-calls' => 0,
'greater-than-or-equal-to-nb-calls' => 0,
'less-than-nb-calls' => 0,
'less-than-or-equal-to-nb-calls' => 0,
'like-nb-calls' => 0,
],
],
[
[
new QueryFilter('key', FilterType::EQUAL_TO, 'value'),
],
[
'equal-to-nb-calls' => 1,
'greater-than-nb-calls' => 0,
'greater-than-or-equal-to-nb-calls' => 0,
'less-than-nb-calls' => 0,
'less-than-or-equal-to-nb-calls' => 0,
'like-nb-calls' => 0,
],
],
[
[
new QueryFilter('key', FilterType::GREATER_THAN, 'value'),
],
[
'equal-to-nb-calls' => 0,
'greater-than-nb-calls' => 1,
'greater-than-or-equal-to-nb-calls' => 0,
'less-than-nb-calls' => 0,
'less-than-or-equal-to-nb-calls' => 0,
'like-nb-calls' => 0,
],
],
[
[
new QueryFilter('key', FilterType::GREATER_THAN_OR_EQUAL_TO, 'value'),
],
[
'equal-to-nb-calls' => 0,
'greater-than-nb-calls' => 0,
'greater-than-or-equal-to-nb-calls' => 1,
'less-than-nb-calls' => 0,
'less-than-or-equal-to-nb-calls' => 0,
'like-nb-calls' => 0,
],
],
[
[
new QueryFilter('key', FilterType::LESS_THAN, 'value'),
],
[
'equal-to-nb-calls' => 0,
'greater-than-nb-calls' => 0,
'greater-than-or-equal-to-nb-calls' => 0,
'less-than-nb-calls' => 1,
'less-than-or-equal-to-nb-calls' => 0,
'like-nb-calls' => 0,
],
],
[
[
new QueryFilter('key', FilterType::LESS_THAN_OR_EQUAL_TO, 'value'),
],
[
'equal-to-nb-calls' => 0,
'greater-than-nb-calls' => 0,
'greater-than-or-equal-to-nb-calls' => 0,
'less-than-nb-calls' => 0,
'less-than-or-equal-to-nb-calls' => 1,
'like-nb-calls' => 0,
],
],
[
[
new QueryFilter('key', FilterType::LIKE, 'value'),
],
[
'equal-to-nb-calls' => 0,
'greater-than-nb-calls' => 0,
'greater-than-or-equal-to-nb-calls' => 0,
'less-than-nb-calls' => 0,
'less-than-or-equal-to-nb-calls' => 0,
'like-nb-calls' => 1,
],
],
[
[
new QueryFilter('key', FilterType::EQUAL_TO, 'value'),
new QueryFilter('key', FilterType::GREATER_THAN, 'value'),
],
[
'equal-to-nb-calls' => 1,
'greater-than-nb-calls' => 1,
'greater-than-or-equal-to-nb-calls' => 0,
'less-than-nb-calls' => 0,
'less-than-or-equal-to-nb-calls' => 0,
'like-nb-calls' => 0,
],
],
[
[
new QueryFilter('key', FilterType::LESS_THAN, 'value'),
new QueryFilter('key', FilterType::LIKE, 'value'),
],
[
'equal-to-nb-calls' => 0,
'greater-than-nb-calls' => 0,
'greater-than-or-equal-to-nb-calls' => 0,
'less-than-nb-calls' => 1,
'less-than-or-equal-to-nb-calls' => 0,
'like-nb-calls' => 1,
],
],
];
}

/**
* @dataProvider applyQueryFiltersProvider
* @param array $filters
* @param array $nbCalls
*/
public function testApplyQueryFilters(array $filters, array $nbCalls)
{
$where = m::mock(Where::class);
$where
->shouldReceive('equalTo')
->with('key', 'value')
->times($nbCalls['equal-to-nb-calls']);
$where
->shouldReceive('greaterThan')
->with('key', 'value')
->times($nbCalls['greater-than-nb-calls']);
$where
->shouldReceive('greaterThanOrEqualTo')
->with('key', 'value')
->times($nbCalls['greater-than-or-equal-to-nb-calls']);
$where
->shouldReceive('lessThan')
->with('key', 'value')
->times($nbCalls['less-than-nb-calls']);
$where
->shouldReceive('lessThanOrEqualTo')
->with('key', 'value')
->times($nbCalls['less-than-or-equal-to-nb-calls']);
$where
->shouldReceive('like')
->with('key', '%value%')
->times($nbCalls['like-nb-calls']);

self::assertSame($where, $this->testedInstance->applyQueryFilters($where, $filters));
}
}

0 comments on commit 7c291af

Please sign in to comment.