test_cache_warm_displays_statistics()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Feature\Console\CacheWarm;
6
7
use Gacela\Console\Infrastructure\Command\CacheWarmCommand;
8
use Gacela\Framework\Bootstrap\GacelaConfig;
9
use Gacela\Framework\ClassResolver\Cache\ClassNamePhpCache;
10
use Gacela\Framework\Config\Config;
11
use Gacela\Framework\Gacela;
12
use PHPUnit\Framework\TestCase;
13
use Symfony\Component\Console\Tester\CommandTester;
14
15
use function dirname;
16
use function file_exists;
17
use function unlink;
18
19
final class CacheWarmCommandTest extends TestCase
20
{
21
    private CommandTester $command;
22
23
    private string $cacheFile;
24
25
    protected function setUp(): void
26
    {
27
        Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void {
28
            $config->resetInMemoryCache();
29
            $config->enableFileCache(__DIR__ . '/cache');
30
        });
31
32
        $this->cacheFile = Config::getInstance()->getCacheDir() . DIRECTORY_SEPARATOR . ClassNamePhpCache::FILENAME;
33
34
        // Clean up cache file before test
35
        if (file_exists($this->cacheFile)) {
36
            unlink($this->cacheFile);
37
        }
38
39
        $this->command = new CommandTester(new CacheWarmCommand());
40
    }
41
42
    protected function tearDown(): void
43
    {
44
        // Clean up cache file after test
45
        if (file_exists($this->cacheFile)) {
46
            unlink($this->cacheFile);
47
        }
48
    }
49
50
    public function test_cache_warm_creates_cache_file(): void
51
    {
52
        $this->command->execute([]);
53
54
        $output = $this->command->getDisplay();
55
56
        self::assertStringContainsString('Warming Gacela cache', $output);
57
        self::assertStringContainsString('Cache warming complete!', $output);
58
        self::assertStringContainsString('Modules processed:', $output);
59
        self::assertStringContainsString('Classes resolved:', $output);
60
        self::assertStringContainsString('Classes skipped:', $output);
61
        self::assertStringContainsString('Time taken:', $output);
62
        self::assertStringContainsString('Memory used:', $output);
63
    }
64
65
    public function test_cache_warm_with_clear_option(): void
66
    {
67
        // Ensure cache directory exists
68
        $cacheDir = dirname($this->cacheFile);
69
        if (!is_dir($cacheDir)) {
70
            mkdir($cacheDir, 0777, true);
71
        }
72
73
        // Create a cache file first
74
        file_put_contents($this->cacheFile, '<?php return [];');
75
        self::assertFileExists($this->cacheFile);
76
77
        $this->command->execute(['--clear' => true]);
78
79
        $output = $this->command->getDisplay();
80
81
        self::assertStringContainsString('Cleared existing cache', $output);
82
        self::assertStringContainsString('Cache warming complete!', $output);
83
    }
84
85
    public function test_cache_warm_finds_test_modules(): void
86
    {
87
        $this->command->execute([]);
88
89
        $output = $this->command->getDisplay();
90
91
        // Should find at least the test facade in this directory
92
        self::assertStringContainsString('Found', $output);
93
        self::assertStringContainsString('modules', $output);
94
    }
95
96
    public function test_cache_warm_displays_statistics(): void
97
    {
98
        $this->command->execute([]);
99
100
        $output = $this->command->getDisplay();
101
102
        // Check for statistics
103
        self::assertMatchesRegularExpression('/Modules processed:\s+\d+/', $output);
104
        self::assertMatchesRegularExpression('/Classes resolved:\s+\d+/', $output);
105
        self::assertMatchesRegularExpression('/Classes skipped:\s+\d+/', $output);
106
        self::assertMatchesRegularExpression('/Time taken:\s+[\d.]+\s+seconds/', $output);
107
        self::assertMatchesRegularExpression('/Memory used:\s+[\d.]+\s+(B|KB|MB)/', $output);
108
    }
109
110
    public function test_cache_warm_success_exit_code(): void
111
    {
112
        $exitCode = $this->command->execute([]);
113
114
        self::assertSame(0, $exitCode);
115
    }
116
}
117