-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from 5pm-HDH/feat/2/ct-session
feat(ct-session): create ct-session to manage multiple connections #2
- Loading branch information
Showing
9 changed files
with
275 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
|
||
namespace CTApi; | ||
|
||
|
||
class CTSession | ||
{ | ||
private const DEFAULT_SESSION_ID = "default"; | ||
|
||
private static array $ctConfigs = []; | ||
private static array $ctClients = []; | ||
|
||
public static function switchSession(string $sessionId = self::DEFAULT_SESSION_ID) | ||
{ | ||
self::storeDefaultSessionIfNotExists(); | ||
if (!self::sessionExists($sessionId)) { | ||
self::createSession($sessionId); | ||
} else { | ||
self::switchToSession($sessionId); | ||
} | ||
} | ||
|
||
private static function storeDefaultSessionIfNotExists() | ||
{ | ||
if (!self::sessionExists(self::DEFAULT_SESSION_ID)) { | ||
self::$ctConfigs[self::DEFAULT_SESSION_ID] = CTConfig::getConfig(); | ||
self::$ctClients[self::DEFAULT_SESSION_ID] = CTClient::getClient(); | ||
} | ||
} | ||
|
||
private static function sessionExists(string $sessionId) | ||
{ | ||
return array_key_exists($sessionId, self::$ctConfigs) && array_key_exists($sessionId, self::$ctClients); | ||
} | ||
|
||
private static function createSession(string $sessionId) | ||
{ | ||
$config = CTConfig::createConfig(); | ||
$client = CTClient::createClient(); | ||
|
||
self::$ctConfigs[$sessionId] = $config; | ||
self::$ctClients[$sessionId] = $client; | ||
|
||
self::switchToSession($sessionId); | ||
} | ||
|
||
private static function switchToSession(string $sessionId) | ||
{ | ||
CTConfig::setConfig(self::$ctConfigs[$sessionId]); | ||
CTClient::setClient(self::$ctClients[$sessionId]); | ||
} | ||
|
||
public static function getSessionIds(): array | ||
{ | ||
self::storeDefaultSessionIfNotExists(); | ||
$keysConfig = array_keys(self::$ctConfigs); | ||
$keysClient = array_keys(self::$ctConfigs); | ||
return array_intersect($keysConfig, $keysClient); | ||
} | ||
|
||
public static function clearSessions() | ||
{ | ||
self::$ctClients = []; | ||
self::$ctConfigs = []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
|
||
namespace CTApi\Test\Integration\Config; | ||
|
||
|
||
use CTApi\CTConfig; | ||
use CTApi\CTSession; | ||
use CTApi\Requests\PersonRequest; | ||
use CTApi\Test\Integration\IntegrationTestData; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CTSessionIntegrationTest extends TestCase | ||
{ | ||
public function testSwitchSession() | ||
{ | ||
// Default Session | ||
$this->configPersonA(); | ||
$personA = PersonRequest::whoami(); | ||
|
||
// Custom Session | ||
CTSession::switchSession("custom_person_session"); | ||
$this->configPersonB(); | ||
$personB = PersonRequest::whoami(); | ||
|
||
// Default Session | ||
CTSession::switchSession(); | ||
$personA_2 = PersonRequest::whoami(); | ||
|
||
// Custom Session | ||
CTSession::switchSession("custom_person_session"); | ||
$personB_2 = PersonRequest::whoami(); | ||
|
||
$this->assertEquals($personA, $personA_2); | ||
$this->assertEquals($personB, $personB_2); | ||
$this->assertNotEquals($personA, $personB); | ||
$this->assertNotEquals($personA_2, $personB_2); | ||
$this->assertNotEquals($personA, $personB_2); | ||
$this->assertNotEquals($personA_2, $personB); | ||
} | ||
|
||
private function configPersonA() | ||
{ | ||
CTConfig::setApiUrl(IntegrationTestData::get()->getApiUrl()); | ||
IntegrationTestData::get()->authenticateUser(); | ||
} | ||
|
||
private function configPersonB() | ||
{ | ||
CTConfig::setApiUrl(IntegrationTestData::get()->getApiUrl()); | ||
IntegrationTestData::get()->authenticateUser("jona"); | ||
} | ||
|
||
} |
Oops, something went wrong.