|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Feature\Source\File; |
| 6 | + |
| 7 | +use Butschster\ContextGenerator\Config\Registry\ConfigRegistryAccessor; |
| 8 | +use Butschster\ContextGenerator\Document\Compiler\DocumentCompiler; |
| 9 | +use Butschster\ContextGenerator\Lib\TreeBuilder\TreeViewConfig; |
| 10 | +use Butschster\ContextGenerator\Source\File\FileSource; |
| 11 | +use PHPUnit\Framework\Attributes\Group; |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests JSON serialization functionality of the FileSource component. |
| 15 | + */ |
| 16 | +#[Group('source')] |
| 17 | +#[Group('file-source')] |
| 18 | +final class FileSourceSerializationTest extends AbstractFileSourceTestCase |
| 19 | +{ |
| 20 | + protected function getConfigPath(): string |
| 21 | + { |
| 22 | + return $this->getFixturesDir('Source/File/basic_file_source.yaml'); |
| 23 | + } |
| 24 | + |
| 25 | + protected function assertConfigItems(DocumentCompiler $compiler, ConfigRegistryAccessor $config): void |
| 26 | + { |
| 27 | + $source = $this->getFileSourceFromConfig($config); |
| 28 | + |
| 29 | + // Test JSON serialization |
| 30 | + $serialized = \json_encode($source); |
| 31 | + $this->assertNotFalse($serialized, 'JSON serialization failed'); |
| 32 | + |
| 33 | + $decoded = \json_decode($serialized, true); |
| 34 | + $this->assertIsArray($decoded); |
| 35 | + |
| 36 | + // Check required fields |
| 37 | + $this->assertArrayHasKey('type', $decoded); |
| 38 | + $this->assertEquals('file', $decoded['type']); |
| 39 | + |
| 40 | + $this->assertArrayHasKey('sourcePaths', $decoded); |
| 41 | + $this->assertArrayHasKey('filePattern', $decoded); |
| 42 | + $this->assertArrayHasKey('treeView', $decoded); |
| 43 | + |
| 44 | + // Create a new FileSource with minimal parameters |
| 45 | + $minimalSource = new FileSource( |
| 46 | + sourcePaths: '/test/path', |
| 47 | + description: 'Minimal source', |
| 48 | + ); |
| 49 | + |
| 50 | + $minimalSerialized = \json_encode($minimalSource); |
| 51 | + $this->assertNotFalse($minimalSerialized, 'Minimal source JSON serialization failed'); |
| 52 | + |
| 53 | + $minimalDecoded = \json_decode($minimalSerialized, true); |
| 54 | + $this->assertIsArray($minimalDecoded); |
| 55 | + |
| 56 | + // Check that optional fields are not included when empty |
| 57 | + $this->assertArrayNotHasKey('path', $minimalDecoded); |
| 58 | + $this->assertArrayNotHasKey('contains', $minimalDecoded); |
| 59 | + $this->assertArrayNotHasKey('notContains', $minimalDecoded); |
| 60 | + $this->assertArrayNotHasKey('size', $minimalDecoded); |
| 61 | + $this->assertArrayNotHasKey('date', $minimalDecoded); |
| 62 | + |
| 63 | + // Test source with all options |
| 64 | + $fullSource = new FileSource( |
| 65 | + sourcePaths: '/test/path', |
| 66 | + description: 'Full source', |
| 67 | + filePattern: '*.php', |
| 68 | + notPath: ['*/vendor/*'], |
| 69 | + path: 'src/Controller', |
| 70 | + contains: 'namespace', |
| 71 | + notContains: 'private', |
| 72 | + size: '> 1K', |
| 73 | + date: 'since yesterday', |
| 74 | + ignoreUnreadableDirs: true, |
| 75 | + treeView: new TreeViewConfig( |
| 76 | + enabled: true, |
| 77 | + showSize: true, |
| 78 | + showLastModified: true, |
| 79 | + ), |
| 80 | + maxFiles: 10, |
| 81 | + ); |
| 82 | + |
| 83 | + $fullSerialized = \json_encode($fullSource); |
| 84 | + $this->assertNotFalse($fullSerialized, 'Full source JSON serialization failed'); |
| 85 | + |
| 86 | + $fullDecoded = \json_decode($fullSerialized, true); |
| 87 | + $this->assertIsArray($fullDecoded); |
| 88 | + |
| 89 | + // Check that all options are serialized |
| 90 | + $this->assertArrayHasKey('path', $fullDecoded); |
| 91 | + $this->assertArrayHasKey('contains', $fullDecoded); |
| 92 | + $this->assertArrayHasKey('notContains', $fullDecoded); |
| 93 | + $this->assertArrayHasKey('size', $fullDecoded); |
| 94 | + $this->assertArrayHasKey('date', $fullDecoded); |
| 95 | + $this->assertArrayHasKey('ignoreUnreadableDirs', $fullDecoded); |
| 96 | + $this->assertArrayHasKey('maxFiles', $fullDecoded); |
| 97 | + $this->assertEquals(10, $fullDecoded['maxFiles']); |
| 98 | + } |
| 99 | +} |
0 commit comments