|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Laravel\Mcp\Server\Support\LoggingManager; |
| 6 | +use Laravel\Mcp\Server\Support\SessionStoreManager; |
| 7 | + |
| 8 | +it('resolves LoggingManager from container without producing output', function (): void { |
| 9 | + // Capture any output that might be produced during resolution |
| 10 | + ob_start(); |
| 11 | + |
| 12 | + // Capture any errors/warnings |
| 13 | + $errors = []; |
| 14 | + set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) use (&$errors): bool { |
| 15 | + $errors[] = [ |
| 16 | + 'type' => $errno, |
| 17 | + 'message' => $errstr, |
| 18 | + 'file' => $errfile, |
| 19 | + 'line' => $errline, |
| 20 | + ]; |
| 21 | + |
| 22 | + return true; // Don't execute PHP's internal error handler |
| 23 | + }); |
| 24 | + |
| 25 | + try { |
| 26 | + // First, bind LoggingManager in the container (simulating what we want to test) |
| 27 | + app()->bind(LoggingManager::class, fn ($app): LoggingManager => new LoggingManager( |
| 28 | + $app->make(SessionStoreManager::class) |
| 29 | + )); |
| 30 | + |
| 31 | + // Now resolve it |
| 32 | + $manager = app(LoggingManager::class); |
| 33 | + |
| 34 | + expect($manager)->toBeInstanceOf(LoggingManager::class); |
| 35 | + } finally { |
| 36 | + restore_error_handler(); |
| 37 | + } |
| 38 | + |
| 39 | + $output = ob_get_clean(); |
| 40 | + |
| 41 | + // If there was any output, fail with diagnostic info |
| 42 | + if ($output !== '' && $output !== false) { |
| 43 | + $hexDump = bin2hex(substr($output, 0, 200)); |
| 44 | + throw new \RuntimeException(sprintf( |
| 45 | + "Unexpected output during LoggingManager resolution!\nLength: %d bytes\nContent: %s\nHex: %s", |
| 46 | + strlen($output), |
| 47 | + substr($output, 0, 500), |
| 48 | + $hexDump |
| 49 | + )); |
| 50 | + } |
| 51 | + |
| 52 | + // If there were any errors/warnings, fail with diagnostic info |
| 53 | + if ($errors !== []) { |
| 54 | + throw new \RuntimeException(sprintf( |
| 55 | + "Errors/warnings during LoggingManager resolution:\n%s", |
| 56 | + json_encode($errors, JSON_PRETTY_PRINT) |
| 57 | + )); |
| 58 | + } |
| 59 | + |
| 60 | + expect($output)->toBe(''); |
| 61 | + expect($errors)->toBe([]); |
| 62 | +}); |
| 63 | + |
| 64 | +it('resolves SessionStoreManager from container without producing output', function (): void { |
| 65 | + ob_start(); |
| 66 | + |
| 67 | + $errors = []; |
| 68 | + set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) use (&$errors): bool { |
| 69 | + $errors[] = [ |
| 70 | + 'type' => $errno, |
| 71 | + 'message' => $errstr, |
| 72 | + 'file' => $errfile, |
| 73 | + 'line' => $errline, |
| 74 | + ]; |
| 75 | + |
| 76 | + return true; |
| 77 | + }); |
| 78 | + |
| 79 | + try { |
| 80 | + $manager = app(SessionStoreManager::class); |
| 81 | + expect($manager)->toBeInstanceOf(SessionStoreManager::class); |
| 82 | + } finally { |
| 83 | + restore_error_handler(); |
| 84 | + } |
| 85 | + |
| 86 | + $output = ob_get_clean(); |
| 87 | + |
| 88 | + if ($output !== '' && $output !== false) { |
| 89 | + throw new \RuntimeException(sprintf( |
| 90 | + "Unexpected output during SessionStoreManager resolution!\nLength: %d bytes\nContent: %s", |
| 91 | + strlen($output), |
| 92 | + substr($output, 0, 500) |
| 93 | + )); |
| 94 | + } |
| 95 | + |
| 96 | + if ($errors !== []) { |
| 97 | + throw new \RuntimeException(sprintf( |
| 98 | + "Errors/warnings during SessionStoreManager resolution:\n%s", |
| 99 | + json_encode($errors, JSON_PRETTY_PRINT) |
| 100 | + )); |
| 101 | + } |
| 102 | + |
| 103 | + expect($output)->toBe(''); |
| 104 | +}); |
| 105 | + |
| 106 | +it('resolves Cache repository without producing output', function (): void { |
| 107 | + ob_start(); |
| 108 | + |
| 109 | + $errors = []; |
| 110 | + set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) use (&$errors): bool { |
| 111 | + $errors[] = [ |
| 112 | + 'type' => $errno, |
| 113 | + 'message' => $errstr, |
| 114 | + 'file' => $errfile, |
| 115 | + 'line' => $errline, |
| 116 | + ]; |
| 117 | + |
| 118 | + return true; |
| 119 | + }); |
| 120 | + |
| 121 | + try { |
| 122 | + $cache = app(\Illuminate\Contracts\Cache\Repository::class); |
| 123 | + expect($cache)->toBeInstanceOf(\Illuminate\Contracts\Cache\Repository::class); |
| 124 | + } finally { |
| 125 | + restore_error_handler(); |
| 126 | + } |
| 127 | + |
| 128 | + $output = ob_get_clean(); |
| 129 | + |
| 130 | + if ($output !== '' && $output !== false) { |
| 131 | + throw new \RuntimeException(sprintf( |
| 132 | + "Unexpected output during Cache resolution!\nLength: %d bytes\nContent: %s", |
| 133 | + strlen($output), |
| 134 | + substr($output, 0, 500) |
| 135 | + )); |
| 136 | + } |
| 137 | + |
| 138 | + if ($errors !== []) { |
| 139 | + throw new \RuntimeException(sprintf( |
| 140 | + "Errors/warnings during Cache resolution:\n%s", |
| 141 | + json_encode($errors, JSON_PRETTY_PRINT) |
| 142 | + )); |
| 143 | + } |
| 144 | + |
| 145 | + expect($output)->toBe(''); |
| 146 | +}); |
0 commit comments