Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Console/OwnerSwapTest.php
- This file was added.
<?php | |||||
namespace Tests\Feature\Console; | |||||
use Illuminate\Support\Facades\Queue; | |||||
use Tests\TestCase; | |||||
class OwnerSwapTest extends TestCase | |||||
{ | |||||
/** | |||||
* {@inheritDoc} | |||||
*/ | |||||
public function setUp(): void | |||||
{ | |||||
parent::setUp(); | |||||
$this->deleteTestUser('user1@owner-swap.com'); | |||||
$this->deleteTestUser('user2@owner-swap.com'); | |||||
$this->deleteTestDomain('owner-swap.com'); | |||||
} | |||||
/** | |||||
* {@inheritDoc} | |||||
*/ | |||||
public function tearDown(): void | |||||
{ | |||||
$this->deleteTestUser('user1@owner-swap.com'); | |||||
$this->deleteTestUser('user2@owner-swap.com'); | |||||
$this->deleteTestDomain('owner-swap.com'); | |||||
parent::tearDown(); | |||||
} | |||||
/** | |||||
* Test the command | |||||
*/ | |||||
public function testHandle(): void | |||||
{ | |||||
Queue::fake(); | |||||
// Create some sample account | |||||
$owner = $this->getTestUser('user1@owner-swap.com'); | |||||
$user = $this->getTestUser('user2@owner-swap.com'); | |||||
$domain = $this->getTestDomain('owner-swap.com', [ | |||||
'status' => \App\Domain::STATUS_NEW, | |||||
'type' => \App\Domain::TYPE_HOSTED, | |||||
]); | |||||
$package_kolab = \App\Package::withEnvTenantContext()->where('title', 'kolab')->first(); | |||||
$package_domain = \App\Package::withEnvTenantContext()->where('title', 'domain-hosting')->first(); | |||||
$owner->assignPackage($package_kolab); | |||||
$owner->assignPackage($package_kolab, $user); | |||||
$domain->assignPackage($package_domain, $owner); | |||||
$wallet = $owner->wallets()->first(); | |||||
$wallet->currency = 'USD'; | |||||
$wallet->balance = 100; | |||||
$wallet->save(); | |||||
$wallet->setSetting('test', 'test'); | |||||
$target_wallet = $user->wallets()->first(); | |||||
$entitlements = $wallet->entitlements()->orderBy('id')->pluck('id')->all(); | |||||
$this->assertCount(15, $entitlements); | |||||
$this->assertSame(0, $target_wallet->entitlements()->count()); | |||||
// Non-existing target user | |||||
$code = \Artisan::call("owner:swap user1@owner-swap.com unknown@unknown.org"); | |||||
$output = trim(\Artisan::output()); | |||||
$this->assertSame(1, $code); | |||||
$this->assertSame("User not found.", $output); | |||||
// The same user | |||||
$code = \Artisan::call("owner:swap user1@owner-swap.com user1@owner-swap.com"); | |||||
$output = trim(\Artisan::output()); | |||||
$this->assertSame(1, $code); | |||||
$this->assertSame("Users cannot be the same.", $output); | |||||
// Success | |||||
$code = \Artisan::call("owner:swap user1@owner-swap.com user2@owner-swap.com"); | |||||
$output = trim(\Artisan::output()); | |||||
$this->assertSame(0, $code); | |||||
$this->assertSame("", $output); | |||||
$user->refresh(); | |||||
$target_wallet->refresh(); | |||||
$target_entitlements = $target_wallet->entitlements()->orderBy('id')->pluck('id')->all(); | |||||
$this->assertSame($target_entitlements, $entitlements); | |||||
$this->assertSame(0, $wallet->entitlements()->count()); | |||||
$this->assertSame($wallet->balance, $target_wallet->balance); | |||||
$this->assertSame($wallet->currency, $target_wallet->currency); | |||||
$this->assertTrue($user->created_at <= \now()->subMonthsWithoutOverflow(1)); | |||||
$this->assertSame('test', $target_wallet->getSetting('test')); | |||||
$wallet->refresh(); | |||||
$this->assertSame(null, $wallet->getSetting('test')); | |||||
$this->assertSame(0, $wallet->balance); | |||||
// TODO: Test case when the target user does not belong to the same account | |||||
} | |||||
} |