|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Laravel\Octane\Tests; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Debug\ExceptionHandler; |
| 6 | +use Illuminate\Support\Carbon; |
| 7 | +use Laravel\Octane\FrankenPhp\InvokeTickCallable; |
| 8 | +use Mockery; |
| 9 | + |
| 10 | +class FrankenPhpInvokeTickCallableTest extends TestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @throws \Throwable |
| 14 | + */ |
| 15 | + public function test_callable_is_invoked_when_due(): void |
| 16 | + { |
| 17 | + Carbon::setTestNow($now = now()); |
| 18 | + |
| 19 | + $instance = new InvokeTickCallable( |
| 20 | + 'key', fn () => $_SERVER['__test.invokeTickCallable'] = true, 1, true, |
| 21 | + $cache = Mockery::mock('stdClass'), Mockery::mock(ExceptionHandler::class) |
| 22 | + ); |
| 23 | + |
| 24 | + $cache->shouldReceive('get')->with('tick-key')->andReturn(time() - 100); |
| 25 | + |
| 26 | + $cache->shouldReceive('forever')->once()->with('tick-key', $now->getTimestamp()); |
| 27 | + |
| 28 | + $instance(); |
| 29 | + |
| 30 | + $this->assertTrue($_SERVER['__test.invokeTickCallable'] ?? false); |
| 31 | + |
| 32 | + Carbon::setTestNow(); |
| 33 | + unset($_SERVER['__test.invokeTickCallable']); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @throws \Throwable |
| 38 | + */ |
| 39 | + public function test_callable_is_not_invoked_when_not_due(): void |
| 40 | + { |
| 41 | + Carbon::setTestNow(now()); |
| 42 | + |
| 43 | + $_SERVER['__test.invokeTickCallable'] = false; |
| 44 | + |
| 45 | + $instance = new InvokeTickCallable( |
| 46 | + 'key', fn () => $_SERVER['__test.invokeTickCallable'] = true, 30, true, |
| 47 | + $cache = Mockery::mock('stdClass'), Mockery::mock(ExceptionHandler::class) |
| 48 | + ); |
| 49 | + |
| 50 | + $cache->shouldReceive('get')->with('tick-key')->andReturn(time() - 10); |
| 51 | + |
| 52 | + $cache->shouldReceive('forever')->never(); |
| 53 | + |
| 54 | + $instance(); |
| 55 | + |
| 56 | + $this->assertFalse($_SERVER['__test.invokeTickCallable'] ?? false); |
| 57 | + |
| 58 | + Carbon::setTestNow(); |
| 59 | + unset($_SERVER['__test.invokeTickCallable']); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @throws \Throwable |
| 64 | + */ |
| 65 | + public function test_callable_is_invoked_when_first_run_and_immediate(): void |
| 66 | + { |
| 67 | + Carbon::setTestNow($now = now()); |
| 68 | + |
| 69 | + $instance = new InvokeTickCallable( |
| 70 | + 'key', fn () => $_SERVER['__test.invokeTickCallable'] = true, 1, true, |
| 71 | + $cache = Mockery::mock('stdClass'), Mockery::mock(ExceptionHandler::class) |
| 72 | + ); |
| 73 | + |
| 74 | + $cache->shouldReceive('get')->with('tick-key')->andReturn(null); |
| 75 | + |
| 76 | + $cache->shouldReceive('forever')->once()->with('tick-key', $now->getTimestamp()); |
| 77 | + |
| 78 | + $instance(); |
| 79 | + |
| 80 | + $this->assertTrue($_SERVER['__test.invokeTickCallable'] ?? false); |
| 81 | + |
| 82 | + Carbon::setTestNow(); |
| 83 | + unset($_SERVER['__test.invokeTickCallable']); |
| 84 | + } |
| 85 | + |
| 86 | + public function test_callable_is_not_invoked_when_first_run_and_not_immediate() |
| 87 | + { |
| 88 | + Carbon::setTestNow($now = now()); |
| 89 | + |
| 90 | + $instance = new InvokeTickCallable( |
| 91 | + 'key', fn () => $_SERVER['__test.invokeTickCallable'] = true, 1, false, |
| 92 | + $cache = Mockery::mock('stdClass'), Mockery::mock(ExceptionHandler::class) |
| 93 | + ); |
| 94 | + |
| 95 | + $cache->shouldReceive('get')->with('tick-key')->andReturn(null); |
| 96 | + |
| 97 | + $cache->shouldReceive('forever')->once()->with('tick-key', $now->getTimestamp()); |
| 98 | + |
| 99 | + $instance(); |
| 100 | + |
| 101 | + $this->assertFalse($_SERVER['__test.invokeTickCallable'] ?? false); |
| 102 | + |
| 103 | + Carbon::setTestNow(); |
| 104 | + unset($_SERVER['__test.invokeTickCallable']); |
| 105 | + } |
| 106 | +} |
0 commit comments