diff --git a/lib/AuditLogs.php b/lib/AuditLogs.php index 464e1ac..852c10e 100644 --- a/lib/AuditLogs.php +++ b/lib/AuditLogs.php @@ -88,9 +88,9 @@ public function createExport($organizationId, $rangeStart, $rangeEnd, $actions = }; if (!is_null($actors)) { - $msg = "'actors' is deprecated. Please use 'actorNames' instead'"; + $msg = "'actors' is deprecated. Please use 'actorNames' instead."; - error_log($msg); + trigger_error($msg, E_USER_DEPRECATED); $params["actors"] = $actors; }; diff --git a/lib/MFA.php b/lib/MFA.php index abdb358..2cd6443 100644 --- a/lib/MFA.php +++ b/lib/MFA.php @@ -28,12 +28,12 @@ public function enrollFactor( $enrollPath = "auth/factors/enroll"; if (!isset($type)) { - $msg = "Incomplete arguments: Need to specify a type of factor"; + $msg = "Incomplete arguments: Need to specify a type of factor."; throw new Exception\UnexpectedValueException($msg); } if ($type != "sms" && $type != "totp") { - $msg = "Type Parameter must either be 'sms' or 'totp'"; + $msg = "Type Parameter must either be 'sms' or 'totp'."; throw new Exception\UnexpectedValueException($msg); } @@ -82,15 +82,15 @@ public function challengeFactor( $smsTemplate = null ) { if (!isset($authenticationFactorId)) { - $msg = "Incomplete arguments: 'authentication_factor_id' is a required parameter"; + $msg = "Incomplete arguments: 'authentication_factor_id' is a required parameter."; throw new Exception\UnexpectedValueException($msg); } $challengePath = "auth/factors/{$authenticationFactorId}/challenge"; $params = [ - "sms_template" => $smsTemplate - ]; + "sms_template" => $smsTemplate + ]; $response = Client::request( Client::METHOD_POST, @@ -120,19 +120,19 @@ public function verifyFactor( $code ) { if (!isset($authenticationChallengeId) || !isset($code)) { - $msg = "Incomplete arguments: 'authenticationChallengeId' and 'code' are required parameters"; + $msg = "Incomplete arguments: 'authenticationChallengeId' and 'code' are required parameters."; throw new Exception\UnexpectedValueException($msg); } - $msg = "'verifyFactor' is deprecated. Please use 'verifyChallenge' instead"; + $msg = "'verifyFactor' is deprecated. Please use 'verifyChallenge' instead."; - error_log($msg); + trigger_error($msg, E_USER_DEPRECATED); $response = (new \WorkOS\MFA()) - ->verifyChallenge( - $authenticationChallengeId, - $code - ); + ->verifyChallenge( + $authenticationChallengeId, + $code + ); return $response; } @@ -151,15 +151,15 @@ public function verifyChallenge( $code ) { if (!isset($authenticationChallengeId) || !isset($code)) { - $msg = "Incomplete arguments: 'authenticationChallengeId' and 'code' are required parameters"; + $msg = "Incomplete arguments: 'authenticationChallengeId' and 'code' are required parameters."; throw new Exception\UnexpectedValueException($msg); } $verifyPath = "auth/challenges/{$authenticationChallengeId}/verify"; $params = [ - "code" => $code - ]; + "code" => $code + ]; $response = Client::request( Client::METHOD_POST, diff --git a/lib/Organizations.php b/lib/Organizations.php index 283be2e..992d781 100644 --- a/lib/Organizations.php +++ b/lib/Organizations.php @@ -89,12 +89,20 @@ public function createOrganization( $params = ["name" => $name]; if (isset($domains)) { + $msg = "'domains' is deprecated. Please use 'domain_data' instead."; + + trigger_error($msg, E_USER_DEPRECATED); + $params["domains"] = $domains; } if (isset($domain_data)) { $params["domain_data"] = $domain_data; } if (isset($allowProfilesOutsideOrganization)) { + $msg = "'allowProfilesOutsideOrganization' is deprecated. If you need to allow sign-ins from any email domain, contact support@workos.com."; + + trigger_error($msg, E_USER_DEPRECATED); + $params["allow_profiles_outside_organization"] = $allowProfilesOutsideOrganization; } if (isset($externalId)) { @@ -139,12 +147,20 @@ public function updateOrganization( $params = ["name" => $name]; if (isset($domains)) { + $msg = "'domains' is deprecated. Please use 'domain_data' instead."; + + trigger_error($msg, E_USER_DEPRECATED); + $params["domains"] = $domains; } if (isset($domain_data)) { $params["domain_data"] = $domain_data; } if (isset($allowProfilesOutsideOrganization)) { + $msg = "'allowProfilesOutsideOrganization' is deprecated. If you need to allow sign-ins from any email domain, contact support@workos.com."; + + trigger_error($msg, E_USER_DEPRECATED); + $params["allow_profiles_outside_organization"] = $allowProfilesOutsideOrganization; } if (isset($stripeCustomerId)) { diff --git a/lib/Resource/BaseWorkOSResource.php b/lib/Resource/BaseWorkOSResource.php index 71b0a48..23c7d25 100644 --- a/lib/Resource/BaseWorkOSResource.php +++ b/lib/Resource/BaseWorkOSResource.php @@ -76,7 +76,7 @@ public function __set($key, $value) $this->values[$key] = $value; } - $msg = "{$key} does not exist on " . static::class; + $msg = "{$key} does not exist on " . static::class . "."; throw new \WorkOS\Exception\UnexpectedValueException($msg); } @@ -100,7 +100,7 @@ public function &__get($key) return $this->raw[$key]; } - $msg = "{$key} does not exist on " . static::class; + $msg = "{$key} does not exist on " . static::class . "."; throw new \WorkOS\Exception\UnexpectedValueException($msg); } } diff --git a/lib/Resource/DirectoryUser.php b/lib/Resource/DirectoryUser.php index 78be8ec..37a43a5 100644 --- a/lib/Resource/DirectoryUser.php +++ b/lib/Resource/DirectoryUser.php @@ -65,6 +65,10 @@ class DirectoryUser extends BaseWorkOSResource */ public function primaryEmail() { + $msg = "'primaryEmail' is deprecated. Please use 'email' instead."; + + trigger_error($msg, E_USER_DEPRECATED); + $response = $this; if (count($response->raw["emails"]) == 0) { diff --git a/lib/SSO.php b/lib/SSO.php index 096f581..4aa606d 100644 --- a/lib/SSO.php +++ b/lib/SSO.php @@ -40,15 +40,15 @@ public function getAuthorizationUrl( $authorizationPath = "sso/authorize"; if (!isset($domain) && !isset($provider) && !isset($connection) && !isset($organization)) { - $msg = "Either \$domain, \$provider, \$connection, or \$organization is required"; + $msg = "Either \$domain, \$provider, \$connection, or \$organization is required."; throw new Exception\UnexpectedValueException($msg); } if (isset($domain)) { - $msg = "Domain is being deprecated, please switch to using Connection or Organization ID"; + $msg = "'domain' is being deprecated, please switch to using 'connection' or 'organization'."; - error_log($msg); + trigger_error($msg, E_USER_DEPRECATED); } $params = [ diff --git a/lib/UserManagement.php b/lib/UserManagement.php index e21eae6..d397f5e 100644 --- a/lib/UserManagement.php +++ b/lib/UserManagement.php @@ -33,7 +33,6 @@ class UserManagement * * @return Resource\User */ - public function createUser( $email, $password = null, @@ -627,7 +626,7 @@ public function getAuthorizationUrl( $path = "user_management/authorize"; if (!isset($provider) && !isset($connectionId) && !isset($organizationId)) { - $msg = "Either \$provider, \$connectionId, or \$organizationId is required"; + $msg = "Either \$provider, \$connectionId, or \$organizationId is required."; throw new Exception\UnexpectedValueException($msg); } @@ -640,7 +639,7 @@ public function getAuthorizationUrl( ]; if (isset($provider) && !\in_array($provider, $supportedProviders)) { - $msg = "Only " . implode("','", $supportedProviders) . " providers are supported"; + $msg = "Only " . implode("','", $supportedProviders) . " providers are supported."; throw new Exception\UnexpectedValueException($msg); } @@ -1110,7 +1109,7 @@ public function sendPasswordResetEmail($email, $passwordResetUrl) { $msg = "'sendPasswordResetEmail' is deprecated. Please use 'createPasswordReset' instead. This method will be removed in a future major version."; - error_log($msg); + trigger_error($msg, E_USER_DEPRECATED); $path = "user_management/password_reset/send"; @@ -1224,7 +1223,7 @@ public function sendMagicAuthCode($email) $msg = "'sendMagicAuthCode' is deprecated. Please use 'createMagicAuth' instead. This method will be removed in a future major version."; - error_log($msg); + trigger_error($msg, E_USER_DEPRECATED); $response = Client::request( Client::METHOD_POST, @@ -1261,17 +1260,17 @@ public function getJwksUrl(string $clientId) * Returns the logout URL to end a user's session and redirect to your home page. * * @param string $sessionId The session ID of the user. - * @param string $return_to The URL to redirect to after the user logs out. + * @param string|null $return_to The URL to redirect to after the user logs out. * * @return string */ - public function getLogoutUrl(string $sessionId, string $return_to = null) + public function getLogoutUrl(string $sessionId, ?string $return_to = null) { if (!isset($sessionId) || empty($sessionId)) { throw new Exception\UnexpectedValueException("sessionId must not be empty"); } - $params = [ "session_id" => $sessionId ]; + $params = ["session_id" => $sessionId]; if ($return_to) { $params["return_to"] = $return_to; } diff --git a/lib/WorkOS.php b/lib/WorkOS.php index fbc9536..197e811 100644 --- a/lib/WorkOS.php +++ b/lib/WorkOS.php @@ -48,7 +48,7 @@ public static function getApiKey() return self::$apiKey; } - $msg = "\$apiKey is required"; + $msg = "\$apiKey is required."; throw new \WorkOS\Exception\ConfigurationException($msg); } @@ -76,7 +76,7 @@ public static function getClientId() return self::$clientId; } - $msg = "\$clientId is required"; + $msg = "\$clientId is required."; throw new \WorkOS\Exception\ConfigurationException($msg); } diff --git a/phpunit.xml b/phpunit.xml index 43711ed..4d566a8 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,7 +1,9 @@ - + tests - + \ No newline at end of file diff --git a/tests/TestHelper.php b/tests/TestHelper.php index 2db39c2..52c1adc 100644 --- a/tests/TestHelper.php +++ b/tests/TestHelper.php @@ -2,9 +2,18 @@ namespace WorkOS; +use WorkOS\Client; + trait TestHelper { + /** + * @var \WorkOS\RequestClient\RequestClientInterface + */ protected $defaultRequestClient; + + /** + * @var \PHPUnit\Framework\MockObject\MockObject + */ protected $requestClientMock; protected function setUp(): void @@ -106,4 +115,34 @@ private function prepareRequestMock($method, $url, $headers, $params) static::identicalTo($params) ); } + + /** + * Asserts that a specific deprecation warning is triggered when callable is executed + * + * @param string $expected_warning The expected deprecation message + * @param callable $callable The function or method that should trigger the deprecation + * @return mixed The return value from the callable + */ + protected function assertDeprecationTriggered(string $expected_warning, callable $callable) + { + $caught = false; + + set_error_handler(function ($errno, $errstr) use ($expected_warning, &$caught) { + if ($errno === E_USER_DEPRECATED && $errstr === $expected_warning) { + $caught = true; + return true; + } + return false; + }); + + $result = $callable(); + + restore_error_handler(); + + if (!$caught) { + $this->fail('Expected deprecation warning was not triggered: ' . $expected_warning); + } + + return $result; + } } diff --git a/tests/WorkOS/AuditLogsTest.php b/tests/WorkOS/AuditLogsTest.php index 757b400..8d20014 100644 --- a/tests/WorkOS/AuditLogsTest.php +++ b/tests/WorkOS/AuditLogsTest.php @@ -2,6 +2,7 @@ namespace WorkOS; +use WorkOS\AuditLogs; use PHPUnit\Framework\TestCase; class AuditLogsTest extends TestCase @@ -9,6 +10,10 @@ class AuditLogsTest extends TestCase use TestHelper { setUp as protected traitSetUp; } + /** + * @var AuditLogs + */ + protected $al; protected function setUp(): void { @@ -25,22 +30,23 @@ public function testCreateEvent() $idempotencyKey = null; $organizationId = "org_123"; $auditLogEvent = - [ - "action" => "document.updated", - "occurred_at" => time(), - "version" => 1, - "actor" => - [ - "Id" => "user_123", - "Type" => "user", - "Name" => "User", - ], - "targets" => [ + "action" => "document.updated", + "occurred_at" => time(), + "version" => 1, + "actor" => + [ + "Id" => "user_123", + "Type" => "user", + "Name" => "User", + ], + "targets" => + [ "id" => "team_123", "type" => "team", "name" => "team", - ]]; + ] + ]; $params = [ "organization_id" => $organizationId, "event" => $auditLogEvent @@ -60,7 +66,6 @@ public function testCreateEvent() true, $result ); - $eventStatus = $this->al->createEvent($organizationId, $auditLogEvent); $eventFixture = $this->createEventFixture(); @@ -105,7 +110,11 @@ public function testCreateExport() $result ); - $auditLogExport = $this->al->createExport($organizationId, $rangeStart, $rangeEnd, $actions, $actors, $targets, $actorNames, $actorIds); + $auditLogExport = $this->assertDeprecationTriggered( + "'actors' is deprecated. Please use 'actorNames' instead.", + fn () => $this->al->createExport($organizationId, $rangeStart, $rangeEnd, $actions, $actors, $targets, $actorNames, $actorIds) + ); + $exportFixture = $this->createExportFixture(); $this->assertSame($exportFixture, $auditLogExport->toArray()); diff --git a/tests/WorkOS/DirectorySyncTest.php b/tests/WorkOS/DirectorySyncTest.php index c75dcd9..73fc128 100644 --- a/tests/WorkOS/DirectorySyncTest.php +++ b/tests/WorkOS/DirectorySyncTest.php @@ -2,6 +2,7 @@ namespace WorkOS; +use WorkOS\DirectorySync; use PHPUnit\Framework\TestCase; class DirectorySyncTest extends TestCase @@ -9,6 +10,10 @@ class DirectorySyncTest extends TestCase use TestHelper { setUp as traitSetUp; } + /** + * @var DirectorySync + */ + protected $ds; protected function setUp(): void { @@ -158,7 +163,10 @@ public function testGetUserPrimaryEmail() ); $user = $this->ds->getUser($directoryUser); - $userEmail = $user->primaryEmail(); + $userEmail = $this->assertDeprecationTriggered( + "'primaryEmail' is deprecated. Please use 'email' instead.", + fn () => $user->primaryEmail(), + ); $this->assertSame($userEmail, $expectedEmail); } @@ -180,7 +188,10 @@ public function testGetUserPrimaryEmailNoPrimaryEmail() ); $user = $this->ds->getUser($directoryUser); - $userEmail = $user->primaryEmail(); + $userEmail = $this->assertDeprecationTriggered( + "'primaryEmail' is deprecated. Please use 'email' instead.", + fn () => $user->primaryEmail(), + ); $this->assertSame($userEmail, $expectedEmail); } diff --git a/tests/WorkOS/MFATest.php b/tests/WorkOS/MFATest.php index 423f38c..138dfe8 100644 --- a/tests/WorkOS/MFATest.php +++ b/tests/WorkOS/MFATest.php @@ -2,6 +2,7 @@ namespace WorkOS; +use WorkOS\MFA; use PHPUnit\Framework\TestCase; class MFATest extends TestCase @@ -9,6 +10,10 @@ class MFATest extends TestCase use TestHelper { setUp as traitSetUp; } + /** + * @var MFA + */ + protected $mfa; protected function setUp(): void { @@ -126,7 +131,10 @@ public function testVerifyFactor() $result ); - $verifyFactor = $this->mfa->verifyFactor($authenticationChallengeId, $code); + $verifyFactor = $this->assertDeprecationTriggered( + "'verifyFactor' is deprecated. Please use 'verifyChallenge' instead.", + fn () => $this->mfa->verifyFactor($authenticationChallengeId, $code) + ); $verifyFactorResponseFixture = $this->verifyFactorFixture(); $this->assertSame($verifyFactorResponseFixture, $verifyFactor->toArray()); @@ -244,8 +252,8 @@ private function enrollFactorSmsResponseFixture() "updated_at" => "2022-03-08T23:12:20.157Z", "type" => "sms", "sms" => [ - "phone_number" => "+7208675309" - ] + "phone_number" => "+7208675309" + ] ]); } @@ -258,8 +266,8 @@ private function enrollFactorSmsFixture() "updatedAt" => "2022-03-08T23:12:20.157Z", "type" => "sms", "sms" => [ - "phone_number" => "+7208675309" - ] + "phone_number" => "+7208675309" + ] ]; } @@ -298,8 +306,8 @@ private function verifyFactorResponseFixture() "expires_at" => "2022-02-15T15:36:53.279Z", "authentication_factor_id" => "auth_factor_01FXNWW32G7F3MG8MYK5D1HJJM", ], - "valid" => "true" - ]); + "valid" => "true" + ]); } private function verifyFactorFixture() @@ -313,8 +321,8 @@ private function verifyFactorFixture() "expires_at" => "2022-02-15T15:36:53.279Z", "authentication_factor_id" => "auth_factor_01FXNWW32G7F3MG8MYK5D1HJJM", ], - "valid" => "true" - ]; + "valid" => "true" + ]; } private function verifyChallengeResponseFixture() @@ -328,8 +336,8 @@ private function verifyChallengeResponseFixture() "expires_at" => "2022-02-15T15:36:53.279Z", "authentication_factor_id" => "auth_factor_01FXNWW32G7F3MG8MYK5D1HJJM", ], - "valid" => "true" - ]); + "valid" => "true" + ]); } private function verifyChallengeFixture() @@ -343,8 +351,8 @@ private function verifyChallengeFixture() "expires_at" => "2022-02-15T15:36:53.279Z", "authentication_factor_id" => "auth_factor_01FXNWW32G7F3MG8MYK5D1HJJM", ], - "valid" => "true" - ]; + "valid" => "true" + ]; } private function getFactorResponseFixture() diff --git a/tests/WorkOS/OrganizationsTest.php b/tests/WorkOS/OrganizationsTest.php index 89bbf42..b3a4750 100644 --- a/tests/WorkOS/OrganizationsTest.php +++ b/tests/WorkOS/OrganizationsTest.php @@ -2,6 +2,7 @@ namespace WorkOS; +use WorkOS\Organizations; use PHPUnit\Framework\TestCase; class OrganizationsTest extends TestCase @@ -9,6 +10,10 @@ class OrganizationsTest extends TestCase use TestHelper { setUp as protected traitSetUp; } + /** + * @var Organizations + */ + protected $organizations; protected function setUp(): void { @@ -40,7 +45,10 @@ public function testCreateOrganizationWithDomains() $organization = $this->organizationFixture(); - $response = $this->organizations->createOrganization("Organization Name", array("example.com")); + $response = $this->assertDeprecationTriggered( + "'domains' is deprecated. Please use 'domain_data' instead.", + fn () => $this->organizations->createOrganization("Organization Name", array("example.com")), + ); $this->assertSame($organization, $response->toArray()); } @@ -79,6 +87,32 @@ public function testCreateOrganizationWithDomainData() $this->assertSame($organization, $response->toArray()); } + public function testCreateOrganizationWithAllowProfilesOutsideOrganizationDeprecationNotice() + { + $organizationsPath = "organizations"; + + $result = $this->createOrganizationResponseFixture(); + + $params = [ + "name" => "Organization Name", + "allow_profiles_outside_organization" => true, + ]; + + $this->mockRequest( + Client::METHOD_POST, + $organizationsPath, + null, + $params, + true, + $result + ); + + $this->assertDeprecationTriggered( + "'allowProfilesOutsideOrganization' is deprecated. If you need to allow sign-ins from any email domain, contact support@workos.com.", + fn () => $this->organizations->createOrganization("Organization Name", null, true), + ); + } + public function testUpdateOrganizationWithDomainData() { $organizationsPath = "organizations/org_01EHQMYV6MBK39QC5PZXHY59C3"; @@ -134,8 +168,14 @@ public function testCreateOrganizationSendsIdempotencyKey() $result ); - $response = $this->organizations->createOrganization("Organization Name", array("example.com"), null, $idempotencyKey); - $response2 = $this->organizations->createOrganization("Organization Name", array("example.com"), null, $idempotencyKey); + $response = $this->assertDeprecationTriggered( + "'domains' is deprecated. Please use 'domain_data' instead.", + fn () => $this->organizations->createOrganization("Organization Name", array("example.com"), null, $idempotencyKey), + ); + $response2 = $this->assertDeprecationTriggered( + "'domains' is deprecated. Please use 'domain_data' instead.", + fn () => $this->organizations->createOrganization("Organization Name", array("example.com"), null, $idempotencyKey), + ); $this->assertSame($response2->toArray()["id"], $response->toArray()["id"]); } @@ -217,49 +257,49 @@ private function organizationsResponseFixture() "object" => "list", "data" => [ [ - "object" => "organization", - "id" => "org_01EHQMYV6MBK39QC5PZXHY59C3", - "name" => "Organization Name", - "allow_profiles_outside_organization" => false, - "domains" => [ - [ - "object" => "organization_domain", - "id" => "org_domain_01EHQMYV71XT8H31WE5HF8YK4A", - "domain" => "example.com" - ] - ], - "external_id" => null, - "metadata" => [] + "object" => "organization", + "id" => "org_01EHQMYV6MBK39QC5PZXHY59C3", + "name" => "Organization Name", + "allow_profiles_outside_organization" => false, + "domains" => [ + [ + "object" => "organization_domain", + "id" => "org_domain_01EHQMYV71XT8H31WE5HF8YK4A", + "domain" => "example.com" + ] + ], + "external_id" => null, + "metadata" => [] ], [ - "object" => "organization", - "id" => "org_01EHQMVDTC2GRAHFCCRNTSKH46", - "name" => "example2.com", - "allow_profiles_outside_organization" => false, - "domains" => [ - [ - "object" => "organization_domain", - "id" => "org_domain_01EHQMVDTZVA27PK614ME4YK7V", - "domain" => "example2.com" - ] - ], - "external_id" => null, - "metadata" => [] + "object" => "organization", + "id" => "org_01EHQMVDTC2GRAHFCCRNTSKH46", + "name" => "example2.com", + "allow_profiles_outside_organization" => false, + "domains" => [ + [ + "object" => "organization_domain", + "id" => "org_domain_01EHQMVDTZVA27PK614ME4YK7V", + "domain" => "example2.com" + ] + ], + "external_id" => null, + "metadata" => [] ], [ - "object" => "organization", - "id" => "org_01EGP9Z6RY2J6YE0ZV57CGEXV2", - "name" => "example5.com", - "allow_profiles_outside_organization" => false, - "domains" => [ - [ - "object" => "organization_domain", - "id" => "org_domain_01EGP9Z6S6HVQ5CPD152GJBEA5", - "domain" => "example5.com" - ] - ], - "external_id" => null, - "metadata" => [] + "object" => "organization", + "id" => "org_01EGP9Z6RY2J6YE0ZV57CGEXV2", + "name" => "example5.com", + "allow_profiles_outside_organization" => false, + "domains" => [ + [ + "object" => "organization_domain", + "id" => "org_domain_01EGP9Z6S6HVQ5CPD152GJBEA5", + "domain" => "example5.com" + ] + ], + "external_id" => null, + "metadata" => [] ] ], "list_metadata" => [ diff --git a/tests/WorkOS/PasswordlessTest.php b/tests/WorkOS/PasswordlessTest.php index 11e03f0..e393059 100644 --- a/tests/WorkOS/PasswordlessTest.php +++ b/tests/WorkOS/PasswordlessTest.php @@ -2,6 +2,7 @@ namespace WorkOS; +use WorkOS\Passwordless; use PHPUnit\Framework\TestCase; class PasswordlessTest extends TestCase @@ -9,6 +10,10 @@ class PasswordlessTest extends TestCase use TestHelper { setUp as traitSetUp; } + /** + * @var Passwordless + */ + protected $passwordless; protected function setUp(): void { diff --git a/tests/WorkOS/PortalTest.php b/tests/WorkOS/PortalTest.php index d0b1b58..30d069e 100644 --- a/tests/WorkOS/PortalTest.php +++ b/tests/WorkOS/PortalTest.php @@ -2,6 +2,7 @@ namespace WorkOS; +use WorkOS\Portal; use PHPUnit\Framework\TestCase; class PortalTest extends TestCase @@ -9,13 +10,17 @@ class PortalTest extends TestCase use TestHelper { setUp as protected traitSetUp; } + /** + * @var Portal + */ + protected $portal; protected function setUp(): void { $this->traitSetUp(); $this->withApiKey(); - $this->ap = new Portal(); + $this->portal = new Portal(); } public function testGenerateLinkSSO() @@ -42,7 +47,7 @@ public function testGenerateLinkSSO() $expectation = "https://id.workos.com/portal/launch?secret=secret"; - $response = $this->ap->generateLink("org_01EHZNVPK3SFK441A1RGBFSHRT", "sso"); + $response = $this->portal->generateLink("org_01EHZNVPK3SFK441A1RGBFSHRT", "sso"); $this->assertSame($expectation, $response->link); } @@ -70,7 +75,7 @@ public function testGenerateLinkDSync() $expectation = "https://id.workos.com/portal/launch?secret=secret"; - $response = $this->ap->generateLink("org_01EHZNVPK3SFK441A1RGBFSHRT", "dsync"); + $response = $this->portal->generateLink("org_01EHZNVPK3SFK441A1RGBFSHRT", "dsync"); $this->assertSame($expectation, $response->link); } @@ -98,7 +103,7 @@ public function testGenerateLinkAuditLogs() $expectation = "https://id.workos.com/portal/launch?secret=secret"; - $response = $this->ap->generateLink("org_01EHZNVPK3SFK441A1RGBFSHRT", "audit_logs"); + $response = $this->portal->generateLink("org_01EHZNVPK3SFK441A1RGBFSHRT", "audit_logs"); $this->assertSame($expectation, $response->link); } @@ -126,7 +131,7 @@ public function testGenerateLinkLogStreams() $expectation = "https://id.workos.com/portal/launch?secret=secret"; - $response = $this->ap->generateLink("org_01EHZNVPK3SFK441A1RGBFSHRT", "log_streams"); + $response = $this->portal->generateLink("org_01EHZNVPK3SFK441A1RGBFSHRT", "log_streams"); $this->assertSame($expectation, $response->link); } diff --git a/tests/WorkOS/SSOTest.php b/tests/WorkOS/SSOTest.php index 9baccda..5bfdb1d 100644 --- a/tests/WorkOS/SSOTest.php +++ b/tests/WorkOS/SSOTest.php @@ -2,15 +2,20 @@ namespace WorkOS; -use PHPUnit\Framework\Attributes\DataProvider; +use WorkOS\SSO; use PHPUnit\Framework\TestCase; use WorkOS\Resource\RoleResponse; +use PHPUnit\Framework\Attributes\DataProvider; class SSOTest extends TestCase { use TestHelper { setUp as traitSetUp; } + /** + * @var SSO + */ + protected $sso; protected function setUp(): void { @@ -37,7 +42,6 @@ public function testAuthorizationURLExpectedParams( "client_id" => WorkOS::getClientId(), "response_type" => "code" ]; - if ($domain) { $expectedParams["domain"] = $domain; } @@ -70,7 +74,7 @@ public function testAuthorizationURLExpectedParams( $expectedParams["login_hint"] = $loginHint; } - $authorizationUrl = $this->sso->getAuthorizationUrl( + $fn = fn () => $this->sso->getAuthorizationUrl( $domain, $redirectUri, $state, @@ -80,6 +84,16 @@ public function testAuthorizationURLExpectedParams( $domainHint, $loginHint ); + + if ($domain) { + $authorizationUrl = $this->assertDeprecationTriggered( + "'domain' is being deprecated, please switch to using 'connection' or 'organization'.", + $fn + ); + } else { + $authorizationUrl = $fn(); + } + $paramsString = \parse_url($authorizationUrl, \PHP_URL_QUERY); \parse_str($paramsString, $paramsArray); @@ -260,10 +274,10 @@ private function connectionFixture() return [ "id" => "conn_01E0CG2C820RP4VS50PRJF8YPX", "domains" => [ - [ - "id" => "conn_dom_01E2GCC7Q3KCNEFA2BW9MXR4T5", - "domain" => "workos.com" - ] + [ + "id" => "conn_dom_01E2GCC7Q3KCNEFA2BW9MXR4T5", + "domain" => "workos.com" + ] ], "state" => "active", "status" => "linked", @@ -300,8 +314,8 @@ private function connectionsResponseFixture() "id" => "conn_01E0CG2C820RP4VS50PRJF8YPX", "domains" => [ [ - "id" => "conn_dom_01E2GCC7Q3KCNEFA2BW9MXR4T5", - "domain" => "workos.com" + "id" => "conn_dom_01E2GCC7Q3KCNEFA2BW9MXR4T5", + "domain" => "workos.com" ] ], "state" => "active", diff --git a/tests/WorkOS/UserManagementTest.php b/tests/WorkOS/UserManagementTest.php index 9a2b74a..e14ac06 100644 --- a/tests/WorkOS/UserManagementTest.php +++ b/tests/WorkOS/UserManagementTest.php @@ -2,14 +2,19 @@ namespace WorkOS; -use PHPUnit\Framework\Attributes\DataProvider; +use WorkOS\UserManagement; use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class UserManagementTest extends TestCase { use TestHelper { setUp as traitSetUp; } + /** + * @var UserManagement + */ + protected $userManagement; protected function setUp(): void { @@ -646,7 +651,12 @@ public function testSendPasswordResetEmail() $responseCode ); - $response = $this->userManagement->sendPasswordResetEmail("test@test.com", "https://your-app.com/reset-password"); + // Call the deprecated method + $response = $this->assertDeprecationTriggered( + "'sendPasswordResetEmail' is deprecated. Please use 'createPasswordReset' instead. This method will be removed in a future major version.", + fn () => $this->userManagement->sendPasswordResetEmail("test@test.com", "https://your-app.com/reset-password") + ); + // Test the functionality $this->assertSame(200, $responseCode); $this->assertSame($response, []); diff --git a/tests/WorkOS/WebhookTest.php b/tests/WorkOS/WebhookTest.php index 298bab0..cdcc16d 100644 --- a/tests/WorkOS/WebhookTest.php +++ b/tests/WorkOS/WebhookTest.php @@ -2,6 +2,7 @@ namespace WorkOS; +use WorkOS\Webhook; use PHPUnit\Framework\TestCase; class WebhookTest extends TestCase @@ -9,19 +10,53 @@ class WebhookTest extends TestCase use TestHelper { setUp as protected traitSetUp; } + /** + * @var Webhook + */ + protected $webhook; + + /** + * @var string + */ + protected $payload; + + /** + * @var string + */ + protected $secret; + + /** + * @var int + */ + protected $tolerance; + + /** + * @var int + */ + protected $time; + + /** + * @var string + */ + protected $expectedSignature; + + /** + * @var string + */ + protected $sigHeader; protected function setUp(): void { $this->traitSetUp(); $this->withApiKey(); - $this->ap = new Webhook(); + $this->webhook = new Webhook(); $this->payload = '{"id":"wh_01FGCG6SDYCT5XWZT9CDW0XEB8","data":{"id":"conn_01EHWNC0FCBHZ3BJ7EGKYXK0E6","name":"Foo Corp\'s Connection","state":"active","object":"connection","domains":[{"id":"conn_domain_01EHWNFTAFCF3CQAE5A9Q0P1YB","domain":"foo-corp.com","object":"connection_domain"}],"connection_type":"OktaSAML","organization_id":"org_01EHWNCE74X7JSDV0X3SZ3KJNY"},"event":"connection.activated"}'; $this->secret = 'secret'; $this->tolerance = 180; $this->time = time(); - $decodedBody = utf8_decode($this->payload); + $decodedBody = mb_convert_encoding($this->payload, 'ISO-8859-1', 'UTF-8'); $signedPayload = $this->time . "." . $decodedBody; $this->expectedSignature = hash_hmac("sha256", $signedPayload, $this->secret, false); $this->sigHeader = 't=' . $this->time . ', v1=' . $this->expectedSignature; @@ -29,11 +64,11 @@ protected function setUp(): void public function testConstructEventWebhook() { - $result = $this->generateConnectionFixture(); + $this->generateConnectionFixture(); $expectation = $this->payload; - $response = $this->ap->constructEvent($this->sigHeader, $this->payload, $this->secret, $this->tolerance); + $response = $this->webhook->constructEvent($this->sigHeader, $this->payload, $this->secret, $this->tolerance); $this->assertSame($expectation, json_encode($response)); } @@ -41,7 +76,7 @@ public function testVerifyHeaderWebhook() { $expectation = 'pass'; - $response = $this->ap->verifyHeader($this->sigHeader, $this->payload, $this->secret, $this->tolerance); + $response = $this->webhook->verifyHeader($this->sigHeader, $this->payload, $this->secret, $this->tolerance); $this->assertSame($expectation, $response); } @@ -49,7 +84,7 @@ public function testGetTimeStamp() { $expectation = strval($this->time); - $response = $this->ap->getTimeStamp($this->sigHeader); + $response = $this->webhook->getTimeStamp($this->sigHeader); $this->assertSame($expectation, $response); } @@ -57,7 +92,7 @@ public function testGetSignature() { $expectation = $this->expectedSignature; - $response = $this->ap->getSignature($this->sigHeader); + $response = $this->webhook->getSignature($this->sigHeader); $this->assertSame($expectation, $response); } @@ -68,10 +103,10 @@ private function generateConnectionFixture() return json_encode([ "id" => "conn_01E0CG2C820RP4VS50PRJF8YPX", "domains" => [ - [ - "id" => "conn_dom_01E2GCC7Q3KCNEFA2BW9MXR4T5", - "domain" => "workos.com" - ] + [ + "id" => "conn_dom_01E2GCC7Q3KCNEFA2BW9MXR4T5", + "domain" => "workos.com" + ] ], "state" => "active", "status" => "linked", diff --git a/tests/WorkOS/WidgetsTest.php b/tests/WorkOS/WidgetsTest.php index 6120f3b..1549a9e 100644 --- a/tests/WorkOS/WidgetsTest.php +++ b/tests/WorkOS/WidgetsTest.php @@ -2,6 +2,7 @@ namespace WorkOS; +use WorkOS\Widgets; use PHPUnit\Framework\TestCase; class WidgetsTest extends TestCase @@ -9,13 +10,17 @@ class WidgetsTest extends TestCase use TestHelper { setUp as protected traitSetUp; } + /** + * @var Widgets + */ + protected $widgets; protected function setUp(): void { $this->traitSetUp(); $this->withApiKey(); - $this->ap = new Widgets(); + $this->widgets = new Widgets(); } public function testGenerateLinkSSO() @@ -41,7 +46,7 @@ public function testGenerateLinkSSO() $expectation = "abc123456"; - $response = $this->ap->getToken("org_01EHZNVPK3SFK441A1RGBFSHRT", "user_01EHZNVPK3SFK441A1RGBFSHRT", ["widgets:users-table:manage"]); + $response = $this->widgets->getToken("org_01EHZNVPK3SFK441A1RGBFSHRT", "user_01EHZNVPK3SFK441A1RGBFSHRT", ["widgets:users-table:manage"]); $this->assertSame($expectation, $response->token); }