|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace KLP\KlpMcpServer\Tests\Services\ResourceService; |
| 4 | + |
| 5 | +use KLP\KlpMcpServer\Services\ResourceService\Examples\McpDocumentationResource; |
| 6 | +use KLP\KlpMcpServer\Services\ResourceService\ResourceInterface; |
| 7 | +use PHPUnit\Framework\Attributes\Small; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 10 | + |
| 11 | +#[Small] |
| 12 | +class McpDocumentationResourceTest extends TestCase |
| 13 | +{ |
| 14 | + private KernelInterface $kernel; |
| 15 | + private McpDocumentationResource $resource; |
| 16 | + private string $baseDir; |
| 17 | + |
| 18 | + protected function setUp(): void |
| 19 | + { |
| 20 | + $this->baseDir = '/tmp/mcp-docs'; |
| 21 | + |
| 22 | + // Create mock kernel |
| 23 | + $this->kernel = $this->createMock(KernelInterface::class); |
| 24 | + $this->kernel->method('getProjectDir') |
| 25 | + ->willReturn('/tmp'); |
| 26 | + |
| 27 | + // Create the resource |
| 28 | + $this->resource = new McpDocumentationResource($this->kernel); |
| 29 | + |
| 30 | + // Use reflection to set the baseDir property |
| 31 | + $reflectionClass = new \ReflectionClass(McpDocumentationResource::class); |
| 32 | + $property = $reflectionClass->getProperty('baseDir'); |
| 33 | + $property->setAccessible(true); |
| 34 | + $property->setValue($this->resource, $this->baseDir); |
| 35 | + |
| 36 | + // Create test directory and files |
| 37 | + if (!file_exists($this->baseDir)) { |
| 38 | + mkdir($this->baseDir, 0777, true); |
| 39 | + } |
| 40 | + |
| 41 | + // Create test markdown files |
| 42 | + file_put_contents($this->baseDir . '/test1.md', "# Test Document 1\nThis is a test document."); |
| 43 | + file_put_contents($this->baseDir . '/test2.md', "# Test Document 2\nThis is another test document."); |
| 44 | + } |
| 45 | + |
| 46 | + protected function tearDown(): void |
| 47 | + { |
| 48 | + // Clean up test files |
| 49 | + if (file_exists($this->baseDir . '/test1.md')) { |
| 50 | + unlink($this->baseDir . '/test1.md'); |
| 51 | + } |
| 52 | + if (file_exists($this->baseDir . '/test2.md')) { |
| 53 | + unlink($this->baseDir . '/test2.md'); |
| 54 | + } |
| 55 | + if (file_exists($this->baseDir)) { |
| 56 | + rmdir($this->baseDir); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Tests that the URI template is correctly formatted for documentation files |
| 62 | + */ |
| 63 | + public function test_get_uri_template(): void |
| 64 | + { |
| 65 | + $this->assertSame("file:/docs/{filename}.md", $this->resource->getUriTemplate()); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Tests that the resource name is correctly returned |
| 70 | + */ |
| 71 | + public function test_get_name(): void |
| 72 | + { |
| 73 | + $this->assertSame("documentation.md", $this->resource->getName()); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Tests that the description contains expected content including references to test files |
| 78 | + */ |
| 79 | + public function test_get_description(): void |
| 80 | + { |
| 81 | + $description = $this->resource->getDescription(); |
| 82 | + $this->assertStringContainsString("The MCP Documentation resources", $description); |
| 83 | + $this->assertStringContainsString("test1", $description); |
| 84 | + $this->assertStringContainsString("test2", $description); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Tests that the MIME type is correctly returned as plain text |
| 89 | + */ |
| 90 | + public function test_get_mime_type(): void |
| 91 | + { |
| 92 | + $this->assertSame("text/plain", $this->resource->getMimeType()); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Tests that a resource can be correctly retrieved and contains expected properties |
| 97 | + */ |
| 98 | + public function test_get_resource(): void |
| 99 | + { |
| 100 | + $uri = "file:/docs/test1.md"; |
| 101 | + $resource = $this->resource->getResource($uri); |
| 102 | + |
| 103 | + $this->assertInstanceOf(ResourceInterface::class, $resource); |
| 104 | + $this->assertSame($uri, $resource->getUri()); |
| 105 | + $this->assertSame("test1.md", $resource->getName()); |
| 106 | + $this->assertSame("# Test Document 1", $resource->getDescription()); |
| 107 | + $this->assertStringContainsString("text/", $resource->getMimeType()); |
| 108 | + $this->assertSame("# Test Document 1\nThis is a test document.", $resource->getData()); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Tests that null is returned when attempting to retrieve a resource with an invalid URI |
| 113 | + */ |
| 114 | + public function test_get_resource_with_invalid_uri(): void |
| 115 | + { |
| 116 | + $uri = "file:/docs/nonexistent.md"; |
| 117 | + $resource = $this->resource->getResource($uri); |
| 118 | + |
| 119 | + $this->assertNull($resource); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Tests that the resource existence check correctly identifies valid and invalid resources |
| 124 | + */ |
| 125 | + public function test_resource_exists(): void |
| 126 | + { |
| 127 | + $this->assertTrue($this->resource->resourceExists("file:/docs/test1.md")); |
| 128 | + $this->assertTrue($this->resource->resourceExists("file:/docs/test2.md")); |
| 129 | + $this->assertFalse($this->resource->resourceExists("file:/docs/nonexistent.md")); |
| 130 | + $this->assertFalse($this->resource->resourceExists("invalid-uri")); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Tests that filenames are correctly extracted from URIs |
| 135 | + */ |
| 136 | + public function test_get_filename_from_uri(): void |
| 137 | + { |
| 138 | + // Use reflection to access private method |
| 139 | + $reflectionClass = new \ReflectionClass(McpDocumentationResource::class); |
| 140 | + $method = $reflectionClass->getMethod('getFilenameFromUri'); |
| 141 | + $method->setAccessible(true); |
| 142 | + |
| 143 | + $this->assertSame("test1", $method->invoke($this->resource, "file:/docs/test1.md")); |
| 144 | + $this->assertSame("test2", $method->invoke($this->resource, "file:/docs/test2.md")); |
| 145 | + $this->assertNull($method->invoke($this->resource, "invalid-uri")); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Tests that the MIME type is correctly guessed from a file |
| 150 | + */ |
| 151 | + public function test_guess_mime_type(): void |
| 152 | + { |
| 153 | + // Use reflection to access protected method |
| 154 | + $reflectionClass = new \ReflectionClass(McpDocumentationResource::class); |
| 155 | + $method = $reflectionClass->getMethod('guessMimeType'); |
| 156 | + $method->setAccessible(true); |
| 157 | + |
| 158 | + $mimeType = $method->invoke($this->resource, $this->baseDir . '/test1.md'); |
| 159 | + $this->assertStringContainsString("text/", $mimeType); |
| 160 | + } |
| 161 | +} |
0 commit comments