|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Integration\Concurrency\Console; |
| 4 | + |
| 5 | +use Illuminate\Concurrency\Console\InvokeSerializedClosureCommand; |
| 6 | +use Illuminate\Contracts\Console\Kernel; |
| 7 | +use Illuminate\Support\Facades\Artisan; |
| 8 | +use Laravel\SerializableClosure\SerializableClosure; |
| 9 | +use Orchestra\Testbench\TestCase; |
| 10 | +use RuntimeException; |
| 11 | +use Symfony\Component\Console\Output\BufferedOutput; |
| 12 | + |
| 13 | +class CustomParameterException extends RuntimeException |
| 14 | +{ |
| 15 | + public function __construct( |
| 16 | + public string $customParam, |
| 17 | + string $message = '', |
| 18 | + ) { |
| 19 | + parent::__construct($message ?: "Exception with param: {$customParam}"); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +class InvokeSerializedClosureCommandTest extends TestCase |
| 24 | +{ |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + parent::setUp(); |
| 28 | + |
| 29 | + $this->app[Kernel::class]->registerCommand(new InvokeSerializedClosureCommand); |
| 30 | + } |
| 31 | + |
| 32 | + public function testItCanInvokeSerializedClosureFromArgument() |
| 33 | + { |
| 34 | + // Create a simple closure and serialize it |
| 35 | + $closure = fn () => 'Hello, World!'; |
| 36 | + $serialized = serialize(new SerializableClosure($closure)); |
| 37 | + |
| 38 | + // Create a new output buffer |
| 39 | + $output = new BufferedOutput; |
| 40 | + |
| 41 | + // Call the command with the serialized closure |
| 42 | + Artisan::call('invoke-serialized-closure', [ |
| 43 | + 'code' => $serialized, |
| 44 | + ], $output); |
| 45 | + |
| 46 | + // Get the output and decode it |
| 47 | + $result = json_decode($output->fetch(), true); |
| 48 | + |
| 49 | + // Verify the result |
| 50 | + $this->assertTrue($result['successful']); |
| 51 | + $this->assertEquals('Hello, World!', unserialize($result['result'])); |
| 52 | + } |
| 53 | + |
| 54 | + public function testItCanInvokeSerializedClosureFromEnvironment() |
| 55 | + { |
| 56 | + // Create a simple closure and serialize it |
| 57 | + $closure = fn () => 'From Environment'; |
| 58 | + $serialized = serialize(new SerializableClosure($closure)); |
| 59 | + |
| 60 | + // Set the environment variable |
| 61 | + $_SERVER['LARAVEL_INVOKABLE_CLOSURE'] = $serialized; |
| 62 | + |
| 63 | + // Create a new output buffer |
| 64 | + $output = new BufferedOutput; |
| 65 | + |
| 66 | + // Call the command without arguments |
| 67 | + Artisan::call('invoke-serialized-closure', [], $output); |
| 68 | + |
| 69 | + // Get the output and decode it |
| 70 | + $result = json_decode($output->fetch(), true); |
| 71 | + |
| 72 | + // Verify the result |
| 73 | + $this->assertTrue($result['successful']); |
| 74 | + $this->assertEquals('From Environment', unserialize($result['result'])); |
| 75 | + |
| 76 | + // Clean up |
| 77 | + unset($_SERVER['LARAVEL_INVOKABLE_CLOSURE']); |
| 78 | + } |
| 79 | + |
| 80 | + public function testItReturnsNullWhenNoClosureIsProvided() |
| 81 | + { |
| 82 | + // Create a new output buffer |
| 83 | + $output = new BufferedOutput; |
| 84 | + |
| 85 | + // Call the command without arguments |
| 86 | + Artisan::call('invoke-serialized-closure', [], $output); |
| 87 | + |
| 88 | + // Get the output and decode it |
| 89 | + $result = json_decode($output->fetch(), true); |
| 90 | + |
| 91 | + // Verify the result |
| 92 | + $this->assertTrue($result['successful']); |
| 93 | + $this->assertNull(unserialize($result['result'])); |
| 94 | + } |
| 95 | + |
| 96 | + public function testItHandlesExceptionsGracefully() |
| 97 | + { |
| 98 | + // Create a closure that throws an exception |
| 99 | + $closure = fn () => throw new RuntimeException('Test exception'); |
| 100 | + $serialized = serialize(new SerializableClosure($closure)); |
| 101 | + |
| 102 | + // Create a new output buffer |
| 103 | + $output = new BufferedOutput; |
| 104 | + |
| 105 | + // Call the command with the serialized closure |
| 106 | + Artisan::call('invoke-serialized-closure', [ |
| 107 | + 'code' => $serialized, |
| 108 | + ], $output); |
| 109 | + |
| 110 | + // Get the output and decode it |
| 111 | + $result = json_decode($output->fetch(), true); |
| 112 | + |
| 113 | + // Verify the exception was caught |
| 114 | + $this->assertFalse($result['successful']); |
| 115 | + $this->assertEquals('RuntimeException', $result['exception']); |
| 116 | + $this->assertEquals('Test exception', $result['message']); |
| 117 | + } |
| 118 | + |
| 119 | + public function testItHandlesCustomExceptionWithParameters() |
| 120 | + { |
| 121 | + // Create a closure that throws an exception with parameters |
| 122 | + $closure = fn () => throw new CustomParameterException('Test param'); |
| 123 | + $serialized = serialize(new SerializableClosure($closure)); |
| 124 | + |
| 125 | + // Create a new output buffer |
| 126 | + $output = new BufferedOutput; |
| 127 | + |
| 128 | + // Call the command with the serialized closure |
| 129 | + Artisan::call('invoke-serialized-closure', [ |
| 130 | + 'code' => $serialized, |
| 131 | + ], $output); |
| 132 | + |
| 133 | + // Get the output and decode it |
| 134 | + $result = json_decode($output->fetch(), true); |
| 135 | + |
| 136 | + // Verify the exception was caught and parameters were captured |
| 137 | + $this->assertFalse($result['successful']); |
| 138 | + $this->assertArrayHasKey('parameters', $result); |
| 139 | + $this->assertEquals('Test param', $result['parameters']['customParam'] ?? null); |
| 140 | + } |
| 141 | +} |
0 commit comments