CounterCacheBehaviorTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 http://www.opensource.org/licenses/mit-license.php MIT License
  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_users',
  43. 'core.counter_cache_posts',
  44. 'core.counter_cache_categories',
  45. ];
  46. /**
  47. * setup
  48. *
  49. * @return void
  50. */
  51. public function setUp() {
  52. parent::setUp();
  53. $this->connection = ConnectionManager::get('test');
  54. $this->user = TableRegistry::get('Users', [
  55. 'table' => 'counter_cache_users',
  56. 'connection' => $this->connection
  57. ]);
  58. $this->category = TableRegistry::get('Categories', [
  59. 'table' => 'counter_cache_categories',
  60. 'connection' => $this->connection
  61. ]);
  62. $this->post = new PostTable([
  63. 'alias' => 'Post',
  64. 'table' => 'counter_cache_posts',
  65. 'connection' => $this->connection
  66. ]);
  67. }
  68. /**
  69. * teardown
  70. *
  71. * @return void
  72. */
  73. public function tearDown() {
  74. parent::tearDown();
  75. unset($this->user, $this->post);
  76. TableRegistry::clear();
  77. }
  78. /**
  79. * Testing simple counter caching when adding a record
  80. *
  81. * @return void
  82. */
  83. public function testAdd() {
  84. $this->post->belongsTo('Users');
  85. $this->post->addBehavior('CounterCache', [
  86. 'Users' => [
  87. 'post_count'
  88. ]
  89. ]);
  90. $before = $this->_getUser();
  91. $entity = $this->_getEntity();
  92. $this->post->save($entity);
  93. $after = $this->_getUser();
  94. $this->assertEquals(2, $before->get('post_count'));
  95. $this->assertEquals(3, $after->get('post_count'));
  96. }
  97. /**
  98. * Testing simple counter caching when adding a record
  99. *
  100. * @return void
  101. */
  102. public function testAddScope() {
  103. $this->post->belongsTo('Users');
  104. $this->post->addBehavior('CounterCache', [
  105. 'Users' => [
  106. 'posts_published' => [
  107. 'conditions' => [
  108. 'published' => true
  109. ]
  110. ]
  111. ]
  112. ]);
  113. $before = $this->_getUser();
  114. $entity = $this->_getEntity()->set('published', true);
  115. $this->post->save($entity);
  116. $after = $this->_getUser();
  117. $this->assertEquals(1, $before->get('posts_published'));
  118. $this->assertEquals(2, $after->get('posts_published'));
  119. }
  120. /**
  121. * Testing simple counter caching when deleting a record
  122. *
  123. * @return void
  124. */
  125. public function testDelete() {
  126. $this->post->belongsTo('Users');
  127. $this->post->addBehavior('CounterCache', [
  128. 'Users' => [
  129. 'post_count'
  130. ]
  131. ]);
  132. $before = $this->_getUser();
  133. $post = $this->post->find('all')->first();
  134. $this->post->delete($post);
  135. $after = $this->_getUser();
  136. $this->assertEquals(2, $before->get('post_count'));
  137. $this->assertEquals(1, $after->get('post_count'));
  138. }
  139. /**
  140. * Testing update simple counter caching when updating a record association
  141. *
  142. * @return void
  143. */
  144. public function testUpdate() {
  145. $this->post->belongsTo('Users');
  146. $this->post->belongsTo('Categories');
  147. $this->post->addBehavior('CounterCache', [
  148. 'Users' => [
  149. 'post_count'
  150. ],
  151. 'Categories' => [
  152. 'post_count'
  153. ],
  154. ]);
  155. $user1 = $this->_getUser(1);
  156. $user2 = $this->_getUser(2);
  157. $category1 = $this->_getCategory(1);
  158. $category2 = $this->_getCategory(2);
  159. $post = $this->post->find('all')->first();
  160. $this->assertEquals(2, $user1->get('post_count'));
  161. $this->assertEquals(1, $user2->get('post_count'));
  162. $this->assertEquals(1, $category1->get('post_count'));
  163. $this->assertEquals(2, $category2->get('post_count'));
  164. $entity = $this->post->patchEntity($post, ['user_id' => 2, 'category_id' => 2]);
  165. $this->post->save($entity);
  166. $user1 = $this->_getUser(1);
  167. $user2 = $this->_getUser(2);
  168. $category1 = $this->_getCategory(1);
  169. $category2 = $this->_getCategory(2);
  170. $this->assertEquals(1, $user1->get('post_count'));
  171. $this->assertEquals(2, $user2->get('post_count'));
  172. $this->assertEquals(0, $category1->get('post_count'));
  173. $this->assertEquals(3, $category2->get('post_count'));
  174. }
  175. /**
  176. * Testing counter cache with custom find
  177. *
  178. * @return void
  179. */
  180. public function testCustomFind() {
  181. $this->post->belongsTo('Users');
  182. $this->post->addBehavior('CounterCache', [
  183. 'Users' => [
  184. 'posts_published' => [
  185. 'finder' => 'published'
  186. ]
  187. ]
  188. ]);
  189. $before = $this->_getUser();
  190. $entity = $this->_getEntity()->set('published', true);
  191. $this->post->save($entity);
  192. $after = $this->_getUser();
  193. $this->assertEquals(1, $before->get('posts_published'));
  194. $this->assertEquals(2, $after->get('posts_published'));
  195. }
  196. /**
  197. * Testing counter cache with lambda returning number
  198. *
  199. * @return void
  200. */
  201. public function testLambdaNumber() {
  202. $this->post->belongsTo('Users');
  203. $table = $this->post;
  204. $entity = $this->_getEntity();
  205. $this->post->addBehavior('CounterCache', [
  206. 'Users' => [
  207. 'posts_published' => function (Event $orgEvent, Entity $orgEntity, Table $orgTable) use ($entity, $table) {
  208. $this->assertSame($orgTable, $table);
  209. $this->assertSame($orgEntity, $entity);
  210. return 2;
  211. }
  212. ]
  213. ]);
  214. $before = $this->_getUser();
  215. $this->post->save($entity);
  216. $after = $this->_getUser();
  217. $this->assertEquals(1, $before->get('posts_published'));
  218. $this->assertEquals(2, $after->get('posts_published'));
  219. }
  220. /**
  221. * Testing counter cache with lambda returning subqueryn
  222. *
  223. * @return void
  224. */
  225. public function testLambdaSubquery() {
  226. $this->post->belongsTo('Users');
  227. $this->post->addBehavior('CounterCache', [
  228. 'Users' => [
  229. 'posts_published' => function (Event $event, Entity $entity, Table $table) {
  230. $query = new Query($this->connection);
  231. return $query->select(4);
  232. }
  233. ]
  234. ]);
  235. $before = $this->_getUser();
  236. $entity = $this->_getEntity();
  237. $this->post->save($entity);
  238. $after = $this->_getUser();
  239. $this->assertEquals(1, $before->get('posts_published'));
  240. $this->assertEquals(4, $after->get('posts_published'));
  241. }
  242. /**
  243. * Testing multiple counter cache when adding a record
  244. *
  245. * @return void
  246. */
  247. public function testMultiple() {
  248. $this->post->belongsTo('Users');
  249. $this->post->addBehavior('CounterCache', [
  250. 'Users' => [
  251. 'post_count',
  252. 'posts_published' => [
  253. 'conditions' => [
  254. 'published' => true
  255. ]
  256. ]
  257. ]
  258. ]);
  259. $before = $this->_getUser();
  260. $entity = $this->_getEntity()->set('published', true);
  261. $this->post->save($entity);
  262. $after = $this->_getUser();
  263. $this->assertEquals(1, $before->get('posts_published'));
  264. $this->assertEquals(2, $after->get('posts_published'));
  265. $this->assertEquals(2, $before->get('post_count'));
  266. $this->assertEquals(3, $after->get('post_count'));
  267. }
  268. /**
  269. * Get a new Entity
  270. *
  271. * @return Entity
  272. */
  273. protected function _getEntity() {
  274. return new Entity([
  275. 'title' => 'Test 123',
  276. 'user_id' => 1
  277. ]);
  278. }
  279. /**
  280. * Returns entity for user
  281. *
  282. * @return Entity
  283. */
  284. protected function _getUser($id = 1) {
  285. return $this->user->find('all')->where(['id' => $id])->first();
  286. }
  287. /**
  288. * Returns entity for category
  289. *
  290. * @return Entity
  291. */
  292. protected function _getCategory($id = 1) {
  293. return $this->category->find('all')->where(['id' => $id])->first();
  294. }
  295. }