CounterCacheBehaviorTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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\Model\Behavior;
  16. use Cake\Database\Query;
  17. use Cake\Datasource\ConnectionManager;
  18. use Cake\Event\Event;
  19. use Cake\Model\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. public function findPublished(Query $query, array $options) {
  29. return $query->where(['published' => true]);
  30. }
  31. }
  32. /**
  33. * CounterCacheBehavior test case
  34. */
  35. class CounterCacheBehaviorTest extends TestCase {
  36. /**
  37. * Fixture
  38. *
  39. * @var array
  40. */
  41. public $fixtures = [
  42. 'core.counter_cache_user',
  43. 'core.counter_cache_post'
  44. ];
  45. /**
  46. * setup
  47. *
  48. * @return void
  49. */
  50. public function setUp() {
  51. parent::setUp();
  52. $this->connection = ConnectionManager::get('test');
  53. $this->user = TableRegistry::get('Users', [
  54. 'table' => 'counter_cache_users',
  55. 'connection' => $this->connection
  56. ]);
  57. $this->post = new PostTable([
  58. 'alias' => 'Post',
  59. 'table' => 'counter_cache_posts',
  60. 'connection' => $this->connection
  61. ]);
  62. }
  63. /**
  64. * teardown
  65. *
  66. * @return void
  67. */
  68. public function tearDown() {
  69. parent::tearDown();
  70. unset($this->user, $this->post);
  71. TableRegistry::clear();
  72. }
  73. /**
  74. * Testing simple counter caching when adding a record
  75. *
  76. * @return void
  77. */
  78. public function testAdd() {
  79. $this->post->belongsTo('Users');
  80. $this->post->addBehavior('CounterCache', [
  81. 'Users' => [
  82. 'post_count'
  83. ]
  84. ]);
  85. $before = $this->_getUser();
  86. $entity = $this->_getEntity();
  87. $this->post->save($entity);
  88. $after = $this->_getUser();
  89. $this->assertEquals(2, $before->get('post_count'));
  90. $this->assertEquals(3, $after->get('post_count'));
  91. }
  92. /**
  93. * Testing simple counter caching when adding a record
  94. *
  95. * @return void
  96. */
  97. public function testAddScope() {
  98. $this->post->belongsTo('Users');
  99. $this->post->addBehavior('CounterCache', [
  100. 'Users' => [
  101. 'posts_published' => [
  102. 'conditions' => [
  103. 'published' => true
  104. ]
  105. ]
  106. ]
  107. ]);
  108. $before = $this->_getUser();
  109. $entity = $this->_getEntity()->set('published', true);
  110. $this->post->save($entity);
  111. $after = $this->_getUser();
  112. $this->assertEquals(1, $before->get('posts_published'));
  113. $this->assertEquals(2, $after->get('posts_published'));
  114. }
  115. /**
  116. * Testing simple counter caching when deleting a record
  117. *
  118. * @return void
  119. */
  120. public function testDelete() {
  121. $this->post->belongsTo('Users');
  122. $this->post->addBehavior('CounterCache', [
  123. 'Users' => [
  124. 'post_count'
  125. ]
  126. ]);
  127. $before = $this->_getUser();
  128. $post = $this->post->find('all')->first();
  129. $this->post->delete($post);
  130. $after = $this->_getUser();
  131. $this->assertEquals(2, $before->get('post_count'));
  132. $this->assertEquals(1, $after->get('post_count'));
  133. }
  134. /**
  135. * Testing counter cache with custom find
  136. *
  137. * @return void
  138. */
  139. public function testCustomFind() {
  140. $this->post->belongsTo('Users');
  141. $this->post->addBehavior('CounterCache', [
  142. 'Users' => [
  143. 'posts_published' => [
  144. 'findType' => 'published'
  145. ]
  146. ]
  147. ]);
  148. $before = $this->_getUser();
  149. $entity = $this->_getEntity()->set('published', true);
  150. $this->post->save($entity);
  151. $after = $this->_getUser();
  152. $this->assertEquals(1, $before->get('posts_published'));
  153. $this->assertEquals(2, $after->get('posts_published'));
  154. }
  155. /**
  156. * Testing counter cache with lambda returning number
  157. *
  158. * @return void
  159. */
  160. public function testLambdaNumber() {
  161. $this->post->belongsTo('Users');
  162. $table = $this->post;
  163. $entity = $this->_getEntity();
  164. $this->post->addBehavior('CounterCache', [
  165. 'Users' => [
  166. 'posts_published' => function (Event $orgEvent, Entity $orgEntity, Table $orgTable) use ($entity, $table) {
  167. $this->assertSame($orgTable, $table);
  168. $this->assertSame($orgEntity, $entity);
  169. return 2;
  170. }
  171. ]
  172. ]);
  173. $before = $this->_getUser();
  174. $this->post->save($entity);
  175. $after = $this->_getUser();
  176. $this->assertEquals(1, $before->get('posts_published'));
  177. $this->assertEquals(2, $after->get('posts_published'));
  178. }
  179. /**
  180. * Testing counter cache with lambda returning subqueryn
  181. *
  182. * @return void
  183. */
  184. public function testLambdaSubquery() {
  185. $this->post->belongsTo('Users');
  186. $this->post->addBehavior('CounterCache', [
  187. 'Users' => [
  188. 'posts_published' => function (Event $event, Entity $entity, Table $table) {
  189. $query = new Query($this->connection);
  190. return $query->select(4);
  191. }
  192. ]
  193. ]);
  194. $before = $this->_getUser();
  195. $entity = $this->_getEntity();
  196. $this->post->save($entity);
  197. $after = $this->_getUser();
  198. $this->assertEquals(1, $before->get('posts_published'));
  199. $this->assertEquals(4, $after->get('posts_published'));
  200. }
  201. /**
  202. * Testing multiple counter cache when adding a record
  203. *
  204. * @return void
  205. */
  206. public function testMultiple() {
  207. $this->post->belongsTo('Users');
  208. $this->post->addBehavior('CounterCache', [
  209. 'Users' => [
  210. 'post_count',
  211. 'posts_published' => [
  212. 'conditions' => [
  213. 'published' => true
  214. ]
  215. ]
  216. ]
  217. ]);
  218. $before = $this->_getUser();
  219. $entity = $this->_getEntity()->set('published', true);
  220. $this->post->save($entity);
  221. $after = $this->_getUser();
  222. $this->assertEquals(1, $before->get('posts_published'));
  223. $this->assertEquals(2, $after->get('posts_published'));
  224. $this->assertEquals(2, $before->get('post_count'));
  225. $this->assertEquals(3, $after->get('post_count'));
  226. }
  227. /**
  228. * Get a new Entity
  229. *
  230. * @return Entity
  231. */
  232. protected function _getEntity() {
  233. return new Entity([
  234. 'title' => 'Test 123',
  235. 'user_id' => 1
  236. ]);
  237. }
  238. /**
  239. * Returns entity for user 1
  240. *
  241. * @return Entity
  242. */
  243. protected function _getUser() {
  244. return $this->user->find('all')->where(['id' => 1])->first();
  245. }
  246. }