浏览代码

Add tests for counter cache on multiple associations.

ndm2 11 年之前
父节点
当前提交
352969a620

+ 97 - 4
Test/Case/Model/Behavior/SoftDeleteBehaviorTest.php

@@ -25,7 +25,8 @@ class SoftDeleteBehaviorTest extends CakeTestCase {
 	 */
 	public $fixtures = array(
 		'plugin.tools.soft_delete_category',
-		'plugin.tools.soft_delete_post'
+		'plugin.tools.soft_delete_post',
+		'plugin.tools.soft_delete_user'
 	);
 
 	/**
@@ -175,6 +176,42 @@ class SoftDeleteBehaviorTest extends CakeTestCase {
 	}
 
 	/**
+	 * testSoftDeleteWithCounterCacheOnMultipleAssociations
+	 *
+	 * @return void
+	 */
+	public function testSoftDeleteWithCounterCacheOnMultipleAssociations() {
+		$this->Post->bindModel(array(
+			'belongsTo' => array(
+				'User' => array(
+					'className' => 'SoftDeleteUser',
+					'counterCache' => true
+				)
+			)
+		),
+		false);
+
+		$this->Post->Category->id = 1;
+		$this->Post->User->id = 1;
+
+		$count = $this->Post->Category->field('post_count');
+		$this->assertEquals(2, $count);
+
+		$count = $this->Post->User->field('post_count');
+		$this->assertEquals(2, $count);
+
+		$this->assertFalse($this->Post->softDeleted);
+		$this->Post->delete(1);
+		$this->assertTrue($this->Post->softDeleted);
+
+		$count = $this->Post->Category->field('post_count');
+		$this->assertEquals(1, $count);
+
+		$count = $this->Post->User->field('post_count');
+		$this->assertEquals(1, $count);
+	}
+
+	/**
 	 * testSoftDeleteWithoutCounterCache
 	 *
 	 * @return void
@@ -195,7 +232,7 @@ class SoftDeleteBehaviorTest extends CakeTestCase {
 	public function testUnDeleteWithCounterCache() {
 		$this->Post->Category->id = 2;
 		$count = $this->Post->Category->field('post_count');
-		$this->assertEquals($count, 0);
+		$this->assertEquals(0, $count);
 
 		$this->assertEmpty($this->Post->read(null, 3));
 
@@ -218,9 +255,9 @@ class SoftDeleteBehaviorTest extends CakeTestCase {
 
 		$this->Post->Category->id = 2;
 		$count = $this->Post->Category->field('post_count');
-		$this->assertEquals($count, 0);
+		$this->assertEquals(0, $count);
 		$count = $this->Post->Category->field('deleted_post_count');
-		$this->assertEquals($count, 1);
+		$this->assertEquals(1, $count);
 
 		$this->assertEmpty($this->Post->read(null, 3));
 
@@ -233,6 +270,40 @@ class SoftDeleteBehaviorTest extends CakeTestCase {
 	}
 
 	/**
+	 * testUnDeleteWithCounterCacheOnMultipleAssociations
+	 *
+	 * @return void
+	 */
+	public function testUnDeleteWithCounterCacheOnMultipleAssociations() {
+		$this->Post->bindModel(array(
+				'belongsTo' => array(
+					'User' => array(
+						'className' => 'SoftDeleteUser',
+						'counterCache' => true
+					)
+				)
+			),
+			false);
+
+		$this->Post->Category->id = 2;
+		$this->Post->User->id = 1;
+
+		$count = $this->Post->Category->field('post_count');
+		$this->assertEquals(0, $count);
+		$count = $this->Post->User->field('post_count');
+		$this->assertEquals(2, $count);
+
+		$this->assertEmpty($this->Post->read(null, 3));
+
+		$this->Post->undelete(3);
+
+		$count = $this->Post->Category->field('post_count');
+		$this->assertEquals(1, $count);
+		$count = $this->Post->User->field('post_count');
+		$this->assertEquals(3, $count);
+	}
+
+	/**
 	 * testUnDeleteWithoutCounterCache
 	 *
 	 * @return void
@@ -296,6 +367,28 @@ class SoftDeleteCategory extends CakeTestModel {
 }
 
 /**
+ * SoftDeleteUser
+ *
+ */
+class SoftDeleteUser extends CakeTestModel {
+
+	/**
+	 * Use Table
+	 *
+	 * @var string
+	 */
+	public $useTable = 'soft_delete_users';
+
+	/**
+	 * Alias
+	 *
+	 * @var string
+	 */
+	public $alias = 'User';
+
+}
+
+/**
  * SoftDeletedPost
  *
  */

+ 4 - 0
Test/Fixture/SoftDeletePostFixture.php

@@ -13,6 +13,7 @@ class SoftDeletePostFixture extends CakeTestFixture {
 	public $fields = array(
 		'id' => array('type' => 'integer', 'key' => 'primary'),
 		'category_id' => array('type' => 'integer'),
+		'user_id' => array('type' => 'integer'),
 		'title' => array('type' => 'string', 'null' => false),
 		'deleted' => array('type' => 'boolean', 'null' => false, 'default' => '0'),
 		'deleted_date' => 'datetime',
@@ -28,6 +29,7 @@ class SoftDeletePostFixture extends CakeTestFixture {
 		array(
 			'id' => 1,
 			'category_id' => 1,
+			'user_id' => 1,
 			'title' => 'First Post',
 			'deleted' => 0,
 			'deleted_date' => null,
@@ -36,6 +38,7 @@ class SoftDeletePostFixture extends CakeTestFixture {
 		array(
 			'id' => 2,
 			'category_id' => 1,
+			'user_id' => 1,
 			'title' => 'Second Post',
 			'deleted' => 0,
 			'deleted_date' => null,
@@ -44,6 +47,7 @@ class SoftDeletePostFixture extends CakeTestFixture {
 		array(
 			'id' => 3,
 			'category_id' => 2,
+			'user_id' => 1,
 			'title' => 'Third Post',
 			'deleted' => 1,
 			'deleted_date' => '2008-01-01 00:00:00',

+ 29 - 0
Test/Fixture/SoftDeleteUserFixture.php

@@ -0,0 +1,29 @@
+<?php
+/**
+ * SoftDeleteUserFixture
+ *
+ */
+class SoftDeleteUserFixture extends CakeTestFixture {
+
+	/**
+	 * Fields property
+	 *
+	 * @var array
+	 */
+	public $fields = array(
+		'id' => array('type' => 'integer', 'key' => 'primary'),
+		'post_count' => array('type' => 'integer'),
+		'name' => array('type' => 'string', 'null' => false));
+
+	/**
+	 * Records property
+	 *
+	 * @var array
+	 */
+	public $records = array(
+		array(
+			'id' => 1,
+			'post_count' => 2,
+			'name' => 'User'));
+
+}