|
| 1 | +<?php |
| 2 | + |
| 3 | +use Eclipse\Common\Helpers\L10nHelper; |
| 4 | +use Eclipse\Common\Support\Callback; |
| 5 | +use Illuminate\Support\Facades\Config; |
| 6 | +use Tests\Support\ConfigLocaleCallback; |
| 7 | +use Tests\Support\InvokableLocaleCallback; |
| 8 | + |
| 9 | +test('it can be created from class method', function () { |
| 10 | + $callback = new Callback(ConfigLocaleCallback::class, 'getLocales'); |
| 11 | + |
| 12 | + expect($callback)->toBeInstanceOf(Callback::class) |
| 13 | + ->and($callback->toCallable())->toBe([ConfigLocaleCallback::class, 'getLocales']) |
| 14 | + ->and($callback())->toBe(['en', 'sl', 'de']); |
| 15 | +}); |
| 16 | + |
| 17 | +test('it throws exception when creating from non-existent class or method', function () { |
| 18 | + expect(fn () => new Callback('NonExistentClass', 'getLocales')) |
| 19 | + ->toThrow(InvalidArgumentException::class, 'Specified class name or method name does not exist'); |
| 20 | + |
| 21 | + expect(fn () => new Callback(ConfigLocaleCallback::class, 'nonExistentMethod')) |
| 22 | + ->toThrow(InvalidArgumentException::class, 'Specified class name or method name does not exist'); |
| 23 | +}); |
| 24 | + |
| 25 | +test('it can be created from invokable class', function () { |
| 26 | + $callback = new Callback(InvokableLocaleCallback::class); |
| 27 | + |
| 28 | + expect($callback)->toBeInstanceOf(Callback::class) |
| 29 | + ->and($callback->toCallable())->toBeInstanceOf(InvokableLocaleCallback::class) |
| 30 | + ->and($callback())->toBe(['en', 'sl', 'de']); |
| 31 | +}); |
| 32 | + |
| 33 | +test('it throws exception when creating from non-invokable class without method', function () { |
| 34 | + expect(fn () => new Callback(ConfigLocaleCallback::class)) |
| 35 | + ->toThrow(InvalidArgumentException::class, 'Specified class is not invokable'); |
| 36 | +}); |
| 37 | + |
| 38 | +test('it can be created from function', function () { |
| 39 | + $callback = new Callback('strtoupper'); |
| 40 | + |
| 41 | + expect($callback)->toBeInstanceOf(Callback::class) |
| 42 | + ->and($callback->toCallable())->toBe('strtoupper') |
| 43 | + ->and($callback('hello'))->toBe('HELLO'); |
| 44 | +}); |
| 45 | + |
| 46 | +test('it throws exception when creating from non-existent function', function () { |
| 47 | + expect(fn () => new Callback('non_existent_function_12345')) |
| 48 | + ->toThrow(InvalidArgumentException::class, 'Specified function name is not callable'); |
| 49 | +}); |
| 50 | + |
| 51 | +test('it throws exception when invoking uninitialized callback', function () { |
| 52 | + $callback = Callback::__set_state([]); |
| 53 | + |
| 54 | + expect(fn () => $callback()) |
| 55 | + ->toThrow(RuntimeException::class, 'Callback is not properly initialized'); |
| 56 | + |
| 57 | + expect(fn () => $callback->toCallable()) |
| 58 | + ->toThrow(RuntimeException::class, 'Callback is not properly initialized'); |
| 59 | +}); |
| 60 | + |
| 61 | +test('it can be serialized and unserialized', function () { |
| 62 | + $callback = new Callback(ConfigLocaleCallback::class, 'getLocales'); |
| 63 | + |
| 64 | + $serialized = serialize($callback); |
| 65 | + $unserialized = unserialize($serialized); |
| 66 | + |
| 67 | + expect($unserialized)->toBeInstanceOf(Callback::class) |
| 68 | + ->and($unserialized())->toBe(['en', 'sl', 'de']); |
| 69 | + |
| 70 | + $functionCallback = new Callback('trim'); |
| 71 | + $unserializedFunction = unserialize(serialize($functionCallback)); |
| 72 | + |
| 73 | + expect($unserializedFunction)->toBeInstanceOf(Callback::class) |
| 74 | + ->and($unserializedFunction(' test '))->toBe('test'); |
| 75 | + |
| 76 | + $invokableCallback = new Callback(InvokableLocaleCallback::class); |
| 77 | + $unserializedInvokable = unserialize(serialize($invokableCallback)); |
| 78 | + |
| 79 | + expect($unserializedInvokable)->toBeInstanceOf(Callback::class) |
| 80 | + ->and($unserializedInvokable())->toBe(['en', 'sl', 'de']); |
| 81 | +}); |
| 82 | + |
| 83 | +test('it supports var_export via set_state for config caching', function () { |
| 84 | + $callback = new Callback(ConfigLocaleCallback::class, 'getLocales'); |
| 85 | + |
| 86 | + $exported = var_export($callback, true); |
| 87 | + $restored = eval("return $exported;"); |
| 88 | + |
| 89 | + expect($restored)->toBeInstanceOf(Callback::class) |
| 90 | + ->and($restored())->toBe(['en', 'sl', 'de']); |
| 91 | + |
| 92 | + $invokableCallback = new Callback(InvokableLocaleCallback::class); |
| 93 | + $exportedInvokable = var_export($invokableCallback, true); |
| 94 | + $restoredInvokable = eval("return $exportedInvokable;"); |
| 95 | + |
| 96 | + expect($restoredInvokable)->toBeInstanceOf(Callback::class) |
| 97 | + ->and($restoredInvokable())->toBe(['en', 'sl', 'de']); |
| 98 | +}); |
| 99 | + |
| 100 | +test('it can be used as a callback for available_locales config value', function () { |
| 101 | + $callback = new Callback(ConfigLocaleCallback::class, 'getLocales'); |
| 102 | + |
| 103 | + Config::set('eclipse-common.available_locales', $callback); |
| 104 | + |
| 105 | + $locales = L10nHelper::getAvailableLocales(); |
| 106 | + |
| 107 | + expect($locales)->toBeArray() |
| 108 | + ->and($locales)->toBe([ |
| 109 | + 'en' => 'en', |
| 110 | + 'sl' => 'sl', |
| 111 | + 'de' => 'de', |
| 112 | + ]); |
| 113 | +}); |
| 114 | + |
| 115 | +test('it can be used as a callback for available_locales when config is cached with var_export', function () { |
| 116 | + $callback = new Callback(ConfigLocaleCallback::class, 'getLocales'); |
| 117 | + |
| 118 | + // Simulate config caching (var_export -> eval) |
| 119 | + $exported = var_export($callback, true); |
| 120 | + $restored = eval("return $exported;"); |
| 121 | + |
| 122 | + Config::set('eclipse-common.available_locales', $restored); |
| 123 | + |
| 124 | + $locales = L10nHelper::getAvailableLocales(); |
| 125 | + $options = L10nHelper::getLocaleOptions(); |
| 126 | + |
| 127 | + expect($locales)->toBe([ |
| 128 | + 'en' => 'en', |
| 129 | + 'sl' => 'sl', |
| 130 | + 'de' => 'de', |
| 131 | + ])->and($options)->toBe([ |
| 132 | + 'en' => 'English (en)', |
| 133 | + 'sl' => 'Slovenian (sl)', |
| 134 | + 'de' => 'German (de)', |
| 135 | + ]); |
| 136 | +}); |
0 commit comments