From 0f596821df40df652008b2c2ece54912427c9ec4 Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Mon, 16 Sep 2019 17:09:44 -0700 Subject: [PATCH] Add lazy-load Management methods --- src/API/Management.php | 266 ++++++++++++++++++ tests/API/Management/BlacklistsTest.php | 9 + tests/API/Management/ClientGrantsTest.php | 10 +- tests/API/Management/ClientsTest.php | 9 + .../API/Management/ConnectionsMockedTest.php | 12 +- .../Management/EmailTemplatesMockedTest.php | 18 +- tests/API/Management/EmailsMockedTest.php | 12 +- tests/API/Management/GrantsMockedTest.php | 9 + tests/API/Management/LogsTest.php | 9 + tests/API/Management/ResourceServersTest.php | 9 + tests/API/Management/RolesMockedTest.php | 14 +- tests/API/Management/RulesTest.php | 9 + tests/API/Management/UsersMockedTest.php | 9 + 13 files changed, 382 insertions(+), 13 deletions(-) diff --git a/src/API/Management.php b/src/API/Management.php index 40d28d1a0..831671aba 100644 --- a/src/API/Management.php +++ b/src/API/Management.php @@ -209,6 +209,272 @@ public function __construct($token, $domain, $guzzleOptions = [], $returnType = $this->usersByEmail = new UsersByEmail($this->apiClient); } + /** + * Return an instance of the Blacklists class. + * + * @return Blacklists + */ + public function blacklists() + { + if (! $this->blacklists instanceof Blacklists) { + $this->blacklists = new Blacklists($this->apiClient); + } + + return $this->blacklists; + } + + /** + * Return an instance of the Clients class. + * + * @return Clients + */ + public function clients() + { + if (! $this->clients instanceof Clients) { + $this->clients = new Clients($this->apiClient); + } + + return $this->clients; + } + + /** + * Return an instance of the ClientGrants class. + * + * @return ClientGrants + */ + public function clientGrants() + { + if (! $this->client_grants instanceof ClientGrants) { + $this->client_grants = new ClientGrants($this->apiClient); + } + + return $this->client_grants; + } + + /** + * Return an instance of the Connections class. + * + * @return Connections + */ + public function connections() + { + if (! $this->connections instanceof Connections) { + $this->connections = new Connections($this->apiClient); + } + + return $this->connections; + } + + /** + * Return an instance of the DeviceCredentials class. + * + * @return DeviceCredentials + */ + public function deviceCredentials() + { + if (! $this->deviceCredentials instanceof DeviceCredentials) { + $this->deviceCredentials = new DeviceCredentials($this->apiClient); + } + + return $this->deviceCredentials; + } + + /** + * Return an instance of the Emails class. + * + * @return Emails + */ + public function emails() + { + if (! $this->emails instanceof Emails) { + $this->emails = new Emails($this->apiClient); + } + + return $this->emails; + } + + /** + * Return an instance of the EmailTemplates class. + * + * @return EmailTemplates + */ + public function emailTemplates() + { + if (! $this->emailTemplates instanceof EmailTemplates) { + $this->emailTemplates = new EmailTemplates($this->apiClient); + } + + return $this->emailTemplates; + } + + /** + * Return an instance of the Grants class. + * + * @return Grants + */ + public function grants() + { + if (! $this->grants instanceof Grants) { + $this->grants = new Grants($this->apiClient); + } + + return $this->grants; + } + + /** + * Return an instance of the Jobs class. + * + * @return Jobs + */ + public function jobs() + { + if (! $this->jobs instanceof Jobs) { + $this->jobs = new Jobs($this->apiClient); + } + + return $this->jobs; + } + + /** + * Return an instance of the Logs class. + * + * @return Logs + */ + public function logs() + { + if (! $this->logs instanceof Logs) { + $this->logs = new Logs($this->apiClient); + } + + return $this->logs; + } + + /** + * Return an instance of the Roles class. + * + * @return Roles + */ + public function roles() + { + if (! $this->roles instanceof Roles) { + $this->roles = new Roles($this->apiClient); + } + + return $this->roles; + } + + /** + * Return an instance of the Rules class. + * + * @return Rules + */ + public function rules() + { + if (! $this->rules instanceof Rules) { + $this->rules = new Rules($this->apiClient); + } + + return $this->rules; + } + + /** + * Return an instance of the ResourceServers class. + * + * @return ResourceServers + */ + public function resourceServers() + { + if (! $this->resource_servers instanceof ResourceServers) { + $this->resource_servers = new ResourceServers($this->apiClient); + } + + return $this->resource_servers; + } + + /** + * Return an instance of the Stats class. + * + * @return Stats + */ + public function stats() + { + if (! $this->stats instanceof Stats) { + $this->stats = new Stats($this->apiClient); + } + + return $this->stats; + } + + /** + * Return an instance of the Tenants class. + * + * @return Tenants + */ + public function tenants() + { + if (! $this->tenants instanceof Tenants) { + $this->tenants = new Tenants($this->apiClient); + } + + return $this->tenants; + } + + /** + * Return an instance of the Tickets class. + * + * @return Tickets + */ + public function tickets() + { + if (! $this->tickets instanceof Tickets) { + $this->tickets = new Tickets($this->apiClient); + } + + return $this->tickets; + } + + /** + * Return an instance of the UserBlocks class. + * + * @return UserBlocks + */ + public function userBlocks() + { + if (! $this->userBlocks instanceof UserBlocks) { + $this->userBlocks = new UserBlocks($this->apiClient); + } + + return $this->userBlocks; + } + + /** + * Return an instance of the Users class. + * + * @return Users + */ + public function users() + { + if (! $this->users instanceof Users) { + $this->users = new Users($this->apiClient); + } + + return $this->users; + } + + /** + * Return an instance of the UsersByEmail class. + * + * @return UsersByEmail + */ + public function usersByEmail() + { + if (! $this->usersByEmail instanceof UsersByEmail) { + $this->usersByEmail = new UsersByEmail($this->apiClient); + } + + return $this->usersByEmail; + } + protected function setApiClient() { $apiDomain = "https://{$this->domain}"; diff --git a/tests/API/Management/BlacklistsTest.php b/tests/API/Management/BlacklistsTest.php index d04e6fa23..eb08b289d 100644 --- a/tests/API/Management/BlacklistsTest.php +++ b/tests/API/Management/BlacklistsTest.php @@ -77,6 +77,15 @@ public function testThatBlacklistRequestIsFormedProperly() $this->assertEquals( 'application/json', $headers['Content-Type'][0] ); } + public function testThatMethodAndPropertyReturnSameClass() + { + $api = new Management(uniqid(), uniqid()); + $this->assertInstanceOf( Management\Blacklists::class, $api->blacklists ); + $this->assertInstanceOf( Management\Blacklists::class, $api->blacklists() ); + $api->blacklists = null; + $this->assertInstanceOf( Management\Blacklists::class, $api->blacklists() ); + } + /** * @throws \Auth0\SDK\Exception\ApiException */ diff --git a/tests/API/Management/ClientGrantsTest.php b/tests/API/Management/ClientGrantsTest.php index 631c1d7b7..bfcde2c1d 100644 --- a/tests/API/Management/ClientGrantsTest.php +++ b/tests/API/Management/ClientGrantsTest.php @@ -59,7 +59,15 @@ public static function setUpBeforeClass() public function setUp() { parent::setUp(); - sleep(2); + } + + public function testThatMethodAndPropertyReturnSameClass() + { + $api = new Management(uniqid(), uniqid()); + $this->assertInstanceOf( Management\ClientGrants::class, $api->client_grants ); + $this->assertInstanceOf( Management\ClientGrants::class, $api->clientGrants() ); + $api->client_grants = null; + $this->assertInstanceOf( Management\ClientGrants::class, $api->clientGrants() ); } diff --git a/tests/API/Management/ClientsTest.php b/tests/API/Management/ClientsTest.php index 1dc3e417c..091a69991 100644 --- a/tests/API/Management/ClientsTest.php +++ b/tests/API/Management/ClientsTest.php @@ -26,6 +26,15 @@ public function setUp() sleep(2); } + public function testThatMethodAndPropertyReturnSameClass() + { + $api = new Management(uniqid(), uniqid()); + $this->assertInstanceOf( Management\Clients::class, $api->clients ); + $this->assertInstanceOf( Management\Clients::class, $api->clients() ); + $api->clients = null; + $this->assertInstanceOf( Management\Clients::class, $api->clients() ); + } + /** * Return the Clients API to test. * diff --git a/tests/API/Management/ConnectionsMockedTest.php b/tests/API/Management/ConnectionsMockedTest.php index 73409d039..b92cb9509 100644 --- a/tests/API/Management/ConnectionsMockedTest.php +++ b/tests/API/Management/ConnectionsMockedTest.php @@ -2,9 +2,8 @@ namespace Auth0\Tests\API\Management; use Auth0\Tests\Traits\ErrorHelpers; - use Auth0\SDK\API\Helpers\InformationHeaders; - +use Auth0\SDK\API\Management; use GuzzleHttp\Psr7\Response; /** @@ -41,6 +40,15 @@ public static function setUpBeforeClass() self::$expectedTelemetry = $infoHeadersData->build(); } + public function testThatMethodAndPropertyReturnSameClass() + { + $api = new Management(uniqid(), uniqid()); + $this->assertInstanceOf( Management\Connections::class, $api->connections ); + $this->assertInstanceOf( Management\Connections::class, $api->connections() ); + $api->connections = null; + $this->assertInstanceOf( Management\Connections::class, $api->connections() ); + } + /** * Test a basic getAll connection call. * diff --git a/tests/API/Management/EmailTemplatesMockedTest.php b/tests/API/Management/EmailTemplatesMockedTest.php index 2e6019da6..6ae88b805 100644 --- a/tests/API/Management/EmailTemplatesMockedTest.php +++ b/tests/API/Management/EmailTemplatesMockedTest.php @@ -3,9 +3,8 @@ namespace Auth0\Tests\API\Management; use Auth0\Tests\Traits\ErrorHelpers; -use Auth0\SDK\API\Management\EmailTemplates; use Auth0\SDK\API\Helpers\InformationHeaders; - +use Auth0\SDK\API\Management; use GuzzleHttp\Psr7\Response; /** @@ -42,13 +41,22 @@ public static function setUpBeforeClass() self::$expectedTelemetry = $infoHeadersData->build(); } + public function testThatMethodAndPropertyReturnSameClass() + { + $api = new Management(uniqid(), uniqid()); + $this->assertInstanceOf( Management\EmailTemplates::class, $api->emailTemplates ); + $this->assertInstanceOf( Management\EmailTemplates::class, $api->emailTemplates() ); + $api->emailTemplates = null; + $this->assertInstanceOf( Management\EmailTemplates::class, $api->emailTemplates() ); + } + /** * @throws \Exception Should not be thrown in this test. */ public function testThatGetTemplateRequestIsFormattedProperly() { $api = new MockManagementApi( [ new Response( 200, self::$headers ) ] ); - $api->call()->emailTemplates->get( EmailTemplates::TEMPLATE_VERIFY_EMAIL ); + $api->call()->emailTemplates->get( Management\EmailTemplates::TEMPLATE_VERIFY_EMAIL ); $this->assertEquals( 'GET', $api->getHistoryMethod() ); $this->assertEquals( 'https://api.test.local/api/v2/email-templates/verify_email', $api->getHistoryUrl() ); @@ -71,7 +79,7 @@ public function testThatPatchTemplateRequestIsFormattedProperly() 'subject' => '__test_email_subject__', ]; - $api->call()->emailTemplates->patch( EmailTemplates::TEMPLATE_RESET_EMAIL, $patch_data ); + $api->call()->emailTemplates->patch( Management\EmailTemplates::TEMPLATE_RESET_EMAIL, $patch_data ); $this->assertEquals( 'PATCH', $api->getHistoryMethod() ); $this->assertEquals( 'https://api.test.local/api/v2/email-templates/reset_email', $api->getHistoryUrl() ); @@ -92,7 +100,7 @@ public function testThatCreateTemplateRequestIsFormattedProperly() $api = new MockManagementApi( [ new Response( 200, self::$headers ) ] ); $api->call()->emailTemplates->create( - EmailTemplates::TEMPLATE_WELCOME_EMAIL, + Management\EmailTemplates::TEMPLATE_WELCOME_EMAIL, true, 'test@auth0.com', '__test_email_subject__', diff --git a/tests/API/Management/EmailsMockedTest.php b/tests/API/Management/EmailsMockedTest.php index c7dd2f152..73cc4d4e1 100644 --- a/tests/API/Management/EmailsMockedTest.php +++ b/tests/API/Management/EmailsMockedTest.php @@ -3,9 +3,8 @@ namespace Auth0\Tests\API\Management; use Auth0\Tests\Traits\ErrorHelpers; -use Auth0\SDK\API\Management\Emails; use Auth0\SDK\API\Helpers\InformationHeaders; - +use Auth0\SDK\API\Management; use GuzzleHttp\Psr7\Response; /** @@ -42,6 +41,15 @@ public static function setUpBeforeClass() self::$expectedTelemetry = $infoHeadersData->build(); } + public function testThatMethodAndPropertyReturnSameClass() + { + $api = new Management(uniqid(), uniqid()); + $this->assertInstanceOf( Management\Emails::class, $api->emails ); + $this->assertInstanceOf( Management\Emails::class, $api->emails() ); + $api->emails = null; + $this->assertInstanceOf( Management\Emails::class, $api->emails() ); + } + /** * @throws \Exception Should not be thrown in this test. */ diff --git a/tests/API/Management/GrantsMockedTest.php b/tests/API/Management/GrantsMockedTest.php index 7cca26be8..b88d45dbf 100644 --- a/tests/API/Management/GrantsMockedTest.php +++ b/tests/API/Management/GrantsMockedTest.php @@ -36,6 +36,15 @@ public static function setUpBeforeClass() self::$telemetry = $infoHeadersData->build(); } + public function testThatMethodAndPropertyReturnSameClass() + { + $api = new Management(uniqid(), uniqid()); + $this->assertInstanceOf( Management\Grants::class, $api->grants ); + $this->assertInstanceOf( Management\Grants::class, $api->grants() ); + $api->grants = null; + $this->assertInstanceOf( Management\Grants::class, $api->grants() ); + } + /** * Test that getAll requests properly. * diff --git a/tests/API/Management/LogsTest.php b/tests/API/Management/LogsTest.php index 7b2ecac26..b60a0f70c 100644 --- a/tests/API/Management/LogsTest.php +++ b/tests/API/Management/LogsTest.php @@ -42,6 +42,15 @@ public function setUp() sleep(1); } + public function testThatMethodAndPropertyReturnSameClass() + { + $api = new Management(uniqid(), uniqid()); + $this->assertInstanceOf( Management\Logs::class, $api->logs ); + $this->assertInstanceOf( Management\Logs::class, $api->logs() ); + $api->logs = null; + $this->assertInstanceOf( Management\Logs::class, $api->logs() ); + } + /** * Test a general search. * diff --git a/tests/API/Management/ResourceServersTest.php b/tests/API/Management/ResourceServersTest.php index 24001c4d8..3987ea467 100644 --- a/tests/API/Management/ResourceServersTest.php +++ b/tests/API/Management/ResourceServersTest.php @@ -64,6 +64,15 @@ public function setUp() sleep(1); } + public function testThatMethodAndPropertyReturnSameClass() + { + $api = new Management(uniqid(), uniqid()); + $this->assertInstanceOf( Management\ResourceServers::class, $api->resource_servers ); + $this->assertInstanceOf( Management\ResourceServers::class, $api->resourceServers() ); + $api->resource_servers = null; + $this->assertInstanceOf( Management\ResourceServers::class, $api->resourceServers() ); + } + /** * Test creating a Resource Server. * diff --git a/tests/API/Management/RolesMockedTest.php b/tests/API/Management/RolesMockedTest.php index 9258f7923..071b8286d 100644 --- a/tests/API/Management/RolesMockedTest.php +++ b/tests/API/Management/RolesMockedTest.php @@ -1,12 +1,11 @@ build(); } + public function testThatMethodAndPropertyReturnSameClass() + { + $api = new Management(uniqid(), uniqid()); + $this->assertInstanceOf( Management\Roles::class, $api->roles ); + $this->assertInstanceOf( Management\Roles::class, $api->roles() ); + $api->roles = null; + $this->assertInstanceOf( Management\Roles::class, $api->roles() ); + } + /** * Test a basic getAll roles call. * diff --git a/tests/API/Management/RulesTest.php b/tests/API/Management/RulesTest.php index 24f4feac5..253ba0bf2 100644 --- a/tests/API/Management/RulesTest.php +++ b/tests/API/Management/RulesTest.php @@ -39,6 +39,15 @@ public function setUp() sleep(2); } + public function testThatMethodAndPropertyReturnSameClass() + { + $api = new Management(uniqid(), uniqid()); + $this->assertInstanceOf( Management\Rules::class, $api->rules ); + $this->assertInstanceOf( Management\Rules::class, $api->rules() ); + $api->rules = null; + $this->assertInstanceOf( Management\Rules::class, $api->rules() ); + } + /** * Test that get methods work as expected. * diff --git a/tests/API/Management/UsersMockedTest.php b/tests/API/Management/UsersMockedTest.php index 052df6def..f51c425d8 100644 --- a/tests/API/Management/UsersMockedTest.php +++ b/tests/API/Management/UsersMockedTest.php @@ -45,6 +45,15 @@ public static function setUpBeforeClass() self::$expectedTelemetry = $infoHeadersData->build(); } + public function testThatMethodAndPropertyReturnSameClass() + { + $api = new Management(uniqid(), uniqid()); + $this->assertInstanceOf( Management\Users::class, $api->users ); + $this->assertInstanceOf( Management\Users::class, $api->users() ); + $api->users = null; + $this->assertInstanceOf( Management\Users::class, $api->users() ); + } + /** * Test a get user call. *