-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to set multiple regionIDs and mocks
- Loading branch information
1 parent
4ab9614
commit 06e1dd4
Showing
14 changed files
with
357 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
/** | ||
* @copyright 2009-2023 Vanilla Forums Inc. | ||
* @license Proprietary | ||
*/ | ||
|
||
namespace Garden\Sites\Mock; | ||
|
||
use Garden\Sites\Cluster; | ||
|
||
/** | ||
* Cluster for usage in mocks. | ||
*/ | ||
class MockCluster extends Cluster | ||
{ | ||
/** | ||
* @param string $clusterID | ||
*/ | ||
public function __construct(string $clusterID) | ||
{ | ||
parent::__construct($clusterID, Cluster::REGION_MOCK); | ||
} | ||
} |
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,181 @@ | ||
<?php | ||
/** | ||
* @copyright 2009-2023 Vanilla Forums Inc. | ||
* @license Proprietary | ||
*/ | ||
|
||
namespace Garden\Sites\Mock; | ||
|
||
use Garden\Sites\Local\LocalCluster; | ||
use Garden\Sites\Site; | ||
use Garden\Sites\SiteRecord; | ||
use Garden\Utils\ArrayUtils; | ||
|
||
/** | ||
* A mock local site for testing purposes. | ||
* | ||
* @extends Site<MockSite, LocalCluster> | ||
*/ | ||
class MockSite extends Site | ||
{ | ||
public const MOCK_CLUSTER_ID = "cl00000"; | ||
|
||
/** @var int */ | ||
public int $maxJobConcurrency = 25; | ||
|
||
/** @var string */ | ||
protected string $baseUrl; | ||
|
||
/** @var array<array-key, mixed>|\ArrayAccess */ | ||
protected $config; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $baseUrl | ||
* @param int $siteID | ||
* @param array $config | ||
* @param string $clusterID | ||
* @param MockSiteProvider|null $mockSiteProvider | ||
*/ | ||
public function __construct( | ||
string $baseUrl, | ||
int $siteID = MockSiteProvider::MOCK_SITE_ID, | ||
array $config = [], | ||
string $clusterID = self::MOCK_CLUSTER_ID, | ||
MockSiteProvider $mockSiteProvider = null | ||
) { | ||
$this->baseUrl = $baseUrl; | ||
$this->config = $config; | ||
$siteRecord = new SiteRecord($siteID, 0, $clusterID, $baseUrl); | ||
parent::__construct($siteRecord, new MockSiteProvider()); | ||
$this->generateKey(); | ||
$this->setSystemToken("test123"); | ||
$this->setConfigs([ | ||
"Vanilla.AccountID" => 1, | ||
"queue.disableFeedback" => true, | ||
]); | ||
} | ||
|
||
/** | ||
* @param MockSiteProvider $mockSiteProvider | ||
* @return void | ||
*/ | ||
public function setSiteProvider(MockSiteProvider $mockSiteProvider): void | ||
{ | ||
$this->siteProvider = $mockSiteProvider; | ||
} | ||
|
||
/** | ||
* @return SiteRecord | ||
*/ | ||
public function getSiteRecord(): SiteRecord | ||
{ | ||
return $this->siteRecord; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function loadSiteConfig(): array | ||
{ | ||
return $this->config; | ||
} | ||
|
||
/** | ||
* Set config values for the site. | ||
* | ||
* @param array<string, mixed> $configs | ||
* | ||
* @return void | ||
*/ | ||
public function setConfigs(array $configs): void | ||
{ | ||
foreach ($configs as $key => $val) { | ||
ArrayUtils::setByPath($key, $this->config, $val); | ||
} | ||
$this->clearConfigCache(); | ||
} | ||
|
||
/** | ||
* Generate a random private key. | ||
*/ | ||
public function generateKey(): void | ||
{ | ||
$privateKey = bin2hex(random_bytes(32)); | ||
$this->setPrivateKey($privateKey); | ||
} | ||
|
||
/** | ||
* Set the config private key. | ||
* | ||
* @param string|null $key | ||
*/ | ||
public function setPrivateKey(?string $key): void | ||
{ | ||
$this->config["VanillaQueue"]["Keys"]["Private"] = $key; | ||
} | ||
|
||
/** | ||
* Set a value for the Garden.Scheduler.Token config field. | ||
* | ||
* @param string|null $value | ||
* @return void | ||
*/ | ||
public function setSchedulerToken(?string $value): void | ||
{ | ||
$this->config["Garden"]["Scheduler"]["Token"] = $value; | ||
} | ||
|
||
/** | ||
* Set Api Key | ||
* | ||
* @param string|null $key | ||
* @return void | ||
*/ | ||
public function setSystemToken(?string $key): void | ||
{ | ||
ArrayUtils::setByPath(self::CONF_SYSTEM_ACCESS_TOKEN, $this->config, $key); | ||
} | ||
|
||
/** | ||
* Set Site Account id | ||
* | ||
* @param int $accountID | ||
* @return void | ||
*/ | ||
public function setAccountID(int $accountID): void | ||
{ | ||
$this->config["Vanilla"]["AccountID"] = $accountID; | ||
} | ||
|
||
/** | ||
* Set Elastic Secret | ||
* | ||
* @param ?string $secret | ||
* @return void | ||
*/ | ||
public function setElasticSecret(?string $secret): void | ||
{ | ||
$this->config["ElasticDev"]["Secret"] = $secret; | ||
} | ||
|
||
/** | ||
* Set the site Plugins. | ||
* | ||
* @param array $plugins | ||
* @return void | ||
*/ | ||
public function setPlugins(array $plugins): void | ||
{ | ||
$this->config["Plugins"] = $plugins; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getSearchServiceBaseUrl(): string | ||
{ | ||
return "https://fake-search-service.test"; | ||
} | ||
} |
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,86 @@ | ||
<?php | ||
/** | ||
* @copyright 2009-2023 Vanilla Forums Inc. | ||
* @license Proprietary | ||
*/ | ||
|
||
namespace Garden\Sites\Mock; | ||
|
||
use Garden\Sites\Cluster; | ||
use Garden\Sites\Exceptions\SiteNotFoundException; | ||
use Garden\Sites\Local\LocalCluster; | ||
use Garden\Sites\Site; | ||
use Garden\Sites\SiteProvider; | ||
use Symfony\Component\Cache\Adapter\NullAdapter; | ||
|
||
/** | ||
* A Mock LocalSiteProvider for testing purposes. | ||
* | ||
* @extends SiteProvider<MockSite, LocalCluster> | ||
*/ | ||
class MockSiteProvider extends SiteProvider | ||
{ | ||
const MOCK_SITE_ID = 123; | ||
|
||
/** @var array<int, MockSite> */ | ||
private array $mockSites = []; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param MockSite ...$mockSites One or more mock sites. | ||
*/ | ||
public function __construct(MockSite ...$mockSites) | ||
{ | ||
foreach ($mockSites as $mockLocalSite) { | ||
$this->mockSites[$mockLocalSite->getSiteID()] = $mockLocalSite; | ||
} | ||
parent::__construct([Cluster::REGION_MOCK]); | ||
$this->setCache(new NullAdapter()); | ||
} | ||
|
||
/** | ||
* Add a site. | ||
* | ||
* @param MockSite $mockSite | ||
* @return void | ||
*/ | ||
public function addSite(MockSite $mockSite): void | ||
{ | ||
$mockSite->setSiteProvider($this); | ||
$this->mockSites[$mockSite->getSiteID()] = $mockSite; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function loadAllSiteRecords(): array | ||
{ | ||
$siteRecords = []; | ||
foreach ($this->mockSites as $siteID => $site) { | ||
$siteRecords[$siteID] = $site->getSiteRecord(); | ||
} | ||
|
||
return $siteRecords; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getSite(int $siteID): Site | ||
{ | ||
$site = $this->mockSites[$siteID] ?? null; | ||
if ($site === null) { | ||
throw new SiteNotFoundException($siteID); | ||
} | ||
return $site; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function loadAllClusters(): array | ||
{ | ||
return [MockSite::MOCK_CLUSTER_ID => new LocalCluster(MockSite::MOCK_CLUSTER_ID)]; | ||
} | ||
} |
Oops, something went wrong.