CounterCacheBehaviorTest.php 11 KB

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