CounterCacheCommandTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP Project
  13. * @since 5.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Command;
  17. use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
  18. use Cake\Datasource\ConnectionManager;
  19. use Cake\ORM\Table;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * CounterCacheCommandTest class
  23. */
  24. class CounterCacheCommandTest extends TestCase
  25. {
  26. use ConsoleIntegrationTestTrait;
  27. protected array $fixtures = [
  28. 'core.CounterCacheComments',
  29. 'core.CounterCacheUsers',
  30. ];
  31. protected Table $Comments;
  32. protected Table $Users;
  33. public function setUp(): void
  34. {
  35. parent::setUp();
  36. $this->setAppNamespace();
  37. $connection = ConnectionManager::get('test');
  38. $this->getTableLocator()->get('Users', [
  39. 'table' => 'counter_cache_users',
  40. 'connection' => $connection,
  41. ]);
  42. $comments = $this->getTableLocator()->get('Comments', [
  43. 'table' => 'counter_cache_comments',
  44. 'connection' => $connection,
  45. ]);
  46. $comments->belongsTo('Users', [
  47. 'foreignKey' => 'user_id',
  48. ]);
  49. $comments->addBehavior('CounterCache', [
  50. 'Users' => ['comment_count'],
  51. ]);
  52. }
  53. public function testExecute(): void
  54. {
  55. $this->exec('counter_cache Comments');
  56. $this->assertExitSuccess();
  57. $this->assertOutputContains('Counter cache updated successfully.');
  58. }
  59. public function testExecuteWithOptions(): void
  60. {
  61. $this->exec('counter_cache Comments --assoc Users --limit 1 --page 1');
  62. $this->assertExitSuccess();
  63. }
  64. public function testExecuteFailure(): void
  65. {
  66. $this->exec('counter_cache Users');
  67. $this->assertExitError();
  68. $this->assertErrorContains('The specified model does not have the CounterCache behavior attached.');
  69. }
  70. }