mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:44:04 +00:00
Get Redis tests to pass
This commit is contained in:
parent
6b0ec1b554
commit
fb58f21f1c
3 changed files with 51 additions and 30 deletions
|
|
@ -59,26 +59,12 @@ class RedisStorageDriver implements StorageDriver
|
||||||
throw new TenantCouldNotBeIdentifiedException($domain);
|
throw new TenantCouldNotBeIdentifiedException($domain);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->find($id);
|
return $this->findById($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function findById(string $id): Tenant
|
public function findById(string $id): Tenant
|
||||||
{
|
{
|
||||||
$data = $this->redis->hgetall("tenants:$id");
|
return $this->makeTenant($this->redis->hgetall("tenants:$id"));
|
||||||
$keys = [];
|
|
||||||
$values = [];
|
|
||||||
foreach ($data as $i => $value) {
|
|
||||||
if ($i & 1) { // is odd
|
|
||||||
$values[] = $value;
|
|
||||||
} else {
|
|
||||||
$keys[] = $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$data = array_combine($keys, $values);
|
|
||||||
$domains = []; // todo2
|
|
||||||
|
|
||||||
return Tenant::fromStorage($data)->withDomains($domains);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTenantIdByDomain(string $domain): ?string
|
public function getTenantIdByDomain(string $domain): ?string
|
||||||
|
|
@ -89,19 +75,23 @@ class RedisStorageDriver implements StorageDriver
|
||||||
public function createTenant(Tenant $tenant): void
|
public function createTenant(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
$this->redis->pipeline(function ($pipe) use ($tenant) {
|
$this->redis->pipeline(function ($pipe) use ($tenant) {
|
||||||
$id = $tenant->id;
|
|
||||||
|
|
||||||
foreach ($tenant->domains as $domain) {
|
foreach ($tenant->domains as $domain) {
|
||||||
$pipe->hmset("domains:$domain", 'tenant_id', $id);
|
$pipe->hmset("domains:$domain", ['tenant_id' => $tenant->id]);
|
||||||
}
|
}
|
||||||
$pipe->hmset("tenants:$id", 'id', json_encode($id), 'domain', json_encode($domain));
|
|
||||||
|
$data = [];
|
||||||
|
foreach ($tenant->data as $key => $value) {
|
||||||
|
$data[$key] = json_encode($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
$pipe->hmset("tenants:{$tenant->id}", array_merge($data, ['_tenancy_domains' => json_encode($tenant->domains)]));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateTenant(Tenant $tenant): void
|
public function updateTenant(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
$this->redis->pipeline(function ($pipe) use ($tenant) {
|
$this->redis->pipeline(function ($pipe) use ($tenant) {
|
||||||
$pipe->hmset("tenants:{$tenant->id}", $tenant->data);
|
$pipe->hmset("tenants:{$tenant->id}", $tenant->data); // todo domains
|
||||||
|
|
||||||
foreach ($tenant->domains as $domain) {
|
foreach ($tenant->domains as $domain) {
|
||||||
$pipe->hmset("domains:$domain", 'tenant_id', $tenant->id);
|
$pipe->hmset("domains:$domain", 'tenant_id', $tenant->id);
|
||||||
|
|
@ -122,6 +112,12 @@ class RedisStorageDriver implements StorageDriver
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a list of all tenants.
|
||||||
|
*
|
||||||
|
* @param string[] $ids
|
||||||
|
* @return Tenant[]
|
||||||
|
*/
|
||||||
public function all(array $ids = []): array
|
public function all(array $ids = []): array
|
||||||
{
|
{
|
||||||
// todo2 $this->redis->pipeline()
|
// todo2 $this->redis->pipeline()
|
||||||
|
|
@ -143,10 +139,28 @@ class RedisStorageDriver implements StorageDriver
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_map(function ($tenant) {
|
return array_map(function ($tenant) {
|
||||||
return $this->redis->hgetall($tenant);
|
return $this->makeTenant($this->redis->hgetall($tenant));
|
||||||
}, $hashes);
|
}, $hashes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make a Tenant instance from low-level array data.
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @return Tenant
|
||||||
|
*/
|
||||||
|
protected function makeTenant(array $data): Tenant
|
||||||
|
{
|
||||||
|
foreach ($data as $key => $value) {
|
||||||
|
$data[$key] = json_decode($value, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$domains = $data['_tenancy_domains'];
|
||||||
|
unset($data['_tenancy_domains']);
|
||||||
|
|
||||||
|
return Tenant::fromStorage($data)->withDomains($domains);
|
||||||
|
}
|
||||||
|
|
||||||
public function get(string $key, Tenant $tenant = null)
|
public function get(string $key, Tenant $tenant = null)
|
||||||
{
|
{
|
||||||
$tenant = $tenant ?? $this->tenant();
|
$tenant = $tenant ?? $this->tenant();
|
||||||
|
|
@ -161,7 +175,7 @@ class RedisStorageDriver implements StorageDriver
|
||||||
$result = [];
|
$result = [];
|
||||||
$values = $this->redis->hmget("tenants:{$tenant->id}", $keys);
|
$values = $this->redis->hmget("tenants:{$tenant->id}", $keys);
|
||||||
foreach ($keys as $i => $key) {
|
foreach ($keys as $i => $key) {
|
||||||
$result[$key] = $values[$i];
|
$result[$key] = json_decode($values[$i], true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
|
|
|
||||||
|
|
@ -15,14 +15,14 @@ class TenantAssetTest extends TestCase
|
||||||
|
|
||||||
// response()->file() returns BinaryFileResponse whose content is
|
// response()->file() returns BinaryFileResponse whose content is
|
||||||
// inaccessible via getContent, so ->assertSee() can't be used
|
// inaccessible via getContent, so ->assertSee() can't be used
|
||||||
// $this->get(tenant_asset($filename))->assertSuccessful(); // TODO2 COMMENTED ASSERTIONS
|
// $this->get(tenant_asset($filename))->assertSuccessful(); // todo commented assertions
|
||||||
// $this->assertFileExists($path); // TODO2 COMMENTED ASSERTIONS
|
// $this->assertFileExists($path); // todo commented assertions
|
||||||
|
|
||||||
$f = \fopen($path, 'r');
|
$f = fopen($path, 'r');
|
||||||
$content = \fread($f, \filesize($path));
|
$content = fread($f, filesize($path));
|
||||||
\fclose($f);
|
fclose($f);
|
||||||
|
|
||||||
// $this->assertSame('bar', $content); // TODO2 COMMENTED ASSERTIONS
|
// $this->assertSame('bar', $content); // todo commented assertions
|
||||||
$this->assertTrue(true); // TODO2 COMMENTED ASSERTIONS
|
$this->assertTrue(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -187,4 +187,11 @@ class TenantManagerTest extends TestCase
|
||||||
$this->expectException(\Stancl\Tenancy\Exceptions\TenantStorageException::class);
|
$this->expectException(\Stancl\Tenancy\Exceptions\TenantStorageException::class);
|
||||||
$tenant2->put('id', 'foo');
|
$tenant2->put('id', 'foo');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function all_returns_a_collection_of_tenant_objects()
|
||||||
|
{
|
||||||
|
Tenant::create('foo.localhost');
|
||||||
|
$this->assertSame('Tenant', class_basename(tenancy()->all()[0]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue