2018-10-18 07:04:36 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit;
|
|
|
|
|
|
|
|
use App\Libraries\MultiDB;
|
2018-10-22 14:04:37 +02:00
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Company;
|
2018-10-18 07:04:36 +02:00
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Libraries\MultiDB
|
|
|
|
*
|
|
|
|
* Proves that we can reliably switch database connections at runtime
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
class MultiDBUserTest extends TestCase
|
|
|
|
{
|
|
|
|
//use DatabaseMigrations;
|
|
|
|
use InteractsWithDatabase;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2018-10-21 00:26:21 +02:00
|
|
|
if (! config('ninja.db.multi_db_enabled'))
|
2018-10-18 07:04:36 +02:00
|
|
|
$this->markTestSkipped('Multi DB not enabled - skipping');
|
|
|
|
|
|
|
|
User::unguard();
|
|
|
|
|
2018-10-22 14:04:37 +02:00
|
|
|
$ac = factory(\App\Models\Account::class)->make();
|
|
|
|
|
2018-10-24 12:24:09 +02:00
|
|
|
$account = Account::on('db-ninja-01')->create($ac->toArray());
|
|
|
|
$account2 = Account::on('db-ninja-02')->create($ac->toArray());
|
2018-10-22 14:04:37 +02:00
|
|
|
|
|
|
|
$company = factory(\App\Models\Company::class)->make([
|
|
|
|
'account_id' => $account->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$company2 = factory(\App\Models\Company::class)->make([
|
|
|
|
'account_id' => $account2->id,
|
|
|
|
]);
|
|
|
|
|
2019-02-17 11:34:46 +01:00
|
|
|
|
|
|
|
$company->setHidden(['settings', 'settings_object']);
|
|
|
|
$company2->setHidden(['settings', 'settings_object']);
|
|
|
|
|
2018-10-24 12:24:09 +02:00
|
|
|
Company::on('db-ninja-01')->create($company->toArray());
|
|
|
|
Company::on('db-ninja-02')->create($company2->toArray());
|
2018-10-22 14:04:37 +02:00
|
|
|
|
2018-10-18 07:04:36 +02:00
|
|
|
$user = [
|
2018-10-22 14:04:37 +02:00
|
|
|
'first_name' => 'user_db_1',
|
|
|
|
'last_name' => 'user_db_1-s',
|
|
|
|
'phone' => '55555',
|
2018-10-18 07:04:36 +02:00
|
|
|
'email_verified_at' => now(),
|
2018-10-22 14:04:37 +02:00
|
|
|
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
|
|
|
|
'remember_token' => str_random(10),
|
|
|
|
'email' => 'db1@example.com',
|
|
|
|
'oauth_user_id' => '123',
|
|
|
|
'db' => config('database.default'),
|
|
|
|
'account_id' => $account->id,
|
2018-10-18 07:04:36 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
$user2 = [
|
|
|
|
'first_name' => 'user_db_2',
|
|
|
|
'last_name' => 'user_db_2-s',
|
|
|
|
'phone' => '55555',
|
|
|
|
'email_verified_at' => now(),
|
|
|
|
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
|
|
|
|
'remember_token' => str_random(10),
|
|
|
|
'email' => 'db2@example.com',
|
2018-10-19 05:45:55 +02:00
|
|
|
'oauth_user_id' => 'abc',
|
2018-10-22 14:04:37 +02:00
|
|
|
'db' => config('database.default'),
|
|
|
|
'account_id' => $account2->id,
|
2018-10-19 05:45:55 +02:00
|
|
|
|
2018-10-18 07:04:36 +02:00
|
|
|
];
|
|
|
|
|
2018-10-24 12:24:09 +02:00
|
|
|
User::on('db-ninja-01')->create($user);
|
|
|
|
User::on('db-ninja-02')->create($user2);
|
2018-10-18 07:04:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_oauth_user_db2_exists()
|
|
|
|
{
|
2018-10-18 12:47:55 +02:00
|
|
|
$user = MultiDB::hasUser(['email' => 'db2@example.com', 'oauth_user_id' => 'abc']);
|
2018-10-18 07:04:36 +02:00
|
|
|
|
|
|
|
$this->assertEquals($user->email, 'db2@example.com');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_oauth_user_db1_exists()
|
|
|
|
{
|
2018-10-18 12:47:55 +02:00
|
|
|
$user = MultiDB::hasUser(['email' => 'db1@example.com', 'oauth_user_id' => '123']);
|
2018-10-18 07:04:36 +02:00
|
|
|
|
|
|
|
$this->assertEquals($user->email, 'db1@example.com');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-18 12:47:55 +02:00
|
|
|
public function test_check_user_exists()
|
|
|
|
{
|
|
|
|
$this->assertTrue(MultiDB::checkUserEmailExists('db1@example.com'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_check_user_does_not_exist()
|
|
|
|
{
|
|
|
|
$this->assertFalse(MultiDB::checkUserEmailExists('bademail@example.com'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is what you do when you demand 100% code coverage :/
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function test_set_db_invokes()
|
|
|
|
{
|
2018-10-24 12:24:09 +02:00
|
|
|
$this->expectNotToPerformAssertions(MultiDB::setDB('db-ninja-01'));
|
2018-10-18 12:47:55 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 07:04:36 +02:00
|
|
|
public function tearDown()
|
|
|
|
{
|
2018-10-24 12:24:09 +02:00
|
|
|
DB::connection('db-ninja-01')->table('users')->delete();
|
|
|
|
DB::connection('db-ninja-02')->table('users')->delete();
|
2018-10-18 07:04:36 +02:00
|
|
|
}
|
|
|
|
}
|