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