Skip to content

Commit

Permalink
[2.x] Add CreateTenant command (#153)
Browse files Browse the repository at this point in the history
* Add CreateTenant command, fix TenantList output

* Create command test
  • Loading branch information
stancl committed Oct 4, 2019
1 parent 40f8fa3 commit d447246
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -20,7 +20,7 @@ before_script:
- export DB_USERNAME=root DB_PASSWORD="" DB_DATABASE=tenancy CODECOV_TOKEN="24382d15-84e7-4a55-bea4-c4df96a24a9b"
- cat vendor/laravel/framework/src/Illuminate/Foundation/Application.php| grep 'const VERSION'

script: ./test
script: ./fulltest

after_success:
- bash <(curl -s https://codecov.io/bash)
7 changes: 7 additions & 0 deletions fulltest
@@ -0,0 +1,7 @@
#!/bin/bash
set -e

# for development
docker-compose up -d
./test "$@"
docker-compose exec test vendor/bin/phpcov merge --clover clover.xml coverage/
47 changes: 47 additions & 0 deletions src/Commands/CreateTenant.php
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Stancl\Tenancy\Commands;

use Illuminate\Console\Command;
use Stancl\Tenancy\Tenant;

class CreateTenant extends Command
{
protected $signature = 'tenants:create
{--d|domain=* : The tenant\'s domains.}
{data?* : The tenant\'s data. Separate keys and values by `=`, e.g. `plan=free`.}';

protected $description = 'Create a tenant.';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$tenant = Tenant::new()
->withDomains($this->getDomains())
->withData($this->getData())
->save();

$this->info($tenant->id);
}

public function getDomains(): array
{
return $this->option('domain');
}

public function getData(): array
{
return array_reduce($this->argument('data'), function ($data, $pair) {
[$key, $value] = explode('=', $pair, 2);
$data[$key] = $value;

return $data;
}, []);
}
}
2 changes: 1 addition & 1 deletion src/Commands/TenantList.php
Expand Up @@ -31,7 +31,7 @@ public function handle()
{
$this->info('Listing all tenants.');
tenancy()->all()->each(function ($tenant) {
$this->line("[Tenant] id: {$tenant['id']} @ ", implode('; ', $tenant->domains));
$this->line("[Tenant] id: {$tenant['id']} @ " . implode('; ', $tenant->domains));
});
}
}
1 change: 1 addition & 0 deletions src/TenancyServiceProvider.php
Expand Up @@ -65,6 +65,7 @@ public function boot(): void
Commands\Migrate::class,
Commands\Rollback::class,
Commands\TenantList::class,
Commands\CreateTenant::class,
Commands\MigrateFresh::class,
]);

Expand Down
3 changes: 0 additions & 3 deletions test
@@ -1,10 +1,7 @@
#!/bin/bash
set -e

# for development
docker-compose up -d
printf "Variant 1\n\n"
docker-compose exec test env TENANCY_TEST_STORAGE_DRIVER=db vendor/bin/phpunit --coverage-php coverage/2.cov "$@"
printf "Variant 2\n\n"
docker-compose exec test env TENANCY_TEST_STORAGE_DRIVER=redis vendor/bin/phpunit --coverage-php coverage/1.cov "$@"
docker-compose exec test vendor/bin/phpcov merge --clover clover.xml coverage/
12 changes: 12 additions & 0 deletions tests/CommandsTest.php
Expand Up @@ -156,4 +156,16 @@ public function migrate_fresh_command_works()
Artisan::call('tenants:migrate-fresh');
$this->assertFalse(DB::table('users')->exists());
}

/** @test */
public function create_command_works()
{
Artisan::call('tenants:create -d aaa.localhost -d bbb.localhost plan=free email=foo@test.local');
$tenant = tenancy()->all()[1]; // a tenant is autocreated prior to this
$data = $tenant->data;
unset($data['id']);

$this->assertSame(['plan' => 'free', 'email' => 'foo@test.local'], $data);
$this->assertSame(['aaa.localhost', 'bbb.localhost'], $tenant->domains);
}
}

0 comments on commit d447246

Please sign in to comment.