From 16341a4dded33a1084351a6b492bd3b722232996 Mon Sep 17 00:00:00 2001 From: ndm2 Date: Sat, 24 Sep 2016 02:54:02 +0200 Subject: [PATCH 1/8] Move test doubles into independent files. refs #125 --- tests/TestApp/Model/Filter/TestFilter.php | 12 ++++ tests/TestApp/Model/Table/ArticlesTable.php | 20 ++++++ tests/TestApp/Model/Table/CommentsTable.php | 22 +++++++ tests/TestApp/Model/Table/GroupsTable.php | 20 ++++++ tests/TestCase/ManagerTest.php | 12 ---- .../Model/Behavior/SearchBehaviorTest.php | 64 ++----------------- tests/TestCase/Model/Filter/BaseTest.php | 18 ++---- 7 files changed, 83 insertions(+), 85 deletions(-) create mode 100644 tests/TestApp/Model/Filter/TestFilter.php create mode 100644 tests/TestApp/Model/Table/ArticlesTable.php create mode 100644 tests/TestApp/Model/Table/CommentsTable.php create mode 100644 tests/TestApp/Model/Table/GroupsTable.php diff --git a/tests/TestApp/Model/Filter/TestFilter.php b/tests/TestApp/Model/Filter/TestFilter.php new file mode 100644 index 00000000..c35988e3 --- /dev/null +++ b/tests/TestApp/Model/Filter/TestFilter.php @@ -0,0 +1,12 @@ +value('foo') + ->like('search', ['filterEmpty' => true]) + ->value('baz') + ->value('group', ['field' => 'Articles.group']); + } +} diff --git a/tests/TestApp/Model/Table/CommentsTable.php b/tests/TestApp/Model/Table/CommentsTable.php new file mode 100644 index 00000000..0a00ce1f --- /dev/null +++ b/tests/TestApp/Model/Table/CommentsTable.php @@ -0,0 +1,22 @@ +value('Comments.foo') + ->like('Comments.search', ['filterEmpty' => true, 'multiValue' => true]) + ->value('Comments.baz') + ->value('Comments.group', ['field' => 'Comments.group']) + ->value('group', ['multiValue' => true]) + ->value('published'); + } +} diff --git a/tests/TestApp/Model/Table/GroupsTable.php b/tests/TestApp/Model/Table/GroupsTable.php new file mode 100644 index 00000000..f6dcd5c6 --- /dev/null +++ b/tests/TestApp/Model/Table/GroupsTable.php @@ -0,0 +1,20 @@ +collection('frontend') + ->value('title') + ->collection('backend') + ->like('title', ['before' => true, 'after' => true]); + } +} diff --git a/tests/TestCase/ManagerTest.php b/tests/TestCase/ManagerTest.php index 492441fd..26bea67f 100644 --- a/tests/TestCase/ManagerTest.php +++ b/tests/TestCase/ManagerTest.php @@ -5,18 +5,6 @@ use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; use Search\Manager; -use Search\Model\Filter\Base; - -class TestFilter extends Base -{ - - /** - * Dummy method for testing - */ - public function process() - { - } -} class ManagerTest extends TestCase { diff --git a/tests/TestCase/Model/Behavior/SearchBehaviorTest.php b/tests/TestCase/Model/Behavior/SearchBehaviorTest.php index 35dcee44..efc992d6 100644 --- a/tests/TestCase/Model/Behavior/SearchBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/SearchBehaviorTest.php @@ -1,65 +1,9 @@ value('foo') - ->like('search', ['filterEmpty' => true]) - ->value('baz') - ->value('group', ['field' => 'Articles.group']); - } -} - -class CommentsTable extends Table -{ - - public function searchConfiguration() - { - $manager = new Manager($this); - - return $manager - ->value('Comments.foo') - ->like('Comments.search', ['filterEmpty' => true, 'multiValue' => true]) - ->value('Comments.baz') - ->value('Comments.group', ['field' => 'Comments.group']) - ->value('group', ['multiValue' => true]) - ->value('published'); - } -} - -class GroupsTable extends Table -{ - - public function searchConfiguration() - { - $manager = new Manager($this); - - return $manager - ->collection('frontend') - ->value('title') - ->collection('backend') - ->like('title', ['before' => true, 'after' => true]); - } -} - -class Filter extends Base -{ - public function process() - { - } -} class SearchBehaviorTest extends TestCase { @@ -86,15 +30,15 @@ public function setUp() TableRegistry::clear(); $this->Articles = TableRegistry::get('Articles', [ - 'className' => 'Search\Test\TestCase\Model\Behavior\ArticlesTable' + 'className' => 'Search\Test\TestApp\Model\Table\ArticlesTable' ]); $this->Articles->addBehavior('Search.Search'); $this->Comments = TableRegistry::get('Comments', [ - 'className' => 'Search\Test\TestCase\Model\Behavior\CommentsTable' + 'className' => 'Search\Test\TestApp\Model\Table\CommentsTable' ]); $this->Comments->addBehavior('Search.Search'); $this->Groups = TableRegistry::get('Groups', [ - 'className' => 'Search\Test\TestCase\Model\Behavior\GroupsTable' + 'className' => 'Search\Test\TestApp\Model\Table\GroupsTable' ]); $this->Groups->addBehavior('Search.Search'); } @@ -179,7 +123,7 @@ public function testProcessFilters() $query = $this->Comments->find(); $filter = $this - ->getMockBuilder('\Search\Test\TestCase\Model\Behavior\Filter') + ->getMockBuilder('\Search\Test\TestApp\Model\Filter\TestFilter') ->setConstructorArgs(['name', new Manager($this->Comments)]) ->setMethods(['args', 'process', 'query']) ->getMock(); diff --git a/tests/TestCase/Model/Filter/BaseTest.php b/tests/TestCase/Model/Filter/BaseTest.php index 2162e6c0..ca8b3b6a 100644 --- a/tests/TestCase/Model/Filter/BaseTest.php +++ b/tests/TestCase/Model/Filter/BaseTest.php @@ -4,15 +4,7 @@ use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; use Search\Manager; -use Search\Model\Filter\Base; - -class Filter extends Base -{ - - public function process() - { - } -} +use Search\Test\TestApp\Model\Filter\TestFilter; class BaseTest extends TestCase { @@ -30,7 +22,7 @@ public function setup() public function testSkip() { - $filter = new Filter( + $filter = new TestFilter( 'field', $this->manager, ['alwaysRun' => true, 'filterEmpty' => true] @@ -54,7 +46,7 @@ public function testSkip() */ public function testValue() { - $filter = new Filter( + $filter = new TestFilter( 'field', $this->manager, ['defaultValue' => 'default'] @@ -76,7 +68,7 @@ public function testValue() public function testFieldAliasing() { - $filter = new Filter( + $filter = new TestFilter( 'field', $this->manager, [] @@ -87,7 +79,7 @@ public function testFieldAliasing() $filter->config('aliasField', false); $this->assertEquals('field', $filter->field()); - $filter = new Filter( + $filter = new TestFilter( ['field1', 'field2'], $this->manager, [] From 3de71033bfe368d56d125bdaf19ad430f3ad002c Mon Sep 17 00:00:00 2001 From: ndm2 Date: Sat, 24 Sep 2016 03:32:50 +0200 Subject: [PATCH 2/8] Add tests for the `Finder` filter. --- .../Model/Table/FinderArticlesTable.php | 22 +++++++ tests/TestCase/Model/Filter/FinderTest.php | 62 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 tests/TestApp/Model/Table/FinderArticlesTable.php create mode 100644 tests/TestCase/Model/Filter/FinderTest.php diff --git a/tests/TestApp/Model/Table/FinderArticlesTable.php b/tests/TestApp/Model/Table/FinderArticlesTable.php new file mode 100644 index 00000000..6d459bed --- /dev/null +++ b/tests/TestApp/Model/Table/FinderArticlesTable.php @@ -0,0 +1,22 @@ +table('articles'); + } + + public function findActive(Query $query, array $options) + { + return $query->where([ + 'Articles.is_active' => true + ] + $options['active']); + } +} diff --git a/tests/TestCase/Model/Filter/FinderTest.php b/tests/TestCase/Model/Filter/FinderTest.php new file mode 100644 index 00000000..2d6a5963 --- /dev/null +++ b/tests/TestCase/Model/Filter/FinderTest.php @@ -0,0 +1,62 @@ + '\Search\Test\TestApp\Model\Table\FinderArticlesTable' + ]); + $manager = new Manager($articles); + $filter = new Finder('active', $manager); + $filter->args(['active' => ['foo' => 'bar']]); + $filter->query($articles->find()); + $filter->process(); + + $this->assertRegExp( + '/WHERE \(Articles\.is_active = \:c0 AND foo = \:c1\)$/', + $filter->query()->sql() + ); + $this->assertEquals( + [true, 'bar'], + Hash::extract($filter->query()->valueBinder()->bindings(), '{s}.value') + ); + } + + /** + * @expectedException \BadMethodCallException + * @expectedExceptionMessage Unknown finder method "nonExistent" + * @return void + */ + public function testProcessNonExistentFinderMethod() + { + $articles = TableRegistry::get('FinderArticles', [ + 'className' => '\Search\Test\TestApp\Model\Table\FinderArticlesTable' + ]); + $manager = new Manager($articles); + $filter = new Finder('nonExistent', $manager); + $filter->args(['nonExistent' => true]); + $filter->query($articles->find()); + $filter->process(); + } +} From 003cd1646906418f423b9af3b469451741a28b07 Mon Sep 17 00:00:00 2001 From: ndm2 Date: Sat, 24 Sep 2016 03:38:49 +0200 Subject: [PATCH 3/8] Add process skipping tests. --- tests/TestCase/Model/Filter/BooleanTest.php | 24 ++++++++++++++++++++ tests/TestCase/Model/Filter/CallbackTest.php | 24 ++++++++++++++++++++ tests/TestCase/Model/Filter/CompareTest.php | 24 ++++++++++++++++++++ tests/TestCase/Model/Filter/FinderTest.php | 24 ++++++++++++++++++++ tests/TestCase/Model/Filter/LikeTest.php | 24 ++++++++++++++++++++ tests/TestCase/Model/Filter/ValueTest.php | 24 ++++++++++++++++++++ 6 files changed, 144 insertions(+) diff --git a/tests/TestCase/Model/Filter/BooleanTest.php b/tests/TestCase/Model/Filter/BooleanTest.php index 0c95a27d..93304d28 100644 --- a/tests/TestCase/Model/Filter/BooleanTest.php +++ b/tests/TestCase/Model/Filter/BooleanTest.php @@ -23,6 +23,30 @@ class BooleanTest extends TestCase 'plugin.Search.Articles' ]; + /** + * @return void + */ + public function testSkipProcess() + { + $articles = TableRegistry::get('Articles'); + $manager = new Manager($articles); + /* @var $filter \Search\Model\Filter\Boolean|\PHPUnit_Framework_MockObject_MockObject */ + $filter = $this + ->getMockBuilder('Search\Model\Filter\Boolean') + ->setConstructorArgs(['is_active', $manager]) + ->setMethods(['skip']) + ->getMock(); + $filter + ->expects($this->once()) + ->method('skip') + ->willReturn(true); + $filter->args(['is_active' => true]); + $filter->query($articles->find()); + $filter->process(); + + $this->assertEmpty($filter->query()->clause('where')); + } + public function testProcessWithFlagOn() { $articles = TableRegistry::get('Articles'); diff --git a/tests/TestCase/Model/Filter/CallbackTest.php b/tests/TestCase/Model/Filter/CallbackTest.php index 7a3c8d2d..c0715ff0 100644 --- a/tests/TestCase/Model/Filter/CallbackTest.php +++ b/tests/TestCase/Model/Filter/CallbackTest.php @@ -22,6 +22,30 @@ class CallbackTest extends TestCase 'plugin.Search.Articles' ]; + /** + * @return void + */ + public function testSkipProcess() + { + $articles = TableRegistry::get('Articles'); + $manager = new Manager($articles); + /* @var $filter \Search\Model\Filter\Callback|\PHPUnit_Framework_MockObject_MockObject */ + $filter = $this + ->getMockBuilder('Search\Model\Filter\Callback') + ->setConstructorArgs(['title', $manager]) + ->setMethods(['skip']) + ->getMock(); + $filter + ->expects($this->once()) + ->method('skip') + ->willReturn(true); + $filter->args(['title' => 'test']); + $filter->query($articles->find()); + $filter->process(); + + $this->assertEmpty($filter->query()->clause('where')); + } + public function testProcess() { $articles = TableRegistry::get('Articles'); diff --git a/tests/TestCase/Model/Filter/CompareTest.php b/tests/TestCase/Model/Filter/CompareTest.php index 1c3c9280..a60297bc 100644 --- a/tests/TestCase/Model/Filter/CompareTest.php +++ b/tests/TestCase/Model/Filter/CompareTest.php @@ -19,6 +19,30 @@ class CompareTest extends TestCase 'plugin.Search.Articles' ]; + /** + * @return void + */ + public function testSkipProcess() + { + $articles = TableRegistry::get('Articles'); + $manager = new Manager($articles); + /* @var $filter \Search\Model\Filter\Compare|\PHPUnit_Framework_MockObject_MockObject */ + $filter = $this + ->getMockBuilder('Search\Model\Filter\Compare') + ->setConstructorArgs(['created', $manager]) + ->setMethods(['skip']) + ->getMock(); + $filter + ->expects($this->once()) + ->method('skip') + ->willReturn(true); + $filter->args(['created' => '2012-01-01 00:00:00']); + $filter->query($articles->find()); + $filter->process(); + + $this->assertEmpty($filter->query()->clause('where')); + } + /** * @return void */ diff --git a/tests/TestCase/Model/Filter/FinderTest.php b/tests/TestCase/Model/Filter/FinderTest.php index 2d6a5963..7b9b31cd 100644 --- a/tests/TestCase/Model/Filter/FinderTest.php +++ b/tests/TestCase/Model/Filter/FinderTest.php @@ -19,6 +19,30 @@ class FinderTest extends TestCase 'plugin.Search.Articles' ]; + /** + * @return void + */ + public function testSkipProcess() + { + $articles = TableRegistry::get('Articles'); + $manager = new Manager($articles); + /* @var $filter \Search\Model\Filter\Finder|\PHPUnit_Framework_MockObject_MockObject */ + $filter = $this + ->getMockBuilder('Search\Model\Filter\Finder') + ->setConstructorArgs(['title', $manager]) + ->setMethods(['skip']) + ->getMock(); + $filter + ->expects($this->once()) + ->method('skip') + ->willReturn(true); + $filter->args(['title' => 'test']); + $filter->query($articles->find()); + $filter->process(); + + $this->assertEmpty($filter->query()->clause('where')); + } + /** * @return void */ diff --git a/tests/TestCase/Model/Filter/LikeTest.php b/tests/TestCase/Model/Filter/LikeTest.php index 4515a337..c7cb7cdc 100644 --- a/tests/TestCase/Model/Filter/LikeTest.php +++ b/tests/TestCase/Model/Filter/LikeTest.php @@ -33,6 +33,30 @@ public function testDeprecatedModeOption() $this->assertEquals('OR', $filter->config('valueMode')); } + /** + * @return void + */ + public function testSkipProcess() + { + $articles = TableRegistry::get('Articles'); + $manager = new Manager($articles); + /* @var $filter \Search\Model\Filter\Like|\PHPUnit_Framework_MockObject_MockObject */ + $filter = $this + ->getMockBuilder('Search\Model\Filter\Like') + ->setConstructorArgs(['title', $manager]) + ->setMethods(['skip']) + ->getMock(); + $filter + ->expects($this->once()) + ->method('skip') + ->willReturn(true); + $filter->args(['title' => 'test']); + $filter->query($articles->find()->select(['id'])); + $filter->process(); + + $this->assertEmpty($filter->query()->clause('where')); + } + public function testProcess() { $articles = TableRegistry::get('Articles'); diff --git a/tests/TestCase/Model/Filter/ValueTest.php b/tests/TestCase/Model/Filter/ValueTest.php index bb633280..50446f8f 100644 --- a/tests/TestCase/Model/Filter/ValueTest.php +++ b/tests/TestCase/Model/Filter/ValueTest.php @@ -19,6 +19,30 @@ class ValueTest extends TestCase 'plugin.Search.Articles' ]; + /** + * @return void + */ + public function testSkipProcess() + { + $articles = TableRegistry::get('Articles'); + $manager = new Manager($articles); + /* @var $filter \Search\Model\Filter\Value|\PHPUnit_Framework_MockObject_MockObject */ + $filter = $this + ->getMockBuilder('Search\Model\Filter\Value') + ->setConstructorArgs(['title', $manager]) + ->setMethods(['skip']) + ->getMock(); + $filter + ->expects($this->once()) + ->method('skip') + ->willReturn(true); + $filter->args(['title' => 'test']); + $filter->query($articles->find()); + $filter->process(); + + $this->assertEmpty($filter->query()->clause('where')); + } + public function testProcess() { $articles = TableRegistry::get('Articles'); From d6667018b27bded33af116f1905b1bd8eea61f56 Mon Sep 17 00:00:00 2001 From: ndm2 Date: Sat, 24 Sep 2016 03:40:08 +0200 Subject: [PATCH 4/8] Make callback process test more specific. --- tests/TestCase/Model/Filter/CallbackTest.php | 21 ++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/tests/TestCase/Model/Filter/CallbackTest.php b/tests/TestCase/Model/Filter/CallbackTest.php index c0715ff0..c08abcc1 100644 --- a/tests/TestCase/Model/Filter/CallbackTest.php +++ b/tests/TestCase/Model/Filter/CallbackTest.php @@ -6,6 +6,7 @@ use Cake\ORM\Table; use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; +use Cake\Utility\Hash; use Search\Manager; use Search\Model\Filter\Base; use Search\Model\Filter\Callback; @@ -51,18 +52,22 @@ public function testProcess() $articles = TableRegistry::get('Articles'); $manager = new Manager($articles); - $callback = new Callback('title', $manager, [ + $filter = new Callback('title', $manager, [ 'callback' => function ($query, $args, $manager) { $query->where(['title' => 'test']); } ]); - $callback->args(['title' => ['test']]); - $callback->query($articles->find()); - - $query = $callback->query(); - $this->assertEmpty($query->clause('where')); + $filter->args(['title' => ['test']]); + $filter->query($articles->find()); + $filter->process(); - $callback->process(); - $this->assertNotEmpty($query->clause('where')); + $this->assertRegExp( + '/WHERE title = \:c0$/', + $filter->query()->sql() + ); + $this->assertEquals( + ['test'], + Hash::extract($filter->query()->valueBinder()->bindings(), '{s}.value') + ); } } From cdb36154bea2444f5ef0a5abcfac12a125e9e0cd Mon Sep 17 00:00:00 2001 From: ndm2 Date: Sat, 24 Sep 2016 04:00:04 +0200 Subject: [PATCH 5/8] Fix wrong `getFilters` exception message placeholder. --- src/Manager.php | 2 +- tests/TestCase/ManagerTest.php | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Manager.php b/src/Manager.php index e1341f35..4570a0b3 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -84,7 +84,7 @@ public function getFilters($collection = 'default') { if (!isset($this->_filters[$collection])) { throw new InvalidArgumentException( - sprintf('The collection "{0}" does not exist.', $collection) + sprintf('The collection "%s" does not exist.', $collection) ); } diff --git a/tests/TestCase/ManagerTest.php b/tests/TestCase/ManagerTest.php index 26bea67f..40698307 100644 --- a/tests/TestCase/ManagerTest.php +++ b/tests/TestCase/ManagerTest.php @@ -77,6 +77,17 @@ public function testGetFilters() $this->assertInstanceOf('\Search\Model\Filter\Compare', $result['test2']); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The collection "nonExistentCollection" does not exist. + */ + public function testGetFiltersNonExistentCollection() + { + $table = TableRegistry::get('Articles'); + $manager = new Manager($table); + $manager->getFilters('nonExistentCollection'); + } + public function testRemove() { $table = TableRegistry::get('Articles'); From b144d4bbf758060216e2609420679a9e002ff072 Mon Sep 17 00:00:00 2001 From: ndm2 Date: Sat, 24 Sep 2016 04:02:31 +0200 Subject: [PATCH 6/8] Add `boolean` filter shorthand, and test all shorthand methods. --- src/Manager.php | 14 ++++++++ tests/TestCase/ManagerTest.php | 58 ++++++++++++++++++++++++++++------ 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/Manager.php b/src/Manager.php index 4570a0b3..86be31a7 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -137,6 +137,20 @@ public function remove($name) unset($this->_filters[$this->_collection][$name]); } + /** + * boolean method + * + * @param string $name Name + * @param array $config Config + * @return $this + */ + public function boolean($name, array $config = []) + { + $this->add($name, 'Search.Boolean', $config); + + return $this; + } + /** * like method * diff --git a/tests/TestCase/ManagerTest.php b/tests/TestCase/ManagerTest.php index 40698307..10ba6558 100644 --- a/tests/TestCase/ManagerTest.php +++ b/tests/TestCase/ManagerTest.php @@ -18,19 +18,57 @@ class ManagerTest extends TestCase 'plugin.Search.Articles' ]; - public function testMethods() + public function testShorthandMethods() { $table = TableRegistry::get('Articles'); + + $options = ['foo' => 'bar']; + + $manager = new Manager($table); + $manager->boolean('boolean', $options); + $manager->callback('callback', $options); + $manager->compare('compare', $options); + $manager->custom('custom', ['className' => '\Search\Test\TestApp\Model\Filter\TestFilter'] + $options); + $manager->finder('finder', $options); + $manager->like('like', $options); + $manager->value('value', $options); + + $result = $manager->getFilters(); + $this->assertCount(7, $result); + $this->assertInstanceOf('\Search\Model\Filter\Boolean', $result['boolean']); + $this->assertInstanceOf('\Search\Model\Filter\Callback', $result['callback']); + $this->assertInstanceOf('\Search\Model\Filter\Compare', $result['compare']); + $this->assertInstanceOf('\Search\Test\TestApp\Model\Filter\TestFilter', $result['custom']); + $this->assertInstanceOf('\Search\Model\Filter\Finder', $result['finder']); + $this->assertInstanceOf('\Search\Model\Filter\Like', $result['like']); + $this->assertInstanceOf('\Search\Model\Filter\Value', $result['value']); + + $this->assertEquals('bar', $result['boolean']->config('foo')); + $this->assertEquals('bar', $result['callback']->config('foo')); + $this->assertEquals('bar', $result['compare']->config('foo')); + $this->assertEquals('bar', $result['custom']->config('foo')); + $this->assertEquals('bar', $result['finder']->config('foo')); + $this->assertEquals('bar', $result['like']->config('foo')); + $this->assertEquals('bar', $result['value']->config('foo')); + } + + public function testMagicShorthandMethods() + { + Configure::write('App.namespace', 'Search\Test\TestApp'); + + $table = TableRegistry::get('Articles'); + $manager = new Manager($table); - $manager->compare('test'); - $all = $manager->all(); - $this->assertInstanceOf('\Search\Model\Filter\Compare', $all['test']); - $this->assertEquals(count($all), 1); - - $manager->value('test2'); - $all = $manager->all(); - $this->assertInstanceOf('\Search\Model\Filter\Value', $all['test2']); - $this->assertEquals(count($all), 2); + $manager->testFilter('test1'); + $manager->testFilter('test2', ['foo' => 'bar']); + + Configure::clear(); + + $result = $manager->getFilters(); + $this->assertCount(2, $result); + $this->assertInstanceOf('\Search\Test\TestApp\Model\Filter\TestFilter', $result['test1']); + $this->assertInstanceOf('\Search\Test\TestApp\Model\Filter\TestFilter', $result['test2']); + $this->assertEquals('bar', $result['test2']->config('foo')); } public function testLoadFilter() From 8def657360e881763f77c328ab9b11d366ec4fb8 Mon Sep 17 00:00:00 2001 From: ndm2 Date: Sat, 24 Sep 2016 04:19:43 +0200 Subject: [PATCH 7/8] Additional tests, clean up a bit, improve readability and consistency. --- tests/TestApp/Model/TestRepository.php | 64 +++++++++++++++++ .../Controller/Component/PrgComponentTest.php | 14 +++- tests/TestCase/ManagerTest.php | 72 +++++++++++++++++++ .../Model/Behavior/SearchBehaviorTest.php | 33 ++++++++- tests/TestCase/Model/Filter/BaseTest.php | 22 +++++- tests/TestCase/Model/Filter/BooleanTest.php | 34 +++++++-- tests/TestCase/Model/Filter/CallbackTest.php | 7 +- tests/TestCase/Model/Filter/LikeTest.php | 3 + tests/TestCase/Model/Filter/ValueTest.php | 3 + 9 files changed, 239 insertions(+), 13 deletions(-) create mode 100644 tests/TestApp/Model/TestRepository.php diff --git a/tests/TestApp/Model/TestRepository.php b/tests/TestApp/Model/TestRepository.php new file mode 100644 index 00000000..3187b4cb --- /dev/null +++ b/tests/TestApp/Model/TestRepository.php @@ -0,0 +1,64 @@ +fallbacks(); }); $request = new Request(); - $response = $this->getMock('Cake\Network\Response', ['stop']); + $response = $this + ->getMockBuilder('Cake\Network\Response') + ->setMethods(['stop']) + ->getMock(); $this->Controller = new Controller($request, $response); $this->Prg = new PrgComponent($this->Controller->components()); @@ -151,4 +154,13 @@ public function testInitializePostWithQueryStringWhitelistEmpty() $response = $this->Prg->startup(); $this->assertEquals('http://localhost/Posts/index/pass?foo=bar', $response->header()['Location']); } + + /** + * @return void + */ + public function testConversionWithoutRedirect() + { + $this->Controller->request->env('REQUEST_METHOD', 'POST'); + $this->assertNull($this->Prg->conversion(false)); + } } diff --git a/tests/TestCase/ManagerTest.php b/tests/TestCase/ManagerTest.php index 10ba6558..dcea11bb 100644 --- a/tests/TestCase/ManagerTest.php +++ b/tests/TestCase/ManagerTest.php @@ -18,6 +18,9 @@ class ManagerTest extends TestCase 'plugin.Search.Articles' ]; + /** + * @return void + */ public function testShorthandMethods() { $table = TableRegistry::get('Articles'); @@ -52,6 +55,9 @@ public function testShorthandMethods() $this->assertEquals('bar', $result['value']->config('foo')); } + /** + * @return void + */ public function testMagicShorthandMethods() { Configure::write('App.namespace', 'Search\Test\TestApp'); @@ -71,22 +77,52 @@ public function testMagicShorthandMethods() $this->assertEquals('bar', $result['test2']->config('foo')); } + /** + * @return void + */ + public function testAll() + { + $table = TableRegistry::get('Articles'); + $manager = new Manager($table); + + $this->assertEmpty($manager->all()); + + $manager->collection('other'); + $manager->add('field', 'Search.Value'); + $this->assertEmpty($manager->all()); + + $manager->collection('default'); + $manager->add('field', 'Search.Value'); + $all = $manager->all(); + $this->assertCount(1, $all); + $this->assertInstanceOf('\Search\Model\Filter\Value', $all['field']); + } + + /** + * @return void + */ public function testLoadFilter() { $table = TableRegistry::get('Articles'); $manager = new Manager($table); + $result = $manager->loadFilter('test', 'Search.Value'); $this->assertInstanceOf('\Search\Model\Filter\Value', $result); + $result = $manager->loadFilter('test', 'Search.Compare'); $this->assertInstanceOf('\Search\Model\Filter\Compare', $result); } + /** + * @return void + */ public function testAdd() { $table = TableRegistry::get('Articles'); $manager = new Manager($table); $manager->add('testOne', 'Search.Value'); $manager->add('testTwo', 'Search.Compare'); + $result = $manager->getFilters(); $this->assertCount(2, $result); $this->assertInstanceOf('\Search\Model\Filter\Value', $result['testOne']); @@ -95,6 +131,7 @@ public function testAdd() /** * @expectedException \InvalidArgumentException + * @return void */ public function testLoadFilterInvalidArgumentException() { @@ -103,12 +140,16 @@ public function testLoadFilterInvalidArgumentException() $manager->loadFilter('test', 'DOES-NOT-EXIST'); } + /** + * @return void + */ public function testGetFilters() { $table = TableRegistry::get('Articles'); $manager = new Manager($table); $manager->add('test', 'Search.Value'); $manager->add('test2', 'Search.Compare'); + $result = $manager->getFilters(); $this->assertCount(2, $result); $this->assertInstanceOf('\Search\Model\Filter\Value', $result['test']); @@ -118,6 +159,7 @@ public function testGetFilters() /** * @expectedException \InvalidArgumentException * @expectedExceptionMessage The collection "nonExistentCollection" does not exist. + * @return void */ public function testGetFiltersNonExistentCollection() { @@ -126,22 +168,31 @@ public function testGetFiltersNonExistentCollection() $manager->getFilters('nonExistentCollection'); } + /** + * @return void + */ public function testRemove() { $table = TableRegistry::get('Articles'); $manager = new Manager($table); + $manager->add('test', 'Search.Value'); $manager->add('test2', 'Search.Compare'); $result = $manager->getFilters(); $this->assertCount(2, $result); + $manager->remove('test2'); $result = $manager->getFilters(); $this->assertCount(1, $result); + $manager->remove('test'); $result = $manager->getFilters(); $this->assertCount(0, $result); } + /** + * @return void + */ public function testRepository() { $table = TableRegistry::get('Articles'); @@ -150,20 +201,41 @@ public function testRepository() $this->assertInstanceOf('\Cake\Datasource\RepositoryInterface', $result); } + /** + * @return void + */ + public function testTable() + { + $table = TableRegistry::get('Articles'); + $manager = new Manager($table); + $result = $manager->table(); + $this->assertInstanceOf('\Cake\Datasource\RepositoryInterface', $result); + } + + /** + * @return void + */ public function testCollection() { $table = TableRegistry::get('Articles'); $manager = new Manager($table); + + $result = $manager->collection(); + $this->assertEquals('default', $result); + $result = $manager->collection('default'); $this->assertInstanceOf('\Search\Manager', $result); + $manager->add('test', 'Search.Value'); $result = $manager->collection('otherFilters'); $this->assertInstanceOf('\Search\Manager', $result); + $manager->add('test2', 'Search.Value'); $manager->add('test3', 'Search.Value'); $result = $manager->getFilters('default'); $this->assertCount(1, $result); $this->assertArrayHasKey('test', $result); + $result = $manager->getFilters('otherFilters'); $this->assertCount(2, $result); $this->assertArrayHasKey('test2', $result); diff --git a/tests/TestCase/Model/Behavior/SearchBehaviorTest.php b/tests/TestCase/Model/Behavior/SearchBehaviorTest.php index efc992d6..02911226 100644 --- a/tests/TestCase/Model/Behavior/SearchBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/SearchBehaviorTest.php @@ -235,7 +235,7 @@ public function testCollectionFinder($collection, $queryString, $expected) /** * DataProvider of testCollectionFinder * - * @return void + * @return array */ public function testCollectionFinderProvider() { @@ -253,13 +253,13 @@ public function testCollectionFinderProvider() /** * testFindSearchException * - * @expectedException Exception + * @expectedException \Exception * @expectedExceptionMessage Custom finder "search" expects search arguments to be nested under key "search" in find() options. * @return void */ public function testFindSearchException() { - $query = $this->Articles->find('search'); + $this->Articles->find('search'); } /** @@ -283,4 +283,31 @@ public function testSearchManager() $manager = $this->Articles->searchManager(); $this->assertInstanceOf('\Search\Manager', $manager); } + + /** + * @return void + */ + public function testNoSearchManager() + { + $behavior = $this + ->getMockBuilder('Search\Model\Behavior\SearchBehavior') + ->setConstructorArgs([$this->Articles]) + ->setMethods(['searchManager']) + ->getMock(); + $this->Articles->behaviors()->reset(); + $this->Articles->addBehavior('Search', [ + 'className' => '\\' . get_class($behavior), + 'searchConfigMethod' => 'nonExistent' + ]); + + /* @var $behavior \Search\Model\Behavior\SearchBehavior|\PHPUnit_Framework_MockObject_MockObject */ + $behavior = $this->Articles->behaviors()->get('Search'); + $behavior + ->expects($this->once()) + ->method('searchManager') + ->willReturn(new Manager($this->Articles)); + + $query = $this->Articles->find('search', ['search' => []]); + $this->assertEmpty($query->clause('where')); + } } diff --git a/tests/TestCase/Model/Filter/BaseTest.php b/tests/TestCase/Model/Filter/BaseTest.php index ca8b3b6a..b08fbdaa 100644 --- a/tests/TestCase/Model/Filter/BaseTest.php +++ b/tests/TestCase/Model/Filter/BaseTest.php @@ -5,6 +5,7 @@ use Cake\TestSuite\TestCase; use Search\Manager; use Search\Test\TestApp\Model\Filter\TestFilter; +use Search\Test\TestApp\Model\TestRepository; class BaseTest extends TestCase { @@ -16,10 +17,12 @@ class BaseTest extends TestCase public function setup() { $table = TableRegistry::get('Articles'); - $manager = new Manager($table); $this->manager = new Manager($table); } + /** + * @return void + */ public function testSkip() { $filter = new TestFilter( @@ -66,6 +69,9 @@ public function testValue() $this->assertEquals(['value1', 'value2'], $filter->value()); } + /** + * @return void + */ public function testFieldAliasing() { $filter = new TestFilter( @@ -88,4 +94,18 @@ public function testFieldAliasing() $expected = ['Articles.field1', 'Articles.field2']; $this->assertEquals($expected, $filter->field()); } + + /** + * @return void + */ + public function testFieldAliasingWithNonSupportingRepository() + { + $filter = new TestFilter( + 'field', + new Manager(new TestRepository()), + ['aliasField' => true] + ); + + $this->assertEquals('field', $filter->field()); + } } diff --git a/tests/TestCase/Model/Filter/BooleanTest.php b/tests/TestCase/Model/Filter/BooleanTest.php index 93304d28..44a493d6 100644 --- a/tests/TestCase/Model/Filter/BooleanTest.php +++ b/tests/TestCase/Model/Filter/BooleanTest.php @@ -1,14 +1,10 @@ assertEmpty($filter->query()->clause('where')); } + /** + * @return void + */ public function testProcessWithFlagOn() { $articles = TableRegistry::get('Articles'); @@ -66,6 +65,9 @@ public function testProcessWithFlagOn() ); } + /** + * @return void + */ public function testProcessWithFlagOff() { $articles = TableRegistry::get('Articles'); @@ -85,6 +87,9 @@ public function testProcessWithFlagOff() ); } + /** + * @return void + */ public function testProcessWithStringFlagTrue() { $articles = TableRegistry::get('Articles'); @@ -104,6 +109,9 @@ public function testProcessWithStringFlagTrue() ); } + /** + * @return void + */ public function testProcessWithStringFlagFalse() { $articles = TableRegistry::get('Articles'); @@ -123,6 +131,9 @@ public function testProcessWithStringFlagFalse() ); } + /** + * @return void + */ public function testProcessWithBooleanFlagTrue() { $articles = TableRegistry::get('Articles'); @@ -142,6 +153,9 @@ public function testProcessWithBooleanFlagTrue() ); } + /** + * @return void + */ public function testProcessWithBooleanFlagFalse() { $articles = TableRegistry::get('Articles'); @@ -161,6 +175,9 @@ public function testProcessWithBooleanFlagFalse() ); } + /** + * @return void + */ public function testProcessWithStringFlag1() { $articles = TableRegistry::get('Articles'); @@ -180,6 +197,9 @@ public function testProcessWithStringFlag1() ); } + /** + * @return void + */ public function testProcessWithStringFlag0() { $articles = TableRegistry::get('Articles'); @@ -199,6 +219,9 @@ public function testProcessWithStringFlag0() ); } + /** + * @return void + */ public function testProcessWithIntegerFlag1() { $articles = TableRegistry::get('Articles'); @@ -237,6 +260,9 @@ public function testProcessWithIntegerFlag0() ); } + /** + * @return void + */ public function testProcessWithFlagInvalid() { $articles = TableRegistry::get('Articles'); diff --git a/tests/TestCase/Model/Filter/CallbackTest.php b/tests/TestCase/Model/Filter/CallbackTest.php index c08abcc1..597b1c00 100644 --- a/tests/TestCase/Model/Filter/CallbackTest.php +++ b/tests/TestCase/Model/Filter/CallbackTest.php @@ -1,14 +1,10 @@ assertEmpty($filter->query()->clause('where')); } + /** + * @return void + */ public function testProcess() { $articles = TableRegistry::get('Articles'); diff --git a/tests/TestCase/Model/Filter/LikeTest.php b/tests/TestCase/Model/Filter/LikeTest.php index c7cb7cdc..2947cbb8 100644 --- a/tests/TestCase/Model/Filter/LikeTest.php +++ b/tests/TestCase/Model/Filter/LikeTest.php @@ -57,6 +57,9 @@ public function testSkipProcess() $this->assertEmpty($filter->query()->clause('where')); } + /** + * @return void + */ public function testProcess() { $articles = TableRegistry::get('Articles'); diff --git a/tests/TestCase/Model/Filter/ValueTest.php b/tests/TestCase/Model/Filter/ValueTest.php index 50446f8f..3cb2fe4c 100644 --- a/tests/TestCase/Model/Filter/ValueTest.php +++ b/tests/TestCase/Model/Filter/ValueTest.php @@ -43,6 +43,9 @@ public function testSkipProcess() $this->assertEmpty($filter->query()->clause('where')); } + /** + * @return void + */ public function testProcess() { $articles = TableRegistry::get('Articles'); From 8b478b2cd42ff29059c116b37cbd4c06a73e1abb Mon Sep 17 00:00:00 2001 From: ndm2 Date: Sat, 24 Sep 2016 04:33:46 +0200 Subject: [PATCH 8/8] Add test subject members and type hints to please the machine overlords. --- tests/TestApp/Model/Table/ArticlesTable.php | 3 +++ tests/TestApp/Model/Table/CommentsTable.php | 6 +++++ .../Model/Table/FinderArticlesTable.php | 3 +++ tests/TestApp/Model/Table/GroupsTable.php | 3 +++ .../Controller/Component/PrgComponentTest.php | 13 +++++++++- tests/TestCase/ManagerTest.php | 2 ++ .../Model/Behavior/SearchBehaviorTest.php | 17 +++++++++++++ tests/TestCase/Model/Filter/BaseTest.php | 24 +++++++++++++++---- tests/TestCase/Model/Filter/CallbackTest.php | 3 ++- 9 files changed, 67 insertions(+), 7 deletions(-) diff --git a/tests/TestApp/Model/Table/ArticlesTable.php b/tests/TestApp/Model/Table/ArticlesTable.php index be9ea478..53d5eea2 100644 --- a/tests/TestApp/Model/Table/ArticlesTable.php +++ b/tests/TestApp/Model/Table/ArticlesTable.php @@ -4,6 +4,9 @@ use Cake\ORM\Table; use Search\Manager; +/** + * @mixin \Search\Model\Behavior\SearchBehavior + */ class ArticlesTable extends Table { diff --git a/tests/TestApp/Model/Table/CommentsTable.php b/tests/TestApp/Model/Table/CommentsTable.php index 0a00ce1f..824761b6 100644 --- a/tests/TestApp/Model/Table/CommentsTable.php +++ b/tests/TestApp/Model/Table/CommentsTable.php @@ -4,9 +4,15 @@ use Cake\ORM\Table; use Search\Manager; +/** + * @mixin \Search\Model\Behavior\SearchBehavior + */ class CommentsTable extends Table { + /** + * @return \Search\Manager + */ public function searchConfiguration() { $manager = new Manager($this); diff --git a/tests/TestApp/Model/Table/FinderArticlesTable.php b/tests/TestApp/Model/Table/FinderArticlesTable.php index 6d459bed..c456e3c5 100644 --- a/tests/TestApp/Model/Table/FinderArticlesTable.php +++ b/tests/TestApp/Model/Table/FinderArticlesTable.php @@ -4,6 +4,9 @@ use Cake\ORM\Query; use Cake\ORM\Table; +/** + * @mixin \Search\Model\Behavior\SearchBehavior + */ class FinderArticlesTable extends Table { public function initialize(array $config) diff --git a/tests/TestApp/Model/Table/GroupsTable.php b/tests/TestApp/Model/Table/GroupsTable.php index f6dcd5c6..30db0ae5 100644 --- a/tests/TestApp/Model/Table/GroupsTable.php +++ b/tests/TestApp/Model/Table/GroupsTable.php @@ -4,6 +4,9 @@ use Cake\ORM\Table; use Search\Manager; +/** + * @mixin \Search\Model\Behavior\SearchBehavior + */ class GroupsTable extends Table { diff --git a/tests/TestCase/Controller/Component/PrgComponentTest.php b/tests/TestCase/Controller/Component/PrgComponentTest.php index d2b71cab..1570ed5a 100644 --- a/tests/TestCase/Controller/Component/PrgComponentTest.php +++ b/tests/TestCase/Controller/Component/PrgComponentTest.php @@ -3,12 +3,23 @@ use Cake\Controller\Controller; use Cake\Network\Request; +use Cake\Routing\RouteBuilder; use Cake\Routing\Router; use Cake\TestSuite\TestCase; use Search\Controller\Component\PrgComponent; class SearchComponentTest extends TestCase { + /** + * @var \Cake\Controller\Controller + */ + public $Controller; + + /** + * @var \Search\Controller\Component\PrgComponent + */ + public $Prg; + /** * @return void */ @@ -17,7 +28,7 @@ public function setUp() parent::setUp(); Router::$initialized = true; - Router::scope('/', function ($routes) { + Router::scope('/', function (RouteBuilder $routes) { $routes->connect( '/users/my-predictions', ['controller' => 'UserAnswers', 'action' => 'index', 'type' => 'open'], diff --git a/tests/TestCase/ManagerTest.php b/tests/TestCase/ManagerTest.php index dcea11bb..3997f85d 100644 --- a/tests/TestCase/ManagerTest.php +++ b/tests/TestCase/ManagerTest.php @@ -36,6 +36,7 @@ public function testShorthandMethods() $manager->like('like', $options); $manager->value('value', $options); + /* @var $result \Search\Model\Filter\Base[] */ $result = $manager->getFilters(); $this->assertCount(7, $result); $this->assertInstanceOf('\Search\Model\Filter\Boolean', $result['boolean']); @@ -70,6 +71,7 @@ public function testMagicShorthandMethods() Configure::clear(); + /* @var $result \Search\Model\Filter\Base[] */ $result = $manager->getFilters(); $this->assertCount(2, $result); $this->assertInstanceOf('\Search\Test\TestApp\Model\Filter\TestFilter', $result['test1']); diff --git a/tests/TestCase/Model/Behavior/SearchBehaviorTest.php b/tests/TestCase/Model/Behavior/SearchBehaviorTest.php index 02911226..0b403ae7 100644 --- a/tests/TestCase/Model/Behavior/SearchBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/SearchBehaviorTest.php @@ -7,6 +7,20 @@ class SearchBehaviorTest extends TestCase { + /** + * @var \Search\Test\TestApp\Model\Table\ArticlesTable + */ + public $Articles; + + /** + * @var \Search\Test\TestApp\Model\Table\CommentsTable + */ + public $Comments; + + /** + * @var \Search\Test\TestApp\Model\Table\GroupsTable + */ + public $Groups; /** * Fixtures @@ -224,6 +238,9 @@ public function testAliasedFinder() * Test the custom "search" finder * * @dataProvider testCollectionFinderProvider + * @param string $collection The collection name. + * @param string $queryString The query string data. + * @param integer $expected The expected record count. * @return void */ public function testCollectionFinder($collection, $queryString, $expected) diff --git a/tests/TestCase/Model/Filter/BaseTest.php b/tests/TestCase/Model/Filter/BaseTest.php index b08fbdaa..22b6acc9 100644 --- a/tests/TestCase/Model/Filter/BaseTest.php +++ b/tests/TestCase/Model/Filter/BaseTest.php @@ -9,15 +9,29 @@ class BaseTest extends TestCase { + /** + * @var \Search\Manager + */ + public $Manager; + /** + * Fixtures + * + * @var array + */ public $fixtures = [ 'plugin.Search.Articles' ]; + /** + * setup + * + * @return void + */ public function setup() { $table = TableRegistry::get('Articles'); - $this->manager = new Manager($table); + $this->Manager = new Manager($table); } /** @@ -27,7 +41,7 @@ public function testSkip() { $filter = new TestFilter( 'field', - $this->manager, + $this->Manager, ['alwaysRun' => true, 'filterEmpty' => true] ); @@ -51,7 +65,7 @@ public function testValue() { $filter = new TestFilter( 'field', - $this->manager, + $this->Manager, ['defaultValue' => 'default'] ); @@ -76,7 +90,7 @@ public function testFieldAliasing() { $filter = new TestFilter( 'field', - $this->manager, + $this->Manager, [] ); @@ -87,7 +101,7 @@ public function testFieldAliasing() $filter = new TestFilter( ['field1', 'field2'], - $this->manager, + $this->Manager, [] ); diff --git a/tests/TestCase/Model/Filter/CallbackTest.php b/tests/TestCase/Model/Filter/CallbackTest.php index 597b1c00..50ba05a9 100644 --- a/tests/TestCase/Model/Filter/CallbackTest.php +++ b/tests/TestCase/Model/Filter/CallbackTest.php @@ -1,6 +1,7 @@ function ($query, $args, $manager) { + 'callback' => function (Query $query, array $args, Callback $filter) { $query->where(['title' => 'test']); } ]);