CounterCacheBehaviorTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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\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_users',
  48. 'core.counter_cache_user_category_posts'
  49. ];
  50. /**
  51. * setup
  52. *
  53. * @return void
  54. */
  55. public function setUp()
  56. {
  57. parent::setUp();
  58. $this->connection = ConnectionManager::get('test');
  59. $this->user = TableRegistry::get('Users', [
  60. 'table' => 'counter_cache_users',
  61. 'connection' => $this->connection
  62. ]);
  63. $this->category = TableRegistry::get('Categories', [
  64. 'table' => 'counter_cache_categories',
  65. 'connection' => $this->connection
  66. ]);
  67. $this->post = new PostTable([
  68. 'alias' => 'Post',
  69. 'table' => 'counter_cache_posts',
  70. 'connection' => $this->connection
  71. ]);
  72. $this->userCategoryPosts = new Table([
  73. 'alias' => 'UserCategoryPosts',
  74. 'table' => 'counter_cache_user_category_posts',
  75. 'connection' => $this->connection
  76. ]);
  77. }
  78. /**
  79. * teardown
  80. *
  81. * @return void
  82. */
  83. public function tearDown()
  84. {
  85. parent::tearDown();
  86. unset($this->user, $this->post);
  87. TableRegistry::clear();
  88. }
  89. /**
  90. * Testing simple counter caching when adding a record
  91. *
  92. * @return void
  93. */
  94. public function testAdd()
  95. {
  96. $this->post->belongsTo('Users');
  97. $this->post->addBehavior('CounterCache', [
  98. 'Users' => [
  99. 'post_count'
  100. ]
  101. ]);
  102. $before = $this->_getUser();
  103. $entity = $this->_getEntity();
  104. $this->post->save($entity);
  105. $after = $this->_getUser();
  106. $this->assertEquals(2, $before->get('post_count'));
  107. $this->assertEquals(3, $after->get('post_count'));
  108. }
  109. /**
  110. * Testing simple counter caching when adding a record
  111. *
  112. * @return void
  113. */
  114. public function testAddScope()
  115. {
  116. $this->post->belongsTo('Users');
  117. $this->post->addBehavior('CounterCache', [
  118. 'Users' => [
  119. 'posts_published' => [
  120. 'conditions' => [
  121. 'published' => true
  122. ]
  123. ]
  124. ]
  125. ]);
  126. $before = $this->_getUser();
  127. $entity = $this->_getEntity()->set('published', true);
  128. $this->post->save($entity);
  129. $after = $this->_getUser();
  130. $this->assertEquals(1, $before->get('posts_published'));
  131. $this->assertEquals(2, $after->get('posts_published'));
  132. }
  133. /**
  134. * Testing simple counter caching when deleting a record
  135. *
  136. * @return void
  137. */
  138. public function testDelete()
  139. {
  140. $this->post->belongsTo('Users');
  141. $this->post->addBehavior('CounterCache', [
  142. 'Users' => [
  143. 'post_count'
  144. ]
  145. ]);
  146. $before = $this->_getUser();
  147. $post = $this->post->find('all')->first();
  148. $this->post->delete($post);
  149. $after = $this->_getUser();
  150. $this->assertEquals(2, $before->get('post_count'));
  151. $this->assertEquals(1, $after->get('post_count'));
  152. }
  153. /**
  154. * Testing update simple counter caching when updating a record association
  155. *
  156. * @return void
  157. */
  158. public function testUpdate()
  159. {
  160. $this->post->belongsTo('Users');
  161. $this->post->belongsTo('Categories');
  162. $this->post->addBehavior('CounterCache', [
  163. 'Users' => [
  164. 'post_count'
  165. ],
  166. 'Categories' => [
  167. 'post_count'
  168. ],
  169. ]);
  170. $user1 = $this->_getUser(1);
  171. $user2 = $this->_getUser(2);
  172. $category1 = $this->_getCategory(1);
  173. $category2 = $this->_getCategory(2);
  174. $post = $this->post->find('all')->first();
  175. $this->assertEquals(2, $user1->get('post_count'));
  176. $this->assertEquals(1, $user2->get('post_count'));
  177. $this->assertEquals(1, $category1->get('post_count'));
  178. $this->assertEquals(2, $category2->get('post_count'));
  179. $entity = $this->post->patchEntity($post, ['user_id' => 2, 'category_id' => 2]);
  180. $this->post->save($entity);
  181. $user1 = $this->_getUser(1);
  182. $user2 = $this->_getUser(2);
  183. $category1 = $this->_getCategory(1);
  184. $category2 = $this->_getCategory(2);
  185. $this->assertEquals(1, $user1->get('post_count'));
  186. $this->assertEquals(2, $user2->get('post_count'));
  187. $this->assertEquals(0, $category1->get('post_count'));
  188. $this->assertEquals(3, $category2->get('post_count'));
  189. }
  190. /**
  191. * Testing counter cache with custom find
  192. *
  193. * @return void
  194. */
  195. public function testCustomFind()
  196. {
  197. $this->post->belongsTo('Users');
  198. $this->post->addBehavior('CounterCache', [
  199. 'Users' => [
  200. 'posts_published' => [
  201. 'finder' => 'published'
  202. ]
  203. ]
  204. ]);
  205. $before = $this->_getUser();
  206. $entity = $this->_getEntity()->set('published', true);
  207. $this->post->save($entity);
  208. $after = $this->_getUser();
  209. $this->assertEquals(1, $before->get('posts_published'));
  210. $this->assertEquals(2, $after->get('posts_published'));
  211. }
  212. /**
  213. * Testing counter cache with lambda returning number
  214. *
  215. * @return void
  216. */
  217. public function testLambdaNumber()
  218. {
  219. $this->post->belongsTo('Users');
  220. $table = $this->post;
  221. $entity = $this->_getEntity();
  222. $this->post->addBehavior('CounterCache', [
  223. 'Users' => [
  224. 'posts_published' => function (Event $orgEvent, EntityInterface $orgEntity, Table $orgTable) use ($entity, $table) {
  225. $this->assertSame($orgTable, $table);
  226. $this->assertSame($orgEntity, $entity);
  227. return 2;
  228. }
  229. ]
  230. ]);
  231. $before = $this->_getUser();
  232. $this->post->save($entity);
  233. $after = $this->_getUser();
  234. $this->assertEquals(1, $before->get('posts_published'));
  235. $this->assertEquals(2, $after->get('posts_published'));
  236. }
  237. /**
  238. * Testing counter cache with lambda returning number and changing of related ID
  239. *
  240. * @return void
  241. */
  242. public function testLambdaNumberUpdate()
  243. {
  244. $this->post->belongsTo('Users');
  245. $table = $this->post;
  246. $entity = $this->_getEntity();
  247. $this->post->addBehavior('CounterCache', [
  248. 'Users' => [
  249. 'posts_published' => function (Event $orgEvent, EntityInterface $orgEntity, Table $orgTable, $original) use ($entity, $table) {
  250. $this->assertSame($orgTable, $table);
  251. $this->assertSame($orgEntity, $entity);
  252. if (!$original) {
  253. return 2;
  254. } else {
  255. return 1;
  256. }
  257. }
  258. ]
  259. ]);
  260. $this->post->save($entity);
  261. $between = $this->_getUser();
  262. $entity->user_id = 2;
  263. $this->post->save($entity);
  264. $afterUser1 = $this->_getUser(1);
  265. $afterUser2 = $this->_getUser(2);
  266. $this->assertEquals(2, $between->get('posts_published'));
  267. $this->assertEquals(1, $afterUser1->get('posts_published'));
  268. $this->assertEquals(2, $afterUser2->get('posts_published'));
  269. }
  270. /**
  271. * Testing counter cache with lambda returning subqueryn
  272. *
  273. * @return void
  274. */
  275. public function testLambdaSubquery()
  276. {
  277. $this->post->belongsTo('Users');
  278. $this->post->addBehavior('CounterCache', [
  279. 'Users' => [
  280. 'posts_published' => function (Event $event, EntityInterface $entity, Table $table) {
  281. $query = new Query($this->connection);
  282. return $query->select(4);
  283. }
  284. ]
  285. ]);
  286. $before = $this->_getUser();
  287. $entity = $this->_getEntity();
  288. $this->post->save($entity);
  289. $after = $this->_getUser();
  290. $this->assertEquals(1, $before->get('posts_published'));
  291. $this->assertEquals(4, $after->get('posts_published'));
  292. }
  293. /**
  294. * Testing multiple counter cache when adding a record
  295. *
  296. * @return void
  297. */
  298. public function testMultiple()
  299. {
  300. $this->post->belongsTo('Users');
  301. $this->post->addBehavior('CounterCache', [
  302. 'Users' => [
  303. 'post_count',
  304. 'posts_published' => [
  305. 'conditions' => [
  306. 'published' => true
  307. ]
  308. ]
  309. ]
  310. ]);
  311. $before = $this->_getUser();
  312. $entity = $this->_getEntity()->set('published', true);
  313. $this->post->save($entity);
  314. $after = $this->_getUser();
  315. $this->assertEquals(1, $before->get('posts_published'));
  316. $this->assertEquals(2, $after->get('posts_published'));
  317. $this->assertEquals(2, $before->get('post_count'));
  318. $this->assertEquals(3, $after->get('post_count'));
  319. }
  320. /**
  321. * Tests to see that the binding key configuration is respected.
  322. *
  323. * @return void
  324. */
  325. public function testBindingKey()
  326. {
  327. $this->post->hasMany('UserCategoryPosts', [
  328. 'bindingKey' => ['category_id', 'user_id'],
  329. 'foreignKey' => ['category_id', 'user_id']
  330. ]);
  331. $this->post->association('UserCategoryPosts')->target($this->userCategoryPosts);
  332. $this->post->addBehavior('CounterCache', [
  333. 'UserCategoryPosts' => ['post_count']
  334. ]);
  335. $before = $this->userCategoryPosts->find()
  336. ->where(['user_id' => 1, 'category_id' => 2])
  337. ->first();
  338. $entity = $this->_getEntity()->set('category_id', 2);
  339. $this->post->save($entity);
  340. $after = $this->userCategoryPosts->find()
  341. ->where(['user_id' => 1, 'category_id' => 2])
  342. ->first();
  343. $this->assertEquals(1, $before->get('post_count'));
  344. $this->assertEquals(2, $after->get('post_count'));
  345. }
  346. /**
  347. * Get a new Entity
  348. *
  349. * @return Entity
  350. */
  351. protected function _getEntity()
  352. {
  353. return new Entity([
  354. 'title' => 'Test 123',
  355. 'user_id' => 1
  356. ]);
  357. }
  358. /**
  359. * Returns entity for user
  360. *
  361. * @return Entity
  362. */
  363. protected function _getUser($id = 1)
  364. {
  365. return $this->user->get($id);
  366. }
  367. /**
  368. * Returns entity for category
  369. *
  370. * @return Entity
  371. */
  372. protected function _getCategory($id = 1)
  373. {
  374. return $this->category->find('all')->where(['id' => $id])->first();
  375. }
  376. }