86 lines
2.2 KiB
PHP
86 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Features\Account\ManageLabels\Services;
|
|
|
|
use Tests\TestCase;
|
|
use App\Models\User;
|
|
use App\Models\Label;
|
|
use App\Models\Account;
|
|
use App\Jobs\CreateAuditLog;
|
|
use Illuminate\Support\Facades\Queue;
|
|
use Illuminate\Validation\ValidationException;
|
|
use App\Exceptions\NotEnoughPermissionException;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use App\Features\Account\ManageLabels\Services\CreateLabel;
|
|
|
|
class CreateLabelTest extends TestCase
|
|
{
|
|
use DatabaseTransactions;
|
|
|
|
/** @test */
|
|
public function it_creates_a_gender(): void
|
|
{
|
|
$ross = $this->createAdministrator();
|
|
$this->executeService($ross, $ross->account);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_fails_if_wrong_parameters_are_given(): void
|
|
{
|
|
$request = [
|
|
'title' => 'Ross',
|
|
];
|
|
|
|
$this->expectException(ValidationException::class);
|
|
(new CreateLabel)->execute($request);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_fails_if_user_doesnt_belong_to_account(): void
|
|
{
|
|
$this->expectException(ModelNotFoundException::class);
|
|
|
|
$ross = $this->createAdministrator();
|
|
$account = $this->createAccount();
|
|
$this->executeService($ross, $account);
|
|
}
|
|
|
|
/** @test */
|
|
public function it_fails_if_user_is_not_administrator(): void
|
|
{
|
|
$this->expectException(NotEnoughPermissionException::class);
|
|
|
|
$ross = $this->createUser();
|
|
$this->executeService($ross, $ross->account);
|
|
}
|
|
|
|
private function executeService(User $author, Account $account): void
|
|
{
|
|
Queue::fake();
|
|
|
|
$request = [
|
|
'account_id' => $account->id,
|
|
'author_id' => $author->id,
|
|
'name' => 'label name',
|
|
];
|
|
|
|
$label = (new CreateLabel)->execute($request);
|
|
|
|
$this->assertDatabaseHas('labels', [
|
|
'id' => $label->id,
|
|
'account_id' => $account->id,
|
|
'name' => 'label name',
|
|
]);
|
|
|
|
$this->assertInstanceOf(
|
|
Label::class,
|
|
$label
|
|
);
|
|
|
|
Queue::assertPushed(CreateAuditLog::class, function ($job) {
|
|
return $job->auditLog['action_name'] === 'label_created';
|
|
});
|
|
}
|
|
}
|