refactor: replace custom CRM with Monica fork
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s

This commit is contained in:
Kroonk
2026-05-22 16:19:55 +02:00
parent 38cc4c09ca
commit a213a1dba0
2215 changed files with 242567 additions and 6015 deletions

View File

@@ -0,0 +1,112 @@
<?php
namespace Tests\Unit\Services\Contact\Address;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Address;
use App\Models\Contact\Contact;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Address\CreateAddress;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class CreateAddressTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_stores_an_address()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'name' => 'work address',
'street' => '199 Lafayette Street',
'city' => 'New York City',
'province' => '',
'postal_code' => '',
'country' => 'USA',
'latitude' => '',
'longitude' => '',
];
$address = app(CreateAddress::class)->execute($request);
$this->assertDatabaseHas('addresses', [
'id' => $address->id,
'account_id' => $contact->account_id,
'name' => 'work address',
]);
$this->assertEquals(
'199 Lafayette Street',
$address->place->street
);
$this->assertInstanceOf(
Address::class,
$address
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$account = factory(Account::class)->create([]);
$request = [
'name' => '199 Lafayette Street',
];
$this->expectException(ValidationException::class);
app(CreateAddress::class)->execute($request);
}
/** @test */
public function it_fails_if_contact_is_archived()
{
$contact = factory(Contact::class)->state('archived')->create();
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'name' => 'work address',
'street' => '199 Lafayette Street',
'city' => 'New York City',
'province' => '',
'postal_code' => '',
'country' => 'USA',
'latitude' => '',
'longitude' => '',
];
$this->expectException(ValidationException::class);
app(CreateAddress::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_is_not_linked_to_account()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create();
$request = [
'account_id' => $account->id,
'contact_id' => $contact->id,
'name' => 'work address',
'street' => '199 Lafayette Street',
'city' => 'New York City',
'province' => '',
'postal_code' => '',
'country' => 'USA',
'latitude' => '',
'longitude' => '',
];
$this->expectException(ModelNotFoundException::class);
app(CreateAddress::class)->execute($request);
}
}

View File

@@ -0,0 +1,78 @@
<?php
namespace Tests\Unit\Services\Contact\Address;
use Tests\TestCase;
use App\Models\Contact\Address;
use App\Models\Contact\Contact;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Address\DestroyAddress;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class DestroyAddressTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_destroys_an_address()
{
$address = factory(Address::class)->create([]);
$request = [
'account_id' => $address->account_id,
'address_id' => $address->id,
];
app(DestroyAddress::class)->execute($request);
$this->assertDatabaseMissing('addresses', [
'id' => $address->id,
]);
}
/** @test */
public function it_throws_an_exception_if_account_is_not_linked_to_address()
{
$contact = factory(Contact::class)->create([]);
$address = factory(Address::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'address_id' => $address->id,
];
$this->expectException(ModelNotFoundException::class);
app(DestroyAddress::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_ids_do_not_exist()
{
$request = [
'account_id' => 11111111,
'address_id' => 11111111,
];
$this->expectException(ValidationException::class);
app(DestroyAddress::class)->execute($request);
}
/** @test */
public function it_fails_if_contact_is_archived()
{
$contact = factory(Contact::class)->state('archived')->create();
$address = factory(Address::class)->create([
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
]);
$request = [
'account_id' => $contact->account_id,
'address_id' => $address->id,
];
$this->expectException(ValidationException::class);
app(DestroyAddress::class)->execute($request);
}
}

View File

@@ -0,0 +1,112 @@
<?php
namespace Tests\Unit\Services\Contact\Address;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Address;
use App\Models\Contact\Contact;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Address\UpdateAddress;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class UpdateAddressTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_an_address()
{
$address = factory(Address::class)->create([]);
$request = [
'account_id' => $address->account_id,
'contact_id' => $address->contact_id,
'address_id' => $address->id,
'name' => 'this is a test',
'street' => '1990 Lafayette Street',
'city' => 'New York City',
'province' => '',
'postal_code' => '',
'country' => 'USA',
'latitude' => '',
'longitude' => '',
];
$address = app(UpdateAddress::class)->execute($request);
$this->assertDatabaseHas('addresses', [
'id' => $address->id,
'account_id' => $address->account_id,
'name' => 'this is a test',
]);
$this->assertEquals(
'1990 Lafayette Street',
$address->place->street
);
$this->assertInstanceOf(
Address::class,
$address
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$address = factory(Address::class)->create([]);
$request = [
'street' => '199 Lafayette Street',
];
$this->expectException(ValidationException::class);
app(UpdateAddress::class)->execute($request);
}
/** @test */
public function it_fails_if_contact_is_archived()
{
$contact = factory(Contact::class)->state('archived')->create();
$address = factory(Address::class)->create([
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
]);
$request = [
'account_id' => $address->account_id,
'contact_id' => $address->contact_id,
'address_id' => $address->id,
'name' => 'this is a test',
'street' => '1990 Lafayette Street',
'city' => 'New York City',
'province' => '',
'postal_code' => '',
'country' => 'USA',
'latitude' => '',
'longitude' => '',
];
$this->expectException(ValidationException::class);
app(UpdateAddress::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_address_is_not_linked_to_account()
{
$account = factory(Account::class)->create([]);
$contact = factory(Contact::class)->create([]);
$address = factory(Address::class)->create([]);
$request = [
'account_id' => $account->id,
'contact_id' => $contact->id,
'address_id' => $address->id,
];
$this->expectException(ModelNotFoundException::class);
app(UpdateAddress::class)->execute($request);
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace Tests\Unit\Services\Contact\Avatar;
use Tests\TestCase;
use App\Models\Contact\Contact;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Avatar\GenerateDefaultAvatar;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class GenerateDefaultAvatarTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_generates_a_default_avatar()
{
$contact = factory(Contact::class)->create([
'default_avatar_color' => '#000',
]);
$request = [
'contact_id' => $contact->id,
];
$contact = app(GenerateDefaultAvatar::class)->execute($request);
$this->assertStringContainsString(
'avatars/',
$contact->avatar_default_url
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$request = [];
$this->expectException(ValidationException::class);
app(GenerateDefaultAvatar::class)->execute($request);
}
/** @test */
public function it_replaces_existing_default_avatar()
{
$file = UploadedFile::fake()->image('image.png');
$contact = factory(Contact::class)->create([
'default_avatar_color' => '#fff',
'avatar_default_url' => $file->getPathname(),
]);
$this->assertFileExists($file->getPathname());
$request = [
'contact_id' => $contact->id,
];
$contact = app(GenerateDefaultAvatar::class)->execute($request);
$this->assertStringContainsString(
'avatars/',
$contact->avatar_default_url
);
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Tests\Unit\Services\Contact\Avatar;
use Tests\TestCase;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Avatar\GetAdorableAvatarURL;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class GetAdorableAvatarTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_returns_an_url()
{
$request = [
'uuid' => 'matt@wordpress.com',
'size' => 400,
];
$url = app(GetAdorableAvatarURL::class)->execute($request);
$this->assertEquals(
'400/matt@wordpress.com.png',
$url
);
}
/** @test */
public function it_returns_an_url_with_a_default_avatar_size()
{
$request = [
'uuid' => 'matt@wordpress.com',
];
$url = app(GetAdorableAvatarURL::class)->execute($request);
// should return an avatar of 200 px wide
$this->assertEquals(
'200/matt@wordpress.com.png',
$url
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$request = [
'size' => 200,
];
$this->expectException(ValidationException::class);
app(GetAdorableAvatarURL::class)->execute($request);
}
}

View File

@@ -0,0 +1,113 @@
<?php
namespace Tests\Unit\Services\Contact\Avatar;
use Tests\TestCase;
use App\Models\Contact\Contact;
use App\Models\Contact\ContactField;
use App\Models\Contact\ContactFieldType;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Avatar\GetAvatarsFromInternet;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class GetAvatarsFromInternetTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_returns_a_contact_object_with_avatars()
{
$contact = factory(Contact::class)->create([]);
$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $contact->account_id,
]);
factory(ContactField::class)->create([
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'contact_field_type_id' => $contactFieldType->id,
'data' => 'matt@wordpress.com',
]);
$request = [
'contact_id' => $contact->id,
];
$contact = app(GetAvatarsFromInternet::class)->execute($request);
$this->assertInstanceOf(
Contact::class,
$contact
);
$this->assertNotNull(
$contact->avatar_adorable_url
);
$this->assertNotNull(
$contact->avatar_gravatar_url
);
}
/** @test */
public function gravatar_is_null_if_contact_doesnt_have_an_email()
{
$contact = factory(Contact::class)->create([]);
$request = [
'contact_id' => $contact->id,
];
$contact = app(GetAvatarsFromInternet::class)->execute($request);
$this->assertNull(
$contact->avatar_gravatar_url
);
}
/** @test */
public function avatar_source_is_reset_and_set_to_adorable_if_gravatar_doesnt_exist_anymore()
{
$contact = factory(Contact::class)->create([
'avatar_source' => 'gravatar',
]);
$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $contact->account_id,
]);
$contactField = factory(ContactField::class)->create([
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'contact_field_type_id' => $contactFieldType->id,
'data' => 'matt@wordpress.com',
]);
$request = [
'contact_id' => $contact->id,
];
$contact = app(GetAvatarsFromInternet::class)->execute($request);
// now we call the service again to reset the gravatar url
$contactField->delete();
$contact = app(GetAvatarsFromInternet::class)->execute($request);
$this->assertNull(
$contact->avatar_gravatar_url
);
$this->assertEquals(
'adorable',
$contact->avatar_source
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$request = [
'size' => 200,
];
$this->expectException(ValidationException::class);
app(GetAvatarsFromInternet::class)->execute($request);
}
}

View File

@@ -0,0 +1,145 @@
<?php
namespace Tests\Unit\Services\Contact\Avatar;
use Tests\TestCase;
use App\Models\Contact\Contact;
use App\Models\Contact\ContactField;
use App\Models\Contact\ContactFieldType;
use App\Services\Contact\Avatar\GetGravatar;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Avatar\GetGravatarURL;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class GetGravatarTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_get_gravatar_url()
{
$contact = factory(Contact::class)->create();
$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $contact->account->id,
]);
factory(ContactField::class)->create([
'contact_id' => $contact->id,
'account_id' => $contact->account->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => 'matt@wordpress.com',
]);
$request = [
'contact_id' => $contact->id,
];
$contact = app(GetGravatar::class)->execute($request);
$this->assertNotNull(
$contact->avatar_gravatar_url
);
}
/** @test */
public function it_get_gravatar_of_real_email()
{
$contact = factory(Contact::class)->create();
$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $contact->account->id,
]);
factory(ContactField::class)->create([
'contact_id' => $contact->id,
'account_id' => $contact->account->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => 'bademail',
]);
factory(ContactField::class)->create([
'contact_id' => $contact->id,
'account_id' => $contact->account->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => 'matt@wordpress.com',
]);
$request = [
'contact_id' => $contact->id,
];
$contact = app(GetGravatar::class)->execute($request);
$this->assertNotNull(
$contact->avatar_gravatar_url
);
}
/** @test */
public function it_returns_an_url()
{
$request = [
'email' => 'matt@wordpress.com',
'size' => 400,
];
$url = app(GetGravatarURL::class)->execute($request);
$this->assertEquals(
'https://www.gravatar.com/avatar/5bbc9048a99ec78cdbc227770e707efb.jpg?s=400&d=404&r=g',
$url
);
}
/** @test */
public function it_returns_an_url_with_a_small_avatar_size()
{
$request = [
'email' => 'matt@wordpress.com',
'size' => 80,
];
$url = app(GetGravatarURL::class)->execute($request);
$this->assertEquals(
'https://www.gravatar.com/avatar/5bbc9048a99ec78cdbc227770e707efb.jpg?s=80&d=404&r=g',
$url
);
}
/** @test */
public function it_returns_an_url_with_a_default_avatar_size()
{
$request = [
'email' => 'matt@wordpress.com',
];
$url = app(GetGravatarURL::class)->execute($request);
// should return an avatar of 200 px wide
$this->assertEquals(
'https://www.gravatar.com/avatar/5bbc9048a99ec78cdbc227770e707efb.jpg?s=200&d=404&r=g',
$url
);
}
/** @test */
public function it_returns_null_if_no_avatar_is_found()
{
$request = [
'email' => 'jlskjdfl@dskfjlsd.com',
];
// should return an avatar of 200 px wide
$this->assertNull(
app(GetGravatarURL::class)->execute($request)
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$request = [
'size' => 200,
];
$this->expectException(ValidationException::class);
$url = app(GetGravatarURL::class)->execute($request);
}
}

View File

@@ -0,0 +1,192 @@
<?php
namespace Tests\Unit\Services\Contact\Avatar;
use Tests\TestCase;
use App\Models\Account\Photo;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Services\Contact\Avatar\UpdateAvatar;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class UpdateAvatarTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_the_avatar_with_gravatar()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'source' => 'gravatar',
];
$contact = app(UpdateAvatar::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'avatar_source' => 'gravatar',
]);
$this->assertInstanceOf(
Contact::class,
$contact
);
}
/** @test */
public function it_updates_the_avatar_with_default_avatar()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'source' => 'default',
];
$contact = app(UpdateAvatar::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'avatar_source' => 'default',
]);
$this->assertInstanceOf(
Contact::class,
$contact
);
}
/** @test */
public function it_updates_the_avatar_with_adorable()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'source' => 'adorable',
];
$contact = app(UpdateAvatar::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'avatar_source' => 'adorable',
]);
$this->assertInstanceOf(
Contact::class,
$contact
);
}
/** @test */
public function it_updates_the_avatar_with_existing_photo()
{
$contact = factory(Contact::class)->create([]);
$photo = factory(Photo::class)->create([
'account_id' => $contact->account_id,
]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'source' => 'photo',
'photo_id' => $photo->id,
];
$contact = app(UpdateAvatar::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'avatar_source' => 'photo',
'avatar_photo_id' => $photo->id,
]);
$this->assertInstanceOf(
Contact::class,
$contact
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
];
$this->expectException(ValidationException::class);
app(UpdateAvatar::class)->execute($request);
}
/** @test */
public function it_fails_if_contact_is_archived()
{
$contact = factory(Contact::class)->state('archived')->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'source' => 'gravatar',
];
$this->expectException(ValidationException::class);
app(UpdateAvatar::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_not_linked_to_account()
{
$account = factory(Account::class)->create([]);
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $account->id,
'contact_id' => $contact->id,
'source' => 'adorable',
];
$this->expectException(ModelNotFoundException::class);
app(UpdateAvatar::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_photo_not_linked_to_account()
{
// Case: photo doesn't exist
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'source' => 'photo',
'photo_id' => 0,
];
$this->expectException(ValidationException::class);
$contact = app(UpdateAvatar::class)->execute($request);
// Case: photo exists but belongs to another account
$photo = factory(Photo::class)->create();
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'source' => 'photo',
'photo_id' => $photo->id,
];
$this->expectException(ModelNotFoundException::class);
app(UpdateAvatar::class)->execute($request);
}
}

View File

@@ -0,0 +1,250 @@
<?php
namespace Tests\Unit\Services\Contact\Call;
use Tests\TestCase;
use App\Models\Contact\Call;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Instance\Emotion\Emotion;
use App\Services\Contact\Call\CreateCall;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class CreateCallTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_stores_a_call()
{
$contact = factory(Contact::class)->create([]);
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'called_at' => now(),
'content' => 'this is the content',
];
$call = app(CreateCall::class)->execute($request);
$this->assertDatabaseHas('calls', [
'id' => $call->id,
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'content' => 'this is the content',
'contact_called' => 0,
]);
$this->assertInstanceOf(
Call::class,
$call
);
}
/** @test */
public function it_stores_a_call_and_who_called_information()
{
$contact = factory(Contact::class)->create([]);
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'called_at' => now(),
'content' => 'this is the content',
'contact_called' => true,
];
$call = app(CreateCall::class)->execute($request);
$this->assertDatabaseHas('calls', [
'id' => $call->id,
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'content' => 'this is the content',
'contact_called' => 1,
]);
}
/** @test */
public function it_adds_emotions()
{
$contact = factory(Contact::class)->create([]);
$emotion = factory(Emotion::class)->create([]);
$emotion2 = factory(Emotion::class)->create([]);
$emotionArray = [];
$emotionArray[] = $emotion->id;
$emotionArray[] = $emotion2->id;
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'called_at' => now(),
'content' => 'this is the content',
'contact_called' => true,
'emotions' => $emotionArray,
];
$call = app(CreateCall::class)->execute($request);
$this->assertDatabaseHas('calls', [
'id' => $call->id,
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'content' => 'this is the content',
'contact_called' => 1,
]);
$this->assertDatabaseHas('emotion_call', [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'call_id' => $call->id,
'emotion_id' => $emotion->id,
]);
$this->assertDatabaseHas('emotion_call', [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'call_id' => $call->id,
'emotion_id' => $emotion2->id,
]);
}
/** @test */
public function it_fails_adding_emotions_when_emotion_is_unknown()
{
$contact = factory(Contact::class)->create([]);
$emotionArray = [];
$emotionArray[] = 1111111;
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'called_at' => now(),
'content' => 'this is the content',
'contact_called' => true,
'emotions' => $emotionArray,
];
$this->expectException(ModelNotFoundException::class);
app(CreateCall::class)->execute($request);
}
/** @test */
public function it_stores_a_call_without_the_content()
{
$contact = factory(Contact::class)->create([]);
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'called_at' => now(),
];
$call = app(CreateCall::class)->execute($request);
$this->assertDatabaseHas('calls', [
'id' => $call->id,
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'content' => null,
]);
}
/** @test */
public function it_updates_the_last_call_info()
{
$contact = factory(Contact::class)->create([
'last_talked_to' => '1900-01-01 00:00:00',
]);
$date = now();
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'called_at' => $date,
];
app(CreateCall::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'last_talked_to' => $date->toDateString(),
]);
}
/** @test */
public function it_doesnt_update_the_last_call_info()
{
$contact = factory(Contact::class)->create([
'last_talked_to' => '2200-01-01 00:00:00',
]);
$date = now();
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'called_at' => $date,
];
app(CreateCall::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'last_talked_to' => '2200-01-01',
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$contact = factory(Contact::class)->create([]);
$request = [
'contact_id' => $contact->id,
'called_at' => now(),
];
$this->expectException(ValidationException::class);
app(CreateCall::class)->execute($request);
}
/** @test */
public function it_fails_if_contact_is_archived()
{
$contact = factory(Contact::class)->state('archived')->create([]);
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'called_at' => now(),
'content' => 'this is the content',
];
$this->expectException(ValidationException::class);
app(CreateCall::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_is_not_linked_to_account()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create();
$request = [
'contact_id' => $contact->id,
'account_id' => $account->id,
'called_at' => now(),
'content' => 'this is the content',
];
$this->expectException(ModelNotFoundException::class);
app(CreateCall::class)->execute($request);
}
}

View File

@@ -0,0 +1,148 @@
<?php
namespace Tests\Unit\Services\Contact\Call;
use Tests\TestCase;
use App\Models\Contact\Call;
use App\Models\Contact\Contact;
use Illuminate\Support\Facades\DB;
use App\Models\Instance\Emotion\Emotion;
use App\Services\Contact\Call\DestroyCall;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class DestroyCallTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_destroys_a_call()
{
$contact = factory(Contact::class)->create([]);
$call = factory(Call::class)->create([
'contact_id' => $contact->id,
'called_at' => '2008-01-01',
]);
$request = [
'account_id' => $call->account_id,
'call_id' => $call->id,
];
$this->assertDatabaseHas('calls', [
'id' => $call->id,
]);
app(DestroyCall::class)->execute($request);
$this->assertDatabaseMissing('calls', [
'id' => $call->id,
]);
}
/** @test */
public function it_removes_emotions()
{
$contact = factory(Contact::class)->create([]);
$call = factory(Call::class)->create([
'contact_id' => $contact->id,
]);
$emotion = factory(Emotion::class)->create([]);
DB::table('emotion_call')->insert([
'account_id' => $call->account_id,
'contact_id' => $call->contact_id,
'call_id' => $call->id,
'emotion_id' => $emotion->id,
]);
$request = [
'account_id' => $call->account_id,
'call_id' => $call->id,
];
app(DestroyCall::class)->execute($request);
$this->assertDatabaseMissing('emotion_call', [
'contact_id' => $call->contact_id,
'account_id' => $call->account_id,
'call_id' => $call->id,
'emotion_id' => $emotion->id,
]);
}
/** @test */
public function it_updates_the_last_talked_to_information()
{
$contact = factory(Contact::class)->create([
'last_talked_to' => '2008-01-01',
]);
$call = factory(Call::class)->create([
'contact_id' => $contact->id,
'called_at' => '2008-01-01',
]);
$call2 = factory(Call::class)->create([
'contact_id' => $contact->id,
'called_at' => '1990-01-01',
]);
$call3 = factory(Call::class)->create([
'contact_id' => $contact->id,
'called_at' => '1980-01-01',
]);
$request = [
'account_id' => $call->account_id,
'call_id' => $call->id,
];
app(DestroyCall::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'last_talked_to' => '1990-01-01',
]);
}
/** @test */
public function it_doesnt_update_the_last_talked_to_information()
{
$contact = factory(Contact::class)->create([
'last_talked_to' => '2008-01-01',
]);
$call = factory(Call::class)->create([
'contact_id' => $contact->id,
'called_at' => '2008-01-01',
]);
$request = [
'account_id' => $call->account_id,
'call_id' => $call->id,
];
app(DestroyCall::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'last_talked_to' => null,
]);
}
/** @test */
public function it_fails_if_contact_is_archived()
{
$contact = factory(Contact::class)->state('archived')->create([]);
$call = factory(Call::class)->create([
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
]);
$request = [
'account_id' => $call->account_id,
'call_id' => $call->id,
];
$this->expectException(ValidationException::class);
app(DestroyCall::class)->execute($request);
}
}

View File

@@ -0,0 +1,333 @@
<?php
namespace Tests\Unit\Services\Contact\Call;
use Tests\TestCase;
use App\Models\Contact\Call;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use Illuminate\Support\Facades\DB;
use App\Models\Instance\Emotion\Emotion;
use App\Services\Contact\Call\UpdateCall;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class UpdateCallTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_a_call()
{
$contact = factory(Contact::class)->create([]);
$call = factory(Call::class)->create([
'contact_id' => $contact,
'account_id' => $contact->account_id,
]);
$request = [
'account_id' => $call->account_id,
'call_id' => $call->id,
'called_at' => now(),
'content' => 'this is the content',
];
$call = app(UpdateCall::class)->execute($request);
$this->assertDatabaseHas('calls', [
'id' => $call->id,
'contact_id' => $call->contact_id,
'account_id' => $call->contact->account_id,
'content' => 'this is the content',
]);
$this->assertInstanceOf(
Call::class,
$call
);
}
/** @test */
public function it_updates_a_call_and_who_called_info()
{
$contact = factory(Contact::class)->create([]);
$call = factory(Call::class)->create([
'contact_id' => $contact,
'account_id' => $contact->account_id,
'contact_called' => 0,
]);
$request = [
'account_id' => $call->account_id,
'call_id' => $call->id,
'called_at' => now(),
'content' => 'this is the content',
'contact_called' => 1,
];
$call = app(UpdateCall::class)->execute($request);
$this->assertDatabaseHas('calls', [
'id' => $call->id,
'contact_id' => $call->contact_id,
'account_id' => $call->contact->account_id,
'content' => 'this is the content',
'contact_called' => 1,
]);
}
/** @test */
public function it_updates_a_call_without_the_content()
{
$contact = factory(Contact::class)->create([]);
$call = factory(Call::class)->create([
'contact_id' => $contact,
'account_id' => $contact->account_id,
]);
$request = [
'account_id' => $call->account_id,
'call_id' => $call->id,
'called_at' => now(),
];
$call = app(UpdateCall::class)->execute($request);
$this->assertDatabaseHas('calls', [
'id' => $call->id,
'contact_id' => $call->contact_id,
'account_id' => $call->contact->account_id,
'content' => null,
]);
}
/**
* Checks that it adds new emotions.
*/
/** @test */
public function it_updates_emotions()
{
$contact = factory(Contact::class)->create([]);
$call = factory(Call::class)->create([
'contact_id' => $contact->id,
]);
$emotion = factory(Emotion::class)->create([]);
$emotion2 = factory(Emotion::class)->create([]);
DB::table('emotion_call')->insert([
'account_id' => $call->account_id,
'contact_id' => $call->contact_id,
'call_id' => $call->id,
'emotion_id' => $emotion->id,
]);
$emotionArray = [];
$emotionArray[] = $emotion->id;
$emotionArray[] = $emotion2->id;
$request = [
'account_id' => $call->account_id,
'call_id' => $call->id,
'called_at' => now(),
'content' => 'this is the content',
'contact_called' => 1,
'emotions' => $emotionArray,
];
$call = app(UpdateCall::class)->execute($request);
$this->assertDatabaseHas('emotion_call', [
'contact_id' => $call->contact_id,
'account_id' => $call->account_id,
'call_id' => $call->id,
'emotion_id' => $emotion->id,
]);
$this->assertDatabaseHas('emotion_call', [
'contact_id' => $call->contact_id,
'account_id' => $call->account_id,
'call_id' => $call->id,
'emotion_id' => $emotion2->id,
]);
}
/**
* Checks that it removes old emotion and add new emotions.
*/
/** @test */
public function it_deletes_and_updates_emotions()
{
$contact = factory(Contact::class)->create([]);
$call = factory(Call::class)->create([
'contact_id' => $contact->id,
]);
$emotion = factory(Emotion::class)->create([]);
$emotion2 = factory(Emotion::class)->create([]);
DB::table('emotion_call')->insert([
'account_id' => $call->account_id,
'contact_id' => $call->contact_id,
'call_id' => $call->id,
'emotion_id' => $emotion->id,
]);
DB::table('emotion_call')->insert([
'account_id' => $call->account_id,
'contact_id' => $call->contact_id,
'call_id' => $call->id,
'emotion_id' => $emotion2->id,
]);
$emotion3 = factory(Emotion::class)->create([]);
$emotion4 = factory(Emotion::class)->create([]);
$emotionArray = [];
$emotionArray[] = $emotion3->id;
$emotionArray[] = $emotion4->id;
$request = [
'account_id' => $call->account_id,
'call_id' => $call->id,
'called_at' => now(),
'content' => 'this is the content',
'contact_called' => 1,
'emotions' => $emotionArray,
];
$call = app(UpdateCall::class)->execute($request);
$this->assertDatabaseHas('emotion_call', [
'contact_id' => $call->contact_id,
'account_id' => $call->account_id,
'call_id' => $call->id,
'emotion_id' => $emotion3->id,
]);
$this->assertDatabaseHas('emotion_call', [
'contact_id' => $call->contact_id,
'account_id' => $call->account_id,
'call_id' => $call->id,
'emotion_id' => $emotion4->id,
]);
$this->assertDatabaseMissing('emotion_call', [
'contact_id' => $call->contact_id,
'account_id' => $call->account_id,
'call_id' => $call->id,
'emotion_id' => $emotion->id,
]);
$this->assertDatabaseMissing('emotion_call', [
'contact_id' => $call->contact_id,
'account_id' => $call->account_id,
'call_id' => $call->id,
'emotion_id' => $emotion2->id,
]);
}
/** @test */
public function it_updates_the_last_call_info()
{
$contact = factory(Contact::class)->create([
'last_talked_to' => '1900-01-01 00:00:00',
]);
$call = factory(Call::class)->create([
'contact_id' => $contact,
'account_id' => $contact->account_id,
]);
$date = now();
$request = [
'account_id' => $call->account_id,
'call_id' => $call->id,
'called_at' => now(),
];
app(UpdateCall::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'last_talked_to' => $date->toDateString(),
]);
}
/** @test */
public function it_doesnt_update_the_last_call_info()
{
$contact = factory(Contact::class)->create([
'last_talked_to' => '2200-01-01 00:00:00',
]);
$call = factory(Call::class)->create([
'contact_id' => $contact,
'account_id' => $contact->account_id,
]);
$date = now();
$request = [
'account_id' => $call->account_id,
'call_id' => $call->id,
'called_at' => now(),
];
app(UpdateCall::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'last_talked_to' => '2200-01-01',
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$contact = factory(Contact::class)->create([]);
$request = [
'contact_id' => $contact->id,
'called_at' => now(),
];
$this->expectException(ValidationException::class);
app(UpdateCall::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_call_is_not_linked_to_account()
{
$account = factory(Account::class)->create();
$call = factory(Call::class)->create();
$request = [
'account_id' => $account->id,
'call_id' => $call->id,
'called_at' => now(),
];
$this->expectException(ModelNotFoundException::class);
app(UpdateCall::class)->execute($request);
}
/** @test */
public function it_fails_if_contact_is_archived()
{
$contact = factory(Contact::class)->state('archived')->create([]);
$call = factory(Call::class)->create([
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
]);
$request = [
'account_id' => $call->account_id,
'call_id' => $call->id,
'called_at' => now(),
'content' => 'this is the content',
];
$this->expectException(ValidationException::class);
app(UpdateCall::class)->execute($request);
}
}

View File

@@ -0,0 +1,232 @@
<?php
namespace Tests\Unit\Services\Contact\Contact;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Contact\Gender;
use function Safe\json_encode;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use Illuminate\Support\Facades\Queue;
use App\Jobs\AuditLog\LogAccountAudit;
use App\Models\Contact\ContactFieldType;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Contact\CreateContact;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class CreateContactTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_stores_a_contact()
{
$account = factory(Account::class)->create([]);
$user = factory(User::class)->create([
'account_id' => $account->id,
]);
$gender = factory(Gender::class)->create([
'account_id' => $account->id,
]);
$request = [
'account_id' => $account->id,
'author_id' => $user->id,
'first_name' => 'john',
'middle_name' => 'franck',
'last_name' => 'doe',
'gender_id' => $gender->id,
'description' => 'this is a test',
'is_partial' => false,
'is_birthdate_known' => false,
'is_deceased' => false,
'is_deceased_date_known' => false,
];
$contact = app(CreateContact::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'first_name' => 'john',
]);
// check that a default color has been set
$this->assertNotNull($contact->default_avatar_color);
// check that the default avatar has been generated
$this->assertNotNull($contact->avatar_adorable_uuid);
$this->assertNotNull($contact->avatar_adorable_url);
$this->assertNotNull($contact->avatar_default_url);
$this->assertInstanceOf(
Contact::class,
$contact
);
}
/** @test */
public function it_stores_a_contact_with_email()
{
$account = factory(Account::class)->create([]);
$user = factory(User::class)->create([
'account_id' => $account->id,
]);
factory(ContactFieldType::class)->create([
'account_id' => $account->id,
'type' => 'email',
]);
$request = [
'account_id' => $account->id,
'author_id' => $user->id,
'first_name' => 'john',
'last_name' => 'doe',
'email' => 'email@example.com',
'is_birthdate_known' => false,
'is_deceased' => false,
'is_deceased_date_known' => false,
];
$contact = app(CreateContact::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'first_name' => 'john',
]);
$this->assertDatabaseHas('contact_fields', [
'account_id' => $account->id,
'contact_id' => $contact->id,
'data' => 'email@example.com',
]);
}
/** @test */
public function it_stores_a_contact_and_triggers_an_audit_log()
{
Queue::fake();
$account = factory(Account::class)->create([]);
$user = factory(User::class)->create([
'account_id' => $account->id,
]);
$gender = factory(Gender::class)->create([
'account_id' => $account->id,
]);
$request = [
'account_id' => $account->id,
'author_id' => $user->id,
'first_name' => 'john',
'middle_name' => 'franck',
'last_name' => 'doe',
'gender_id' => $gender->id,
'description' => 'this is a test',
'is_partial' => false,
'is_birthdate_known' => false,
'is_deceased' => false,
'is_deceased_date_known' => false,
];
$contact = app(CreateContact::class)->execute($request);
Queue::assertPushed(LogAccountAudit::class, function ($job) use ($contact, $user) {
return $job->auditLog['action'] === 'contact_created' &&
$job->auditLog['author_id'] === $user->id &&
$job->auditLog['about_contact_id'] === $contact->id &&
$job->auditLog['should_appear_on_dashboard'] === true &&
$job->auditLog['objects'] === json_encode([
'contact_name' => $contact->name,
'contact_id' => $contact->id,
]);
});
}
/** @test */
public function it_stores_a_contact_without_gender()
{
$account = factory(Account::class)->create([]);
$user = factory(User::class)->create([
'account_id' => $account->id,
]);
$gender = factory(Gender::class)->create([
'account_id' => $account->id,
]);
$request = [
'account_id' => $account->id,
'author_id' => $user->id,
'first_name' => 'john',
'middle_name' => 'franck',
'last_name' => 'doe',
'description' => 'this is a test',
'is_partial' => false,
'is_birthdate_known' => false,
'is_deceased' => false,
'is_deceased_date_known' => false,
];
$contact = app(CreateContact::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'first_name' => 'john',
'gender_id' => null,
]);
$this->assertInstanceOf(
Contact::class,
$contact
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$account = factory(Account::class)->create([]);
$gender = factory(Gender::class)->create([
'account_id' => $account->id,
]);
$request = [
'account_id' => $account->id,
'middle_name' => 'franck',
'last_name' => 'doe',
'gender_id' => $gender->id,
'description' => 'this is a test',
'is_partial' => false,
'is_birthdate_known' => false,
'is_deceased' => false,
'is_deceased_date_known' => false,
];
$this->expectException(ValidationException::class);
app(CreateContact::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_account_doesnt_exist()
{
$gender = factory(Gender::class)->create([]);
$request = [
'account_id' => 111111111,
'middle_name' => 'franck',
'last_name' => 'doe',
'gender_id' => $gender->id,
'description' => 'this is a test',
'is_partial' => false,
'is_birthdate_known' => false,
'is_deceased' => false,
'is_deceased_date_known' => false,
];
$this->expectException(ValidationException::class);
app(CreateContact::class)->execute($request);
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace Tests\Unit\Services\Contact\Contact;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Contact\DeleteMeContact;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class DeleteMeContactTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_set_me_as_a_a_contact()
{
$user = factory(User::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $user->account->id,
]);
$user->me_contact_id = $contact->id;
$user->save();
$request = [
'account_id' => $user->account->id,
'user_id' => $user->id,
];
$user = app(DeleteMeContact::class)->execute($request);
$this->assertDatabaseHas('users', [
'id' => $user->id,
'account_id' => $user->account->id,
'me_contact_id' => null,
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$account = factory(Account::class)->create();
$request = [
'account_id' => $account->id,
'user_id' => 0,
];
$this->expectException(ValidationException::class);
app(DeleteMeContact::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_not_found()
{
$account = factory(Account::class)->create();
$user = factory(User::class)->create();
$request = [
'account_id' => $account->id,
'user_id' => $user->id,
];
$this->expectException(ModelNotFoundException::class);
app(DeleteMeContact::class)->execute($request);
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace Tests\Unit\Services\Contact\Contact;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Contact\DestroyContact;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class DestroyContactTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_destroys_a_contact()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
];
app(DestroyContact::class)->handle($request);
$this->assertDatabaseMissing('contacts', [
'id' => $contact->id,
'deleted_at' => null,
]);
}
/** @test */
public function it_fails_if_contact_is_archived()
{
$contact = factory(Contact::class)->state('archived')->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
];
$this->expectException(ValidationException::class);
app(DestroyContact::class)->handle($request);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
];
$this->expectException(ValidationException::class);
app(DestroyContact::class)->handle($request);
}
/** @test */
public function it_throws_an_exception_if_contact_doesnt_exist()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $account->id,
'contact_id' => $contact->id,
];
$this->expectException(ModelNotFoundException::class);
app(DestroyContact::class)->handle($request);
}
}

View File

@@ -0,0 +1,73 @@
<?php
namespace Tests\Unit\Services\Contact\Contact;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Contact\Contact;
use App\Services\Contact\Contact\SetMeContact;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class SetMeContactTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_set_me_as_a_a_contact()
{
$user = factory(User::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $user->account_id,
]);
$request = [
'account_id' => $user->account_id,
'user_id' => $user->id,
'contact_id' => $contact->id,
];
$user = app(SetMeContact::class)->execute($request);
$this->assertDatabaseHas('users', [
'id' => $user->id,
'account_id' => $user->account_id,
'me_contact_id' => $contact->id,
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$user = factory(User::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $user->account_id,
]);
$request = [
'account_id' => $user->account_id,
'user_id' => $user->id,
'contact_id' => 0,
];
$this->expectException(ValidationException::class);
app(SetMeContact::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_not_found()
{
$user = factory(User::class)->create();
$contact = factory(Contact::class)->create();
$request = [
'account_id' => $user->account_id,
'user_id' => $user->id,
'contact_id' => $contact->id,
];
$this->expectException(ModelNotFoundException::class);
app(SetMeContact::class)->execute($request);
}
}

View File

@@ -0,0 +1,197 @@
<?php
namespace Tests\Unit\Services\Contact\Contact;
use Tests\TestCase;
use App\Models\Contact\Contact;
use App\Models\Instance\SpecialDate;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\Contact\Contact\UpdateBirthdayInformation;
class UpdateBirthdayInformationTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_deletes_all_birthday_information()
{
// to delete birthday information, we need first to update the contact
// with its birthday info, then update it again by indicating that
// we don't know his birthday info
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'is_date_known' => true,
'day' => 10,
'month' => 10,
'year' => 1980,
'is_age_based' => false,
'age' => 0,
'add_reminder' => true,
'is_deceased' => false,
];
app(UpdateBirthdayInformation::class)->execute($request);
$specialDate = SpecialDate::where('contact_id', $contact->id)->first();
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'birthday_special_date_id' => $specialDate->id,
]);
$this->assertDatabaseHas('special_dates', [
'id' => $specialDate->id,
'account_id' => $contact->account_id,
'is_age_based' => false,
]);
// then we update it again
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'is_date_known' => false,
];
$contact = app(UpdateBirthdayInformation::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'birthday_special_date_id' => null,
]);
}
/** @test */
public function it_sets_a_date_if_age_is_provided()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'is_date_known' => true,
'is_age_based' => true,
'age' => 10,
];
$contact = app(UpdateBirthdayInformation::class)->execute($request);
$specialDate = SpecialDate::where('contact_id', $contact->id)->first();
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'birthday_special_date_id' => $specialDate->id,
]);
$this->assertDatabaseHas('special_dates', [
'id' => $specialDate->id,
'account_id' => $contact->account_id,
'is_age_based' => true,
]);
}
/** @test */
public function it_sets_a_complete_date()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'is_date_known' => true,
'day' => 10,
'month' => 10,
'year' => 1980,
'is_age_based' => false,
'add_reminder' => false,
];
$contact = app(UpdateBirthdayInformation::class)->execute($request);
$specialDate = SpecialDate::where('contact_id', $contact->id)->first();
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'birthday_special_date_id' => $specialDate->id,
]);
$this->assertDatabaseHas('special_dates', [
'id' => $specialDate->id,
'account_id' => $contact->account_id,
'is_age_based' => false,
'is_year_unknown' => false,
]);
}
/** @test */
public function it_sets_a_complete_date_and_sets_a_reminder()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'is_date_known' => true,
'day' => 10,
'month' => 10,
'year' => 1980,
'is_age_based' => false,
'add_reminder' => true,
'is_deceased' => false,
];
$contact = app(UpdateBirthdayInformation::class)->execute($request);
$specialDate = SpecialDate::where('contact_id', $contact->id)->first();
$this->assertNotNull($contact->birthday_reminder_id);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'day' => 10,
'month' => 10,
'year' => 1980,
'is_age_based' => false,
'add_reminder' => false,
];
$this->expectException(ValidationException::class);
app(UpdateBirthdayInformation::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_and_account_are_not_linked()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => 11111111,
'contact_id' => $contact->id,
'is_date_known' => true,
'day' => 10,
'month' => 10,
'year' => 1980,
'is_age_based' => false,
'add_reminder' => false,
];
$this->expectException(ValidationException::class);
app(UpdateBirthdayInformation::class)->execute($request);
}
}

View File

@@ -0,0 +1,162 @@
<?php
namespace Tests\Unit\Services\Contact\Contact;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Contact\Contact;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Contact\UpdateContact;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class UpdateContactTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_a_contact()
{
$contact = factory(Contact::class)->create([]);
$user = factory(User::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'author_id' => $user->id,
'contact_id' => $contact->id,
'first_name' => 'john',
'middle_name' => 'franck',
'last_name' => 'doe',
'gender_id' => $contact->gender_id,
'description' => 'this is a test',
'is_partial' => false,
'is_birthdate_known' => true,
'birthdate_day' => 10,
'birthdate_month' => 10,
'birthdate_year' => 1980,
'birthdate_is_age_based' => false,
'birthdate_age' => 0,
'birthdate_add_reminder' => false,
'is_deceased' => true,
'is_deceased_date_known' => true,
'deceased_date_day' => 10,
'deceased_date_month' => 10,
'deceased_date_year' => 1980,
'deceased_date_add_reminder' => true,
];
$contact = app(UpdateContact::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'first_name' => 'john',
]);
$this->assertInstanceOf(
Contact::class,
$contact
);
}
/** @test */
public function it_fails_if_contact_is_archived()
{
$contact = factory(Contact::class)->state('archived')->create([]);
$user = factory(User::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'author_id' => $user->id,
'contact_id' => $contact->id,
'first_name' => 'john',
'middle_name' => 'franck',
'last_name' => 'doe',
'gender_id' => $contact->gender_id,
'description' => 'this is a test',
'is_partial' => false,
'is_birthdate_known' => true,
'birthdate_day' => 10,
'birthdate_month' => 10,
'birthdate_year' => 1980,
'birthdate_is_age_based' => false,
'birthdate_age' => 0,
'birthdate_add_reminder' => false,
'is_deceased' => true,
'is_deceased_date_known' => true,
'deceased_date_day' => 10,
'deceased_date_month' => 10,
'deceased_date_year' => 1980,
'deceased_date_add_reminder' => true,
];
$this->expectException(ValidationException::class);
app(UpdateContact::class)->execute($request);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'middle_name' => 'franck',
'last_name' => 'doe',
'gender_id' => $contact->gender_id,
'description' => 'this is a test',
'is_partial' => false,
'is_birthdate_known' => true,
'birthdate_day' => 10,
'birthdate_month' => 10,
'birthdate_year' => 1980,
'birthdate_is_age_based' => false,
'birthdate_age' => 0,
'birthdate_add_reminder' => false,
'is_deceased' => true,
'is_deceased_date_known' => true,
'deceased_date_day' => 10,
'deceased_date_month' => 10,
'deceased_date_year' => 1980,
'deceased_date_add_reminder' => true,
];
$this->expectException(ValidationException::class);
app(UpdateContact::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_account_doesnt_exist()
{
$contact = factory(Contact::class)->create([]);
$user = factory(User::class)->create([]);
$request = [
'account_id' => 11111,
'author_id' => $user->id,
'contact_id' => $contact->id,
'first_name' => 'john',
'middle_name' => 'franck',
'last_name' => 'doe',
'gender_id' => $contact->gender_id,
'description' => 'this is a test',
'is_partial' => false,
'is_birthdate_known' => true,
'birthdate_day' => 10,
'birthdate_month' => 10,
'birthdate_year' => 1980,
'birthdate_is_age_based' => false,
'birthdate_age' => 0,
'birthdate_add_reminder' => false,
'is_deceased' => true,
'is_deceased_date_known' => true,
'deceased_date_day' => 10,
'deceased_date_month' => 10,
'deceased_date_year' => 1980,
'deceased_date_add_reminder' => true,
];
$this->expectException(ValidationException::class);
app(UpdateContact::class)->execute($request);
}
}

View File

@@ -0,0 +1,249 @@
<?php
namespace Tests\Unit\Services\Contact\Contact;
use Tests\TestCase;
use App\Models\Contact\Contact;
use App\Models\Contact\Reminder;
use App\Models\Instance\SpecialDate;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\Contact\Contact\UpdateDeceasedInformation;
class UpdateDeceasedInformationTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_sets_contact_as_not_deceased()
{
// first we are going to update a contact and set it as deceased,
// then we are going to update it again and set it as non deceased
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'is_deceased' => true,
'is_date_known' => false,
'add_reminder' => false,
];
app(UpdateDeceasedInformation::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'is_dead' => 1,
]);
// now set the contact as not dead anymore (a zombie, basically)
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'is_deceased' => false,
'is_date_known' => false,
'add_reminder' => false,
];
app(UpdateDeceasedInformation::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'is_dead' => 0,
'deceased_special_date_id' => null,
'deceased_reminder_id' => null,
]);
}
/** @test */
public function it_sets_a_complete_date()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'is_deceased' => true,
'is_date_known' => true,
'day' => 10,
'month' => 10,
'year' => 1980,
'add_reminder' => false,
];
$contact = app(UpdateDeceasedInformation::class)->execute($request);
$specialDate = SpecialDate::where('contact_id', $contact->id)->first();
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'deceased_special_date_id' => $specialDate->id,
]);
$this->assertDatabaseHas('special_dates', [
'id' => $specialDate->id,
'account_id' => $contact->account_id,
'is_age_based' => false,
'is_year_unknown' => false,
]);
}
/** @test */
public function it_sets_a_complete_date_with_unknown_year()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'is_deceased' => true,
'is_date_known' => true,
'day' => 10,
'month' => 10,
'year' => 0,
'add_reminder' => false,
];
$contact = app(UpdateDeceasedInformation::class)->execute($request);
$specialDate = SpecialDate::where('contact_id', $contact->id)->first();
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'deceased_special_date_id' => $specialDate->id,
]);
$this->assertDatabaseHas('special_dates', [
'id' => $specialDate->id,
'account_id' => $contact->account_id,
'is_age_based' => false,
'is_year_unknown' => true,
]);
}
/** @test */
public function it_sets_a_complete_date_and_sets_a_reminder()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'is_deceased' => true,
'is_date_known' => true,
'day' => 10,
'month' => 10,
'year' => 1980,
'add_reminder' => true,
];
$contact = app(UpdateDeceasedInformation::class)->execute($request);
$specialDate = SpecialDate::where('contact_id', $contact->id)->first();
$reminder = Reminder::where('contact_id', $contact->id)->first();
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'deceased_special_date_id' => $specialDate->id,
'deceased_reminder_id' => $reminder->id,
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'is_date_known' => true,
'day' => 10,
'month' => 10,
'year' => 1980,
'add_reminder' => false,
];
$this->expectException(ValidationException::class);
app(UpdateDeceasedInformation::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_and_account_are_not_linked()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => 11111111,
'contact_id' => $contact->id,
'is_deceased' => true,
'is_date_known' => true,
'day' => 10,
'month' => 10,
'year' => 1980,
'add_reminder' => false,
];
$this->expectException(ValidationException::class);
app(UpdateDeceasedInformation::class)->execute($request);
}
/** @test */
public function it_removes_deceased_reminder()
{
$reminder = factory(Reminder::class)->create([]);
$contact = factory(Contact::class)->create([
'account_id' => $reminder->account_id,
'deceased_reminder_id' => $reminder->id,
]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'is_deceased' => true,
'is_date_known' => true,
'day' => 10,
'month' => 10,
'year' => 1980,
'add_reminder' => true,
];
app(UpdateDeceasedInformation::class)->execute($request);
$this->assertDatabaseMissing('reminders', [
'id' => $reminder->id,
]);
}
/** @test */
public function it_removes_deceased_special_date()
{
$special_date = factory(SpecialDate::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $special_date->account_id,
'deceased_special_date_id' => $special_date->id,
]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'is_deceased' => true,
'is_date_known' => true,
'day' => 10,
'month' => 10,
'year' => 1980,
'add_reminder' => true,
];
app(UpdateDeceasedInformation::class)->execute($request);
$this->assertDatabaseMissing('special_dates', [
'id' => $special_date->id,
]);
}
}

View File

@@ -0,0 +1,114 @@
<?php
namespace Tests\Unit\Services\Contact\Contact;
use Tests\TestCase;
use App\Models\User\User;
use function Safe\json_encode;
use App\Models\Contact\Contact;
use Illuminate\Support\Facades\Queue;
use App\Jobs\AuditLog\LogAccountAudit;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Contact\CreateContact;
use App\Services\Contact\Contact\UpdateWorkInformation;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class UpdateWorkInformationTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_work_information()
{
Queue::fake();
$user = factory(User::class)->create([]);
$contact = factory(Contact::class)->create([
'account_id' => $user->account_id,
]);
$request = [
'account_id' => $user->account_id,
'author_id' => $user->id,
'contact_id' => $contact->id,
'job' => 'Dunder',
];
$contact = app(UpdateWorkInformation::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'job' => 'Dunder',
'company' => null,
]);
$this->assertInstanceOf(
Contact::class,
$contact
);
$contact = factory(Contact::class)->create([
'account_id' => $user->account_id,
]);
$request = [
'account_id' => $user->account_id,
'author_id' => $user->id,
'contact_id' => $contact->id,
'company' => 'Sales',
];
$contact = app(UpdateWorkInformation::class)->execute($request);
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'account_id' => $contact->account_id,
'job' => null,
'company' => 'Sales',
]);
Queue::assertPushed(LogAccountAudit::class, function ($job) use ($contact, $user) {
return $job->auditLog['action'] === 'contact_work_updated' &&
$job->auditLog['author_id'] === $user->id &&
$job->auditLog['about_contact_id'] === $contact->id &&
$job->auditLog['should_appear_on_dashboard'] === true &&
$job->auditLog['objects'] === json_encode([
'contact_name' => $contact->name,
'contact_id' => $contact->id,
]);
});
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$user = factory(User::class)->create([]);
$request = [
'account_id' => $user->account_id,
];
$this->expectException(ValidationException::class);
app(UpdateWorkInformation::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_account_doesnt_exist()
{
$user = factory(User::class)->create([]);
$contact = factory(Contact::class)->create([
'account_id' => $user->account_id,
]);
$request = [
'account_id' => 111111111,
'author_id' => $user->id,
'contact_id' => $contact->id,
'job' => 'Dunder',
];
$this->expectException(ValidationException::class);
app(CreateContact::class)->execute($request);
}
}

View File

@@ -0,0 +1,117 @@
<?php
namespace Tests\Unit\Services\Contact\ContactField;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Contact\ContactFieldType;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\Contact\ContactField\CreateContactField;
class CreateContactFieldTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_stores_a_contact_field()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $account->id,
]);
$contactField = app(CreateContactField::class)->execute([
'account_id' => $account->id,
'contact_id' => $contact->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => 'john@doe.com',
]);
$this->assertDatabaseHas('contact_fields', [
'id' => $contactField->id,
'account_id' => $account->id,
'data' => 'john@doe.com',
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $account->id,
]);
$this->expectException(ValidationException::class);
app(CreateContactField::class)->execute([
'account_id' => $account->id,
'contact_id' => $contact->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => '',
]);
}
/** @test */
public function it_throws_an_exception_if_account_doesnt_exist()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $account->id,
]);
$this->expectException(ValidationException::class);
app(CreateContactField::class)->execute([
'account_id' => -1,
'contact_id' => $contact->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => 'john@doe.com',
]);
}
/** @test */
public function it_throws_an_exception_if_contact_use_wrong_account()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create();
$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $account->id,
]);
$this->expectException(ValidationException::class);
app(CreateContactField::class)->execute([
'account_id' => $account->id,
'contact_id' => $contact->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => '',
]);
}
/** @test */
public function it_throws_an_exception_if_contact_field_use_wrong_account()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$contactFieldType = factory(ContactFieldType::class)->create();
$this->expectException(ValidationException::class);
app(CreateContactField::class)->execute([
'account_id' => $account->id,
'contact_id' => $contact->id,
'contact_field_type_id' => $contactFieldType->id,
'data' => '',
]);
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace Tests\Unit\Services\Contact\ContactField;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\ContactField;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Services\Contact\ContactField\DestroyContactField;
class DestroyContactFieldTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_destroys_a_contact_field()
{
$contactField = factory(ContactField::class)->create();
$request = [
'account_id' => $contactField->account_id,
'contact_field_id' => $contactField->id,
];
app(DestroyContactField::class)->execute($request);
$this->assertDatabaseMissing('contact_fields', [
'id' => $contactField->id,
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$account = factory(Account::class)->create();
$request = [
'account_id' => $account->id,
];
$this->expectException(ValidationException::class);
app(DestroyContactField::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_field_doesnt_exist()
{
$account = factory(Account::class)->create();
$request = [
'account_id' => $account->id,
'contact_field_id' => -1,
];
$this->expectException(ValidationException::class);
app(DestroyContactField::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_field_use_wrong_account()
{
$account = factory(Account::class)->create();
$contactField = factory(ContactField::class)->create();
$request = [
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
];
$this->expectException(ModelNotFoundException::class);
app(DestroyContactField::class)->execute($request);
}
}

View File

@@ -0,0 +1,107 @@
<?php
namespace Tests\Unit\Services\Contact\ContactField;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\ContactField;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Services\Contact\ContactField\UpdateContactField;
class UpdateContactFieldTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_a_contact_field()
{
$contactField = factory(ContactField::class)->create();
$request = [
'account_id' => $contactField->account_id,
'contact_field_id' => $contactField->id,
'contact_id' => $contactField->contact_id,
'contact_field_type_id' => $contactField->contactFieldType->id,
'data' => 'mark@twain.com',
];
$contactField = app(UpdateContactField::class)->execute($request);
$this->assertDatabaseHas('contact_fields', [
'id' => $contactField->id,
'account_id' => $contactField->account_id,
'data' => 'mark@twain.com',
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$contactField = factory(ContactField::class)->create();
$request = [
'account_id' => $contactField->account_id,
'contact_field_id' => $contactField->id,
'contact_id' => $contactField->contact_id,
'contact_field_type_id' => $contactField->contactFieldType->id,
'data' => null,
];
$this->expectException(ValidationException::class);
app(UpdateContactField::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_account_doesnt_exist()
{
$contactField = factory(ContactField::class)->create([]);
$request = [
'account_id' => -1,
'contact_field_id' => $contactField->id,
'contact_id' => $contactField->contact_id,
'contact_field_type_id' => $contactField->contactFieldType->id,
'data' => 'mark@twain.com',
];
$this->expectException(ValidationException::class);
app(UpdateContactField::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_field_doesnt_exist()
{
$contactField = factory(ContactField::class)->create();
$request = [
'account_id' => $contactField->account_id,
'contact_field_id' => -1,
'contact_id' => $contactField->contact_id,
'contact_field_type_id' => $contactField->contactFieldType->id,
'data' => 'mark@twain.com',
];
$this->expectException(ValidationException::class);
app(UpdateContactField::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_field_type_is_wrong_account()
{
$account = factory(Account::class)->create();
$contactField = factory(ContactField::class)->create();
$request = [
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'contact_id' => $contactField->contact_id,
'contact_field_type_id' => $contactField->contactFieldType->id,
'data' => 'mark@twain.com',
];
$this->expectException(ModelNotFoundException::class);
app(UpdateContactField::class)->execute($request);
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace Tests\Unit\Services\Contact\Conversation;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Contact\Message;
use App\Models\Contact\Conversation;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Services\Contact\Conversation\AddMessageToConversation;
class AddMessageToConversationTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$request = [
'contact_id' => 1,
'happened_at' => now(),
];
$this->expectException(ValidationException::class);
app(AddMessageToConversation::class)->execute($request);
}
/** @test */
public function it_stores_a_message()
{
$conversation = factory(Conversation::class)->create([]);
$request = [
'account_id' => $conversation->account_id,
'contact_id' => $conversation->contact_id,
'conversation_id' => $conversation->id,
'written_by_me' => true,
'written_at' => now(),
'content' => 'lorem ipsum',
];
$message = app(AddMessageToConversation::class)->execute($request);
$this->assertDatabaseHas('messages', [
'id' => $message->id,
'conversation_id' => $conversation->id,
'contact_id' => $message->contact_id,
'account_id' => $message->account_id,
'written_by_me' => true,
'content' => 'lorem ipsum',
]);
$this->assertInstanceOf(
Message::class,
$message
);
}
/** @test */
public function it_throws_an_exception_if_contact_is_not_found()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$conversation = factory(Conversation::class)->create([
'account_id' => $account->id,
]);
$request = [
'conversation_id' => $conversation->id,
'contact_id' => $contact->id,
'account_id' => $account->id,
'written_by_me' => true,
'written_at' => now(),
'content' => 'lorem ipsum',
];
$this->expectException(ModelNotFoundException::class);
app(AddMessageToConversation::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_conversation_is_not_found2()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$conversation = factory(Conversation::class)->create([
'contact_id' => $contact->id,
]);
$request = [
'conversation_id' => $conversation->id,
'contact_id' => $contact->id,
'account_id' => $account->id,
'written_by_me' => true,
'written_at' => now(),
'content' => 'lorem ipsum',
];
$this->expectException(ModelNotFoundException::class);
app(AddMessageToConversation::class)->execute($request);
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace Tests\Unit\Services\Contact\Conversation;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Contact\Conversation;
use App\Models\Contact\ContactFieldType;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Services\Contact\Conversation\CreateConversation;
class CreateConversationTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_stores_a_conversation()
{
$contact = factory(Contact::class)->create([]);
$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $contact->account_id,
]);
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'happened_at' => now(),
'contact_field_type_id' => $contactFieldType->id,
];
$conversation = app(CreateConversation::class)->execute($request);
$this->assertDatabaseHas('conversations', [
'id' => $conversation->id,
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'contact_field_type_id' => $contactFieldType->id,
]);
$this->assertInstanceOf(
Conversation::class,
$conversation
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$contact = factory(Contact::class)->create([]);
$request = [
'contact_id' => $contact->id,
'happened_at' => now(),
];
$this->expectException(ValidationException::class);
app(CreateConversation::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_is_not_linked_to_account()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create();
$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $contact->account_id,
]);
$request = [
'contact_id' => $contact->id,
'account_id' => $account->id,
'happened_at' => now(),
'contact_field_type_id' => $contactFieldType->id,
];
$this->expectException(ModelNotFoundException::class);
app(CreateConversation::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contactfieldtype_is_not_linked_to_account()
{
$contact = factory(Contact::class)->create([]);
$contactFieldType = factory(ContactFieldType::class)->create([]);
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'happened_at' => now(),
'contact_field_type_id' => $contactFieldType->id,
];
$this->expectException(ModelNotFoundException::class);
app(CreateConversation::class)->execute($request);
}
}

View File

@@ -0,0 +1,104 @@
<?php
namespace Tests\Unit\Services\Contact\Conversation;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Message;
use App\Models\Contact\Conversation;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Services\Contact\Conversation\DestroyConversation;
class DestroyConversationTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_destroys_a_conversation()
{
$conversation = factory(Conversation::class)->create([
'happened_at' => '2008-01-01',
]);
$request = [
'account_id' => $conversation->account_id,
'conversation_id' => $conversation->id,
];
$this->assertDatabaseHas('conversations', [
'id' => $conversation->id,
]);
app(DestroyConversation::class)->execute($request);
$this->assertDatabaseMissing('conversations', [
'id' => $conversation->id,
]);
}
/** @test */
public function destroying_a_conversation_destroys_corresponding_messages()
{
$conversation = factory(Conversation::class)->create([
'happened_at' => '2008-01-01',
]);
$message = factory(Message::class)->create([
'conversation_id' => $conversation->id,
'account_id' => $conversation->account_id,
'contact_id' => $conversation->contact_id,
'content' => 'tititi',
'written_at' => '2009-01-01',
'written_by_me' => false,
]);
$this->assertDatabaseHas('messages', [
'id' => $message->id,
]);
$request = [
'account_id' => $conversation->account_id,
'conversation_id' => $conversation->id,
];
app(DestroyConversation::class)->execute($request);
$this->assertDatabaseMissing('messages', [
'id' => $message->id,
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$conversation = factory(Conversation::class)->create([
'happened_at' => '2008-01-01',
]);
$request = [
'account_id' => $conversation->account_id,
];
$this->expectException(ValidationException::class);
app(DestroyConversation::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_conversation_doesnt_exist()
{
$account = factory(Account::class)->create();
$conversation = factory(Conversation::class)->create([]);
$request = [
'account_id' => $account->id,
'conversation_id' => $conversation->id,
];
$this->expectException(ModelNotFoundException::class);
app(DestroyConversation::class)->execute($request);
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace Tests\Unit\Services\Contact\Conversation;
use Tests\TestCase;
use App\Models\Contact\Message;
use App\Models\Contact\Conversation;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Conversation\DestroyMessage;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class DestroyMessageTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_destroys_a_message()
{
$conversation = factory(Conversation::class)->create([]);
$message = factory(Message::class)->create([
'conversation_id' => $conversation->id,
'account_id' => $conversation->account_id,
'contact_id' => $conversation->contact_id,
'content' => 'tititi',
'written_at' => '2009-01-01',
'written_by_me' => false,
]);
$request = [
'account_id' => $conversation->account_id,
'conversation_id' => $conversation->id,
'message_id' => $message->id,
];
$this->assertDatabaseHas('messages', [
'id' => $message->id,
]);
app(DestroyMessage::class)->execute($request);
$this->assertDatabaseMissing('messages', [
'id' => $message->id,
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$request = [
'conversation_id' => 2,
'message_id' => 3,
];
$this->expectException(ValidationException::class);
app(DestroyMessage::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_message_doesnt_exist()
{
$conversation = factory(Conversation::class)->create([]);
$message = factory(Message::class)->create([]);
$request = [
'account_id' => $conversation->account_id,
'conversation_id' => $conversation->id,
'message_id' => $message->id,
];
$this->expectException(ModelNotFoundException::class);
app(DestroyMessage::class)->execute($request);
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace Tests\Unit\Services\Contact\Conversation;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Contact\Conversation;
use App\Models\Contact\ContactFieldType;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Services\Contact\Conversation\UpdateConversation;
class UpdateConversationTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_a_conversation()
{
$conversation = factory(Conversation::class)->create([
'happened_at' => '2008-01-01',
]);
$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $conversation->account_id,
]);
$request = [
'account_id' => $conversation->account_id,
'conversation_id' => $conversation->id,
'happened_at' => '2010-02-02',
'contact_field_type_id' => $contactFieldType->id,
];
$conversation = app(UpdateConversation::class)->execute($request);
$this->assertDatabaseHas('conversations', [
'id' => $conversation->id,
'happened_at' => '2010-02-02 00:00:00',
'contact_field_type_id' => $contactFieldType->id,
]);
$this->assertInstanceOf(
Conversation::class,
$conversation
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$contact = factory(Contact::class)->create([]);
$request = [
'contact_id' => $contact->id,
'happened_at' => now(),
];
$this->expectException(ValidationException::class);
app(UpdateConversation::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_conversation_doesnt_exist()
{
$account = factory(Account::class)->create();
$conversation = factory(Conversation::class)->create([]);
$contactFieldType = factory(ContactFieldType::class)->create([
'account_id' => $conversation->account_id,
]);
$request = [
'account_id' => $account->id,
'conversation_id' => $conversation->id,
'happened_at' => '2010-02-02',
'contact_field_type_id' => $contactFieldType->id,
];
$this->expectException(ModelNotFoundException::class);
app(UpdateConversation::class)->execute($request);
}
}

View File

@@ -0,0 +1,104 @@
<?php
namespace Tests\Unit\Services\Contact\Conversation;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Contact\Message;
use App\Models\Contact\Conversation;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Conversation\UpdateMessage;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class UpdateMessageTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_a_conversation()
{
$conversation = factory(Conversation::class)->create([]);
$message = factory(Message::class)->create([
'conversation_id' => $conversation->id,
'account_id' => $conversation->account_id,
'contact_id' => $conversation->contact_id,
'content' => 'tititi',
'written_at' => '2009-01-01',
'written_by_me' => false,
]);
$request = [
'account_id' => $conversation->account_id,
'contact_id' => $conversation->contact_id,
'conversation_id' => $conversation->id,
'message_id' => $message->id,
'written_at' => now(),
'written_by_me' => true,
'content' => 'lorem',
];
$message = app(UpdateMessage::class)->execute($request);
$this->assertDatabaseHas('messages', [
'id' => $message->id,
'account_id' => $conversation->account_id,
'contact_id' => $conversation->contact_id,
'conversation_id' => $conversation->id,
'written_by_me' => true,
'content' => 'lorem',
]);
$this->assertInstanceOf(
Message::class,
$message
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$request = [
'account_id' => 1,
'conversation_id' => 2,
'message_id' => 3,
'written_at' => now(),
'written_by_me' => true,
'content' => 'lorem',
];
$this->expectException(ValidationException::class);
app(UpdateMessage::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_message_does_not_exist()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$conversation = factory(Conversation::class)->create([
'account_id' => $account->id,
'contact_id' => $contact->id,
]);
$message = factory(Message::class)->create([]);
$request = [
'account_id' => $account->id,
'contact_id' => $contact->id,
'conversation_id' => $conversation->id,
'message_id' => $message->id,
'written_at' => now(),
'written_by_me' => true,
'content' => 'lorem',
];
$this->expectException(ModelNotFoundException::class);
app(UpdateMessage::class)->execute($request);
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace Tests\Unit\Services\Contact\Description;
use Tests\TestCase;
use App\Models\User\User;
use function Safe\json_encode;
use App\Models\Contact\Contact;
use Illuminate\Support\Facades\Queue;
use App\Jobs\AuditLog\LogAccountAudit;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\Contact\Description\ClearPersonalDescription;
class ClearPersonalDescriptionTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_clears_a_personal_description(): void
{
Queue::fake();
$contact = factory(Contact::class)->create([]);
$user = factory(User::class)->create([
'account_id' => $contact->account_id,
]);
$request = [
'account_id' => $contact->account_id,
'author_id' => $user->id,
'contact_id' => $contact->id,
];
$contact = (new ClearPersonalDescription)->execute($request);
$this->assertDatabaseHas('contacts', [
'account_id' => $contact->account_id,
'id' => $contact->id,
'description' => null,
]);
$this->assertInstanceOf(
Contact::class,
$contact
);
// check that a job has been triggered to create an auditlog
Queue::assertPushed(LogAccountAudit::class, function ($job) use ($contact, $user) {
return $job->auditLog['action'] === 'contact_description_cleared' &&
$job->auditLog['author_id'] === $user->id &&
$job->auditLog['about_contact_id'] === $contact->id &&
$job->auditLog['should_appear_on_dashboard'] === true &&
$job->auditLog['objects'] === json_encode([
'contact_name' => $contact->name,
'contact_id' => $contact->id,
]);
});
}
/** @test */
public function it_fails_if_wrong_parameters_are_given(): void
{
$request = [
'first_name' => 'Dwight',
];
$this->expectException(ValidationException::class);
(new ClearPersonalDescription)->execute($request);
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace Tests\Unit\Services\Contact\Description;
use Tests\TestCase;
use App\Models\User\User;
use function Safe\json_encode;
use App\Models\Contact\Contact;
use Illuminate\Support\Facades\Queue;
use App\Jobs\AuditLog\LogAccountAudit;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\Contact\Description\SetPersonalDescription;
class SetPersonalDescriptionTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_sets_a_personal_description(): void
{
Queue::fake();
$contact = factory(Contact::class)->create([]);
$user = factory(User::class)->create([
'account_id' => $contact->account_id,
]);
$request = [
'account_id' => $contact->account_id,
'author_id' => $user->id,
'contact_id' => $contact->id,
'description' => 'This is just great',
];
$contact = (new SetPersonalDescription)->execute($request);
$this->assertDatabaseHas('contacts', [
'account_id' => $contact->account_id,
'id' => $contact->id,
'description' => 'This is just great',
]);
$this->assertInstanceOf(
Contact::class,
$contact
);
// check that a job has been triggered to create an auditlog
Queue::assertPushed(LogAccountAudit::class, function ($job) use ($contact, $user) {
return $job->auditLog['action'] === 'contact_description_updated' &&
$job->auditLog['author_id'] === $user->id &&
$job->auditLog['about_contact_id'] === $contact->id &&
$job->auditLog['should_appear_on_dashboard'] === true &&
$job->auditLog['objects'] === json_encode([
'contact_name' => $contact->name,
'contact_id' => $contact->id,
]);
});
}
/** @test */
public function it_fails_if_wrong_parameters_are_given(): void
{
$request = [
'first_name' => 'Dwight',
];
$this->expectException(ValidationException::class);
(new SetPersonalDescription)->execute($request);
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace Tests\Unit\Services\Contact\Document;
use Tests\TestCase;
use App\Models\Contact\Contact;
use App\Models\Contact\Document;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Document\UploadDocument;
use App\Services\Contact\Document\DestroyDocument;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class DestroyDocumentTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_destroys_a_document()
{
Storage::fake();
$contact = factory(Contact::class)->create([]);
$document = $this->uploadDocument($contact);
$request = [
'account_id' => $document->account_id,
'document_id' => $document->id,
];
$this->assertDatabaseHas('documents', [
'id' => $document->id,
]);
app(DestroyDocument::class)->execute($request);
$this->assertDatabaseMissing('documents', [
'id' => $document->id,
]);
Storage::disk('public')->assertMissing($document->new_filename);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$request = [
'document_id' => 2,
];
$this->expectException(ValidationException::class);
app(DestroyDocument::class)->execute($request);
}
/** @test */
public function it_throws_a_document_doesnt_exist()
{
$document = factory(Document::class)->create([]);
$request = [
'account_id' => $document->account_id,
'document_id' => 3,
];
$this->expectException(ModelNotFoundException::class);
app(DestroyDocument::class)->execute($request);
}
private function uploadDocument($contact)
{
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'document' => UploadedFile::fake()->image('document.pdf'),
];
$document = app(UploadDocument::class)->execute($request);
Storage::disk('public')->assertExists($document->new_filename);
return $document;
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace Tests\Unit\Services\Contact\Document;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Contact\Document;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Document\UploadDocument;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class UploadDocumentTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_uploads_a_document()
{
Storage::fake();
$contact = factory(Contact::class)->create([]);
$file = UploadedFile::fake()->image('document.pdf');
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'document' => $file,
];
$document = app(UploadDocument::class)->execute($request);
$this->assertDatabaseHas('documents', [
'id' => $document->id,
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'type' => 'pdf',
]);
$this->assertInstanceOf(
Document::class,
$document
);
Storage::disk('public')->assertExists($document->new_filename);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$request = [
'account_id' => 1,
'contact_id' => 2,
];
$this->expectException(ValidationException::class);
app(UploadDocument::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_does_not_exist()
{
Storage::fake();
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $account->id,
'contact_id' => 2,
'document' => UploadedFile::fake()->image('document.pdf'),
];
$this->expectException(ModelNotFoundException::class);
$document = app(UploadDocument::class)->execute($request);
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace Tests\Unit\Services\Contact\Gift;
use Tests\TestCase;
use App\Models\Contact\Gift;
use App\Models\Account\Photo;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Gift\AssociatePhotoToGift;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class AssociateGiftToPhotoTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_associates_a_photo_to_a_gift()
{
$gift = factory(Gift::class)->create();
$photo = factory(Photo::class)->create([
'account_id' => $gift->account_id,
]);
$giftUpdated = app(AssociatePhotoToGift::class)->execute([
'account_id' => $gift->account_id,
'gift_id' => $gift->id,
'photo_id' => $photo->id,
]);
$this->assertInstanceOf(Gift::class, $giftUpdated);
$this->assertEquals($gift->id, $giftUpdated->id);
$this->assertDatabaseHas('gift_photo', [
'gift_id' => $gift->id,
'photo_id' => $photo->id,
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$this->expectException(ValidationException::class);
app(AssociatePhotoToGift::class)->execute([
'account_id' => -1,
'gift_id' => -1,
'photo_id' => -1,
]);
}
/** @test */
public function it_fails_if_photo_is_wrong_account()
{
$gift = factory(Gift::class)->create();
$photo = factory(Photo::class)->create();
$this->expectException(ModelNotFoundException::class);
app(AssociatePhotoToGift::class)->execute([
'account_id' => $gift->account_id,
'gift_id' => $gift->id,
'photo_id' => $photo->id,
]);
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace Tests\Unit\Services\Contact\Gift;
use Tests\TestCase;
use App\Models\Contact\Gift;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Services\Contact\Gift\CreateGift;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class CreateGiftTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_creates_a_gift()
{
$contact = factory(Contact::class)->create();
$gift = app(CreateGift::class)->execute([
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'name' => 'Book',
'status' => 'idea',
]);
$this->assertDatabaseHas('gifts', [
'id' => $gift->id,
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'name' => 'Book',
'status' => 'idea',
]);
$this->assertInstanceOf(
Gift::class,
$gift
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$this->expectException(ValidationException::class);
app(CreateGift::class)->execute([
'account_id' => -1,
'contact_id' => -1,
'name' => 'Book',
'status' => 'idea',
]);
}
/** @test */
public function it_fails_if_contact_is_wrong_account()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create();
$this->expectException(ModelNotFoundException::class);
$gift = app(CreateGift::class)->execute([
'account_id' => $account->id,
'contact_id' => $contact->id,
'name' => 'Book',
'status' => 'idea',
]);
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Tests\Unit\Services\Contact\Gift;
use Tests\TestCase;
use App\Models\Contact\Gift;
use App\Models\Account\Account;
use App\Services\Contact\Gift\DestroyGift;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class DestroyGiftTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_destroys_a_gift()
{
$gift = factory(Gift::class)->create();
$this->assertDatabaseHas('gifts', [
'account_id' => $gift->account_id,
'contact_id' => $gift->contact_id,
'id' => $gift->id,
]);
app(DestroyGift::class)->execute([
'account_id' => $gift->account_id,
'gift_id' => $gift->id,
]);
$this->assertDatabaseMissing('gifts', [
'id' => $gift->id,
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$this->expectException(ValidationException::class);
app(DestroyGift::class)->execute([
'account_id' => -1,
]);
}
/** @test */
public function it_fails_if_gift_is_wrong_account()
{
$account = factory(Account::class)->create();
$gift = factory(Gift::class)->create();
$this->expectException(ModelNotFoundException::class);
app(DestroyGift::class)->execute([
'account_id' => $account->id,
'gift_id' => $gift->id,
]);
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace Tests\Unit\Services\Contact\Gift;
use Tests\TestCase;
use App\Models\Contact\Gift;
use App\Models\Account\Account;
use App\Services\Contact\Gift\UpdateGift;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class UpdateGiftTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_a_gift()
{
$gift = factory(Gift::class)->create();
$gift = app(UpdateGift::class)->execute([
'account_id' => $gift->account_id,
'gift_id' => $gift->id,
'contact_id' => $gift->contact_id,
'name' => 'Book',
'status' => 'offered',
]);
$this->assertDatabaseHas('gifts', [
'id' => $gift->id,
'name' => 'Book',
'status' => 'offered',
]);
$this->assertInstanceOf(
Gift::class,
$gift
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$this->expectException(ValidationException::class);
app(UpdateGift::class)->execute([
'account_id' => -1,
'gift_id' => -1,
]);
}
/** @test */
public function it_throws_an_exception_if_gift_wrong_account()
{
$account = factory(Account::class)->create();
$gift = factory(Gift::class)->create();
$this->expectException(ModelNotFoundException::class);
app(UpdateGift::class)->execute([
'account_id' => $account->id,
'gift_id' => $gift->id,
'contact_id' => $gift->contact_id,
'name' => 'Book',
'status' => 'offered',
]);
}
}

View File

@@ -0,0 +1,228 @@
<?php
namespace Tests\Unit\Services\Contact\Label;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Address;
use App\Models\Contact\ContactFieldLabel;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Label\UpdateAddressLabels;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class UpdateAddessLabelTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_creates_contact_field_labels()
{
$account = factory(Account::class)->create([]);
$address = factory(Address::class)->create([
'account_id' => $account->id,
]);
app(UpdateAddressLabels::class)->execute([
'account_id' => $account->id,
'address_id' => $address->id,
'labels' => ['home'],
]);
$this->assertDatabaseHas('contact_field_labels', [
'account_id' => $account->id,
'label_i18n' => 'home',
]);
$contactFieldLabel = ContactFieldLabel::where([
'account_id' => $account->id,
'label_i18n' => 'home',
])->first();
$this->assertDatabaseHas('address_contact_field_label', [
'account_id' => $account->id,
'address_id' => $address->id,
'contact_field_label_id' => $contactFieldLabel->id,
]);
}
/** @test */
public function it_creates_contact_field_multiple_labels()
{
$account = factory(Account::class)->create([]);
$address = factory(Address::class)->create([
'account_id' => $account->id,
]);
app(UpdateAddressLabels::class)->execute([
'account_id' => $account->id,
'address_id' => $address->id,
'labels' => ['home', 'main', 'cell'],
]);
$this->assertDatabaseHas('contact_field_labels', [
'account_id' => $account->id,
'label_i18n' => 'home',
]);
$this->assertDatabaseHas('contact_field_labels', [
'account_id' => $account->id,
'label_i18n' => 'main',
]);
$this->assertDatabaseHas('contact_field_labels', [
'account_id' => $account->id,
'label_i18n' => 'cell',
]);
$homeLabel = ContactFieldLabel::where([
'account_id' => $account->id,
'label_i18n' => 'home',
])->first();
$this->assertDatabaseHas('address_contact_field_label', [
'account_id' => $account->id,
'address_id' => $address->id,
'contact_field_label_id' => $homeLabel->id,
]);
$mainLabel = ContactFieldLabel::where([
'account_id' => $account->id,
'label_i18n' => 'main',
])->first();
$this->assertDatabaseHas('address_contact_field_label', [
'account_id' => $account->id,
'address_id' => $address->id,
'contact_field_label_id' => $mainLabel->id,
]);
$cellLabel = ContactFieldLabel::where([
'account_id' => $account->id,
'label_i18n' => 'cell',
])->first();
$this->assertDatabaseHas('address_contact_field_label', [
'account_id' => $account->id,
'address_id' => $address->id,
'contact_field_label_id' => $cellLabel->id,
]);
}
/** @test */
public function it_adds_contact_field_labels()
{
$account = factory(Account::class)->create([]);
$address = factory(Address::class)->create([
'account_id' => $account->id,
]);
$homeLabel = factory(ContactFieldLabel::class)->create([
'account_id' => $account->id,
'label_i18n' => 'home',
]);
$cellLabel = factory(ContactFieldLabel::class)->create([
'account_id' => $account->id,
'label_i18n' => 'cell',
]);
$address->labels()->sync([$homeLabel->id => ['account_id' => $account->id]]);
$this->assertDatabaseHas('address_contact_field_label', [
'account_id' => $account->id,
'address_id' => $address->id,
'contact_field_label_id' => $homeLabel->id,
]);
app(UpdateAddressLabels::class)->execute([
'account_id' => $account->id,
'address_id' => $address->id,
'labels' => ['home', 'cell'],
]);
$this->assertDatabaseHas('address_contact_field_label', [
'account_id' => $account->id,
'address_id' => $address->id,
'contact_field_label_id' => $homeLabel->id,
]);
$this->assertDatabaseHas('address_contact_field_label', [
'account_id' => $account->id,
'address_id' => $address->id,
'contact_field_label_id' => $cellLabel->id,
]);
}
/** @test */
public function it_removes_contact_field_labels()
{
$account = factory(Account::class)->create([]);
$address = factory(Address::class)->create([
'account_id' => $account->id,
]);
$homeLabel = factory(ContactFieldLabel::class)->create([
'account_id' => $account->id,
'label_i18n' => 'home',
]);
$cellLabel = factory(ContactFieldLabel::class)->create([
'account_id' => $account->id,
'label_i18n' => 'cell',
]);
$address->labels()->sync([$homeLabel->id => ['account_id' => $account->id]]);
$this->assertDatabaseHas('address_contact_field_label', [
'account_id' => $account->id,
'address_id' => $address->id,
'contact_field_label_id' => $homeLabel->id,
]);
app(UpdateAddressLabels::class)->execute([
'account_id' => $account->id,
'address_id' => $address->id,
'labels' => ['cell'],
]);
$this->assertDatabaseMissing('address_contact_field_label', [
'account_id' => $account->id,
'address_id' => $address->id,
'contact_field_label_id' => $homeLabel->id,
]);
$this->assertDatabaseHas('address_contact_field_label', [
'account_id' => $account->id,
'address_id' => $address->id,
'contact_field_label_id' => $cellLabel->id,
]);
}
/** @test */
public function it_throws_an_exception_if_account_doesnt_exist()
{
$address = factory(Address::class)->create();
$this->expectException(ValidationException::class);
app(UpdateAddressLabels::class)->execute([
'account_id' => -1,
'address_id' => $address->id,
'labels' => ['cell'],
]);
}
/** @test */
public function it_throws_an_exception_if_contact_field_doesnt_exist()
{
$account = factory(Account::class)->create([]);
$this->expectException(ValidationException::class);
app(UpdateAddressLabels::class)->execute([
'account_id' => $account->id,
'address_id' => -1,
'labels' => ['cell'],
]);
}
/** @test */
public function it_throws_an_exception_if_contact_field_is_wrong_account()
{
$account = factory(Account::class)->create([]);
$address = factory(Address::class)->create();
$this->expectException(ModelNotFoundException::class);
app(UpdateAddressLabels::class)->execute([
'account_id' => $account->id,
'address_id' => $address->id,
'labels' => ['cell'],
]);
}
}

View File

@@ -0,0 +1,258 @@
<?php
namespace Tests\Unit\Services\Contact\Label;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\ContactField;
use App\Models\Contact\ContactFieldLabel;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Services\Contact\Label\UpdateContactFieldLabels;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class UpdateContactFieldLabelTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_creates_contact_field_labels()
{
$account = factory(Account::class)->create([]);
$contactField = factory(ContactField::class)->create([
'account_id' => $account->id,
]);
app(UpdateContactFieldLabels::class)->execute([
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'labels' => ['HOME'],
]);
$this->assertDatabaseHas('contact_field_labels', [
'account_id' => $account->id,
'label_i18n' => 'home',
]);
$contactFieldLabel = ContactFieldLabel::where([
'account_id' => $account->id,
'label_i18n' => 'home',
])->first();
$this->assertDatabaseHas('contact_field_contact_field_label', [
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'contact_field_label_id' => $contactFieldLabel->id,
]);
}
/** @test */
public function it_creates_personal_contact_field_labels()
{
$account = factory(Account::class)->create([]);
$contactField = factory(ContactField::class)->create([
'account_id' => $account->id,
]);
app(UpdateContactFieldLabels::class)->execute([
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'labels' => ['Family'],
]);
$this->assertDatabaseHas('contact_field_labels', [
'account_id' => $account->id,
'label' => 'Family',
]);
$contactFieldLabel = ContactFieldLabel::where([
'account_id' => $account->id,
'label' => 'Family',
])->first();
$this->assertDatabaseHas('contact_field_contact_field_label', [
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'contact_field_label_id' => $contactFieldLabel->id,
]);
}
/** @test */
public function it_creates_contact_field_multiple_labels()
{
$account = factory(Account::class)->create([]);
$contactField = factory(ContactField::class)->create([
'account_id' => $account->id,
]);
app(UpdateContactFieldLabels::class)->execute([
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'labels' => ['home', 'main', 'cell'],
]);
$this->assertDatabaseHas('contact_field_labels', [
'account_id' => $account->id,
'label_i18n' => 'home',
]);
$this->assertDatabaseHas('contact_field_labels', [
'account_id' => $account->id,
'label_i18n' => 'main',
]);
$this->assertDatabaseHas('contact_field_labels', [
'account_id' => $account->id,
'label_i18n' => 'cell',
]);
$homeLabel = ContactFieldLabel::where([
'account_id' => $account->id,
'label_i18n' => 'home',
])->first();
$this->assertDatabaseHas('contact_field_contact_field_label', [
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'contact_field_label_id' => $homeLabel->id,
]);
$mainLabel = ContactFieldLabel::where([
'account_id' => $account->id,
'label_i18n' => 'main',
])->first();
$this->assertDatabaseHas('contact_field_contact_field_label', [
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'contact_field_label_id' => $mainLabel->id,
]);
$cellLabel = ContactFieldLabel::where([
'account_id' => $account->id,
'label_i18n' => 'cell',
])->first();
$this->assertDatabaseHas('contact_field_contact_field_label', [
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'contact_field_label_id' => $cellLabel->id,
]);
}
/** @test */
public function it_adds_contact_field_labels()
{
$account = factory(Account::class)->create([]);
$contactField = factory(ContactField::class)->create([
'account_id' => $account->id,
]);
$homeLabel = factory(ContactFieldLabel::class)->create([
'account_id' => $account->id,
'label_i18n' => 'home',
]);
$cellLabel = factory(ContactFieldLabel::class)->create([
'account_id' => $account->id,
'label_i18n' => 'cell',
]);
$contactField->labels()->sync([$homeLabel->id => ['account_id' => $account->id]]);
$this->assertDatabaseHas('contact_field_contact_field_label', [
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'contact_field_label_id' => $homeLabel->id,
]);
app(UpdateContactFieldLabels::class)->execute([
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'labels' => ['home', 'cell'],
]);
$this->assertDatabaseHas('contact_field_contact_field_label', [
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'contact_field_label_id' => $homeLabel->id,
]);
$this->assertDatabaseHas('contact_field_contact_field_label', [
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'contact_field_label_id' => $cellLabel->id,
]);
}
/** @test */
public function it_removes_contact_field_labels()
{
$account = factory(Account::class)->create([]);
$contactField = factory(ContactField::class)->create([
'account_id' => $account->id,
]);
$homeLabel = factory(ContactFieldLabel::class)->create([
'account_id' => $account->id,
'label_i18n' => 'home',
]);
$cellLabel = factory(ContactFieldLabel::class)->create([
'account_id' => $account->id,
'label_i18n' => 'cell',
]);
$contactField->labels()->sync([$homeLabel->id => ['account_id' => $account->id]]);
$this->assertDatabaseHas('contact_field_contact_field_label', [
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'contact_field_label_id' => $homeLabel->id,
]);
app(UpdateContactFieldLabels::class)->execute([
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'labels' => ['cell'],
]);
$this->assertDatabaseMissing('contact_field_contact_field_label', [
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'contact_field_label_id' => $homeLabel->id,
]);
$this->assertDatabaseHas('contact_field_contact_field_label', [
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'contact_field_label_id' => $cellLabel->id,
]);
}
/** @test */
public function it_throws_an_exception_if_account_doesnt_exist()
{
$contactField = factory(ContactField::class)->create();
$this->expectException(ValidationException::class);
app(UpdateContactFieldLabels::class)->execute([
'account_id' => -1,
'contact_field_id' => $contactField->id,
'labels' => ['cell'],
]);
}
/** @test */
public function it_throws_an_exception_if_contact_field_doesnt_exist()
{
$account = factory(Account::class)->create([]);
$this->expectException(ValidationException::class);
app(UpdateContactFieldLabels::class)->execute([
'account_id' => $account->id,
'contact_field_id' => -1,
'labels' => ['cell'],
]);
}
/** @test */
public function it_throws_an_exception_if_contact_field_is_wrong_account()
{
$account = factory(Account::class)->create([]);
$contactField = factory(ContactField::class)->create();
$this->expectException(ModelNotFoundException::class);
app(UpdateContactFieldLabels::class)->execute([
'account_id' => $account->id,
'contact_field_id' => $contactField->id,
'labels' => ['cell'],
]);
}
}

View File

@@ -0,0 +1,148 @@
<?php
namespace Tests\Unit\Services\Contact\LifeEvent;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Contact\LifeEvent;
use App\Models\Contact\LifeEventType;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\LifeEvent\CreateLifeEvent;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class CreateLifeEventTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_stores_a_life_event()
{
$contact = factory(Contact::class)->create([]);
$lifeEventType = factory(LifeEventType::class)->create([
'account_id' => $contact->account_id,
]);
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'life_event_type_id' => $lifeEventType->id,
'happened_at' => now(),
'name' => 'This is a name',
'note' => 'This is a note',
'has_reminder' => false,
'happened_at_day_unknown' => false,
'happened_at_month_unknown' => false,
];
$lifeEvent = app(CreateLifeEvent::class)->execute($request);
$this->assertDatabaseHas('life_events', [
'id' => $lifeEvent->id,
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'life_event_type_id' => $lifeEventType->id,
'name' => 'This is a name',
'note' => 'This is a note',
'reminder_id' => null,
]);
$this->assertInstanceOf(
LifeEvent::class,
$lifeEvent
);
}
/** @test */
public function it_stores_a_life_event_and_set_a_reminder()
{
$contact = factory(Contact::class)->create([]);
$lifeEventType = factory(LifeEventType::class)->create([
'account_id' => $contact->account_id,
]);
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'life_event_type_id' => $lifeEventType->id,
'happened_at' => now(),
'name' => 'This is a name',
'note' => 'This is a note',
'has_reminder' => true,
'happened_at_day_unknown' => false,
'happened_at_month_unknown' => false,
];
$lifeEvent = app(CreateLifeEvent::class)->execute($request);
$this->assertDatabaseHas('reminders', [
'id' => $lifeEvent->reminder->id,
]);
$this->assertDatabaseHas('life_events', [
'reminder_id' => $lifeEvent->reminder->id,
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$contact = factory(Contact::class)->create([]);
$request = [
'contact_id' => $contact->id,
'happened_at' => now(),
];
$this->expectException(ValidationException::class);
app(CreateLifeEvent::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_is_not_linked_to_account()
{
$account = factory(Account::class)->create();
$lifeEvent = factory(LifeEvent::class)->create([]);
$request = [
'contact_id' => $lifeEvent->contact_id,
'account_id' => $account->id,
'life_event_type_id' => $lifeEvent->lifeEventType->id,
'name' => 'This is a name',
'note' => 'This is a note',
'has_reminder' => false,
'happened_at_day_unknown' => false,
'happened_at_month_unknown' => false,
'happened_at' => now(),
];
$this->expectException(ModelNotFoundException::class);
app(CreateLifeEvent::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_life_event_type_is_not_linked_to_account()
{
$contact = factory(Contact::class)->create([]);
$lifeEventType = factory(LifeEventType::class)->create([]);
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'life_event_type_id' => $lifeEventType->id,
'name' => 'This is a name',
'note' => 'This is a note',
'has_reminder' => false,
'happened_at_day_unknown' => false,
'happened_at_month_unknown' => false,
'happened_at' => now(),
];
$this->expectException(ModelNotFoundException::class);
app(CreateLifeEvent::class)->execute($request);
}
}

View File

@@ -0,0 +1,88 @@
<?php
namespace Tests\Unit\Services\Contact\LifeEvent;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Reminder;
use App\Models\Contact\LifeEvent;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\LifeEvent\DestroyLifeEvent;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class DestroyLifeEventTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_destroys_a_life_event()
{
$lifeEvent = factory(LifeEvent::class)->create([]);
$request = [
'account_id' => $lifeEvent->account_id,
'life_event_id' => $lifeEvent->id,
];
$this->assertDatabaseHas('life_events', [
'id' => $lifeEvent->id,
]);
app(DestroyLifeEvent::class)->execute($request);
$this->assertDatabaseMissing('life_events', [
'id' => $lifeEvent->id,
]);
}
/** @test */
public function it_destroys_a_life_event_and_associated_reminder()
{
$lifeEvent = factory(LifeEvent::class)->create([]);
$reminder = factory(Reminder::class)->create([
'account_id' => $lifeEvent->account_id,
]);
$lifeEvent->reminder_id = $reminder->id;
$lifeEvent->save();
$request = [
'account_id' => $lifeEvent->account_id,
'life_event_id' => $lifeEvent->id,
];
app(DestroyLifeEvent::class)->execute($request);
$this->assertDatabaseMissing('reminders', [
'id' => $reminder->id,
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$request = [
'account_id' => 1,
];
$this->expectException(ValidationException::class);
app(DestroyLifeEvent::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_life_event_doesnt_exist()
{
$account = factory(Account::class)->create();
$lifeEvent = factory(LifeEvent::class)->create([]);
$request = [
'account_id' => $account->id,
'life_event_id' => $lifeEvent->id,
];
$this->expectException(ModelNotFoundException::class);
app(DestroyLifeEvent::class)->execute($request);
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace Tests\Unit\Services\Contact\LifeEvent;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Contact\LifeEvent;
use App\Models\Contact\LifeEventType;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\LifeEvent\UpdateLifeEvent;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class UpdateLifeEventTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_a_life_event()
{
$lifeEvent = factory(LifeEvent::class)->create([
'happened_at' => '2008-01-01',
]);
$lifeEventType = factory(LifeEventType::class)->create([
'account_id' => $lifeEvent->account_id,
]);
$request = [
'life_event_id' => $lifeEvent->id,
'account_id' => $lifeEvent->account_id,
'life_event_type_id' => $lifeEventType->id,
'happened_at' => '2018-01-01',
'name' => 'This is a name',
'note' => 'This is a note',
];
$lifeEvent = app(UpdateLifeEvent::class)->execute($request);
$this->assertDatabaseHas('life_events', [
'id' => $lifeEvent->id,
'happened_at' => '2018-01-01 00:00:00',
'life_event_type_id' => $lifeEventType->id,
'contact_id' => $lifeEvent->contact_id,
'account_id' => $lifeEvent->account_id,
'name' => 'This is a name',
'note' => 'This is a note',
]);
$this->assertInstanceOf(
LifeEvent::class,
$lifeEvent
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$contact = factory(Contact::class)->create([]);
$request = [
'contact_id' => $contact->id,
'happened_at' => now(),
];
$this->expectException(ValidationException::class);
app(UpdateLifeEvent::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_life_type_doesnt_exist()
{
$account = factory(Account::class)->create();
$lifeEvent = factory(LifeEvent::class)->create([]);
$lifeEventType = factory(LifeEventType::class)->create([
'account_id' => $lifeEvent->account_id,
]);
$request = [
'account_id' => $account->id,
'contact_id' => $lifeEvent->contact_id,
'life_event_id' => $lifeEvent->id,
'happened_at' => '2010-02-02',
'life_event_type_id' => $lifeEventType->id,
'name' => 'This is a name',
'note' => 'This is a note',
];
$this->expectException(ModelNotFoundException::class);
app(UpdateLifeEvent::class)->execute($request);
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace Tests\Unit\Services\Contact\Occupation;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Account\Company;
use App\Models\Contact\Contact;
use App\Models\Contact\Occupation;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Occupation\CreateOccupation;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class CreateOccupationTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_stores_an_occupation()
{
$account = factory(Account::class)->create([]);
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$company = factory(Company::class)->create([
'account_id' => $account->id,
]);
$request = [
'account_id' => $account->id,
'contact_id' => $contact->id,
'company_id' => $company->id,
'title' => 'Waiter',
];
$occupation = app(CreateOccupation::class)->execute($request);
$this->assertDatabaseHas('occupations', [
'id' => $occupation->id,
'account_id' => $account->id,
'title' => 'Waiter',
'description' => null,
]);
$this->assertInstanceOf(
Occupation::class,
$occupation
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$account = factory(Account::class)->create([]);
$request = [
'street' => '199 Lafayette Street',
];
$this->expectException(ValidationException::class);
app(CreateOccupation::class)->execute($request);
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Tests\Unit\Services\Contact\Occupation;
use Tests\TestCase;
use App\Models\Contact\Occupation;
use App\Services\Contact\Occupation\DestroyOccupation;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class DestroyOccupationTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_destroys_a_occupation()
{
$occupation = factory(Occupation::class)->create([]);
$request = [
'account_id' => $occupation->account_id,
'occupation_id' => $occupation->id,
];
$this->assertDatabaseHas('occupations', [
'id' => $occupation->id,
]);
app(DestroyOccupation::class)->execute($request);
$this->assertDatabaseMissing('occupations', [
'id' => $occupation->id,
]);
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace Tests\Unit\Services\Contact\Occupation;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Occupation;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Occupation\UpdateOccupation;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class UpdateOccupationTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_an_occupation()
{
$occupation = factory(Occupation::class)->create([]);
$request = [
'account_id' => $occupation->account_id,
'contact_id' => $occupation->contact_id,
'company_id' => $occupation->company_id,
'occupation_id' => $occupation->id,
'title' => 'Fashion girl',
'description' => null,
'salary' => '30000',
];
$occupation = app(UpdateOccupation::class)->execute($request);
$this->assertDatabaseHas('occupations', [
'id' => $occupation->id,
'account_id' => $occupation->account_id,
'contact_id' => $occupation->contact_id,
'company_id' => $occupation->company_id,
'title' => 'Fashion girl',
'salary' => 30000,
]);
$this->assertInstanceOf(
Occupation::class,
$occupation
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$occupation = factory(Occupation::class)->create([]);
$request = [
'name' => '199 Lafayette Street',
];
$this->expectException(ValidationException::class);
app(UpdateOccupation::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_occupation_is_not_linked_to_account()
{
$account = factory(Account::class)->create([]);
$occupation = factory(Occupation::class)->create([]);
$request = [
'account_id' => $account->id,
'contact_id' => $occupation->contact_id,
'company_id' => $occupation->company_id,
'occupation_id' => $occupation->id,
'title' => 'Fashion',
];
$this->expectException(ModelNotFoundException::class);
app(UpdateOccupation::class)->execute($request);
}
}

View File

@@ -0,0 +1,163 @@
<?php
namespace Tests\Unit\Services\Contact\Relationship;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Relationship\RelationshipType;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Services\Contact\Relationship\CreateRelationship;
class CreateRelationshipTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_stores_a_relationship()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$otherContact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
]);
$request = [
'contact_is' => $contact->id,
'of_contact' => $otherContact->id,
'account_id' => $account->id,
'relationship_type_id' => $relationshipType->id,
];
$relationship = app(CreateRelationship::class)->execute($request);
$this->assertDatabaseHas('relationships', [
'id' => $relationship->id,
'account_id' => $account->id,
'relationship_type_id' => $relationshipType->id,
'contact_is' => $contact->id,
'of_contact' => $otherContact->id,
]);
}
/** @test */
public function it_fails_adding_relationship_when_relationship_type_is_unknown()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$otherContact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$relationshipType = factory(RelationshipType::class)->create();
$request = [
'contact_is' => $contact->id,
'of_contact' => $otherContact->id,
'account_id' => $account->id,
'relationship_type_id' => $relationshipType->id,
];
$this->expectException(ModelNotFoundException::class);
app(CreateRelationship::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_is_not_linked_to_account()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create();
$otherContact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
]);
$request = [
'contact_is' => $contact->id,
'of_contact' => $otherContact->id,
'account_id' => $account->id,
'relationship_type_id' => $relationshipType->id,
];
$this->expectException(ModelNotFoundException::class);
app(CreateRelationship::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_other_contact_is_not_linked_to_account()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$otherContact = factory(Contact::class)->create();
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
]);
$request = [
'contact_is' => $contact->id,
'of_contact' => $otherContact->id,
'account_id' => $account->id,
'relationship_type_id' => $relationshipType->id,
];
$this->expectException(ModelNotFoundException::class);
app(CreateRelationship::class)->execute($request);
}
/** @test */
public function it_creates_a_relationship_and_reverse()
{
$account = factory(Account::class)->create();
$contactA = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$contactB = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$relationshipTypeA = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'uncle',
'name_reverse_relationship' => 'nephew',
]);
$relationshipTypeB = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'nephew',
'name_reverse_relationship' => 'uncle',
]);
$request = [
'account_id' => $account->id,
'contact_is' => $contactA->id,
'of_contact' => $contactB->id,
'relationship_type_id' => $relationshipTypeA->id,
];
app(CreateRelationship::class)->execute($request);
$this->assertDatabaseHas('relationships', [
'account_id' => $account->id,
'contact_is' => $contactA->id,
'of_contact' => $contactB->id,
'relationship_type_id' => $relationshipTypeA->id,
]);
$this->assertDatabaseHas('relationships', [
'account_id' => $account->id,
'contact_is' => $contactB->id,
'of_contact' => $contactA->id,
'relationship_type_id' => $relationshipTypeB->id,
]);
}
}

View File

@@ -0,0 +1,262 @@
<?php
namespace Tests\Unit\Services\Contact\Relationship;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Relationship\Relationship;
use App\Models\Relationship\RelationshipType;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Services\Contact\Relationship\DestroyRelationship;
class DestroyRelationshipTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_destroys_a_relationship()
{
$contactA = factory(Contact::class)->create([]);
$contactB = factory(Contact::class)->create([
'account_id' => $contactA->account_id,
]);
$relationship = factory(Relationship::class)->create([
'account_id' => $contactA->account_id,
'contact_is' => $contactA,
'of_contact' => $contactB,
]);
$request = [
'account_id' => $contactA->account_id,
'relationship_id' => $relationship->id,
];
app(DestroyRelationship::class)->execute($request);
$this->assertDatabaseMissing('relationships', [
'id' => $relationship->id,
]);
}
/** @test */
public function it_destroys_a_relationship_and_reverse()
{
$contactA = factory(Contact::class)->create([]);
$contactB = factory(Contact::class)->create([
'account_id' => $contactA->account_id,
]);
$relationshipTypeA = factory(RelationshipType::class)->create([
'account_id' => $contactA->account_id,
'name' => 'uncle',
'name_reverse_relationship' => 'nephew',
]);
$relationshipA = factory(Relationship::class)->create([
'account_id' => $contactA->account_id,
'contact_is' => $contactA,
'of_contact' => $contactB,
'relationship_type_id' => $relationshipTypeA->id,
]);
$relationshipTypeB = factory(RelationshipType::class)->create([
'account_id' => $contactA->account_id,
'name' => 'nephew',
'name_reverse_relationship' => 'uncle',
]);
$relationshipB = factory(Relationship::class)->create([
'account_id' => $contactA->account_id,
'contact_is' => $contactB,
'of_contact' => $contactA,
'relationship_type_id' => $relationshipTypeB->id,
]);
$request = [
'account_id' => $contactA->account_id,
'relationship_id' => $relationshipA->id,
];
$this->assertDatabaseHas('relationships', [
'id' => $relationshipA->id,
]);
$this->assertDatabaseHas('relationships', [
'id' => $relationshipB->id,
]);
app(DestroyRelationship::class)->execute($request);
$this->assertDatabaseMissing('relationships', [
'id' => $relationshipA->id,
]);
$this->assertDatabaseMissing('relationships', [
'id' => $relationshipB->id,
]);
}
/** @test */
public function it_destroys_a_relationship_and_reverse_and_partial_contact()
{
$contactA = factory(Contact::class)->create([]);
$contactB = factory(Contact::class)->create([
'account_id' => $contactA->account_id,
'is_partial' => true,
]);
$relationshipTypeA = factory(RelationshipType::class)->create([
'account_id' => $contactA->account_id,
'name' => 'uncle',
'name_reverse_relationship' => 'nephew',
]);
$relationshipA = factory(Relationship::class)->create([
'account_id' => $contactA->account_id,
'contact_is' => $contactA,
'of_contact' => $contactB,
'relationship_type_id' => $relationshipTypeA->id,
]);
$relationshipTypeB = factory(RelationshipType::class)->create([
'account_id' => $contactA->account_id,
'name' => 'nephew',
'name_reverse_relationship' => 'uncle',
]);
$relationshipB = factory(Relationship::class)->create([
'account_id' => $contactA->account_id,
'contact_is' => $contactB,
'of_contact' => $contactA,
'relationship_type_id' => $relationshipTypeB->id,
]);
$request = [
'account_id' => $contactA->account_id,
'relationship_id' => $relationshipA->id,
];
$this->assertDatabaseHas('relationships', [
'id' => $relationshipA->id,
]);
$this->assertDatabaseHas('relationships', [
'id' => $relationshipB->id,
]);
app(DestroyRelationship::class)->execute($request);
$this->assertDatabaseMissing('relationships', [
'id' => $relationshipA->id,
]);
$this->assertDatabaseMissing('relationships', [
'id' => $relationshipB->id,
]);
$this->assertDatabaseMissing('contacts', [
'id' => $contactB->id,
'deleted_at' => null,
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$account = factory(Account::class)->create([]);
$request = [
'account_id' => $account->id,
];
$this->expectException(ValidationException::class);
app(DestroyRelationship::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_relationship_is_not_linked_to_account()
{
$account = factory(Account::class)->create();
$relationship = factory(Relationship::class)->create([]);
$request = [
'account_id' => $account->id,
'relationship_id' => $relationship->id,
];
$this->expectException(ModelNotFoundException::class);
app(DestroyRelationship::class)->execute($request);
}
/** @test */
public function it_deletes_relationship_between_two_contacts_and_deletes_the_contact()
{
$account = factory(Account::class)->create([]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$partner = factory(Contact::class)->create([
'account_id' => $account->id,
'is_partial' => true,
]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
]);
$relationship = factory(Relationship::class)->create([
'account_id' => $account->id,
'contact_is' => $contact->id,
'of_contact' => $partner->id,
'relationship_type_id' => $relationshipType->id,
]);
app(DestroyRelationship::class)->execute([
'account_id' => $account->id,
'relationship_id' => $relationship->id,
]);
$this->assertDatabaseMissing(
'relationships',
[
'contact_is' => $contact->id,
'of_contact' => $partner->id,
'relationship_type_id' => $relationshipType->id,
]
);
}
/** @test */
public function it_deletes_relationship_between_two_contacts_and_doesnt_delete_the_contact()
{
$account = factory(Account::class)->create([]);
$contact = factory(Contact::class)->create(['account_id' => $account->id]);
$partner = factory(Contact::class)->create([
'account_id' => $account->id,
'is_partial' => false,
]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
]);
$relationship = factory(Relationship::class)->create([
'account_id' => $account->id,
'contact_is' => $contact->id,
'of_contact' => $partner->id,
'relationship_type_id' => $relationshipType->id,
]);
app(DestroyRelationship::class)->execute([
'account_id' => $account->id,
'relationship_id' => $relationship->id,
]);
$this->assertDatabaseMissing(
'relationships',
[
'contact_is' => $contact->id,
'of_contact' => $partner->id,
'relationship_type_id' => $relationshipType->id,
]
);
$this->assertDatabaseHas(
'contacts',
[
'id' => $partner->id,
]
);
}
}

View File

@@ -0,0 +1,157 @@
<?php
namespace Tests\Unit\Services\Contact\Relationship;
use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Relationship\Relationship;
use App\Models\Relationship\RelationshipType;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Services\Contact\Relationship\CreateRelationship;
use App\Services\Contact\Relationship\UpdateRelationship;
class UpdateRelationshipTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_a_relationship()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$otherContact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$relationshipType0 = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'son',
'name_reverse_relationship' => 'father',
]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => $relationshipType0->name_reverse_relationship,
'name_reverse_relationship' => $relationshipType0->name,
]);
$request = [
'contact_is' => $contact->id,
'of_contact' => $otherContact->id,
'account_id' => $account->id,
'relationship_type_id' => $relationshipType->id,
];
$relationship = app(CreateRelationship::class)->execute($request);
$relationshipType0 = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'uncle',
'name_reverse_relationship' => 'nephew',
]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => $relationshipType0->name_reverse_relationship,
'name_reverse_relationship' => $relationshipType0->name,
]);
$request = [
'account_id' => $account->id,
'relationship_id' => $relationship->id,
'relationship_type_id' => $relationshipType->id,
];
$newRelationship = app(UpdateRelationship::class)->execute($request);
$this->assertDatabaseHas('relationships', [
'id' => $newRelationship->id,
'account_id' => $account->id,
'relationship_type_id' => $relationshipType->id,
'contact_is' => $contact->id,
'of_contact' => $otherContact->id,
]);
}
/** @test */
public function it_updates_a_partial_relationship()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$otherContact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$relationship = factory(Relationship::class)->create([
'account_id' => $account->id,
'contact_is' => $contact->id,
'of_contact' => $otherContact->id,
]);
$relationshipType0 = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name' => 'name',
]);
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
'name_reverse_relationship' => $relationshipType0->name,
]);
$request = [
'account_id' => $account->id,
'relationship_id' => $relationship->id,
'relationship_type_id' => $relationshipType->id,
];
$newRelationship = app(UpdateRelationship::class)->execute($request);
$this->assertDatabaseHas('relationships', [
'id' => $newRelationship->id,
'account_id' => $account->id,
'relationship_type_id' => $relationshipType->id,
'contact_is' => $contact->id,
'of_contact' => $otherContact->id,
]);
}
/** @test */
public function it_throws_an_exception_if_relationship_is_not_linked_to_account()
{
$account = factory(Account::class)->create();
$relationship = factory(Relationship::class)->create();
$relationshipType = factory(RelationshipType::class)->create([
'account_id' => $account->id,
]);
$request = [
'account_id' => $account->id,
'relationship_id' => $relationship->id,
'relationship_type_id' => $relationshipType->id,
];
$this->expectException(ModelNotFoundException::class);
app(UpdateRelationship::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_relationship_type_is_not_linked_to_account()
{
$account = factory(Account::class)->create();
$relationship = factory(Relationship::class)->create([
'account_id' => $account->id,
]);
$relationshipType = factory(RelationshipType::class)->create();
$request = [
'account_id' => $account->id,
'relationship_id' => $relationship->id,
'relationship_type_id' => $relationshipType->id,
];
$this->expectException(ModelNotFoundException::class);
app(UpdateRelationship::class)->execute($request);
}
}

View File

@@ -0,0 +1,228 @@
<?php
namespace Tests\Unit\Services\Contact\Reminder;
use Carbon\Carbon;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Models\Contact\Reminder;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Reminder\CreateReminder;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class CreateReminderTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_stores_a_recurring_reminder()
{
Carbon::setTestNow(Carbon::create(2017, 1, 1));
$user = factory(User::class)->create([]);
$contact = factory(Contact::class)->create([
'account_id' => $user->account_id,
]);
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'initial_date' => '2017-02-01',
'frequency_type' => 'year',
'frequency_number' => 1,
'title' => 'title',
'description' => 'description',
];
$reminder = app(CreateReminder::class)->execute($request);
$this->assertDatabaseHas('reminders', [
'id' => $reminder->id,
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
]);
$this->assertInstanceOf(
Reminder::class,
$reminder
);
$this->assertDatabaseHas('reminder_outbox', [
'reminder_id' => $reminder->id,
'account_id' => $contact->account_id,
'planned_date' => '2017-02-01',
'nature' => 'reminder',
]);
}
/** @test */
public function it_stores_a_one_time_reminder()
{
Carbon::setTestNow(Carbon::create(2017, 1, 1));
$user = factory(User::class)->create([]);
$contact = factory(Contact::class)->create([
'account_id' => $user->account_id,
]);
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'initial_date' => '2017-02-01',
'frequency_type' => 'one_time',
'frequency_number' => 1,
'title' => 'title',
'description' => 'description',
];
$reminder = app(CreateReminder::class)->execute($request);
$this->assertDatabaseHas('reminders', [
'id' => $reminder->id,
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
]);
$this->assertInstanceOf(
Reminder::class,
$reminder
);
$this->assertDatabaseHas('reminder_outbox', [
'reminder_id' => $reminder->id,
'account_id' => $contact->account_id,
'planned_date' => '2017-02-01',
'nature' => 'reminder',
]);
}
/** @test */
public function it_stores_a_reminder_for_each_user_of_an_account()
{
Carbon::setTestNow(Carbon::create(2017, 1, 1));
$account = factory(Account::class)->create([]);
$userA = factory(User::class)->create([
'account_id' => $account->id,
]);
$userB = factory(User::class)->create([
'account_id' => $account->id,
]);
$contact = factory(Contact::class)->create([
'account_id' => $account->id,
]);
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'initial_date' => '2017-02-01',
'frequency_type' => 'one_time',
'frequency_number' => 1,
'title' => 'title',
'description' => 'description',
];
$reminder = app(CreateReminder::class)->execute($request);
$this->assertDatabaseHas('reminders', [
'id' => $reminder->id,
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
]);
$this->assertInstanceOf(
Reminder::class,
$reminder
);
$this->assertDatabaseHas('reminder_outbox', [
'reminder_id' => $reminder->id,
'account_id' => $contact->account_id,
'planned_date' => '2017-02-01',
'nature' => 'reminder',
'user_id' => $userA->id,
]);
$this->assertDatabaseHas('reminder_outbox', [
'reminder_id' => $reminder->id,
'account_id' => $contact->account_id,
'planned_date' => '2017-02-01',
'nature' => 'reminder',
'user_id' => $userB->id,
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$contact = factory(Contact::class)->create([]);
$request = [
'contact_id' => $contact->id,
'initial_date' => now(),
];
$this->expectException(ValidationException::class);
$reminderService = app(CreateReminder::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_ids_are_not_found()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create([]);
$request = [
'contact_id' => $contact->id,
'account_id' => $account->id,
'initial_date' => '2017-02-02',
'frequency_type' => 'year',
'frequency_number' => 1,
'title' => 'title',
'description' => 'description',
];
$this->expectException(ModelNotFoundException::class);
$reminder = app(CreateReminder::class)->execute($request);
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'initial_date' => '2017-02-02',
'frequency_type' => 'year',
'frequency_number' => 1,
'title' => 'title',
'description' => 'description',
];
$this->expectException(ModelNotFoundException::class);
$reminderService = app(CreateReminder::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_frequency_type_is_not_right()
{
$contact = factory(Contact::class)->create([]);
$request = [
'contact_id' => $contact->id,
'account_id' => $contact->account_id,
'initial_date' => '2017-02-02',
'frequency_type' => 'blabla',
'frequency_number' => 1,
'title' => 'title',
'description' => 'description',
];
$this->expectException(ValidationException::class);
try {
$reminderService = app(CreateReminder::class)->execute($request);
} catch (ValidationException $e) {
$this->assertEquals(['The selected frequency type is invalid.'], $e->validator->errors()->all());
throw $e;
}
}
}

View File

@@ -0,0 +1,89 @@
<?php
namespace Tests\Unit\Services\Contact\Reminder;
use Carbon\Carbon;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Contact\Reminder;
use App\Models\Contact\ReminderRule;
use App\Services\Contact\Reminder\DestroyReminder;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class DestroyReminderTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_destroys_a_reminder()
{
$reminder = factory(Reminder::class)->create([
'initial_date' => '2017-02-02',
'frequency_type' => 'year',
'frequency_number' => 1,
'title' => 'title',
'description' => 'description',
]);
$request = [
'account_id' => $reminder->account_id,
'reminder_id' => $reminder->id,
];
$this->assertDatabaseHas('reminders', [
'id' => $reminder->id,
]);
app(DestroyReminder::class)->execute($request);
$this->assertDatabaseMissing('reminders', [
'id' => $reminder->id,
]);
}
/** @test */
public function it_destroys_scheduled_reminders()
{
// prepare a reminder and schedule some notifications
Carbon::setTestNow(Carbon::create(2017, 2, 1));
$user = factory(User::class)->create([]);
$reminder = factory(Reminder::class)->create([
'account_id' => $user->account_id,
'initial_date' => '2017-02-02',
'frequency_type' => 'year',
'frequency_number' => 1,
'title' => 'title',
'description' => 'description',
]);
$reminderRule = factory(ReminderRule::class)->create([
'account_id' => $reminder->account_id,
'number_of_days_before' => 30,
'active' => 1,
]);
$reminder->schedule($user);
$this->assertDatabaseHas('reminder_outbox', [
'reminder_id' => $reminder->id,
]);
$request = [
'account_id' => $reminder->account_id,
'reminder_id' => $reminder->id,
];
$this->assertDatabaseHas('reminders', [
'id' => $reminder->id,
]);
app(DestroyReminder::class)->execute($request);
$this->assertDatabaseMissing('reminders', [
'id' => $reminder->id,
]);
$this->assertDatabaseMissing('reminder_outbox', [
'reminder_id' => $reminder->id,
]);
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace Tests\Unit\Services\Contact\Reminder;
use Carbon\Carbon;
use Tests\TestCase;
use App\Models\User\User;
use App\Models\Contact\Contact;
use App\Models\Contact\Reminder;
use Illuminate\Validation\ValidationException;
use App\Services\Contact\Reminder\UpdateReminder;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class UpdateReminderTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_a_reminder()
{
Carbon::setTestNow(Carbon::create(2017, 1, 1));
$user = factory(User::class)->create([]);
$reminder = factory(Reminder::class)->create([
'account_id' => $user->account_id,
'initial_date' => '2017-02-02',
'frequency_type' => 'year',
'frequency_number' => 1,
'title' => 'title',
'description' => 'description',
]);
$request = [
'contact_id' => $reminder->contact_id,
'account_id' => $reminder->contact->account_id,
'reminder_id' => $reminder->id,
'initial_date' => '2017-10-01',
'frequency_type' => 'month',
'frequency_number' => 1,
'title' => 'title',
];
$reminder = app(UpdateReminder::class)->execute($request);
$this->assertDatabaseHas('reminders', [
'id' => $reminder->id,
'contact_id' => $reminder->contact_id,
'account_id' => $reminder->contact->account_id,
'initial_date' => '2017-10-01',
]);
$this->assertInstanceOf(
Reminder::class,
$reminder
);
$this->assertDatabaseHas('reminder_outbox', [
'reminder_id' => $reminder->id,
'account_id' => $reminder->contact->account_id,
'planned_date' => '2017-10-01',
'nature' => 'reminder',
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$contact = factory(Contact::class)->create([]);
$request = [
'contact_id' => $contact->id,
'initial_date' => now(),
];
$this->expectException(ValidationException::class);
app(UpdateReminder::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_frequency_type_is_not_right()
{
$reminder = factory(Reminder::class)->create([
'initial_date' => '2017-02-02',
'frequency_type' => 'year',
'frequency_number' => 1,
'title' => 'title',
'description' => 'description',
]);
$request = [
'contact_id' => $reminder->contact_id,
'account_id' => $reminder->contact->account_id,
'reminder_id' => $reminder->id,
'initial_date' => '2017-02-02',
'frequency_type' => 'blabla',
'frequency_number' => 1,
'title' => 'title',
'description' => 'description',
];
$this->expectException(ValidationException::class);
try {
app(UpdateReminder::class)->execute($request);
} catch (ValidationException $e) {
$this->assertEquals(['The selected frequency type is invalid.'], $e->validator->errors()->all());
throw $e;
}
}
}

View File

@@ -0,0 +1,154 @@
<?php
namespace Tests\Unit\Services\Contact\Tag;
use Tests\TestCase;
use App\Models\Contact\Tag;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Services\Contact\Tag\AssociateTag;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class AssociateTagTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_sets_a_non_english_tag_to_a_contact_when_tag_doesnt_exist_yet()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'name' => '朋友',
];
$tag = app(AssociateTag::class)->execute($request);
$this->assertDatabaseHas('tags', [
'account_id' => $contact->account_id,
'name' => '朋友',
'name_slug' => '朋友',
]);
$this->assertDatabaseHas('contact_tag', [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'tag_id' => $tag->id,
]);
$this->assertInstanceOf(
Tag::class,
$tag
);
}
/** @test */
public function it_sets_a_tag_to_a_contact_when_tag_doesnt_exist_yet()
{
$contact = factory(Contact::class)->create([]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'name' => 'Central Perk',
];
$tag = app(AssociateTag::class)->execute($request);
$this->assertDatabaseHas('tags', [
'account_id' => $contact->account_id,
'name' => 'Central Perk',
'name_slug' => 'central-perk',
]);
$this->assertDatabaseHas('contact_tag', [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'tag_id' => $tag->id,
]);
$this->assertInstanceOf(
Tag::class,
$tag
);
}
/** @test */
public function it_sets_a_tag_to_a_contact_when_tag_does_exist_yet()
{
$contact = factory(Contact::class)->create([]);
$tag = factory(Tag::class)->create([
'account_id' => $contact->account_id,
]);
$this->assertDatabaseHas('tags', [
'account_id' => $contact->account_id,
'name' => $tag->name,
'name_slug' => $tag->name_slug,
]);
$this->assertDatabaseMissing('contact_tag', [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'tag_id' => $tag->id,
]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'name' => 'Central Perk',
];
$tag = app(AssociateTag::class)->execute($request);
$this->assertDatabaseHas('tags', [
'account_id' => $contact->account_id,
'name' => 'Central Perk',
'name_slug' => 'central-perk',
]);
$this->assertDatabaseHas('contact_tag', [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'tag_id' => $tag->id,
]);
$this->assertInstanceOf(
Tag::class,
$tag
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$request = [
'account_id' => 1,
'contact_id' => 2,
];
$this->expectException(ValidationException::class);
app(AssociateTag::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_does_not_exist()
{
$account = factory(Account::class)->create();
$contact = factory(Contact::class)->create();
$request = [
'account_id' => $account->id,
'contact_id' => $contact->id,
'name' => 'Central Perk',
];
$this->expectException(ModelNotFoundException::class);
app(AssociateTag::class)->execute($request);
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Tests\Unit\Services\Contact\Tag;
use Tests\TestCase;
use App\Models\Contact\Tag;
use App\Services\Contact\Tag\CreateTag;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class CreateTagTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_creates_a_tag()
{
$tag = factory(Tag::class)->create([]);
$request = [
'account_id' => $tag->account_id,
'name' => 'Central Perk',
];
$tag = app(CreateTag::class)->execute($request);
$this->assertDatabaseHas('tags', [
'id' => $tag->id,
'name' => 'Central Perk',
'name_slug' => 'central-perk',
]);
$this->assertInstanceOf(
Tag::class,
$tag
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$request = [
'account_id' => 1,
'tag_id' => 2,
];
$this->expectException(ValidationException::class);
app(CreateTag::class)->execute($request);
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace Tests\Unit\Services\Contact\Tag;
use Tests\TestCase;
use App\Models\Contact\Tag;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Services\Contact\Tag\DestroyTag;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class DestroyTagTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_destroys_a_tag()
{
$contact = factory(Contact::class)->create([]);
$tag = factory(Tag::class)->create([
'account_id' => $contact->account_id,
]);
$contact->tags()->syncWithoutDetaching([
$tag->id => [
'account_id' => $contact->account_id,
],
]);
$this->assertDatabaseHas('contact_tag', [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'tag_id' => $tag->id,
]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'tag_id' => $tag->id,
];
app(DestroyTag::class)->execute($request);
$this->assertDatabaseMissing('contact_tag', [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
]);
$this->assertDatabaseMissing('tags', [
'account_id' => $contact->account_id,
'id' => $tag->id,
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$request = [
'account_id' => 1,
];
$this->expectException(ValidationException::class);
app(DestroyTag::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_tag_does_not_exist()
{
$account = factory(Account::class)->create();
$request = [
'account_id' => $account->id,
'tag_id' => 123232,
];
$this->expectException(ModelNotFoundException::class);
app(DestroyTag::class)->execute($request);
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace Tests\Unit\Services\Contact\Tag;
use Tests\TestCase;
use App\Models\Contact\Tag;
use App\Models\Account\Account;
use App\Models\Contact\Contact;
use App\Services\Contact\Tag\DetachTag;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class DetachTagTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_detachs_a_tag()
{
$contact = factory(Contact::class)->create([]);
$tag = factory(Tag::class)->create([
'account_id' => $contact->account_id,
]);
$contact->tags()->syncWithoutDetaching([
$tag->id => [
'account_id' => $contact->account_id,
],
]);
$this->assertDatabaseHas('contact_tag', [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'tag_id' => $tag->id,
]);
$request = [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'tag_id' => $tag->id,
];
app(DetachTag::class)->execute($request);
$this->assertDatabaseMissing('contact_tag', [
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
]);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$request = [
'account_id' => 1,
];
$this->expectException(ValidationException::class);
app(DetachTag::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_contact_does_not_exist()
{
$account = factory(Account::class)->create();
$tag = factory(Tag::class)->create([
'account_id' => $account->id,
]);
$request = [
'account_id' => $account->id,
'contact_id' => 12322,
'tag_id' => $tag->id,
];
$this->expectException(ModelNotFoundException::class);
app(DetachTag::class)->execute($request);
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace Tests\Unit\Services\Contact\Tag;
use Tests\TestCase;
use App\Models\Contact\Tag;
use App\Models\Account\Account;
use App\Services\Contact\Tag\UpdateTag;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class UpdateTagTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_a_tag()
{
$tag = factory(Tag::class)->create([]);
$request = [
'account_id' => $tag->account_id,
'tag_id' => $tag->id,
'name' => 'Central Perk',
];
$tag = app(UpdateTag::class)->execute($request);
$this->assertDatabaseHas('tags', [
'id' => $tag->id,
'name' => 'Central Perk',
'name_slug' => 'central-perk',
]);
$this->assertInstanceOf(
Tag::class,
$tag
);
}
/** @test */
public function it_fails_if_wrong_parameters_are_given()
{
$request = [
'account_id' => 1,
'tag_id' => 2,
];
$this->expectException(ValidationException::class);
app(UpdateTag::class)->execute($request);
}
/** @test */
public function it_throws_an_exception_if_tag_does_not_exist()
{
$account = factory(Account::class)->create();
$request = [
'account_id' => $account->id,
'tag_id' => 1232322,
'name' => 'Central Perk',
];
$this->expectException(ModelNotFoundException::class);
app(UpdateTag::class)->execute($request);
}
}