|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Support\Facades\Auth; |
| 4 | +use Mockery; |
| 5 | +use Soap\LaravelWorkflowProcess\GuardFunctions\Authenticated; |
| 6 | + |
| 7 | +afterEach(function () { |
| 8 | + // Ensure that all mock expectations are met and clean up |
| 9 | + Mockery::close(); |
| 10 | +}); |
| 11 | + |
| 12 | +test('compile returns authenticated("web") when no guard argument is provided', function () { |
| 13 | + $guardFunction = new Authenticated; |
| 14 | + $compiled = $guardFunction->compile(); |
| 15 | + expect($compiled)->toBe('authenticated("web")'); |
| 16 | +}); |
| 17 | + |
| 18 | +test('compile returns authenticated("custom") when a custom guard is provided', function () { |
| 19 | + $guardFunction = new Authenticated; |
| 20 | + $compiled = $guardFunction->compile('custom'); |
| 21 | + expect($compiled)->toBe('authenticated("custom")'); |
| 22 | +}); |
| 23 | + |
| 24 | +test('evaluate returns true when the default guard is authenticated', function () { |
| 25 | + $guardFunction = new Authenticated; |
| 26 | + |
| 27 | + // Create a mock for the default "web" guard that returns true for check(). |
| 28 | + $guardMock = Mockery::mock(); |
| 29 | + $guardMock->shouldReceive('check')->once()->andReturn(true); |
| 30 | + |
| 31 | + // Expect the default guard to be used. |
| 32 | + Auth::shouldReceive('guard') |
| 33 | + ->once() |
| 34 | + ->with('web') |
| 35 | + ->andReturn($guardMock); |
| 36 | + |
| 37 | + $result = $guardFunction->evaluate([]); |
| 38 | + expect($result)->toBeTrue(); |
| 39 | +}); |
| 40 | + |
| 41 | +test('evaluate returns false when the custom guard is not authenticated', function () { |
| 42 | + $guardFunction = new Authenticated; |
| 43 | + |
| 44 | + // Create a mock for the "custom" guard that returns false for check(). |
| 45 | + $guardMock = Mockery::mock(); |
| 46 | + $guardMock->shouldReceive('check')->once()->andReturn(false); |
| 47 | + |
| 48 | + // Expect the custom guard to be used. |
| 49 | + Auth::shouldReceive('guard') |
| 50 | + ->once() |
| 51 | + ->with('custom') |
| 52 | + ->andReturn($guardMock); |
| 53 | + |
| 54 | + $result = $guardFunction->evaluate([], 'custom'); |
| 55 | + expect($result)->toBeFalse(); |
| 56 | +}); |
0 commit comments