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. } else {
  302. return 1;
  303. }
  304. }
  305. ]
  306. ]);
  307. $this->post->save($entity);
  308. $between = $this->_getUser();
  309. $entity->user_id = 2;
  310. $this->post->save($entity);
  311. $afterUser1 = $this->_getUser(1);
  312. $afterUser2 = $this->_getUser(2);
  313. $this->assertEquals(2, $between->get('posts_published'));
  314. $this->assertEquals(1, $afterUser1->get('posts_published'));
  315. $this->assertEquals(2, $afterUser2->get('posts_published'));
  316. }
  317. /**
  318. * Testing counter cache with lambda returning a subquery
  319. *
  320. * @return void
  321. */
  322. public function testLambdaSubquery()
  323. {
  324. $this->post->belongsTo('Users');
  325. $this->post->addBehavior('CounterCache', [
  326. 'Users' => [
  327. 'posts_published' => function (Event $event, EntityInterface $entity, Table $table) {
  328. $query = new Query($this->connection);
  329. return $query->select(4);
  330. }
  331. ]
  332. ]);
  333. $before = $this->_getUser();
  334. $entity = $this->_getEntity();
  335. $this->post->save($entity);
  336. $after = $this->_getUser();
  337. $this->assertEquals(1, $before->get('posts_published'));
  338. $this->assertEquals(4, $after->get('posts_published'));
  339. }
  340. /**
  341. * Testing multiple counter cache when adding a record
  342. *
  343. * @return void
  344. */
  345. public function testMultiple()
  346. {
  347. $this->post->belongsTo('Users');
  348. $this->post->addBehavior('CounterCache', [
  349. 'Users' => [
  350. 'post_count',
  351. 'posts_published' => [
  352. 'conditions' => [
  353. 'published' => true
  354. ]
  355. ]
  356. ]
  357. ]);
  358. $before = $this->_getUser();
  359. $entity = $this->_getEntity()->set('published', true);
  360. $this->post->save($entity);
  361. $after = $this->_getUser();
  362. $this->assertEquals(1, $before->get('posts_published'));
  363. $this->assertEquals(2, $after->get('posts_published'));
  364. $this->assertEquals(2, $before->get('post_count'));
  365. $this->assertEquals(3, $after->get('post_count'));
  366. }
  367. /**
  368. * Tests to see that the binding key configuration is respected.
  369. *
  370. * @return void
  371. */
  372. public function testBindingKey()
  373. {
  374. $this->post->hasMany('UserCategoryPosts', [
  375. 'bindingKey' => ['category_id', 'user_id'],
  376. 'foreignKey' => ['category_id', 'user_id']
  377. ]);
  378. $this->post->getAssociation('UserCategoryPosts')->target($this->userCategoryPosts);
  379. $this->post->addBehavior('CounterCache', [
  380. 'UserCategoryPosts' => ['post_count']
  381. ]);
  382. $before = $this->userCategoryPosts->find()
  383. ->where(['user_id' => 1, 'category_id' => 2])
  384. ->first();
  385. $entity = $this->_getEntity()->set('category_id', 2);
  386. $this->post->save($entity);
  387. $after = $this->userCategoryPosts->find()
  388. ->where(['user_id' => 1, 'category_id' => 2])
  389. ->first();
  390. $this->assertEquals(1, $before->get('post_count'));
  391. $this->assertEquals(2, $after->get('post_count'));
  392. }
  393. /**
  394. * Testing the ignore if dirty option
  395. *
  396. * @return void
  397. */
  398. public function testIgnoreDirty()
  399. {
  400. $this->post->belongsTo('Users');
  401. $this->comment->belongsTo('Users');
  402. $this->post->addBehavior('CounterCache', [
  403. 'Users' => [
  404. 'post_count' => [
  405. 'ignoreDirty' => true
  406. ],
  407. 'comment_count' => [
  408. 'ignoreDirty' => true
  409. ],
  410. ]
  411. ]);
  412. $user = $this->_getUser(1);
  413. $this->assertSame(2, $user->get('post_count'));
  414. $this->assertSame(2, $user->get('comment_count'));
  415. $this->assertSame(1, $user->get('posts_published'));
  416. $post = $this->post->find('all')
  417. ->contain('Users')
  418. ->where(['title' => 'Rock and Roll'])
  419. ->first();
  420. $post = $this->post->patchEntity($post, [
  421. 'posts_published' => true,
  422. 'user' => [
  423. 'id' => 1,
  424. 'post_count' => 10,
  425. 'comment_count' => 10
  426. ]
  427. ]);
  428. $save = $this->post->save($post);
  429. $user = $this->_getUser(1);
  430. $this->assertSame(10, $user->get('post_count'));
  431. $this->assertSame(10, $user->get('comment_count'));
  432. $this->assertSame(1, $user->get('posts_published'));
  433. }
  434. /**
  435. * Testing the ignore if dirty option with just one field set to ignoreDirty
  436. *
  437. * @return void
  438. */
  439. public function testIgnoreDirtyMixed()
  440. {
  441. $this->post->belongsTo('Users');
  442. $this->comment->belongsTo('Users');
  443. $this->post->addBehavior('CounterCache', [
  444. 'Users' => [
  445. 'post_count' => [
  446. 'ignoreDirty' => true
  447. ]
  448. ]
  449. ]);
  450. $user = $this->_getUser(1);
  451. $this->assertSame(2, $user->get('post_count'));
  452. $this->assertSame(2, $user->get('comment_count'));
  453. $this->assertSame(1, $user->get('posts_published'));
  454. $post = $this->post->find('all')
  455. ->contain('Users')
  456. ->where(['title' => 'Rock and Roll'])
  457. ->first();
  458. $post = $this->post->patchEntity($post, [
  459. 'posts_published' => true,
  460. 'user' => [
  461. 'id' => 1,
  462. 'post_count' => 10
  463. ]
  464. ]);
  465. $save = $this->post->save($post);
  466. $user = $this->_getUser(1);
  467. $this->assertSame(10, $user->get('post_count'));
  468. $this->assertSame(2, $user->get('comment_count'));
  469. $this->assertSame(1, $user->get('posts_published'));
  470. }
  471. /**
  472. * Get a new Entity
  473. *
  474. * @return Entity
  475. */
  476. protected function _getEntity()
  477. {
  478. return new Entity([
  479. 'title' => 'Test 123',
  480. 'user_id' => 1
  481. ]);
  482. }
  483. /**
  484. * Returns entity for user
  485. *
  486. * @return Entity
  487. */
  488. protected function _getUser($id = 1)
  489. {
  490. return $this->user->get($id);
  491. }
  492. /**
  493. * Returns entity for category
  494. *
  495. * @return Entity
  496. */
  497. protected function _getCategory($id = 1)
  498. {
  499. return $this->category->find('all')->where(['id' => $id])->first();
  500. }
  501. }