CounterCacheBehaviorTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM\Behavior;
  16. use Cake\Database\Query;
  17. use Cake\Datasource\ConnectionManager;
  18. use Cake\Datasource\EntityInterface;
  19. use Cake\Event\Event;
  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. {
  29. public function findPublished(Query $query, array $options)
  30. {
  31. return $query->where(['published' => true]);
  32. }
  33. }
  34. /**
  35. * CounterCacheBehavior test case
  36. */
  37. class CounterCacheBehaviorTest extends TestCase
  38. {
  39. /**
  40. * Fixture
  41. *
  42. * @var array
  43. */
  44. public $fixtures = [
  45. 'core.counter_cache_categories',
  46. 'core.counter_cache_posts',
  47. 'core.counter_cache_comments',
  48. 'core.counter_cache_users',
  49. 'core.counter_cache_user_category_posts'
  50. ];
  51. /**
  52. * setup
  53. *
  54. * @return void
  55. */
  56. public function setUp()
  57. {
  58. parent::setUp();
  59. $this->connection = ConnectionManager::get('test');
  60. $this->user = TableRegistry::get('Users', [
  61. 'table' => 'counter_cache_users',
  62. 'connection' => $this->connection
  63. ]);
  64. $this->category = TableRegistry::get('Categories', [
  65. 'table' => 'counter_cache_categories',
  66. 'connection' => $this->connection
  67. ]);
  68. $this->comment = TableRegistry::get('Comments', [
  69. 'alias' => 'Comment',
  70. 'table' => 'counter_cache_comments',
  71. 'connection' => $this->connection
  72. ]);
  73. $this->post = new PostTable([
  74. 'alias' => 'Post',
  75. 'table' => 'counter_cache_posts',
  76. 'connection' => $this->connection
  77. ]);
  78. $this->userCategoryPosts = new Table([
  79. 'alias' => 'UserCategoryPosts',
  80. 'table' => 'counter_cache_user_category_posts',
  81. 'connection' => $this->connection
  82. ]);
  83. }
  84. /**
  85. * teardown
  86. *
  87. * @return void
  88. */
  89. public function tearDown()
  90. {
  91. parent::tearDown();
  92. unset($this->user, $this->post);
  93. TableRegistry::clear();
  94. }
  95. /**
  96. * Testing simple counter caching when adding a record
  97. *
  98. * @return void
  99. */
  100. public function testAdd()
  101. {
  102. $this->post->belongsTo('Users');
  103. $this->post->addBehavior('CounterCache', [
  104. 'Users' => [
  105. 'post_count'
  106. ]
  107. ]);
  108. $before = $this->_getUser();
  109. $entity = $this->_getEntity();
  110. $this->post->save($entity);
  111. $after = $this->_getUser();
  112. $this->assertEquals(2, $before->get('post_count'));
  113. $this->assertEquals(3, $after->get('post_count'));
  114. }
  115. /**
  116. * Testing simple counter caching when adding a record
  117. *
  118. * @return void
  119. */
  120. public function testAddIgnore()
  121. {
  122. $this->post->belongsTo('Users');
  123. $this->post->addBehavior('CounterCache', [
  124. 'Users' => [
  125. 'post_count'
  126. ]
  127. ]);
  128. $before = $this->_getUser();
  129. $entity = $this->_getEntity();
  130. $this->post->save($entity, ['ignoreCounterCache' => true]);
  131. $after = $this->_getUser();
  132. $this->assertEquals(2, $before->get('post_count'));
  133. $this->assertEquals(2, $after->get('post_count'));
  134. }
  135. /**
  136. * Testing simple counter caching when adding a record
  137. *
  138. * @return void
  139. */
  140. public function testAddScope()
  141. {
  142. $this->post->belongsTo('Users');
  143. $this->post->addBehavior('CounterCache', [
  144. 'Users' => [
  145. 'posts_published' => [
  146. 'conditions' => [
  147. 'published' => true
  148. ]
  149. ]
  150. ]
  151. ]);
  152. $before = $this->_getUser();
  153. $entity = $this->_getEntity()->set('published', true);
  154. $this->post->save($entity);
  155. $after = $this->_getUser();
  156. $this->assertEquals(1, $before->get('posts_published'));
  157. $this->assertEquals(2, $after->get('posts_published'));
  158. }
  159. /**
  160. * Testing simple counter caching when deleting a record
  161. *
  162. * @return void
  163. */
  164. public function testDelete()
  165. {
  166. $this->post->belongsTo('Users');
  167. $this->post->addBehavior('CounterCache', [
  168. 'Users' => [
  169. 'post_count'
  170. ]
  171. ]);
  172. $before = $this->_getUser();
  173. $post = $this->post->find('all')->first();
  174. $this->post->delete($post);
  175. $after = $this->_getUser();
  176. $this->assertEquals(2, $before->get('post_count'));
  177. $this->assertEquals(1, $after->get('post_count'));
  178. }
  179. /**
  180. * Testing simple counter caching when deleting a record
  181. *
  182. * @return void
  183. */
  184. public function testDeleteIgnore()
  185. {
  186. $this->post->belongsTo('Users');
  187. $this->post->addBehavior('CounterCache', [
  188. 'Users' => [
  189. 'post_count'
  190. ]
  191. ]);
  192. $before = $this->_getUser();
  193. $post = $this->post->find('all')
  194. ->first();
  195. $this->post->delete($post, ['ignoreCounterCache' => true]);
  196. $after = $this->_getUser();
  197. $this->assertEquals(2, $before->get('post_count'));
  198. $this->assertEquals(2, $after->get('post_count'));
  199. }
  200. /**
  201. * Testing update simple counter caching when updating a record association
  202. *
  203. * @return void
  204. */
  205. public function testUpdate()
  206. {
  207. $this->post->belongsTo('Users');
  208. $this->post->belongsTo('Categories');
  209. $this->post->addBehavior('CounterCache', [
  210. 'Users' => [
  211. 'post_count'
  212. ],
  213. 'Categories' => [
  214. 'post_count'
  215. ],
  216. ]);
  217. $user1 = $this->_getUser(1);
  218. $user2 = $this->_getUser(2);
  219. $category1 = $this->_getCategory(1);
  220. $category2 = $this->_getCategory(2);
  221. $post = $this->post->find('all')->first();
  222. $this->assertEquals(2, $user1->get('post_count'));
  223. $this->assertEquals(1, $user2->get('post_count'));
  224. $this->assertEquals(1, $category1->get('post_count'));
  225. $this->assertEquals(2, $category2->get('post_count'));
  226. $entity = $this->post->patchEntity($post, ['user_id' => 2, 'category_id' => 2]);
  227. $this->post->save($entity);
  228. $user1 = $this->_getUser(1);
  229. $user2 = $this->_getUser(2);
  230. $category1 = $this->_getCategory(1);
  231. $category2 = $this->_getCategory(2);
  232. $this->assertEquals(1, $user1->get('post_count'));
  233. $this->assertEquals(2, $user2->get('post_count'));
  234. $this->assertEquals(0, $category1->get('post_count'));
  235. $this->assertEquals(3, $category2->get('post_count'));
  236. }
  237. /**
  238. * Testing counter cache with custom find
  239. *
  240. * @return void
  241. */
  242. public function testCustomFind()
  243. {
  244. $this->post->belongsTo('Users');
  245. $this->post->addBehavior('CounterCache', [
  246. 'Users' => [
  247. 'posts_published' => [
  248. 'finder' => 'published'
  249. ]
  250. ]
  251. ]);
  252. $before = $this->_getUser();
  253. $entity = $this->_getEntity()->set('published', true);
  254. $this->post->save($entity);
  255. $after = $this->_getUser();
  256. $this->assertEquals(1, $before->get('posts_published'));
  257. $this->assertEquals(2, $after->get('posts_published'));
  258. }
  259. /**
  260. * Testing counter cache with lambda returning number
  261. *
  262. * @return void
  263. */
  264. public function testLambdaNumber()
  265. {
  266. $this->post->belongsTo('Users');
  267. $table = $this->post;
  268. $entity = $this->_getEntity();
  269. $this->post->addBehavior('CounterCache', [
  270. 'Users' => [
  271. 'posts_published' => function (Event $orgEvent, EntityInterface $orgEntity, Table $orgTable) use ($entity, $table) {
  272. $this->assertSame($orgTable, $table);
  273. $this->assertSame($orgEntity, $entity);
  274. return 2;
  275. }
  276. ]
  277. ]);
  278. $before = $this->_getUser();
  279. $this->post->save($entity);
  280. $after = $this->_getUser();
  281. $this->assertEquals(1, $before->get('posts_published'));
  282. $this->assertEquals(2, $after->get('posts_published'));
  283. }
  284. /**
  285. * Testing counter cache with lambda returning number and changing of related ID
  286. *
  287. * @return void
  288. */
  289. public function testLambdaNumberUpdate()
  290. {
  291. $this->post->belongsTo('Users');
  292. $table = $this->post;
  293. $entity = $this->_getEntity();
  294. $this->post->addBehavior('CounterCache', [
  295. 'Users' => [
  296. 'posts_published' => function (Event $orgEvent, EntityInterface $orgEntity, Table $orgTable, $original) use ($entity, $table) {
  297. $this->assertSame($orgTable, $table);
  298. $this->assertSame($orgEntity, $entity);
  299. if (!$original) {
  300. return 2;
  301. }
  302. return 1;
  303. }
  304. ]
  305. ]);
  306. $this->post->save($entity);
  307. $between = $this->_getUser();
  308. $entity->user_id = 2;
  309. $this->post->save($entity);
  310. $afterUser1 = $this->_getUser(1);
  311. $afterUser2 = $this->_getUser(2);
  312. $this->assertEquals(2, $between->get('posts_published'));
  313. $this->assertEquals(1, $afterUser1->get('posts_published'));
  314. $this->assertEquals(2, $afterUser2->get('posts_published'));
  315. }
  316. /**
  317. * Testing counter cache with lambda returning a subquery
  318. *
  319. * @return void
  320. */
  321. public function testLambdaSubquery()
  322. {
  323. $this->post->belongsTo('Users');
  324. $this->post->addBehavior('CounterCache', [
  325. 'Users' => [
  326. 'posts_published' => function (Event $event, EntityInterface $entity, Table $table) {
  327. $query = new Query($this->connection);
  328. return $query->select(4);
  329. }
  330. ]
  331. ]);
  332. $before = $this->_getUser();
  333. $entity = $this->_getEntity();
  334. $this->post->save($entity);
  335. $after = $this->_getUser();
  336. $this->assertEquals(1, $before->get('posts_published'));
  337. $this->assertEquals(4, $after->get('posts_published'));
  338. }
  339. /**
  340. * Testing multiple counter cache when adding a record
  341. *
  342. * @return void
  343. */
  344. public function testMultiple()
  345. {
  346. $this->post->belongsTo('Users');
  347. $this->post->addBehavior('CounterCache', [
  348. 'Users' => [
  349. 'post_count',
  350. 'posts_published' => [
  351. 'conditions' => [
  352. 'published' => true
  353. ]
  354. ]
  355. ]
  356. ]);
  357. $before = $this->_getUser();
  358. $entity = $this->_getEntity()->set('published', true);
  359. $this->post->save($entity);
  360. $after = $this->_getUser();
  361. $this->assertEquals(1, $before->get('posts_published'));
  362. $this->assertEquals(2, $after->get('posts_published'));
  363. $this->assertEquals(2, $before->get('post_count'));
  364. $this->assertEquals(3, $after->get('post_count'));
  365. }
  366. /**
  367. * Tests to see that the binding key configuration is respected.
  368. *
  369. * @return void
  370. */
  371. public function testBindingKey()
  372. {
  373. $this->post->hasMany('UserCategoryPosts', [
  374. 'bindingKey' => ['category_id', 'user_id'],
  375. 'foreignKey' => ['category_id', 'user_id']
  376. ]);
  377. $this->post->association('UserCategoryPosts')->target($this->userCategoryPosts);
  378. $this->post->addBehavior('CounterCache', [
  379. 'UserCategoryPosts' => ['post_count']
  380. ]);
  381. $before = $this->userCategoryPosts->find()
  382. ->where(['user_id' => 1, 'category_id' => 2])
  383. ->first();
  384. $entity = $this->_getEntity()->set('category_id', 2);
  385. $this->post->save($entity);
  386. $after = $this->userCategoryPosts->find()
  387. ->where(['user_id' => 1, 'category_id' => 2])
  388. ->first();
  389. $this->assertEquals(1, $before->get('post_count'));
  390. $this->assertEquals(2, $after->get('post_count'));
  391. }
  392. /**
  393. * Testing the ignore if dirty option
  394. *
  395. * @return void
  396. */
  397. public function testIgnoreDirty()
  398. {
  399. $this->post->belongsTo('Users');
  400. $this->comment->belongsTo('Users');
  401. $this->post->addBehavior('CounterCache', [
  402. 'Users' => [
  403. 'post_count' => [
  404. 'ignoreDirty' => true
  405. ],
  406. 'comment_count' => [
  407. 'ignoreDirty' => true
  408. ],
  409. ]
  410. ]);
  411. $user = $this->_getUser(1);
  412. $this->assertSame(2, $user->get('post_count'));
  413. $this->assertSame(2, $user->get('comment_count'));
  414. $this->assertSame(1, $user->get('posts_published'));
  415. $post = $this->post->find('all')
  416. ->contain('Users')
  417. ->where(['title' => 'Rock and Roll'])
  418. ->first();
  419. $post = $this->post->patchEntity($post, [
  420. 'posts_published' => true,
  421. 'user' => [
  422. 'id' => 1,
  423. 'post_count' => 10,
  424. 'comment_count' => 10
  425. ]
  426. ]);
  427. $save = $this->post->save($post);
  428. $user = $this->_getUser(1);
  429. $this->assertSame(10, $user->get('post_count'));
  430. $this->assertSame(10, $user->get('comment_count'));
  431. $this->assertSame(1, $user->get('posts_published'));
  432. }
  433. /**
  434. * Testing the ignore if dirty option with just one field set to ignoreDirty
  435. *
  436. * @return void
  437. */
  438. public function testIgnoreDirtyMixed()
  439. {
  440. $this->post->belongsTo('Users');
  441. $this->comment->belongsTo('Users');
  442. $this->post->addBehavior('CounterCache', [
  443. 'Users' => [
  444. 'post_count' => [
  445. 'ignoreDirty' => true
  446. ]
  447. ]
  448. ]);
  449. $user = $this->_getUser(1);
  450. $this->assertSame(2, $user->get('post_count'));
  451. $this->assertSame(2, $user->get('comment_count'));
  452. $this->assertSame(1, $user->get('posts_published'));
  453. $post = $this->post->find('all')
  454. ->contain('Users')
  455. ->where(['title' => 'Rock and Roll'])
  456. ->first();
  457. $post = $this->post->patchEntity($post, [
  458. 'posts_published' => true,
  459. 'user' => [
  460. 'id' => 1,
  461. 'post_count' => 10
  462. ]
  463. ]);
  464. $save = $this->post->save($post);
  465. $user = $this->_getUser(1);
  466. $this->assertSame(10, $user->get('post_count'));
  467. $this->assertSame(2, $user->get('comment_count'));
  468. $this->assertSame(1, $user->get('posts_published'));
  469. }
  470. /**
  471. * Get a new Entity
  472. *
  473. * @return Entity
  474. */
  475. protected function _getEntity()
  476. {
  477. return new Entity([
  478. 'title' => 'Test 123',
  479. 'user_id' => 1
  480. ]);
  481. }
  482. /**
  483. * Returns entity for user
  484. *
  485. * @return Entity
  486. */
  487. protected function _getUser($id = 1)
  488. {
  489. return $this->user->get($id);
  490. }
  491. /**
  492. * Returns entity for category
  493. *
  494. * @return Entity
  495. */
  496. protected function _getCategory($id = 1)
  497. {
  498. return $this->category->find('all')->where(['id' => $id])->first();
  499. }
  500. }