Passed
Push — feat/cache-warming ( 7624ee )
by Chema
05:29
created

test_cache_warm_creates_cache_file()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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