CounterCacheBehaviorTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Test\TestCase\Model\Behavior;
  16. use Cake\Database\Query;
  17. use Cake\Datasource\ConnectionManager;
  18. use Cake\Event\Event;
  19. use Cake\Model\Behavior\CounterCacheBehavior;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * Used for testing counter cache with custom finder
  26. */
  27. class PostTable extends Table {
  28. public function findPublished(Query $query, array $options = []) {
  29. return $query->where(['published' => true]);
  30. }
  31. }
  32. /**
  33. * CounterCacheBehavior test case
  34. */
  35. class CounterCacheBehaviorTest extends TestCase {
  36. /**
  37. * Fixture
  38. *
  39. * @var array
  40. */
  41. public $fixtures = [
  42. 'core.counter_cache_user',
  43. 'core.counter_cache_post'
  44. ];
  45. /**
  46. * setup
  47. *
  48. * @return void
  49. */
  50. public function setUp() {
  51. parent::setUp();
  52. $this->connection = ConnectionManager::get('test');
  53. $this->user = TableRegistry::get('Users', [
  54. 'table' => 'counter_cache_users',
  55. 'connection' => $this->connection
  56. ]);
  57. $this->post = new PostTable([
  58. 'alias' => 'Post',
  59. 'table' => 'counter_cache_posts',
  60. 'connection' => $this->connection
  61. ]);
  62. }
  63. /**
  64. * teardown
  65. *
  66. * @return void
  67. */
  68. public function tearDown() {
  69. unset($this->user, $this->post);
  70. TableRegistry::clear();
  71. }
  72. /**
  73. * Testing simple counter caching when adding a record
  74. *
  75. * @return void
  76. */
  77. public function testAdd() {
  78. $this->post->belongsTo('Users');
  79. $this->post->addBehavior('CounterCache', [
  80. 'Users' => [
  81. 'post_count'
  82. ]
  83. ]);
  84. $before = $this->_getUser();
  85. $entity = $this->_getEntity();
  86. $this->post->save($entity);
  87. $after = $this->_getUser();
  88. $this->assertEquals(2, $before->get('post_count'));
  89. $this->assertEquals(3, $after->get('post_count'));
  90. }
  91. /**
  92. * Testing simple counter caching when adding a record
  93. *
  94. * @return void
  95. */
  96. public function testAddScope() {
  97. $this->post->belongsTo('Users');
  98. $this->post->addBehavior('CounterCache', [
  99. 'Users' => [
  100. 'posts_published' => [
  101. 'conditions' => [
  102. 'published' => true
  103. ]
  104. ]
  105. ]
  106. ]);
  107. $before = $this->_getUser();
  108. $entity = $this->_getEntity()->set('published', true);
  109. $this->post->save($entity);
  110. $after = $this->_getUser();
  111. $this->assertEquals(1, $before->get('posts_published'));
  112. $this->assertEquals(2, $after->get('posts_published'));
  113. }
  114. /**
  115. * Testing simple counter caching when deleting a record
  116. *
  117. * @return void
  118. */
  119. public function testDelete() {
  120. $this->post->belongsTo('Users');
  121. $this->post->addBehavior('CounterCache', [
  122. 'Users' => [
  123. 'post_count'
  124. ]
  125. ]);
  126. $before = $this->_getUser();
  127. $post = $this->post->find('all')->first();
  128. $this->post->delete($post);
  129. $after = $this->_getUser();
  130. $this->assertEquals(2, $before->get('post_count'));
  131. $this->assertEquals(1, $after->get('post_count'));
  132. }
  133. /**
  134. * Testing counter cache with custom find
  135. *
  136. * @return void
  137. */
  138. public function testCustomFind() {
  139. $this->post->belongsTo('Users');
  140. $this->post->addBehavior('CounterCache', [
  141. 'Users' => [
  142. 'posts_published' => [
  143. 'findType' => 'published'
  144. ]
  145. ]
  146. ]);
  147. $before = $this->_getUser();
  148. $entity = $this->_getEntity()->set('published', true);
  149. $this->post->save($entity);
  150. $after = $this->_getUser();
  151. $this->assertEquals(1, $before->get('posts_published'));
  152. $this->assertEquals(2, $after->get('posts_published'));
  153. }
  154. /**
  155. * Testing counter cache with lambda returning number
  156. *
  157. * @return void
  158. */
  159. public function testLambdaNumber() {
  160. $this->post->belongsTo('Users');
  161. $table = $this->post;
  162. $entity = $this->_getEntity();
  163. $this->post->addBehavior('CounterCache', [
  164. 'Users' => [
  165. 'posts_published' => function (Event $orgEvent, Entity $orgEntity, Table $orgTable) use ($entity, $table) {
  166. $this->assertSame($orgTable, $table);
  167. $this->assertSame($orgEntity, $entity);
  168. return 2;
  169. }
  170. ]
  171. ]);
  172. $before = $this->_getUser();
  173. $this->post->save($entity);
  174. $after = $this->_getUser();
  175. $this->assertEquals(1, $before->get('posts_published'));
  176. $this->assertEquals(2, $after->get('posts_published'));
  177. }
  178. /**
  179. * Testing counter cache with lambda returning subqueryn
  180. *
  181. * @return void
  182. */
  183. public function testLambdaSubquery() {
  184. $this->post->belongsTo('Users');
  185. $this->post->addBehavior('CounterCache', [
  186. 'Users' => [
  187. 'posts_published' => function (Event $event, Entity $entity, Table $table) {
  188. $query = new Query($this->connection);
  189. return $query->select(4);
  190. }
  191. ]
  192. ]);
  193. $before = $this->_getUser();
  194. $entity = $this->_getEntity();
  195. $this->post->save($entity);
  196. $after = $this->_getUser();
  197. $this->assertEquals(1, $before->get('posts_published'));
  198. $this->assertEquals(4, $after->get('posts_published'));
  199. }
  200. /**
  201. * Testing multiple counter cache when adding a record
  202. *
  203. * @return void
  204. */
  205. public function testMultiple() {
  206. $this->post->belongsTo('Users');
  207. $this->post->addBehavior('CounterCache', [
  208. 'Users' => [
  209. 'post_count',
  210. 'posts_published' => [
  211. 'conditions' => [
  212. 'published' => true
  213. ]
  214. ]
  215. ]
  216. ]);
  217. $before = $this->_getUser();
  218. $entity = $this->_getEntity()->set('published', true);
  219. $this->post->save($entity);
  220. $after = $this->_getUser();
  221. $this->assertEquals(1, $before->get('posts_published'));
  222. $this->assertEquals(2, $after->get('posts_published'));
  223. $this->assertEquals(2, $before->get('post_count'));
  224. $this->assertEquals(3, $after->get('post_count'));
  225. }
  226. /**
  227. * Get a new Entity
  228. *
  229. * @return Entity
  230. */
  231. protected function _getEntity() {
  232. return new Entity([
  233. 'title' => 'Test 123',
  234. 'user_id' => 1
  235. ]);
  236. }
  237. /**
  238. * Returns entity for user 1
  239. *
  240. * @return Entity
  241. */
  242. protected function _getUser() {
  243. return $this->user->find('all')->where(['id' => 1])->first();
  244. }
  245. }