|
|
@@ -50,7 +50,8 @@ class CounterCacheBehaviorTest extends TestCase
|
|
|
public $fixtures = [
|
|
|
'core.counter_cache_categories',
|
|
|
'core.counter_cache_posts',
|
|
|
- 'core.counter_cache_users'
|
|
|
+ 'core.counter_cache_users',
|
|
|
+ 'core.counter_cache_user_category_posts'
|
|
|
];
|
|
|
|
|
|
/**
|
|
|
@@ -78,6 +79,12 @@ class CounterCacheBehaviorTest extends TestCase
|
|
|
'table' => 'counter_cache_posts',
|
|
|
'connection' => $this->connection
|
|
|
]);
|
|
|
+
|
|
|
+ $this->user_category_posts = new Table([
|
|
|
+ 'alias' => 'UserCategoryPosts',
|
|
|
+ 'table' => 'counter_cache_user_category_posts',
|
|
|
+ 'connection' => $this->connection
|
|
|
+ ]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -365,6 +372,35 @@ class CounterCacheBehaviorTest extends TestCase
|
|
|
$this->assertEquals(2, $before->get('post_count'));
|
|
|
$this->assertEquals(3, $after->get('post_count'));
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Tests to see that the binding key configuration is respected.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testBindingKey()
|
|
|
+ {
|
|
|
+ $this->post->hasMany('UserCategoryPosts', [
|
|
|
+ 'bindingKey' => ['category_id', 'user_id'],
|
|
|
+ 'foreignKey' => ['category_id', 'user_id']
|
|
|
+ ]);
|
|
|
+ $this->post->association('UserCategoryPosts')->target($this->user_category_posts);
|
|
|
+ $this->post->addBehavior('CounterCache', [
|
|
|
+ 'UserCategoryPosts' => ['post_count']
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $before = $this->user_category_posts->find()
|
|
|
+ ->where(['user_id' => 1, 'category_id' => 2])
|
|
|
+ ->first();
|
|
|
+ $entity = $this->_getEntity()->set('category_id', 2);
|
|
|
+ $this->post->save($entity);
|
|
|
+ $after = $this->user_category_posts->find()
|
|
|
+ ->where(['user_id' => 1, 'category_id' => 2])
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ $this->assertEquals(1, $before->get('post_count'));
|
|
|
+ $this->assertEquals(2, $after->get('post_count'));
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* Get a new Entity
|