|
2 | 2 |
|
3 | 3 | namespace WorkOS\Laravel; |
4 | 4 |
|
| 5 | +use WorkOS\Laravel\Services\WorkOSService; |
| 6 | + |
5 | 7 | class WorkOSServiceProviderTest extends LaravelTestCase |
6 | 8 | { |
7 | 9 | protected $app; |
8 | 10 |
|
9 | 11 | protected function setUp(): void |
10 | 12 | { |
11 | 13 | $this->app = $this->setupApplication(); |
| 14 | + $this->setDefaultConfig(); |
| 15 | + $this->setupProvider($this->app); |
12 | 16 | } |
13 | 17 |
|
14 | | - public function testRegisterWorkOSServiceProviderYieldsExpectedConfig() |
| 18 | + protected function setDefaultConfig(array $overrides = []): void |
15 | 19 | { |
16 | | - $this->app["config"]->set("workos.api_key", "pk_secretsauce"); |
17 | | - $this->app["config"]->set("workos.client_id", "client_pizza"); |
18 | | - $this->app["config"]->set("workos.api_base_url", "https://workos-hop.com/"); |
19 | | - $this->setupProvider($this->app); |
| 20 | + $defaults = [ |
| 21 | + 'api_key' => 'pk_test', |
| 22 | + 'client_id' => 'client_test', |
| 23 | + ]; |
| 24 | + |
| 25 | + foreach (array_merge($defaults, $overrides) as $key => $value) { |
| 26 | + $this->app['config']->set("workos.{$key}", $value); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public function test_register_work_os_service_provider_yields_expected_config() |
| 31 | + { |
| 32 | + $this->setDefaultConfig([ |
| 33 | + 'api_key' => 'pk_secretsauce', |
| 34 | + 'client_id' => 'client_pizza', |
| 35 | + 'api_base_url' => 'https://workos-hop.com/', |
| 36 | + ]); |
| 37 | + |
| 38 | + // Resolve the service to trigger lazy initialization |
| 39 | + $this->app->make('workos'); |
| 40 | + |
| 41 | + $this->assertEquals('pk_secretsauce', \WorkOS\WorkOS::getApiKey()); |
| 42 | + $this->assertEquals('client_pizza', \WorkOS\WorkOS::getClientId()); |
| 43 | + $this->assertEquals('https://workos-hop.com/', \WorkOS\WorkOS::getApiBaseUrl()); |
| 44 | + } |
| 45 | + |
| 46 | + public function test_workos_helper_function_returns_work_os_service_instance() |
| 47 | + { |
| 48 | + $this->assertInstanceOf(WorkOSService::class, workos()); |
| 49 | + } |
| 50 | + |
| 51 | + public function test_workos_helper_function_enables_fluent_access() |
| 52 | + { |
| 53 | + $this->assertInstanceOf(\WorkOS\UserManagement::class, workos()->userManagement()); |
| 54 | + } |
| 55 | + |
| 56 | + public function test_it_resolves_service_via_injection_and_configures_sdk() |
| 57 | + { |
| 58 | + $service = $this->app->make(WorkOSService::class); |
| 59 | + |
| 60 | + $this->assertInstanceOf(WorkOSService::class, $service); |
| 61 | + $this->assertSame($service, $this->app->make('workos')); |
| 62 | + $this->assertSame($service, workos()); |
| 63 | + } |
| 64 | + |
| 65 | + public function test_workos_service_resolves_all_supported_services() |
| 66 | + { |
| 67 | + $service = workos(); |
| 68 | + |
| 69 | + $this->assertInstanceOf(\WorkOS\AuditLogs::class, $service->auditLogs()); |
| 70 | + $this->assertInstanceOf(\WorkOS\DirectorySync::class, $service->directorySync()); |
| 71 | + $this->assertInstanceOf(\WorkOS\MFA::class, $service->mfa()); |
| 72 | + $this->assertInstanceOf(\WorkOS\Organizations::class, $service->organizations()); |
| 73 | + $this->assertInstanceOf(\WorkOS\Portal::class, $service->portal()); |
| 74 | + $this->assertInstanceOf(\WorkOS\SSO::class, $service->sso()); |
| 75 | + $this->assertInstanceOf(\WorkOS\UserManagement::class, $service->userManagement()); |
| 76 | + } |
| 77 | + |
| 78 | + public function test_workos_service_caches_service_instances() |
| 79 | + { |
| 80 | + $service = workos(); |
| 81 | + |
| 82 | + $userManagement1 = $service->userManagement(); |
| 83 | + $userManagement2 = $service->userManagement(); |
| 84 | + |
| 85 | + $this->assertSame($userManagement1, $userManagement2); |
| 86 | + } |
| 87 | + |
| 88 | + public function test_workos_service_throws_exception_for_unsupported_service() |
| 89 | + { |
| 90 | + $this->expectException(\InvalidArgumentException::class); |
| 91 | + $this->expectExceptionMessage('WorkOS service [unsupportedService] is not supported.'); |
| 92 | + |
| 93 | + $service = workos(); |
| 94 | + $service->unsupportedService(); |
| 95 | + } |
| 96 | + |
| 97 | + public function test_api_base_url_is_set_when_provided() |
| 98 | + { |
| 99 | + $this->setDefaultConfig(['api_base_url' => 'https://custom-api.workos.com/']); |
| 100 | + |
| 101 | + $this->app->make('workos'); |
20 | 102 |
|
21 | | - $this->assertEquals("pk_secretsauce", \WorkOS\WorkOS::getApiKey()); |
22 | | - $this->assertEquals("client_pizza", \WorkOS\WorkOS::getClientId()); |
23 | | - $this->assertEquals("https://workos-hop.com/", \WorkOS\WorkOS::getApiBaseUrl()); |
| 103 | + $this->assertEquals('https://custom-api.workos.com/', \WorkOS\WorkOS::getApiBaseUrl()); |
24 | 104 | } |
25 | 105 | } |
0 commit comments