where(['published' => true]); } } /** * CounterCacheBehavior test case */ class CounterCacheBehaviorTest extends TestCase { /** * Fixture * * @var array */ public $fixtures = [ 'core.counter_cache_user', 'core.counter_cache_post' ]; /** * setup * * @return void */ public function setUp() { parent::setUp(); $this->connection = ConnectionManager::get('test'); $this->user = TableRegistry::get('User', [ 'table' => 'counter_cache_users', 'connection' => $this->connection ]); $this->post = new PostTable([ 'alias' => 'Post', 'table' => 'counter_cache_posts', 'connection' => $this->connection ]); } /** * teardown * * @return void */ public function tearDown() { unset($this->user, $this->post); TableRegistry::clear(); } /** * Testing simple counter caching when adding a record * * @return void */ public function testAdd() { $this->post->belongsTo('User'); $this->post->addBehavior('CounterCache', [ 'User' => [ 'post_count' ] ]); $before = $this->_getUser(); $entity = $this->_getEntity(); $this->post->save($entity); $after = $this->_getUser(); $this->assertEquals(2, $before->get('post_count')); $this->assertEquals(3, $after->get('post_count')); } /** * Testing simple counter caching when adding a record * * @return void */ public function testAddScope() { $this->post->belongsTo('User'); $this->post->addBehavior('CounterCache', [ 'User' => [ 'posts_published' => [ 'conditions' => [ 'published' => true ] ] ] ]); $before = $this->_getUser(); $entity = $this->_getEntity()->set('published', true); $this->post->save($entity); $after = $this->_getUser(); $this->assertEquals(1, $before->get('posts_published')); $this->assertEquals(2, $after->get('posts_published')); } /** * Testing simple counter caching when deleting a record * * @return void */ public function testDelete() { $this->post->belongsTo('User'); $this->post->addBehavior('CounterCache', [ 'User' => [ 'post_count' ] ]); $before = $this->_getUser(); $post = $this->post->find('all')->first(); $this->post->delete($post); $after = $this->_getUser(); $this->assertEquals(2, $before->get('post_count')); $this->assertEquals(1, $after->get('post_count')); } /** * Testing counter cache with custom find * * @return void */ public function testCustomFind() { $this->post->belongsTo('User'); $this->post->addBehavior('CounterCache', [ 'User' => [ 'posts_published' => [ 'findType' => 'published' ] ] ]); $before = $this->_getUser(); $entity = $this->_getEntity()->set('published', true); $this->post->save($entity); $after = $this->_getUser(); $this->assertEquals(1, $before->get('posts_published')); $this->assertEquals(2, $after->get('posts_published')); } /** * Testing counter cache with lambda returning number * * @return void */ public function testLambdaNumber() { $this->post->belongsTo('User'); $table = $this->post; $entity = $this->_getEntity(); $this->post->addBehavior('CounterCache', [ 'User' => [ 'posts_published' => function (Event $orgEvent, Entity $orgEntity, Table $orgTable) use ($entity, $table) { $this->assertSame($orgTable, $table); $this->assertSame($orgEntity, $entity); return 2; } ] ]); $before = $this->_getUser(); $this->post->save($entity); $after = $this->_getUser(); $this->assertEquals(1, $before->get('posts_published')); $this->assertEquals(2, $after->get('posts_published')); } /** * Testing counter cache with lambda returning subqueryn * * @return void */ public function testLambdaSubquery() { $this->post->belongsTo('User'); $this->post->addBehavior('CounterCache', [ 'User' => [ 'posts_published' => function (Event $event, Entity $entity, Table $table) { $query = new Query($this->connection); return $query->select(4); } ] ]); $before = $this->_getUser(); $entity = $this->_getEntity(); $this->post->save($entity); $after = $this->_getUser(); $this->assertEquals(1, $before->get('posts_published')); $this->assertEquals(4, $after->get('posts_published')); } /** * Testing multiple counter cache when adding a record * * @return void */ public function testMultiple() { $this->post->belongsTo('User'); $this->post->addBehavior('CounterCache', [ 'User' => [ 'post_count', 'posts_published' => [ 'conditions' => [ 'published' => true ] ] ] ]); $before = $this->_getUser(); $entity = $this->_getEntity()->set('published', true); $this->post->save($entity); $after = $this->_getUser(); $this->assertEquals(1, $before->get('posts_published')); $this->assertEquals(2, $after->get('posts_published')); $this->assertEquals(2, $before->get('post_count')); $this->assertEquals(3, $after->get('post_count')); } /** * Get a new Entity * * @return Entity */ protected function _getEntity() { return new Entity([ 'title' => 'Test 123', 'user_id' => 66 ]); } /** * Returns entity for user 66 * * @return Entity */ protected function _getUser() { return $this->user->find('all')->where(['id' => 66])->first(); } }