|
| 1 | +<?php namespace CodeIgniter\Commands; |
| 2 | + |
| 3 | +use CodeIgniter\CLI\CLI; |
| 4 | +use CodeIgniter\CLI\CommandRunner; |
| 5 | +use CodeIgniter\Config\Config; |
| 6 | +use CodeIgniter\HTTP\UserAgent; |
| 7 | +use CodeIgniter\Test\CIUnitTestCase; |
| 8 | +use CodeIgniter\Test\Filters\CITestStreamFilter; |
| 9 | +use CodeIgniter\Test\Mock\MockAppConfig; |
| 10 | +use Config\Services; |
| 11 | + |
| 12 | +class ClearCacheTest extends CIUnitTestCase |
| 13 | +{ |
| 14 | + protected $streamFilter; |
| 15 | + protected $result; |
| 16 | + |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + CITestStreamFilter::$buffer = ''; |
| 22 | + $this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter'); |
| 23 | + |
| 24 | + $this->env = new \CodeIgniter\Config\DotEnv(ROOTPATH); |
| 25 | + $this->env->load(); |
| 26 | + |
| 27 | + // Set environment values that would otherwise stop the framework from functioning during tests. |
| 28 | + if (! isset($_SERVER['app.baseURL'])) |
| 29 | + { |
| 30 | + $_SERVER['app.baseURL'] = 'http://example.com'; |
| 31 | + } |
| 32 | + |
| 33 | + $_SERVER['argv'] = [ |
| 34 | + 'spark', |
| 35 | + 'list', |
| 36 | + ]; |
| 37 | + $_SERVER['argc'] = 2; |
| 38 | + CLI::init(); |
| 39 | + |
| 40 | + $this->config = new MockAppConfig(); |
| 41 | + $this->request = new \CodeIgniter\HTTP\IncomingRequest($this->config, new \CodeIgniter\HTTP\URI('https://somwhere.com'), null, new UserAgent()); |
| 42 | + $this->response = new \CodeIgniter\HTTP\Response($this->config); |
| 43 | + $this->logger = Services::logger(); |
| 44 | + $this->runner = new CommandRunner(); |
| 45 | + $this->runner->initController($this->request, $this->response, $this->logger); |
| 46 | + } |
| 47 | + |
| 48 | + public function tearDown(): void |
| 49 | + { |
| 50 | + if (! $this->result) |
| 51 | + { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + stream_filter_remove($this->streamFilter); |
| 56 | + } |
| 57 | + |
| 58 | + public function testClearCacheInvalidHandler() |
| 59 | + { |
| 60 | + $config = config('Cache'); |
| 61 | + $config->handler = 'junk'; |
| 62 | + Config::injectMock('Cache', $config); |
| 63 | + |
| 64 | + $this->runner->index(['cache:clear']); |
| 65 | + $result = CITestStreamFilter::$buffer; |
| 66 | + |
| 67 | + $this->assertStringContainsString('junk is not a valid cache handler.', $result); |
| 68 | + } |
| 69 | +} |
0 commit comments