Browse Source

Update to test for pagination options with custom finders

Patrick Conroy 11 years ago
parent
commit
f99d3587d6

+ 21 - 0
tests/TestCase/Controller/Component/PaginatorComponentTest.php

@@ -142,6 +142,27 @@ class PaginatorComponentTest extends TestCase {
 	}
 
 /**
+ * Test to make sure options get sent to custom finder methods via paginate
+ * @return void
+ */
+	public function testPaginateCustomFinderOptions() {
+		$this->loadFixtures('Post');
+		$settings = [
+			'PaginatorPosts' => [
+				'finder' => 'author',
+				'author_id' => 1
+			]
+		];
+		$table = TableRegistry::get('PaginatorPosts');
+
+		$expected = $table->find('author', ['conditions' => ['PaginatorPosts.author_id' => $settings['PaginatorPosts']['author_id']]])
+			->count();
+		$result = $this->Paginator->paginate($table, $settings)->count();
+
+		$this->assertEquals($expected, $result);
+	}
+
+/**
  * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  *
  * @return void

+ 1 - 36
tests/TestCase/Controller/ControllerTest.php

@@ -207,10 +207,7 @@ class ControllerTest extends TestCase {
  */
 	public $fixtures = array(
 		'core.post',
-		'core.comment',
-		'core.article',
-		'core.articles_tag',
-		'core.tag'
+		'core.comment'
 	);
 
 /**
@@ -874,36 +871,4 @@ class ControllerTest extends TestCase {
 		$this->assertTrue(isset($registry->Paginator));
 	}
 
-/**
- * Testing that when you paginate, your options persist over to your custom finder.
- * Using fixture data, this tests by uses matching() in both places.
- * If the Table gets the 'tags' array successfully, it will do a matching().
- *
- * @return void
- */
-	public function testPaginateSendsFinderOptions() {
-		$request = new Request('/');
-		$request->params['pass'] = array();
-		$response = $this->getMock('Cake\Network\Response');
-		$testTags = [2, 3];
-		$Controller = new Controller($request, $response);
-		$Controller->loadModel('Articles');
-		$this->assertInstanceOf('Cake\ORM\Table', $Controller->Articles);
-		$this->assertInstanceOf('Cake\ORM\Association\BelongsToMany', $Controller->Articles->Tags);
-		
-		$Controller->paginate = [
-			'Articles' => [
-				'finder' => 'customTags',
-				'tags' => $testTags,
-				'maxLimit' => 1000
-			]
-		];
-
-		$result = $Controller->paginate('Articles')->count();
-		$expected = $Controller->Articles->find('all')->contain(['Tags'])->matching('Tags', function($q) use ($testTags) {
-			return $q->where(['Tags.id IN' => $testTags]);
-		})->count();
-		
-		$this->assertEquals($expected, $result);
-	}
 }

+ 1 - 1
tests/TestCase/Shell/Task/TestTaskTest.php

@@ -200,7 +200,7 @@ class TestTaskTest extends TestCase {
  */
 	public function testMethodIntrospection() {
 		$result = $this->Task->getTestableMethods('TestApp\Model\Table\ArticlesTable');
-		$expected = ['findpublished', 'dosomething', 'dosomethingelse', 'findcustomtags'];
+		$expected = ['findpublished', 'dosomething', 'dosomethingelse'];
 		$this->assertEquals($expected, array_map('strtolower', $result));
 	}
 

+ 0 - 17
tests/test_app/TestApp/Model/Table/ArticlesTable.php

@@ -12,7 +12,6 @@
 namespace TestApp\Model\Table;
 
 use Cake\ORM\Query;
-use Cake\ORM\Table;
 
 /**
  * Article table class
@@ -53,22 +52,6 @@ class ArticlesTable extends Table {
 	}
 
 /**
- * Custom finder, used with fixture data to ensure Paginator is sending options
- *
- * @param Cake\ORM\Query $query
- * @param array $options
- * @return Cake\ORM\Query
- */
-	public function findCustomTags(Query $query, array $options = []) {
-		if (isset($options['tags']) && is_array($options['tags'])) {
-			return $query->contain(['Tags'])->matching('Tags', function($q) use ($options) {
-				return $q->where(['Tags.id IN' => $options['tags']]);
-			});
-		}
-		return $query;
-	}
-
-/**
  * Example protected method
  *
  * @return void

+ 14 - 0
tests/test_app/TestApp/Model/Table/PaginatorPostsTable.php

@@ -52,4 +52,18 @@ class PaginatorPostsTable extends Table {
 		return $query;
 	}
 
+/**
+ * Custom finder, used with fixture data to ensure Paginator is sending options
+ *
+ * @param Cake\ORM\Query $query
+ * @param array $options
+ * @return Cake\ORM\Query
+ */
+	public function findAuthor(Query $query, array $options = []) {
+		if (isset($options['author_id'])) {
+			$query->where(['PaginatorPosts.author_id' => $options['author_id']]);
+		}
+		return $query;
+	}
+
 }