2019-04-24 07:18:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Feature;
|
|
|
|
|
|
|
|
use App\DataMapper\ClientSettings;
|
2020-03-09 10:38:15 +01:00
|
|
|
use App\DataMapper\CompanySettings;
|
2019-04-24 07:18:48 +02:00
|
|
|
use App\DataMapper\DefaultSettings;
|
|
|
|
use App\Events\Invoice\InvoiceWasMarkedSent;
|
2019-06-02 08:36:31 +02:00
|
|
|
use App\Factory\InvoiceInvitationFactory;
|
2019-04-24 07:18:48 +02:00
|
|
|
use App\Jobs\Account\CreateAccount;
|
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\InvoiceInvitation;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use App\Utils\Traits\UserSessionAttributes;
|
|
|
|
use Faker\Factory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Event;
|
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2020-01-29 05:25:08 +01:00
|
|
|
* @covers App\Models\InvoiceInvitation
|
2019-04-24 07:18:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
class InvitationTest extends TestCase
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
use MakesHash;
|
2019-04-24 12:01:40 +02:00
|
|
|
use DatabaseTransactions;
|
2019-04-24 07:18:48 +02:00
|
|
|
|
2019-04-24 12:01:40 +02:00
|
|
|
public function setUp() :void
|
2019-04-24 07:18:48 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Session::start();
|
|
|
|
|
|
|
|
$this->faker = \Faker\Factory::create();
|
|
|
|
|
|
|
|
Model::reguard();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvoiceCreationAfterInvoiceMarkedSent()
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
$account = factory(\App\Models\Account::class)->create();
|
|
|
|
$company = factory(\App\Models\Company::class)->create([
|
|
|
|
'account_id' => $account->id,
|
|
|
|
]);
|
2019-04-24 07:18:48 +02:00
|
|
|
|
|
|
|
$account->default_company_id = $company->id;
|
|
|
|
$account->save();
|
|
|
|
|
|
|
|
$user = factory(\App\Models\User::class)->create([
|
2020-03-24 10:15:30 +01:00
|
|
|
'account_id' => $account->id,
|
2019-04-24 07:18:48 +02:00
|
|
|
'confirmation_code' => $this->createDbHash(config('database.default'))
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
$userPermissions = collect([
|
|
|
|
'view_invoice',
|
|
|
|
'view_client',
|
|
|
|
'edit_client',
|
|
|
|
'edit_invoice',
|
|
|
|
'create_invoice',
|
|
|
|
'create_client'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$userSettings = DefaultSettings::userSettings();
|
|
|
|
|
|
|
|
$user->companies()->attach($company->id, [
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'is_owner' => 1,
|
|
|
|
'is_admin' => 1,
|
2020-03-09 10:38:15 +01:00
|
|
|
'notifications' => CompanySettings::notificationDefaults(),
|
2019-04-24 07:18:48 +02:00
|
|
|
'permissions' => $userPermissions->toJson(),
|
|
|
|
'settings' => json_encode($userSettings),
|
|
|
|
'is_locked' => 0,
|
|
|
|
]);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
factory(\App\Models\Client::class)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company) {
|
|
|
|
factory(\App\Models\ClientContact::class, 1)->create([
|
2019-04-24 07:18:48 +02:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'client_id' => $c->id,
|
|
|
|
'company_id' => $company->id,
|
|
|
|
'is_primary' => 1
|
|
|
|
]);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
factory(\App\Models\ClientContact::class, 2)->create([
|
2019-04-24 07:18:48 +02:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'client_id' => $c->id,
|
|
|
|
'company_id' => $company->id
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
$client = Client::whereUserId($user->id)->whereCompanyId($company->id)->first();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
factory(\App\Models\Invoice::class, 5)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]);
|
2019-04-24 07:18:48 +02:00
|
|
|
|
|
|
|
$invoice = Invoice::whereUserId($user->id)->whereCompanyId($company->id)->whereClientId($client->id)->first();
|
|
|
|
|
|
|
|
$this->assertNotNull($invoice);
|
|
|
|
$this->assertNotNull($invoice->client);
|
|
|
|
$this->assertNotNull($invoice->client->primary_contact);
|
|
|
|
|
|
|
|
|
2019-06-02 08:36:31 +02:00
|
|
|
$i = InvoiceInvitationFactory::create($invoice->company_id, $invoice->user_id);
|
|
|
|
$i->client_contact_id = $client->primary_contact->first()->id;
|
|
|
|
$i->invoice_id = $invoice->id;
|
|
|
|
$i->save();
|
2019-04-24 07:18:48 +02:00
|
|
|
|
2019-06-02 08:36:31 +02:00
|
|
|
$this->assertNotNull($invoice->invitations);
|
2019-04-24 07:18:48 +02:00
|
|
|
}
|
|
|
|
}
|