refactor: replace custom CRM with Monica fork
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s
This commit is contained in:
@@ -0,0 +1,249 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Activity;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Account\Activity;
|
||||
use App\Models\Account\ActivityType;
|
||||
use App\Models\Account\ActivityStatistic;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Services\Account\Activity\ActivityStatisticService;
|
||||
|
||||
class ActivityStatisticServiceTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_gets_a_list_of_activities_since_a_given_number_of_months()
|
||||
{
|
||||
$service = new ActivityStatisticService;
|
||||
$contact = factory(Contact::class)->create();
|
||||
|
||||
for ($i = 0; $i <= 2; $i++) {
|
||||
$activity = factory(Activity::class)->create([
|
||||
'happened_at' => now()->subMonth(),
|
||||
'account_id' => $contact->account_id,
|
||||
]);
|
||||
$contact->activities()->attach($activity, ['account_id' => $contact->account_id]);
|
||||
}
|
||||
|
||||
$this->assertCount(
|
||||
3,
|
||||
$service->activitiesWithContactInTimeRange($contact, now()->subMonths(2), now())
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Activity::class,
|
||||
$service->activitiesWithContactInTimeRange($contact, now()->subMonths(2), now())[1]
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_gets_an_empty_list_of_activities()
|
||||
{
|
||||
$service = new ActivityStatisticService;
|
||||
$contact = factory(Contact::class)->create();
|
||||
|
||||
for ($i = 0; $i <= 2; $i++) {
|
||||
$activity = factory(Activity::class)->create([
|
||||
'happened_at' => now()->subYears(2),
|
||||
'account_id' => $contact->account_id,
|
||||
]);
|
||||
$contact->activities()->attach($activity, ['account_id' => $contact->account_id]);
|
||||
}
|
||||
|
||||
$this->assertCount(
|
||||
0,
|
||||
$service->activitiesWithContactInTimeRange($contact, now()->subMonths(2), now())
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_gets_a_list_of_unique_activity_types()
|
||||
{
|
||||
$service = new ActivityStatisticService;
|
||||
$account = factory(Account::class)->create();
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
// creation of 3 activities with a given activity type
|
||||
$activityType = factory(ActivityType::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
for ($i = 0; $i <= 2; $i++) {
|
||||
$activity = factory(Activity::class)->create([
|
||||
'happened_at' => now(),
|
||||
'account_id' => $account->id,
|
||||
'activity_type_id' => $activityType->id,
|
||||
]);
|
||||
$contact->activities()->attach($activity, ['account_id' => $contact->account_id]);
|
||||
}
|
||||
|
||||
// creation of 1 activity with a given activity type
|
||||
$activityType = factory(ActivityType::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$activity = factory(Activity::class)->create([
|
||||
'happened_at' => now(),
|
||||
'account_id' => $account->id,
|
||||
'activity_type_id' => $activityType->id,
|
||||
]);
|
||||
$contact->activities()->attach($activity, ['account_id' => $contact->account_id]);
|
||||
|
||||
// here we should have 2 uniques activity types, one with 3 and the other with 1 occurence
|
||||
$response = $service->uniqueActivityTypesInTimeRange($contact, now()->subMonths(2), now());
|
||||
|
||||
$this->assertCount(
|
||||
2,
|
||||
$response
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
ActivityType::class,
|
||||
$response[0]['object']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
3,
|
||||
$response[0]['occurences']
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
ActivityType::class,
|
||||
$response[1]['object']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
1,
|
||||
$response[1]['occurences']
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_gets_the_breakdown_of_activities_per_year()
|
||||
{
|
||||
$service = new ActivityStatisticService;
|
||||
$account = factory(Account::class)->create();
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
for ($i = 0; $i <= 2; $i++) {
|
||||
$activity = factory(Activity::class)->create([
|
||||
'happened_at' => now()->subYears(2),
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
$contact->activities()->attach($activity, ['account_id' => $contact->account_id]);
|
||||
}
|
||||
|
||||
for ($i = 0; $i <= 5; $i++) {
|
||||
$activity = factory(Activity::class)->create([
|
||||
'happened_at' => now(),
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
$contact->activities()->attach($activity, ['account_id' => $contact->account_id]);
|
||||
}
|
||||
|
||||
$activityStatistic = $contact->activityStatistics()->make();
|
||||
$activityStatistic->account_id = $contact->account_id;
|
||||
$activityStatistic->contact_id = $contact->id;
|
||||
$activityStatistic->year = now()->year;
|
||||
$activityStatistic->count = 6;
|
||||
$activityStatistic->save();
|
||||
|
||||
$activityStatistic = $contact->activityStatistics()->make();
|
||||
$activityStatistic->account_id = $contact->account_id;
|
||||
$activityStatistic->contact_id = $contact->id;
|
||||
$activityStatistic->year = now()->subYears(2)->year;
|
||||
$activityStatistic->count = 3;
|
||||
$activityStatistic->save();
|
||||
|
||||
$response = $service->activitiesPerYearWithContact($contact);
|
||||
|
||||
$this->assertCount(
|
||||
2,
|
||||
$response
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
6,
|
||||
$response[0]->count
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
3,
|
||||
$response[1]->count
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
ActivityStatistic::class,
|
||||
$response[0]
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_gets_a_list_of_activities_per_month_for_given_year()
|
||||
{
|
||||
$service = new ActivityStatisticService;
|
||||
$account = factory(Account::class)->create();
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
Carbon::setTestNow(Carbon::create(2017, 1, 1));
|
||||
|
||||
for ($i = 0; $i <= 2; $i++) {
|
||||
$activity = factory(Activity::class)->create([
|
||||
'happened_at' => '2017-01-02',
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
$contact->activities()->attach($activity, ['account_id' => $contact->account_id]);
|
||||
}
|
||||
|
||||
for ($i = 0; $i <= 5; $i++) {
|
||||
$activity = factory(Activity::class)->create([
|
||||
'happened_at' => '2017-02-01',
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
$contact->activities()->attach($activity, ['account_id' => $contact->account_id]);
|
||||
}
|
||||
|
||||
$response = $service->activitiesPerMonthForYear($contact, 2017);
|
||||
|
||||
$this->assertCount(
|
||||
12,
|
||||
$response
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
1,
|
||||
$response[0]['month']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
3,
|
||||
$response[0]['occurences']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
2,
|
||||
$response[1]['month']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
6,
|
||||
$response[1]['occurences']
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Activity::class,
|
||||
$response[1]['activities'][0]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Activity\ActivityType;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Account\ActivityType;
|
||||
use App\Models\Account\ActivityTypeCategory;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use App\Services\Account\Activity\ActivityType\CreateActivityType;
|
||||
|
||||
class CreateActivityTypeTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_stores_an_activity_type()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$activityTypeCategory = factory(ActivityTypeCategory::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'activity_type_category_id' => $activityTypeCategory->id,
|
||||
'name' => 'central perk',
|
||||
'translation_key' => 'central_perk',
|
||||
];
|
||||
|
||||
$activityType = app(CreateActivityType::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('activity_types', [
|
||||
'id' => $activityType->id,
|
||||
'account_id' => $account->id,
|
||||
'activity_type_category_id' => $activityTypeCategory->id,
|
||||
'name' => 'central perk',
|
||||
'translation_key' => 'central_perk',
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
ActivityType::class,
|
||||
$activityType
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$request = [
|
||||
'name' => '199 Lafayette Street',
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(CreateActivityType::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_activity_type_category_is_not_linked_to_account()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$activityTypeCategory = factory(ActivityTypeCategory::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'activity_type_category_id' => $activityTypeCategory->id,
|
||||
'name' => 'central perk',
|
||||
'translation_key' => 'central_perk',
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(CreateActivityType::class)->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Activity\ActivityType;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Account\ActivityType;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use App\Services\Account\Activity\ActivityType\DestroyActivityType;
|
||||
|
||||
class DestroyActivityTypeTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_destroys_a_activity_type()
|
||||
{
|
||||
$activityType = factory(ActivityType::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $activityType->account_id,
|
||||
'activity_type_id' => $activityType->id,
|
||||
];
|
||||
|
||||
app(DestroyActivityType::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseMissing('activity_types', [
|
||||
'id' => $activityType->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_account_is_not_linked_to_activity_type()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$activityType = factory(ActivityType::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'activity_type_id' => $activityType->id,
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(DestroyActivityType::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_ids_do_not_exist()
|
||||
{
|
||||
$request = [
|
||||
'account_id' => 11111111,
|
||||
'activity_type_id' => 11111111,
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(DestroyActivityType::class)->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Activity\ActivityType;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Account\ActivityType;
|
||||
use App\Models\Account\ActivityTypeCategory;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use App\Services\Account\Activity\ActivityType\UpdateActivityType;
|
||||
|
||||
class UpdateActivityTypeTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_updates_an_activity_type()
|
||||
{
|
||||
$activityType = factory(ActivityType::class)->create([]);
|
||||
$activityTypeCategory = factory(ActivityTypeCategory::class)->create([
|
||||
'account_id' => $activityType->account_id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $activityType->account_id,
|
||||
'activity_type_id' => $activityType->id,
|
||||
'activity_type_category_id' => $activityTypeCategory->id,
|
||||
'name' => 'Chandler House',
|
||||
'translation_key' => 'https://centralperk.com',
|
||||
];
|
||||
|
||||
$activityType = app(UpdateActivityType::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('activity_types', [
|
||||
'id' => $activityType->id,
|
||||
'account_id' => $activityType->account_id,
|
||||
'activity_type_category_id' => $activityTypeCategory->id,
|
||||
'name' => 'Chandler House',
|
||||
'translation_key' => 'https://centralperk.com',
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
ActivityType::class,
|
||||
$activityType
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$activityType = factory(ActivityType::class)->create([]);
|
||||
$activityTypeCategory = factory(ActivityTypeCategory::class)->create([
|
||||
'account_id' => $activityType->account_id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $activityType->account_id,
|
||||
'activity_type_category_id' => $activityTypeCategory->id,
|
||||
'name' => 'Chandler House',
|
||||
'translation_key' => 'https://centralperk.com',
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(UpdateActivityType::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_activity_is_not_linked_to_account()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$activityType = factory(ActivityType::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'activity_type_id' => $activityType->id,
|
||||
'activity_type_category_id' => $activityType->activity_type_category_id,
|
||||
'name' => 'Chandler House',
|
||||
'translation_key' => 'https://centralperk.com',
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(UpdateActivityType::class)->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Activity\ActivityTypeCategory;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Account\ActivityTypeCategory;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Services\Account\Activity\ActivityTypeCategory\CreateActivityTypeCategory;
|
||||
|
||||
class CreateActivityTypeCategoryTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_stores_an_activity_type_category()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'name' => 'central perk',
|
||||
'translation_key' => 'central_perk',
|
||||
];
|
||||
|
||||
$activityTypeCategory = app(CreateActivityTypeCategory::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('activity_type_categories', [
|
||||
'id' => $activityTypeCategory->id,
|
||||
'account_id' => $account->id,
|
||||
'name' => 'central perk',
|
||||
'translation_key' => 'central_perk',
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
ActivityTypeCategory::class,
|
||||
$activityTypeCategory
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$request = [
|
||||
'name' => '199 Lafayette Street',
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(CreateActivityTypeCategory::class)->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Activity\ActivityTypeCategory;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Account\ActivityTypeCategory;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use App\Services\Account\Activity\ActivityTypeCategory\DestroyActivityTypeCategory;
|
||||
|
||||
class DestroyActivityTypeCategoryTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_destroys_a_activity_type_category()
|
||||
{
|
||||
$activityTypeCategory = factory(ActivityTypeCategory::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $activityTypeCategory->account_id,
|
||||
'activity_type_category_id' => $activityTypeCategory->id,
|
||||
];
|
||||
|
||||
app(DestroyActivityTypeCategory::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseMissing('activity_type_categories', [
|
||||
'id' => $activityTypeCategory->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_account_is_not_linked_to_activity_type_category()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$activityTypeCategory = factory(ActivityTypeCategory::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'activity_type_category_id' => $activityTypeCategory->id,
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(DestroyActivityTypeCategory::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_ids_do_not_exist()
|
||||
{
|
||||
$request = [
|
||||
'account_id' => 11111111,
|
||||
'activity_type_category_id' => 11111111,
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(DestroyActivityTypeCategory::class)->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Activity\ActivityTypeCategory;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Account\ActivityTypeCategory;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use App\Services\Account\Activity\ActivityTypeCategory\UpdateActivityTypeCategory;
|
||||
|
||||
class UpdateActivityTypeCategoryTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_updates_a_activity_type_category()
|
||||
{
|
||||
$activityTypeCategory = factory(ActivityTypeCategory::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $activityTypeCategory->account_id,
|
||||
'activity_type_category_id' => $activityTypeCategory->id,
|
||||
'name' => 'Chandler House',
|
||||
'translation_key' => 'https://centralperk.com',
|
||||
];
|
||||
|
||||
$activityTypeCategory = app(UpdateActivityTypeCategory::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('activity_type_categories', [
|
||||
'id' => $activityTypeCategory->id,
|
||||
'account_id' => $activityTypeCategory->account_id,
|
||||
'name' => 'Chandler House',
|
||||
'translation_key' => 'https://centralperk.com',
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
ActivityTypeCategory::class,
|
||||
$activityTypeCategory
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$activityTypeCategory = factory(ActivityTypeCategory::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'name' => '199 Lafayette Street',
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(UpdateActivityTypeCategory::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_activity_is_not_linked_to_account()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$activityTypeCategory = factory(ActivityTypeCategory::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'activity_type_category_id' => $activityTypeCategory->id,
|
||||
'name' => '199 Lafayette Street',
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(UpdateActivityTypeCategory::class)->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Activity;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Account\Activity;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use App\Services\Account\Activity\Activity\AttachContactToActivity;
|
||||
|
||||
class AttachContactToActivityTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_attaches_contacts()
|
||||
{
|
||||
$activity = factory(Activity::class)->create([]);
|
||||
$contactA = factory(Contact::class)->create([
|
||||
'account_id' => $activity->account_id,
|
||||
]);
|
||||
$contactB = factory(Contact::class)->create([
|
||||
'account_id' => $activity->account_id,
|
||||
]);
|
||||
$contactC = factory(Contact::class)->create([
|
||||
'account_id' => $activity->account_id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $activity->account_id,
|
||||
'activity_id' => $activity->id,
|
||||
'contacts' => [$contactA->id, $contactB->id, $contactC->id],
|
||||
];
|
||||
|
||||
$activity = app(AttachContactToActivity::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('activity_contact', [
|
||||
'activity_id' => $activity->id,
|
||||
'contact_id' => $contactA->id,
|
||||
'account_id' => $activity->account_id,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('activity_contact', [
|
||||
'activity_id' => $activity->id,
|
||||
'contact_id' => $contactB->id,
|
||||
'account_id' => $activity->account_id,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('activity_contact', [
|
||||
'activity_id' => $activity->id,
|
||||
'contact_id' => $contactC->id,
|
||||
'account_id' => $activity->account_id,
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Activity::class,
|
||||
$activity
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$activity = factory(Activity::class)->create([]);
|
||||
$contactA = factory(Contact::class)->create([
|
||||
'account_id' => $activity->account_id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'activity_id' => $activity->id,
|
||||
'contacts' => [$contactA->id],
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(AttachContactToActivity::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_contact_is_not_linked_to_account()
|
||||
{
|
||||
$activity = factory(Activity::class)->create([]);
|
||||
$account = factory(Account::class)->create([]);
|
||||
$contactA = factory(Contact::class)->create([
|
||||
'account_id' => $activity->account_id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'activity_id' => $activity->id,
|
||||
'account_id' => $account->id,
|
||||
'contacts' => [$contactA->id],
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(AttachContactToActivity::class)->execute($request);
|
||||
}
|
||||
}
|
||||
149
tests/Unit/Services/Account/Activity/CreateActivityTest.php
Normal file
149
tests/Unit/Services/Account/Activity/CreateActivityTest.php
Normal file
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Activity;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Account\Activity;
|
||||
use App\Models\Account\ActivityType;
|
||||
use App\Models\Instance\Emotion\Emotion;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use App\Services\Account\Activity\Activity\CreateActivity;
|
||||
|
||||
class CreateActivityTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_stores_an_activity_and_creates_an_entry_in_the_journal()
|
||||
{
|
||||
$account = factory(Account::class)->create();
|
||||
$contacts = factory(Contact::class, 3)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
$activityType = factory(ActivityType::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'activity_type_id' => $activityType->id,
|
||||
'summary' => 'we went to central perk',
|
||||
'description' => 'it was awesome',
|
||||
'happened_at' => '2009-09-09',
|
||||
'contacts' => $contacts->map(function ($contact) {
|
||||
return $contact->id;
|
||||
})->toArray(),
|
||||
];
|
||||
|
||||
$activity = app(CreateActivity::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('activities', [
|
||||
'id' => $activity->id,
|
||||
'account_id' => $account->id,
|
||||
'summary' => 'we went to central perk',
|
||||
'description' => 'it was awesome',
|
||||
'happened_at' => '2009-09-09',
|
||||
]);
|
||||
|
||||
foreach ($contacts as $contact) {
|
||||
$this->assertDatabaseHas('activity_contact', [
|
||||
'account_id' => $account->id,
|
||||
'activity_id' => $activity->id,
|
||||
'contact_id' => $contact->id,
|
||||
]);
|
||||
}
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Activity::class,
|
||||
$activity
|
||||
);
|
||||
|
||||
$this->assertDatabaseHas('journal_entries', [
|
||||
'account_id' => $account->id,
|
||||
'journalable_id' => $activity->id,
|
||||
'journalable_type' => get_class($activity),
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_adds_emotions()
|
||||
{
|
||||
$account = factory(Account::class)->create();
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
$emotion = factory(Emotion::class)->create([]);
|
||||
$emotion2 = factory(Emotion::class)->create([]);
|
||||
|
||||
$emotionArray = [];
|
||||
$emotionArray[] = $emotion->id;
|
||||
$emotionArray[] = $emotion2->id;
|
||||
|
||||
$activityType = factory(ActivityType::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'activity_type_id' => $activityType->id,
|
||||
'summary' => 'we went to central perk',
|
||||
'description' => 'it was awesome',
|
||||
'happened_at' => '2009-09-09',
|
||||
'emotions' => $emotionArray,
|
||||
'contacts' => [$contact->id],
|
||||
];
|
||||
|
||||
$activity = app(CreateActivity::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('emotion_activity', [
|
||||
'account_id' => $account->id,
|
||||
'activity_id' => $activity->id,
|
||||
'emotion_id' => $emotion->id,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('emotion_activity', [
|
||||
'account_id' => $account->id,
|
||||
'activity_id' => $activity->id,
|
||||
'emotion_id' => $emotion2->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(CreateActivity::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_activity_type_is_not_linked_to_account()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$activityType = factory(ActivityType::class)->create([]);
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'activity_type_id' => $activityType->id,
|
||||
'summary' => 'we went to central perk',
|
||||
'description' => 'it was awesome',
|
||||
'happened_at' => '2009-09-09',
|
||||
'contacts' => [$contact->id],
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(CreateActivity::class)->execute($request);
|
||||
}
|
||||
}
|
||||
94
tests/Unit/Services/Account/Activity/DestroyActivityTest.php
Normal file
94
tests/Unit/Services/Account/Activity/DestroyActivityTest.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Activity;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Account\Activity;
|
||||
use App\Models\Account\ActivityType;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Services\Account\Activity\Activity\CreateActivity;
|
||||
use App\Services\Account\Activity\Activity\DestroyActivity;
|
||||
|
||||
class DestroyActivityTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_destroys_a_activity()
|
||||
{
|
||||
$activity = factory(Activity::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $activity->account_id,
|
||||
'activity_id' => $activity->id,
|
||||
];
|
||||
|
||||
$this->assertDatabaseHas('activities', [
|
||||
'id' => $activity->id,
|
||||
]);
|
||||
|
||||
app(DestroyActivity::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseMissing('activities', [
|
||||
'id' => $activity->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_removes_the_journal_entry_when_destroying_the_activity()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$activityType = factory(ActivityType::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'activity_type_id' => $activityType->id,
|
||||
'summary' => 'we went to central perk',
|
||||
'description' => 'it was awesome',
|
||||
'happened_at' => '2009-09-09',
|
||||
'contacts' => [$contact->id],
|
||||
];
|
||||
|
||||
$activity = app(CreateActivity::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('activities', [
|
||||
'id' => $activity->id,
|
||||
]);
|
||||
$this->assertDatabaseHas('activity_contact', [
|
||||
'activity_id' => $activity->id,
|
||||
'contact_id' => $contact->id,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('journal_entries', [
|
||||
'account_id' => $account->id,
|
||||
'journalable_id' => $activity->id,
|
||||
'journalable_type' => get_class($activity),
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $activity->account_id,
|
||||
'activity_id' => $activity->id,
|
||||
];
|
||||
app(DestroyActivity::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseMissing('activities', [
|
||||
'id' => $activity->id,
|
||||
]);
|
||||
$this->assertDatabaseMissing('activity_contact', [
|
||||
'activity_id' => $activity->id,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseMissing('journal_entries', [
|
||||
'account_id' => $account->id,
|
||||
'journalable_id' => $activity->id,
|
||||
'journalable_type' => get_class($activity),
|
||||
]);
|
||||
}
|
||||
}
|
||||
191
tests/Unit/Services/Account/Activity/UpdateActivityTest.php
Normal file
191
tests/Unit/Services/Account/Activity/UpdateActivityTest.php
Normal file
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Activity;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Account\Activity;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use App\Services\Account\Activity\Activity\UpdateActivity;
|
||||
|
||||
class UpdateActivityTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_updates_an_activity()
|
||||
{
|
||||
$activity = factory(Activity::class)->create([]);
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $activity->account_id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $activity->account_id,
|
||||
'activity_id' => $activity->id,
|
||||
'activity_type_id' => $activity->activity_type_id,
|
||||
'summary' => 'we went to central perk',
|
||||
'description' => 'it was awesome',
|
||||
'happened_at' => '2009-09-09',
|
||||
'contacts' => [$contact->id],
|
||||
];
|
||||
|
||||
app(UpdateActivity::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('activities', [
|
||||
'id' => $activity->id,
|
||||
'account_id' => $activity->account_id,
|
||||
'summary' => 'we went to central perk',
|
||||
'description' => 'it was awesome',
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Activity::class,
|
||||
$activity
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_removes_old_associated_contacts()
|
||||
{
|
||||
$activity = factory(Activity::class)->create();
|
||||
$contacts = factory(Contact::class, 3)->create([
|
||||
'account_id' => $activity->account_id,
|
||||
]);
|
||||
foreach ($contacts as $contact) {
|
||||
$activity->contacts()->syncWithoutDetaching([$contact->id => [
|
||||
'account_id' => $activity->account_id,
|
||||
]]);
|
||||
}
|
||||
|
||||
foreach ($contacts as $contact) {
|
||||
$this->assertDatabaseHas('activity_contact', [
|
||||
'account_id' => $activity->account_id,
|
||||
'activity_id' => $activity->id,
|
||||
'contact_id' => $contact->id,
|
||||
]);
|
||||
}
|
||||
|
||||
$newContact = factory(Contact::class)->create([
|
||||
'account_id' => $activity->account_id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $activity->account_id,
|
||||
'activity_id' => $activity->id,
|
||||
'activity_type_id' => $activity->activity_type_id,
|
||||
'summary' => 'we went to central perk',
|
||||
'description' => 'it was awesome',
|
||||
'happened_at' => '2009-09-09',
|
||||
'contacts' => [$newContact->id],
|
||||
];
|
||||
|
||||
app(UpdateActivity::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('activity_contact', [
|
||||
'account_id' => $activity->account_id,
|
||||
'activity_id' => $activity->id,
|
||||
'contact_id' => $newContact->id,
|
||||
]);
|
||||
foreach ($contacts as $contact) {
|
||||
$this->assertDatabaseMissing('activity_contact', [
|
||||
'account_id' => $activity->account_id,
|
||||
'activity_id' => $activity->id,
|
||||
'contact_id' => $contact->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_removes_old_associated_contacts_and_keep_previous_one()
|
||||
{
|
||||
$activity = factory(Activity::class)->create();
|
||||
$contacts = factory(Contact::class, 3)->create([
|
||||
'account_id' => $activity->account_id,
|
||||
]);
|
||||
foreach ($contacts as $contact) {
|
||||
$activity->contacts()->syncWithoutDetaching([$contact->id => [
|
||||
'account_id' => $activity->account_id,
|
||||
]]);
|
||||
}
|
||||
|
||||
foreach ($contacts as $contact) {
|
||||
$this->assertDatabaseHas('activity_contact', [
|
||||
'account_id' => $activity->account_id,
|
||||
'activity_id' => $activity->id,
|
||||
'contact_id' => $contact->id,
|
||||
]);
|
||||
}
|
||||
|
||||
$request = [
|
||||
'account_id' => $activity->account_id,
|
||||
'activity_id' => $activity->id,
|
||||
'activity_type_id' => $activity->activity_type_id,
|
||||
'summary' => 'we went to central perk',
|
||||
'description' => 'it was awesome',
|
||||
'happened_at' => '2009-09-09',
|
||||
'contacts' => [$contacts[0]->id, $contacts[1]->id],
|
||||
];
|
||||
|
||||
app(UpdateActivity::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('activity_contact', [
|
||||
'account_id' => $activity->account_id,
|
||||
'activity_id' => $activity->id,
|
||||
'contact_id' => $contacts[0]->id,
|
||||
]);
|
||||
$this->assertDatabaseHas('activity_contact', [
|
||||
'account_id' => $activity->account_id,
|
||||
'activity_id' => $activity->id,
|
||||
'contact_id' => $contacts[1]->id,
|
||||
]);
|
||||
$this->assertDatabaseMissing('activity_contact', [
|
||||
'account_id' => $activity->account_id,
|
||||
'activity_id' => $activity->id,
|
||||
'contact_id' => $contacts[2]->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$activity = factory(Activity::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'activity_id' => $activity->id,
|
||||
'activity_type_id' => $activity->activity_type_id,
|
||||
'summary' => 'we went to central perk',
|
||||
'description' => 'it was awesome',
|
||||
'happened_at' => '2009-09-09',
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(UpdateActivity::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_contact_is_not_linked_to_account()
|
||||
{
|
||||
$activity = factory(Activity::class)->create([]);
|
||||
$account = factory(Account::class)->create([]);
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $activity->account_id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'activity_id' => $activity->id,
|
||||
'activity_type_id' => $activity->activity_type_id,
|
||||
'summary' => 'we went to central perk',
|
||||
'description' => 'it was awesome',
|
||||
'happened_at' => '2009-09-09',
|
||||
'contacts' => [$contact->id],
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(UpdateActivity::class)->execute($request);
|
||||
}
|
||||
}
|
||||
76
tests/Unit/Services/Account/Company/CreateCompanyTest.php
Normal file
76
tests/Unit/Services/Account/Company/CreateCompanyTest.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Company;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use function Safe\json_encode;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Account\Company;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use App\Jobs\AuditLog\LogAccountAudit;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Services\Account\Company\CreateCompany;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class CreateCompanyTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_stores_a_company()
|
||||
{
|
||||
Queue::fake();
|
||||
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'author_id' => $user->id,
|
||||
'name' => 'central perk',
|
||||
'website' => 'https://centralperk.com',
|
||||
'number_of_employees' => 3,
|
||||
];
|
||||
|
||||
$company = app(CreateCompany::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('companies', [
|
||||
'id' => $company->id,
|
||||
'account_id' => $account->id,
|
||||
'name' => 'central perk',
|
||||
'website' => 'https://centralperk.com',
|
||||
'number_of_employees' => 3,
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Company::class,
|
||||
$company
|
||||
);
|
||||
|
||||
Queue::assertPushed(LogAccountAudit::class, function ($job) use ($user) {
|
||||
return $job->auditLog['action'] === 'company_created' &&
|
||||
$job->auditLog['author_id'] === $user->id &&
|
||||
$job->auditLog['about_contact_id'] === null &&
|
||||
$job->auditLog['should_appear_on_dashboard'] === true &&
|
||||
$job->auditLog['objects'] === json_encode([
|
||||
'name' => 'central perk',
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/** @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(CreateCompany::class)->execute($request);
|
||||
}
|
||||
}
|
||||
60
tests/Unit/Services/Account/Company/DestroyCompanyTest.php
Normal file
60
tests/Unit/Services/Account/Company/DestroyCompanyTest.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Company;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Account\Company;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Services\Account\Company\DestroyCompany;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
class DestroyCompanyTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_destroys_a_company()
|
||||
{
|
||||
$company = factory(Company::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $company->account_id,
|
||||
'company_id' => $company->id,
|
||||
];
|
||||
|
||||
app(DestroyCompany::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseMissing('companies', [
|
||||
'id' => $company->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_account_is_not_linked_to_company()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$company = factory(Company::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'company_id' => $company->id,
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(DestroyCompany::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_ids_do_not_exist()
|
||||
{
|
||||
$request = [
|
||||
'account_id' => 11111111,
|
||||
'company_id' => 11111111,
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(DestroyCompany::class)->execute($request);
|
||||
}
|
||||
}
|
||||
74
tests/Unit/Services/Account/Company/UpdateCompanyTest.php
Normal file
74
tests/Unit/Services/Account/Company/UpdateCompanyTest.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Company;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Account\Company;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Services\Account\Company\UpdateCompany;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
class UpdateCompanyTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_updates_a_company()
|
||||
{
|
||||
$company = factory(Company::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $company->account_id,
|
||||
'company_id' => $company->id,
|
||||
'name' => 'Chandler House',
|
||||
'website' => 'https://centralperk.com',
|
||||
'number_of_employees' => 300,
|
||||
];
|
||||
|
||||
app(UpdateCompany::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('companies', [
|
||||
'id' => $company->id,
|
||||
'account_id' => $company->account_id,
|
||||
'name' => 'Chandler House',
|
||||
'website' => 'https://centralperk.com',
|
||||
'number_of_employees' => 300,
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Company::class,
|
||||
$company
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$company = factory(Company::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'name' => '199 Lafayette Street',
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(UpdateCompany::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_place_is_not_linked_to_account()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$company = factory(Company::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'company_id' => $company->id,
|
||||
'name' => '199 Lafayette Street',
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(UpdateCompany::class)->execute($request);
|
||||
}
|
||||
}
|
||||
55
tests/Unit/Services/Account/Gender/CreateGenderTest.php
Normal file
55
tests/Unit/Services/Account/Gender/CreateGenderTest.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Gender;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Contact\Gender;
|
||||
use App\Models\Account\Account;
|
||||
use App\Services\Account\Gender\CreateGender;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class CreateGenderTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_stores_a_gender()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'name' => 'man',
|
||||
'type' => 'M',
|
||||
];
|
||||
|
||||
$gender = app(CreateGender::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('genders', [
|
||||
'id' => $gender->id,
|
||||
'account_id' => $account->id,
|
||||
'name' => 'man',
|
||||
'type' => 'M',
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Gender::class,
|
||||
$gender
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'name' => 'man',
|
||||
'type' => 'X',
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(CreateGender::class)->execute($request);
|
||||
}
|
||||
}
|
||||
60
tests/Unit/Services/Account/Gender/DestroyGenderTest.php
Normal file
60
tests/Unit/Services/Account/Gender/DestroyGenderTest.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Gender;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Contact\Gender;
|
||||
use App\Models\Account\Account;
|
||||
use App\Services\Account\Gender\DestroyGender;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
class DestroyGenderTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_destroys_a_gender()
|
||||
{
|
||||
$gender = factory(Gender::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $gender->account_id,
|
||||
'gender_id' => $gender->id,
|
||||
];
|
||||
|
||||
app(DestroyGender::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseMissing('genders', [
|
||||
'id' => $gender->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_account_is_not_linked_to_gender()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$gender = factory(Gender::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'gender_id' => $gender->id,
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(DestroyGender::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_ids_do_not_exist()
|
||||
{
|
||||
$request = [
|
||||
'account_id' => 11111111,
|
||||
'gender_id' => 11111111,
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(DestroyGender::class)->execute($request);
|
||||
}
|
||||
}
|
||||
74
tests/Unit/Services/Account/Gender/UpdateGenderTest.php
Normal file
74
tests/Unit/Services/Account/Gender/UpdateGenderTest.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Gender;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Contact\Gender;
|
||||
use App\Models\Account\Account;
|
||||
use App\Services\Account\Gender\UpdateGender;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
class UpdateGenderTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_updates_a_gender()
|
||||
{
|
||||
$gender = factory(Gender::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $gender->account_id,
|
||||
'gender_id' => $gender->id,
|
||||
'name' => 'man',
|
||||
'type' => 'M',
|
||||
];
|
||||
|
||||
$gender = app(UpdateGender::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('genders', [
|
||||
'id' => $gender->id,
|
||||
'account_id' => $gender->account_id,
|
||||
'name' => 'man',
|
||||
'type' => 'M',
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Gender::class,
|
||||
$gender
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$gender = factory(Gender::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'name' => 'man',
|
||||
'type' => 'X',
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(UpdateGender::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_place_is_not_linked_to_account()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$gender = factory(Gender::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'gender_id' => $gender->id,
|
||||
'name' => 'man',
|
||||
'type' => 'M',
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(UpdateGender::class)->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\LifeEvent\LifeEventType;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Contact\LifeEventType;
|
||||
use App\Models\Contact\LifeEventCategory;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use App\Services\Account\LifeEvent\LifeEventType\CreateLifeEventType;
|
||||
|
||||
class CreateLifeEventTypeTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_stores_a_life_event_type()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$lifeEventCategory = factory(LifeEventCategory::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'life_event_category_id' => $lifeEventCategory->id,
|
||||
'name' => 'Had a major health problem',
|
||||
];
|
||||
|
||||
$lifeEventType = app(CreateLifeEventType::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('life_event_types', [
|
||||
'id' => $lifeEventType->id,
|
||||
'account_id' => $account->id,
|
||||
'life_event_category_id' => $lifeEventCategory->id,
|
||||
'name' => 'Had a major health problem',
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
LifeEventType::class,
|
||||
$lifeEventType
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$request = [
|
||||
'name' => 'Had a major health problem',
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(CreateLifeEventType::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_life_event_category_is_not_linked_to_account()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$lifeEventCategory = factory(LifeEventCategory::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'life_event_category_id' => $lifeEventCategory->id,
|
||||
'name' => 'Had a major health problem',
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(CreateLifeEventType::class)->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\LifeEvent\LifeEventType;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Contact\LifeEventType;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use App\Services\Account\LifeEvent\LifeEventType\DestroyLifeEventType;
|
||||
|
||||
class DestroyLifeEventTypeTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_destroys_a_life_event_type()
|
||||
{
|
||||
$lifeEventType = factory(LifeEventType::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $lifeEventType->account_id,
|
||||
'life_event_type_id' => $lifeEventType->id,
|
||||
];
|
||||
|
||||
app(DestroyLifeEventType::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseMissing('life_event_types', [
|
||||
'id' => $lifeEventType->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_account_is_not_linked_to_life_event_type()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$lifeEventType = factory(LifeEventType::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'life_event_type_id' => $lifeEventType->id,
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(DestroyLifeEventType::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_ids_do_not_exist()
|
||||
{
|
||||
$request = [
|
||||
'account_id' => 11111111,
|
||||
'life_event_type_id' => 11111111,
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(DestroyLifeEventType::class)->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\LifeEvent\LifeEventType;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Contact\LifeEventType;
|
||||
use App\Models\Contact\LifeEventCategory;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use App\Services\Account\LifeEvent\LifeEventType\UpdateLifeEventType;
|
||||
|
||||
class UpdateLifeEventTypeTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_updates_a_life_event_type()
|
||||
{
|
||||
$lifeEventType = factory(LifeEventType::class)->create([]);
|
||||
$lifeEventCategory = factory(LifeEventCategory::class)->create([
|
||||
'account_id' => $lifeEventType->account_id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $lifeEventType->account_id,
|
||||
'life_event_type_id' => $lifeEventType->id,
|
||||
'life_event_category_id' => $lifeEventCategory->id,
|
||||
'name' => 'Had a major health problem',
|
||||
];
|
||||
|
||||
$lifeEventType = app(UpdateLifeEventType::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('life_event_types', [
|
||||
'id' => $lifeEventType->id,
|
||||
'account_id' => $lifeEventType->account_id,
|
||||
'life_event_category_id' => $lifeEventCategory->id,
|
||||
'name' => 'Had a major health problem',
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
LifeEventType::class,
|
||||
$lifeEventType
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$lifeEventType = factory(LifeEventType::class)->create([]);
|
||||
$lifeEventCategory = factory(LifeEventCategory::class)->create([
|
||||
'account_id' => $lifeEventType->account_id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $lifeEventType->account_id,
|
||||
'life_event_category_id' => $lifeEventCategory->id,
|
||||
'name' => 'Had a major health problem',
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(UpdateLifeEventType::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_life_event_is_not_linked_to_account()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$lifeEventType = factory(LifeEventType::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'life_event_type_id' => $lifeEventType->id,
|
||||
'life_event_category_id' => $lifeEventType->life_event_category_id,
|
||||
'name' => 'Had a major health problem',
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(UpdateLifeEventType::class)->execute($request);
|
||||
}
|
||||
}
|
||||
85
tests/Unit/Services/Account/Photo/DestroyPhotoTest.php
Normal file
85
tests/Unit/Services/Account/Photo/DestroyPhotoTest.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Photo;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Photo;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Contact\Contact;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use App\Services\Account\Photo\UploadPhoto;
|
||||
use App\Services\Account\Photo\DestroyPhoto;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
class DestroyPhotoTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_destroys_a_photo()
|
||||
{
|
||||
$contact = factory(Contact::class)->create([]);
|
||||
$photo = $this->uploadPhoto($contact);
|
||||
|
||||
$request = [
|
||||
'account_id' => $photo->account_id,
|
||||
'photo_id' => $photo->id,
|
||||
];
|
||||
|
||||
$this->assertDatabaseHas('photos', [
|
||||
'id' => $photo->id,
|
||||
]);
|
||||
|
||||
app(DestroyPhoto::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseMissing('photos', [
|
||||
'id' => $photo->id,
|
||||
]);
|
||||
|
||||
Storage::disk('photos')->assertMissing('photo.png');
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$request = [
|
||||
'photo_id' => 2,
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
|
||||
app(DestroyPhoto::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_a_photo_doesnt_exist()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$photo = factory(Photo::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'photo_id' => $photo->id,
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
|
||||
app(DestroyPhoto::class)->execute($request);
|
||||
}
|
||||
|
||||
private function uploadPhoto($contact)
|
||||
{
|
||||
Storage::fake('photos');
|
||||
|
||||
$request = [
|
||||
'account_id' => $contact->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'photo' => UploadedFile::fake()->image('photo.png'),
|
||||
];
|
||||
|
||||
return app(UploadPhoto::class)->execute($request);
|
||||
}
|
||||
}
|
||||
74
tests/Unit/Services/Account/Photo/UploadPhotoTest.php
Normal file
74
tests/Unit/Services/Account/Photo/UploadPhotoTest.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Photo;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Photo;
|
||||
use App\Models\Contact\Contact;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use App\Services\Account\Photo\UploadPhoto;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class UploadPhotoTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_uploads_a_photo()
|
||||
{
|
||||
Storage::fake('photos');
|
||||
|
||||
$contact = factory(Contact::class)->create([]);
|
||||
|
||||
$file = UploadedFile::fake()->image('imag.png');
|
||||
|
||||
$request = [
|
||||
'account_id' => $contact->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'photo' => $file,
|
||||
];
|
||||
|
||||
$photo = app(UploadPhoto::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('photos', [
|
||||
'id' => $photo->id,
|
||||
'account_id' => $contact->account_id,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Photo::class,
|
||||
$photo
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$request = [
|
||||
'account_id' => 'wrong',
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
|
||||
app(UploadPhoto::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_account_does_not_exist()
|
||||
{
|
||||
Storage::fake('photos');
|
||||
|
||||
$request = [
|
||||
'account_id' => 0,
|
||||
'contact_id' => 0,
|
||||
'photo' => UploadedFile::fake()->image('document.pdf'),
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
|
||||
app(UploadPhoto::class)->execute($request);
|
||||
}
|
||||
}
|
||||
95
tests/Unit/Services/Account/Place/CreatePlaceTest.php
Normal file
95
tests/Unit/Services/Account/Place/CreatePlaceTest.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Place;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Place;
|
||||
use App\Models\Account\Account;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Services\Account\Place\CreatePlace;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class CreatePlaceTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_stores_a_place_without_fetching_geolocation_information()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'street' => '199 Lafayette Street',
|
||||
'city' => 'New York City',
|
||||
'province' => '',
|
||||
'postal_code' => '',
|
||||
'country' => 'USA',
|
||||
'latitude' => '10',
|
||||
'longitude' => '10',
|
||||
];
|
||||
|
||||
$place = app(CreatePlace::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('places', [
|
||||
'id' => $place->id,
|
||||
'account_id' => $account->id,
|
||||
'street' => '199 Lafayette Street',
|
||||
'latitude' => 10,
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Place::class,
|
||||
$place
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_stores_a_place_and_fetch_geolocation_information()
|
||||
{
|
||||
config(['monica.enable_geolocation' => true]);
|
||||
config(['monica.location_iq_api_key' => 'test']);
|
||||
|
||||
$body = file_get_contents(base_path('tests/Fixtures/Services/Account/Place/CreatePlaceSampleResponse.json'));
|
||||
Http::fake([
|
||||
'us1.locationiq.com/v1/*' => Http::response($body, 200),
|
||||
]);
|
||||
|
||||
$account = factory(Account::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'street' => '12',
|
||||
'city' => 'beverly hills',
|
||||
'province' => '',
|
||||
'postal_code' => '90210',
|
||||
'country' => 'US',
|
||||
'latitude' => '',
|
||||
'longitude' => '',
|
||||
];
|
||||
|
||||
$place = app(CreatePlace::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('places', [
|
||||
'id' => $place->id,
|
||||
'account_id' => $account->id,
|
||||
'street' => '12',
|
||||
'latitude' => 34.0736204,
|
||||
'longitude' => -118.4003563,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
factory(Account::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'street' => '199 Lafayette Street',
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(CreatePlace::class)->execute($request);
|
||||
}
|
||||
}
|
||||
60
tests/Unit/Services/Account/Place/DestroyPlaceTest.php
Normal file
60
tests/Unit/Services/Account/Place/DestroyPlaceTest.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Place;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Place;
|
||||
use App\Models\Account\Account;
|
||||
use App\Services\Account\Place\DestroyPlace;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
class DestroyPlaceTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_destroys_a_place()
|
||||
{
|
||||
$place = factory(Place::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $place->account_id,
|
||||
'place_id' => $place->id,
|
||||
];
|
||||
|
||||
app(DestroyPlace::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseMissing('places', [
|
||||
'id' => $place->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_account_is_not_linked_to_places()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$place = factory(Place::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'place_id' => $place->id,
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(DestroyPlace::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_ids_do_not_exist()
|
||||
{
|
||||
$request = [
|
||||
'account_id' => 11111111,
|
||||
'place_id' => 11111111,
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(DestroyPlace::class)->execute($request);
|
||||
}
|
||||
}
|
||||
113
tests/Unit/Services/Account/Place/UpdatePlaceTest.php
Normal file
113
tests/Unit/Services/Account/Place/UpdatePlaceTest.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Place;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Place;
|
||||
use App\Models\Account\Account;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Services\Account\Place\UpdatePlace;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
class UpdatePlaceTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_updates_a_place_without_fetching_geolocation_information()
|
||||
{
|
||||
$place = factory(Place::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $place->account_id,
|
||||
'place_id' => $place->id,
|
||||
'street' => '199 Lafayette Street',
|
||||
'city' => 'New York City',
|
||||
'province' => '',
|
||||
'postal_code' => '',
|
||||
'country' => 'USA',
|
||||
'latitude' => '10',
|
||||
'longitude' => '10',
|
||||
];
|
||||
|
||||
$place = app(UpdatePlace::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('places', [
|
||||
'id' => $place->id,
|
||||
'account_id' => $place->account_id,
|
||||
'latitude' => 10,
|
||||
'city' => 'New York City',
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Place::class,
|
||||
$place
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_updates_a_place_and_fetch_geolocation_information()
|
||||
{
|
||||
config(['monica.enable_geolocation' => true]);
|
||||
config(['monica.location_iq_api_key' => 'test']);
|
||||
|
||||
$body = file_get_contents(base_path('tests/Fixtures/Services/Account/Place/UpdatePlaceSampleResponse.json'));
|
||||
Http::fake([
|
||||
'us1.locationiq.com/v1/*' => Http::response($body, 200),
|
||||
]);
|
||||
|
||||
$place = factory(Place::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $place->account_id,
|
||||
'place_id' => $place->id,
|
||||
'street' => '12',
|
||||
'city' => 'beverly hills',
|
||||
'province' => '',
|
||||
'postal_code' => '90210',
|
||||
'country' => 'US',
|
||||
'latitude' => '',
|
||||
'longitude' => '',
|
||||
];
|
||||
|
||||
$place = app(UpdatePlace::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('places', [
|
||||
'id' => $place->id,
|
||||
'account_id' => $place->account_id,
|
||||
'street' => '12',
|
||||
'latitude' => 34.0736204,
|
||||
'longitude' => -118.4003563,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$place = factory(Place::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'street' => '199 Lafayette Street',
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(UpdatePlace::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_throws_an_exception_if_place_is_not_linked_to_account()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$place = factory(Place::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'place_id' => $place->id,
|
||||
];
|
||||
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
app(UpdatePlace::class)->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Settings;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use App\Models\Contact\Contact;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Services\Account\Settings\DestroyAccount;
|
||||
use App\Services\Account\Settings\ArchiveAllContacts;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class ArchiveAllContactsTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_archives_all_the_contacts_in_an_account()
|
||||
{
|
||||
$user = factory(User::class)->create([]);
|
||||
factory(Contact::class, 3)->create([
|
||||
'account_id' => $user->account_id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $user->account_id,
|
||||
];
|
||||
|
||||
$result = app(ArchiveAllContacts::class)->execute($request);
|
||||
|
||||
$this->assertTrue($result);
|
||||
|
||||
$this->assertDatabaseHas('contacts', [
|
||||
'is_active' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$request = [];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(DestroyAccount::class)->execute($request);
|
||||
}
|
||||
}
|
||||
47
tests/Unit/Services/Account/Settings/DestroyAccountTest.php
Normal file
47
tests/Unit/Services/Account/Settings/DestroyAccountTest.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Settings;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use App\Models\Contact\Contact;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Services\Account\Settings\DestroyAccount;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class DestroyAccountTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_destroys_an_account()
|
||||
{
|
||||
$user = factory(User::class)->create([]);
|
||||
factory(Contact::class, 3)->create([
|
||||
'account_id' => $user->account_id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $user->account_id,
|
||||
];
|
||||
|
||||
app(DestroyAccount::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseMissing('contacts', [
|
||||
'account_id' => $user->account_id,
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseMissing('accounts', [
|
||||
'id' => $user->account_id,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$request = [];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(DestroyAccount::class)->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Settings;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Contact\Contact;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Services\Contact\Document\UploadDocument;
|
||||
use App\Services\Account\Settings\DestroyAllDocuments;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class DestroyAllDocumentsTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_destroys_all_documents()
|
||||
{
|
||||
Storage::fake();
|
||||
|
||||
$contact = factory(Contact::class)->create([]);
|
||||
|
||||
$documents = [];
|
||||
for ($i = 0; $i < 2; $i++) {
|
||||
$documents[] = $this->uploadDocument($contact);
|
||||
}
|
||||
|
||||
$request = [
|
||||
'account_id' => $contact->account_id,
|
||||
];
|
||||
|
||||
app(DestroyAllDocuments::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseMissing('documents', [
|
||||
'account_id' => $contact->account_id,
|
||||
]);
|
||||
|
||||
foreach ($documents as $document) {
|
||||
Storage::disk('public')->assertMissing($document->new_filename);
|
||||
}
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$request = [
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
|
||||
app(DestroyAllDocuments::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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Settings;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Contact\Contact;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use App\Services\Account\Photo\UploadPhoto;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Services\Account\Settings\DestroyAllPhotos;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class DestroyAllPhotosTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_destroys_all_photos()
|
||||
{
|
||||
Storage::fake();
|
||||
|
||||
$contact = factory(Contact::class)->create([]);
|
||||
|
||||
$photos = [];
|
||||
for ($i = 0; $i < 2; $i++) {
|
||||
$photos[] = $this->uploadPhoto($contact);
|
||||
}
|
||||
|
||||
$request = [
|
||||
'account_id' => $contact->account_id,
|
||||
];
|
||||
|
||||
app(DestroyAllPhotos::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseMissing('photos', [
|
||||
'account_id' => $contact->account_id,
|
||||
]);
|
||||
|
||||
foreach ($photos as $photo) {
|
||||
Storage::disk('public')->assertMissing($photo->new_filename);
|
||||
}
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$request = [];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
|
||||
app(DestroyAllPhotos::class)->execute($request);
|
||||
}
|
||||
|
||||
private function uploadPhoto($contact)
|
||||
{
|
||||
$request = [
|
||||
'account_id' => $contact->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'photo' => UploadedFile::fake()->image('imag.png'),
|
||||
];
|
||||
|
||||
$photo = app(UploadPhoto::class)->execute($request);
|
||||
|
||||
Storage::disk('public')->assertExists($photo->new_filename);
|
||||
|
||||
return $photo;
|
||||
}
|
||||
}
|
||||
363
tests/Unit/Services/Account/Settings/ExportAccountTest.php
Normal file
363
tests/Unit/Services/Account/Settings/ExportAccountTest.php
Normal file
@@ -0,0 +1,363 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Settings;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Mockery\MockInterface;
|
||||
use App\Jobs\ExportAccount;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Account\ExportJob;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use App\Notifications\ExportAccountDone;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Illuminate\Testing\AssertableJsonString;
|
||||
use App\Services\Account\Settings\SqlExportAccount;
|
||||
use App\Services\Account\Settings\JsonExportAccount;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class ExportAccountTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_exports_account_json()
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
Storage::fake();
|
||||
$fake = Storage::fake('local');
|
||||
$fake->put('temp/test.json', 'null');
|
||||
|
||||
$job = ExportJob::factory()->create();
|
||||
|
||||
$this->mock(JsonExportAccount::class, function (MockInterface $mock) use ($job) {
|
||||
$mock->shouldReceive('execute')
|
||||
->once()
|
||||
->with([
|
||||
'account_id' => $job->account_id,
|
||||
'user_id' => $job->user_id,
|
||||
])
|
||||
->andReturn('temp/test.json');
|
||||
});
|
||||
|
||||
ExportAccount::dispatchSync($job);
|
||||
$job->refresh();
|
||||
|
||||
Storage::disk('public')->assertExists($job->filename);
|
||||
|
||||
Notification::assertSentTo(
|
||||
[$job->user], ExportAccountDone::class
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_exports_account_sql()
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
Storage::fake();
|
||||
$fake = Storage::fake('local');
|
||||
$fake->put('temp/test.sql', 'null');
|
||||
|
||||
$job = ExportJob::factory()->create([
|
||||
'type' => 'sql',
|
||||
]);
|
||||
|
||||
$this->mock(SqlExportAccount::class, function (MockInterface $mock) use ($job) {
|
||||
$mock->shouldReceive('execute')
|
||||
->once()
|
||||
->with([
|
||||
'account_id' => $job->account_id,
|
||||
'user_id' => $job->user_id,
|
||||
])
|
||||
->andReturn('temp/test.sql');
|
||||
});
|
||||
|
||||
ExportAccount::dispatchSync($job);
|
||||
$job->refresh();
|
||||
|
||||
Storage::disk('public')->assertExists($job->filename);
|
||||
|
||||
Notification::assertSentTo(
|
||||
[$job->user], ExportAccountDone::class
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_exports_account_file()
|
||||
{
|
||||
Storage::fake();
|
||||
Storage::fake('local');
|
||||
|
||||
$job = ExportJob::factory()->create();
|
||||
ExportAccount::dispatchSync($job);
|
||||
|
||||
$job->refresh();
|
||||
|
||||
$this->assertStringStartsWith('exports/', $job->filename);
|
||||
$this->assertStringEndsWith('.json', $job->filename);
|
||||
Storage::disk('public')->assertExists($job->filename);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_exports_account_file_sql()
|
||||
{
|
||||
Storage::fake();
|
||||
Storage::fake('local');
|
||||
|
||||
$job = ExportJob::factory()->create([
|
||||
'type' => 'sql',
|
||||
]);
|
||||
ExportAccount::dispatchSync($job);
|
||||
|
||||
$job->refresh();
|
||||
|
||||
$this->assertStringStartsWith('exports/', $job->filename);
|
||||
$this->assertStringEndsWith('.sql', $job->filename);
|
||||
Storage::disk('public')->assertExists($job->filename);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_exports_json_file()
|
||||
{
|
||||
Storage::fake();
|
||||
Storage::fake('local');
|
||||
|
||||
$job = ExportJob::factory()->create();
|
||||
ExportAccount::dispatchSync($job);
|
||||
|
||||
$job->refresh();
|
||||
|
||||
$this->assertStringStartsWith('exports/', $job->filename);
|
||||
$this->assertStringEndsWith('.json', $job->filename);
|
||||
Storage::disk('public')->assertExists($job->filename);
|
||||
|
||||
$json = Storage::disk('public')->get($job->filename);
|
||||
$test = new AssertableJsonString($json);
|
||||
|
||||
$test->assertStructure([
|
||||
'account' => [
|
||||
'uuid',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'data' => [
|
||||
'*' => [
|
||||
'count',
|
||||
'type',
|
||||
'values' => [
|
||||
'*' => [
|
||||
'uuid',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'properties' => [
|
||||
'modules' => [
|
||||
'*' => [
|
||||
'key',
|
||||
'translation_key',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties',
|
||||
],
|
||||
],
|
||||
'reminder_rules' => [
|
||||
'*' => [
|
||||
'number_of_days_before',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties',
|
||||
],
|
||||
],
|
||||
],
|
||||
'instance' => [
|
||||
'activity_types' => [
|
||||
'*' => [
|
||||
'uuid',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties' => [
|
||||
'name',
|
||||
'translation_key',
|
||||
'category',
|
||||
],
|
||||
],
|
||||
],
|
||||
'activity_type_categories' => [
|
||||
'*' => [
|
||||
'uuid',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties' => [
|
||||
'name',
|
||||
'translation_key',
|
||||
],
|
||||
],
|
||||
],
|
||||
'life_event_types' => [
|
||||
'*' => [
|
||||
'uuid',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties' => [
|
||||
'translation_key',
|
||||
'core_monica_data',
|
||||
'category',
|
||||
],
|
||||
],
|
||||
],
|
||||
'life_event_categories' => [
|
||||
'*' => [
|
||||
'uuid',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties' => [
|
||||
'translation_key',
|
||||
'core_monica_data',
|
||||
],
|
||||
],
|
||||
],
|
||||
'contact_field_types' => [
|
||||
'*' => [
|
||||
'uuid',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties' => [
|
||||
'name',
|
||||
'fontawesome_icon',
|
||||
'delible',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_exports_json_file_contacts()
|
||||
{
|
||||
Storage::fake();
|
||||
Storage::fake('local');
|
||||
|
||||
$job = ExportJob::factory()->create();
|
||||
factory(Contact::class, 5)->create([
|
||||
'account_id' => $job->account->id,
|
||||
]);
|
||||
|
||||
ExportAccount::dispatchSync($job);
|
||||
|
||||
$job->refresh();
|
||||
|
||||
$this->assertStringStartsWith('exports/', $job->filename);
|
||||
$this->assertStringEndsWith('.json', $job->filename);
|
||||
Storage::disk('public')->assertExists($job->filename);
|
||||
|
||||
$json = Storage::disk('public')->get($job->filename);
|
||||
$test = new AssertableJsonString($json);
|
||||
|
||||
$test->assertStructure([
|
||||
'account' => [
|
||||
'uuid',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'data' => [
|
||||
'*' => [
|
||||
'count',
|
||||
'type',
|
||||
'values' => [
|
||||
'*' => [
|
||||
'uuid',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'properties' => [
|
||||
'modules' => [
|
||||
'*' => [
|
||||
'key',
|
||||
'translation_key',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties',
|
||||
],
|
||||
],
|
||||
'reminder_rules' => [
|
||||
'*' => [
|
||||
'number_of_days_before',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties',
|
||||
],
|
||||
],
|
||||
],
|
||||
'instance' => [
|
||||
'activity_types' => [
|
||||
'*' => [
|
||||
'uuid',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties' => [
|
||||
'name',
|
||||
'translation_key',
|
||||
'category',
|
||||
],
|
||||
],
|
||||
],
|
||||
'activity_type_categories' => [
|
||||
'*' => [
|
||||
'uuid',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties' => [
|
||||
'name',
|
||||
'translation_key',
|
||||
],
|
||||
],
|
||||
],
|
||||
'life_event_types' => [
|
||||
'*' => [
|
||||
'uuid',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties' => [
|
||||
'translation_key',
|
||||
'core_monica_data',
|
||||
'category',
|
||||
],
|
||||
],
|
||||
],
|
||||
'life_event_categories' => [
|
||||
'*' => [
|
||||
'uuid',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties' => [
|
||||
'translation_key',
|
||||
'core_monica_data',
|
||||
],
|
||||
],
|
||||
],
|
||||
'contact_field_types' => [
|
||||
'*' => [
|
||||
'uuid',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'properties' => [
|
||||
'name',
|
||||
'fontawesome_icon',
|
||||
'delible',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
67
tests/Unit/Services/Account/Settings/ResetAccountTest.php
Normal file
67
tests/Unit/Services/Account/Settings/ResetAccountTest.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Settings;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Account\ActivityType;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Services\Account\Settings\ResetAccount;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Services\Account\Activity\Activity\CreateActivity;
|
||||
|
||||
class ResetAccountTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_resets_an_account()
|
||||
{
|
||||
// populate the account with fake contacts and activities
|
||||
$user = factory(User::class)->create();
|
||||
$contacts = factory(Contact::class, 3)->create([
|
||||
'account_id' => $user->account_id,
|
||||
]);
|
||||
|
||||
$activityType = factory(ActivityType::class)->create([
|
||||
'account_id' => $user->account_id,
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $user->account_id,
|
||||
'activity_type_id' => $activityType->id,
|
||||
'summary' => 'we went to central perk',
|
||||
'description' => 'it was awesome',
|
||||
'happened_at' => '2009-09-09',
|
||||
'contacts' => $contacts->map(function ($contact) {
|
||||
return $contact->id;
|
||||
})->toArray(),
|
||||
];
|
||||
|
||||
app(CreateActivity::class)->execute($request);
|
||||
|
||||
$request = [
|
||||
'account_id' => $user->account_id,
|
||||
];
|
||||
|
||||
app(ResetAccount::class)->handle($request);
|
||||
|
||||
$this->assertDatabaseMissing('contacts', [
|
||||
'account_id' => $user->account_id,
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseMissing('activities', [
|
||||
'account_id' => $user->account_id,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$request = [];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(ResetAccount::class)->handle($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Account\Settings;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Services\Account\Settings\SqlExportAccount;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class SqlExportAccountTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_exports_account_information()
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$user = factory(User::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $user->account_id,
|
||||
'user_id' => $user->id,
|
||||
];
|
||||
|
||||
$filename = app(SqlExportAccount::class)->execute($request);
|
||||
|
||||
$this->assertStringStartsWith('temp/', $filename);
|
||||
$this->assertStringEndsWith('.sql', $filename);
|
||||
Storage::disk('local')->assertExists($filename);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$request = [];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(SqlExportAccount::class)->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Auth;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use App\Models\Account\Account;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Services\Auth\Population\PopulateContactFieldTypesTable;
|
||||
|
||||
class PopulateContactFieldTypesTableTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$request = [
|
||||
'account_id' => 1,
|
||||
];
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
app(PopulateContactFieldTypesTable::class)->execute($request);
|
||||
|
||||
$request = [
|
||||
'migrate_existing_data' => false,
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(PopulateContactFieldTypesTable::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_populate_contact_field_types_tables()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$number = DB::table('contact_field_types')
|
||||
->where('account_id', $account->id)
|
||||
->count();
|
||||
|
||||
DB::table('default_contact_field_types')
|
||||
->where('name', 'Phone')
|
||||
->update(['migrated' => 0]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'migrate_existing_data' => false,
|
||||
];
|
||||
|
||||
app(PopulateContactFieldTypesTable::class)->execute($request);
|
||||
|
||||
$this->assertEquals(
|
||||
$number + 1,
|
||||
DB::table('contact_field_types')->where('account_id', $account->id)->count()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_only_populates_partially()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$numberOfDefault = DB::table('default_contact_field_types')
|
||||
->count();
|
||||
|
||||
DB::table('default_contact_field_types')
|
||||
->update(['migrated' => 0]);
|
||||
|
||||
$numberOfContactFieldTypesAssociatedWithAccount = DB::table('contact_field_types')
|
||||
->where('account_id', $account->id)
|
||||
->count();
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'migrate_existing_data' => true,
|
||||
];
|
||||
|
||||
app(PopulateContactFieldTypesTable::class)->execute($request);
|
||||
|
||||
$this->assertEquals(
|
||||
$numberOfContactFieldTypesAssociatedWithAccount + $numberOfDefault,
|
||||
DB::table('contact_field_types')->where('account_id', $account->id)->get()->count()
|
||||
);
|
||||
}
|
||||
}
|
||||
122
tests/Unit/Services/Auth/PopulateLifeEventsTableTest.php
Normal file
122
tests/Unit/Services/Auth/PopulateLifeEventsTableTest.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Auth;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use App\Models\Account\Account;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Services\Auth\Population\PopulateLifeEventsTable;
|
||||
|
||||
class PopulateLifeEventsTableTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$request = [
|
||||
'account_id' => 1,
|
||||
];
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
app(PopulateLifeEventsTable::class)->execute($request);
|
||||
|
||||
$request = [
|
||||
'migrate_existing_data' => false,
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
|
||||
app(PopulateLifeEventsTable::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_populate_life_event_tables()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
DB::table('default_life_event_categories')
|
||||
->where('translation_key', 'work_education')
|
||||
->update(['migrated' => 0]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'migrate_existing_data' => 1,
|
||||
];
|
||||
|
||||
app(PopulateLifeEventsTable::class)->execute($request);
|
||||
|
||||
$this->assertEquals(
|
||||
5,
|
||||
DB::table('life_event_categories')->where('account_id', $account->id)->get()->count()
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
43,
|
||||
DB::table('life_event_types')->where('account_id', $account->id)->get()->count()
|
||||
);
|
||||
|
||||
// make sure tables have been set to migrated = 1
|
||||
$this->assertDatabaseMissing('default_life_event_categories', [
|
||||
'migrated' => 0,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseMissing('default_life_event_types', [
|
||||
'migrated' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_refuses_to_populate_table_if_account_doesnt_have_locale()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'migrate_existing_data' => 0,
|
||||
];
|
||||
|
||||
$this->assertFalse(app(PopulateLifeEventsTable::class)->execute($request));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_only_populates_life_event_tables_partially()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
DB::table('default_life_event_categories')
|
||||
->update(['migrated' => 0]);
|
||||
|
||||
DB::table('default_life_event_categories')
|
||||
->where('translation_key', 'work_education')
|
||||
->update(['migrated' => 1]);
|
||||
|
||||
// we will only migrate the ones that haven't been populated yet
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'migrate_existing_data' => 0,
|
||||
];
|
||||
|
||||
app(PopulateLifeEventsTable::class)->execute($request);
|
||||
|
||||
$this->assertEquals(
|
||||
4,
|
||||
DB::table('life_event_categories')->where('account_id', $account->id)->get()->count()
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
36,
|
||||
DB::table('life_event_types')->where('account_id', $account->id)->get()->count()
|
||||
);
|
||||
}
|
||||
}
|
||||
100
tests/Unit/Services/Auth/PopulateModulesTableTest.php
Normal file
100
tests/Unit/Services/Auth/PopulateModulesTableTest.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Auth;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use App\Models\Account\Account;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Services\Auth\Population\PopulateModulesTable;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class PopulateModulesTableTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
$request = [
|
||||
'account_id' => 1,
|
||||
];
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$populateModulesService = new PopulateModulesTable;
|
||||
$populateModulesService->execute($request);
|
||||
|
||||
$request = [
|
||||
'migrate_existing_data' => false,
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
|
||||
app(PopulateModulesTable::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_populate_modules_tables()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
DB::table('default_contact_modules')
|
||||
->where('key', 'work_education')
|
||||
->update(['migrated' => 0]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'migrate_existing_data' => 1,
|
||||
];
|
||||
|
||||
app(PopulateModulesTable::class)->execute($request);
|
||||
|
||||
// by defauult there is 18 columns in the default table.
|
||||
// therefore, we need 18 entries for the new account.
|
||||
$this->assertEquals(
|
||||
18,
|
||||
DB::table('modules')->where('account_id', $account->id)->get()->count()
|
||||
);
|
||||
|
||||
// make sure tables have been set to migrated = 1
|
||||
$this->assertDatabaseMissing('default_contact_modules', [
|
||||
'migrated' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_only_populates_module_tables_partially()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
DB::table('default_contact_modules')
|
||||
->update(['migrated' => 0]);
|
||||
|
||||
DB::table('default_contact_modules')
|
||||
->where('key', 'love_relationships')
|
||||
->update(['migrated' => 1]);
|
||||
|
||||
// we will only migrate the ones that haven't been populated yet
|
||||
$request = [
|
||||
'account_id' => $account->id,
|
||||
'migrate_existing_data' => 0,
|
||||
];
|
||||
|
||||
app(PopulateModulesTable::class)->execute($request);
|
||||
|
||||
// by defauult there is 18 columns in the default table.
|
||||
// therefore, we need 17 entries for the new account.
|
||||
$this->assertEquals(
|
||||
17,
|
||||
DB::table('modules')->where('account_id', $account->id)->get()->count()
|
||||
);
|
||||
}
|
||||
}
|
||||
121
tests/Unit/Services/BaseServiceTest.php
Normal file
121
tests/Unit/Services/BaseServiceTest.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Tests\TestCase;
|
||||
use App\Services\BaseService;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class BaseServiceTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_returns_an_empty_rule_array(): void
|
||||
{
|
||||
$stub = $this->getMockForAbstractClass(BaseService::class);
|
||||
|
||||
$this->assertIsArray(
|
||||
$stub->rules()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_validates_rules(): void
|
||||
{
|
||||
$rules = [
|
||||
'street' => 'nullable|string|max:255',
|
||||
];
|
||||
|
||||
$stub = $this->getMockForAbstractClass(BaseService::class);
|
||||
$stub->rules([$rules]);
|
||||
|
||||
$this->assertTrue(
|
||||
$stub->validate([
|
||||
'street' => 'la rue du bonheur',
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_returns_null_or_the_actual_value(): void
|
||||
{
|
||||
$stub = $this->getMockForAbstractClass(BaseService::class);
|
||||
$array = [
|
||||
'value' => 'this',
|
||||
];
|
||||
|
||||
$this->assertEquals(
|
||||
'this',
|
||||
$stub->nullOrValue($array, 'value')
|
||||
);
|
||||
|
||||
$array = [
|
||||
'otherValue' => '',
|
||||
];
|
||||
|
||||
$this->assertNull(
|
||||
$stub->nullOrValue($array, 'otherValue')
|
||||
);
|
||||
|
||||
$array = [];
|
||||
|
||||
$this->assertNull(
|
||||
$stub->nullOrValue($array, 'value')
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_returns_null_or_the_actual_date(): void
|
||||
{
|
||||
$stub = $this->getMockForAbstractClass(BaseService::class);
|
||||
$array = [
|
||||
'value' => '1990-01-01',
|
||||
];
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Carbon::class,
|
||||
$stub->nullOrDate($array, 'value')
|
||||
);
|
||||
|
||||
$array = [
|
||||
'otherValue' => '',
|
||||
];
|
||||
|
||||
$this->assertNull(
|
||||
$stub->nullOrDate($array, 'otherValue')
|
||||
);
|
||||
|
||||
$array = [];
|
||||
|
||||
$this->assertNull(
|
||||
$stub->nullOrDate($array, 'value')
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_returns_the_default_value_or_the_given_value(): void
|
||||
{
|
||||
$stub = $this->getMockForAbstractClass(BaseService::class);
|
||||
$array = [
|
||||
'value' => true,
|
||||
];
|
||||
|
||||
$this->assertTrue(
|
||||
$stub->valueOrFalse($array, 'value')
|
||||
);
|
||||
|
||||
$array = [
|
||||
'value' => false,
|
||||
];
|
||||
|
||||
$this->assertFalse(
|
||||
$stub->valueOrFalse($array, 'value')
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$stub->valueOrFalse([], 'value')
|
||||
);
|
||||
}
|
||||
}
|
||||
112
tests/Unit/Services/Contact/Address/CreateAddressTest.php
Normal file
112
tests/Unit/Services/Contact/Address/CreateAddressTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
78
tests/Unit/Services/Contact/Address/DestroyAddressTest.php
Normal file
78
tests/Unit/Services/Contact/Address/DestroyAddressTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
112
tests/Unit/Services/Contact/Address/UpdateAddressTest.php
Normal file
112
tests/Unit/Services/Contact/Address/UpdateAddressTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
56
tests/Unit/Services/Contact/Avatar/GetAdorableAvatarTest.php
Normal file
56
tests/Unit/Services/Contact/Avatar/GetAdorableAvatarTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
145
tests/Unit/Services/Contact/Avatar/GetGravatarTest.php
Normal file
145
tests/Unit/Services/Contact/Avatar/GetGravatarTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
192
tests/Unit/Services/Contact/Avatar/UpdateAvatarTest.php
Normal file
192
tests/Unit/Services/Contact/Avatar/UpdateAvatarTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
250
tests/Unit/Services/Contact/Call/CreateCallTest.php
Normal file
250
tests/Unit/Services/Contact/Call/CreateCallTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
148
tests/Unit/Services/Contact/Call/DestroyCallTest.php
Normal file
148
tests/Unit/Services/Contact/Call/DestroyCallTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
333
tests/Unit/Services/Contact/Call/UpdateCallTest.php
Normal file
333
tests/Unit/Services/Contact/Call/UpdateCallTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
232
tests/Unit/Services/Contact/Contact/CreateContactTest.php
Normal file
232
tests/Unit/Services/Contact/Contact/CreateContactTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
70
tests/Unit/Services/Contact/Contact/DeleteMeContactTest.php
Normal file
70
tests/Unit/Services/Contact/Contact/DeleteMeContactTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
76
tests/Unit/Services/Contact/Contact/DestroyContactTest.php
Normal file
76
tests/Unit/Services/Contact/Contact/DestroyContactTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
73
tests/Unit/Services/Contact/Contact/SetMeContactTest.php
Normal file
73
tests/Unit/Services/Contact/Contact/SetMeContactTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
162
tests/Unit/Services/Contact/Contact/UpdateContactTest.php
Normal file
162
tests/Unit/Services/Contact/Contact/UpdateContactTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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' => '',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
104
tests/Unit/Services/Contact/Conversation/UpdateMessageTest.php
Normal file
104
tests/Unit/Services/Contact/Conversation/UpdateMessageTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
87
tests/Unit/Services/Contact/Document/DestroyDocumentTest.php
Normal file
87
tests/Unit/Services/Contact/Document/DestroyDocumentTest.php
Normal 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;
|
||||
}
|
||||
}
|
||||
83
tests/Unit/Services/Contact/Document/UploadDocumentTest.php
Normal file
83
tests/Unit/Services/Contact/Document/UploadDocumentTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
72
tests/Unit/Services/Contact/Gift/CreateGiftTest.php
Normal file
72
tests/Unit/Services/Contact/Gift/CreateGiftTest.php
Normal 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',
|
||||
]);
|
||||
}
|
||||
}
|
||||
61
tests/Unit/Services/Contact/Gift/DestroyGiftTest.php
Normal file
61
tests/Unit/Services/Contact/Gift/DestroyGiftTest.php
Normal 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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
69
tests/Unit/Services/Contact/Gift/UpdateGiftTest.php
Normal file
69
tests/Unit/Services/Contact/Gift/UpdateGiftTest.php
Normal 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',
|
||||
]);
|
||||
}
|
||||
}
|
||||
228
tests/Unit/Services/Contact/Label/UpdateAddessLabelTest.php
Normal file
228
tests/Unit/Services/Contact/Label/UpdateAddessLabelTest.php
Normal 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'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -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'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
148
tests/Unit/Services/Contact/LifeEvent/CreateLifeEventTest.php
Normal file
148
tests/Unit/Services/Contact/LifeEvent/CreateLifeEventTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
228
tests/Unit/Services/Contact/Reminder/CreateReminderTest.php
Normal file
228
tests/Unit/Services/Contact/Reminder/CreateReminderTest.php
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
89
tests/Unit/Services/Contact/Reminder/DestroyReminderTest.php
Normal file
89
tests/Unit/Services/Contact/Reminder/DestroyReminderTest.php
Normal 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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
110
tests/Unit/Services/Contact/Reminder/UpdateReminderTest.php
Normal file
110
tests/Unit/Services/Contact/Reminder/UpdateReminderTest.php
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
154
tests/Unit/Services/Contact/Tag/AssociateTagTest.php
Normal file
154
tests/Unit/Services/Contact/Tag/AssociateTagTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
51
tests/Unit/Services/Contact/Tag/CreateTagTest.php
Normal file
51
tests/Unit/Services/Contact/Tag/CreateTagTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
83
tests/Unit/Services/Contact/Tag/DestroyTagTest.php
Normal file
83
tests/Unit/Services/Contact/Tag/DestroyTagTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
82
tests/Unit/Services/Contact/Tag/DetachTagTest.php
Normal file
82
tests/Unit/Services/Contact/Tag/DetachTagTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
69
tests/Unit/Services/Contact/Tag/UpdateTagTest.php
Normal file
69
tests/Unit/Services/Contact/Tag/UpdateTagTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
129
tests/Unit/Services/DavClient/AddAddressBookTest.php
Normal file
129
tests/Unit/Services/DavClient/AddAddressBookTest.php
Normal file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\DavClient;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use Mockery\MockInterface;
|
||||
use function Safe\json_encode;
|
||||
use App\Models\Account\AddressBook;
|
||||
use App\Models\Account\AddressBookSubscription;
|
||||
use App\Services\DavClient\Utils\AddressBookGetter;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Services\DavClient\CreateAddressBookSubscription;
|
||||
|
||||
class AddAddressBookTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_creates_an_addressbook()
|
||||
{
|
||||
$user = factory(User::class)->create([]);
|
||||
|
||||
$this->mock(AddressBookGetter::class, function (MockInterface $mock) {
|
||||
$mock->shouldReceive('execute')
|
||||
->once()
|
||||
->andReturn($this->mockReturn());
|
||||
});
|
||||
|
||||
$request = [
|
||||
'account_id' => $user->account_id,
|
||||
'user_id' => $user->id,
|
||||
'base_uri' => 'https://test',
|
||||
'username' => 'test',
|
||||
'password' => 'test',
|
||||
];
|
||||
|
||||
$addressBookSubscription = (new CreateAddressBookSubscription())->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('addressbooks', [
|
||||
'id' => $addressBookSubscription->address_book_id,
|
||||
'account_id' => $user->account_id,
|
||||
'user_id' => $user->id,
|
||||
'name' => 'contacts1',
|
||||
]);
|
||||
$this->assertDatabaseHas('addressbook_subscriptions', [
|
||||
'id' => $addressBookSubscription->id,
|
||||
'account_id' => $user->account_id,
|
||||
'user_id' => $user->id,
|
||||
'address_book_id' => $addressBookSubscription->address_book_id,
|
||||
'capabilities' => json_encode([
|
||||
'addressbookMultiget' => true,
|
||||
'addressbookQuery' => true,
|
||||
'syncCollection' => true,
|
||||
'addressData' => [
|
||||
'content-type' => 'text/vcard',
|
||||
'version' => '4.0',
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
AddressBookSubscription::class,
|
||||
$addressBookSubscription
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_creates_next_addressbook()
|
||||
{
|
||||
$user = factory(User::class)->create([]);
|
||||
AddressBook::factory()->create([
|
||||
'account_id' => $user->account_id,
|
||||
'user_id' => $user->id,
|
||||
'name' => 'contacts5',
|
||||
]);
|
||||
|
||||
$this->mock(AddressBookGetter::class, function (MockInterface $mock) {
|
||||
$mock->shouldReceive('execute')
|
||||
->once()
|
||||
->andReturn($this->mockReturn());
|
||||
});
|
||||
|
||||
$request = [
|
||||
'account_id' => $user->account_id,
|
||||
'user_id' => $user->id,
|
||||
'base_uri' => 'https://test',
|
||||
'username' => 'test',
|
||||
'password' => 'test',
|
||||
];
|
||||
|
||||
$addressBookSubscription = app(CreateAddressBookSubscription::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('addressbooks', [
|
||||
'id' => $addressBookSubscription->address_book_id,
|
||||
'account_id' => $user->account_id,
|
||||
'user_id' => $user->id,
|
||||
'name' => 'contacts6',
|
||||
]);
|
||||
$this->assertDatabaseHas('addressbook_subscriptions', [
|
||||
'id' => $addressBookSubscription->id,
|
||||
'account_id' => $user->account_id,
|
||||
'user_id' => $user->id,
|
||||
'address_book_id' => $addressBookSubscription->address_book_id,
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
AddressBookSubscription::class,
|
||||
$addressBookSubscription
|
||||
);
|
||||
}
|
||||
|
||||
private function mockReturn(): array
|
||||
{
|
||||
return [
|
||||
'uri' => 'https://test/dav',
|
||||
'capabilities' => [
|
||||
'addressbookMultiget' => true,
|
||||
'addressbookQuery' => true,
|
||||
'syncCollection' => true,
|
||||
'addressData' => [
|
||||
'content-type' => 'text/vcard',
|
||||
'version' => '4.0',
|
||||
],
|
||||
],
|
||||
'name' => 'Test',
|
||||
];
|
||||
}
|
||||
}
|
||||
62
tests/Unit/Services/DavClient/SynchronizeAddressBookTest.php
Normal file
62
tests/Unit/Services/DavClient/SynchronizeAddressBookTest.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\DavClient;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Mockery\MockInterface;
|
||||
use App\Models\Account\AddressBookSubscription;
|
||||
use App\Services\DavClient\SynchronizeAddressBook;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Services\DavClient\Utils\AddressBookSynchronizer;
|
||||
|
||||
class SynchronizeAddressBookTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_runs_sync()
|
||||
{
|
||||
$this->mock(AddressBookSynchronizer::class, function (MockInterface $mock) {
|
||||
$mock->shouldReceive('execute')
|
||||
->once()
|
||||
->withArgs(function ($sync, $force) {
|
||||
$this->assertFalse($force);
|
||||
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
$subscription = AddressBookSubscription::factory()->create();
|
||||
|
||||
$request = [
|
||||
'account_id' => $subscription->account_id,
|
||||
'addressbook_subscription_id' => $subscription->id,
|
||||
];
|
||||
|
||||
(new SynchronizeAddressBook())->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_runs_sync_force()
|
||||
{
|
||||
$this->mock(AddressBookSynchronizer::class, function (MockInterface $mock) {
|
||||
$mock->shouldReceive('execute')
|
||||
->once()
|
||||
->withArgs(function ($sync, $force) {
|
||||
$this->assertTrue($force);
|
||||
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
$subscription = AddressBookSubscription::factory()->create();
|
||||
|
||||
$request = [
|
||||
'account_id' => $subscription->account_id,
|
||||
'addressbook_subscription_id' => $subscription->id,
|
||||
'force' => true,
|
||||
];
|
||||
|
||||
(new SynchronizeAddressBook())->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\DavClient;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Mockery\MockInterface;
|
||||
use App\Models\User\SyncToken;
|
||||
use App\Models\Account\AddressBookSubscription;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Http\Controllers\DAV\Backend\CardDAV\CardDAVBackend;
|
||||
use App\Services\DavClient\UpdateSubscriptionLocalSyncToken;
|
||||
|
||||
class UpdateSubscriptionLocalSyncTokenTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_update_token()
|
||||
{
|
||||
$subscription = AddressBookSubscription::factory()->create([
|
||||
'name' => 'contacts1',
|
||||
]);
|
||||
$token = factory(SyncToken::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'user_id' => $subscription->user_id,
|
||||
'name' => 'contacts1',
|
||||
'timestamp' => now()->addDays(-1),
|
||||
]);
|
||||
|
||||
$this->mock(CardDAVBackend::class, function (MockInterface $mock) use ($token) {
|
||||
$mock->shouldReceive('init')->andReturn($mock);
|
||||
$mock->shouldReceive('getCurrentSyncToken')
|
||||
->withArgs(function ($name) {
|
||||
$this->assertEquals($name, 'contacts1');
|
||||
|
||||
return true;
|
||||
})
|
||||
->andReturn($token);
|
||||
});
|
||||
|
||||
(new UpdateSubscriptionLocalSyncToken())->execute([
|
||||
'account_id' => $subscription->account_id,
|
||||
'addressbook_subscription_id' => $subscription->id,
|
||||
]);
|
||||
|
||||
$subscription->refresh();
|
||||
|
||||
$this->assertEquals($token->id, $subscription->localSyncToken);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_wont_update_null_token()
|
||||
{
|
||||
$subscription = AddressBookSubscription::factory()->create([
|
||||
'name' => 'contacts1',
|
||||
]);
|
||||
|
||||
$this->mock(CardDAVBackend::class, function (MockInterface $mock) {
|
||||
$mock->shouldReceive('init')->andReturn($mock);
|
||||
$mock->shouldReceive('getCurrentSyncToken')
|
||||
->withArgs(function ($name) {
|
||||
$this->assertEquals($name, 'contacts1');
|
||||
|
||||
return true;
|
||||
})
|
||||
->andReturn(null);
|
||||
});
|
||||
|
||||
(new UpdateSubscriptionLocalSyncToken())->execute([
|
||||
'account_id' => $subscription->account_id,
|
||||
'addressbook_subscription_id' => $subscription->id,
|
||||
]);
|
||||
|
||||
$subscription->refresh();
|
||||
|
||||
$this->assertNull($subscription->localSyncToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\DavClient\Utils;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Mockery\MockInterface;
|
||||
use App\Jobs\Dav\PushVCard;
|
||||
use Tests\Api\DAV\CardEtag;
|
||||
use Tests\Helpers\DavTester;
|
||||
use App\Models\User\SyncToken;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Account\AddressBookSubscription;
|
||||
use App\Services\DavClient\Utils\Model\SyncDto;
|
||||
use App\Services\DavClient\Utils\Model\ContactDto;
|
||||
use App\Services\DavClient\Utils\Model\ContactPushDto;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Http\Controllers\DAV\Backend\CardDAV\CardDAVBackend;
|
||||
use App\Services\DavClient\Utils\AddressBookContactsPushMissed;
|
||||
|
||||
class AddressBookContactsPushMissedTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
use CardEtag;
|
||||
|
||||
/** @test */
|
||||
public function it_push_contacts_missed()
|
||||
{
|
||||
$subscription = AddressBookSubscription::factory()->create();
|
||||
$token = factory(SyncToken::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'user_id' => $subscription->user_id,
|
||||
'name' => 'contacts1',
|
||||
'timestamp' => now()->addDays(-1),
|
||||
]);
|
||||
$subscription->localSyncToken = $token->id;
|
||||
$subscription->save();
|
||||
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'first_name' => 'Test',
|
||||
'uuid' => 'affacde9-b2fe-4371-9acb-6612aaee6971',
|
||||
]);
|
||||
$card = $this->getCard($contact);
|
||||
$etag = $this->getEtag($contact, true);
|
||||
|
||||
$this->mock(CardDAVBackend::class, function (MockInterface $mock) use ($card, $etag, $contact) {
|
||||
$mock->shouldReceive('init')->andReturn($mock);
|
||||
$mock->shouldReceive('getUuid')
|
||||
->once()
|
||||
->withArgs(function ($uri) {
|
||||
$this->assertEquals('uuid6', $uri);
|
||||
|
||||
return true;
|
||||
})
|
||||
->andReturn('uuid3');
|
||||
$mock->shouldReceive('prepareCard')
|
||||
->once()
|
||||
->withArgs(function ($c) use ($contact) {
|
||||
$this->assertEquals($contact, $c);
|
||||
|
||||
return true;
|
||||
})
|
||||
->andReturn([
|
||||
'account_id' => $contact->account_id,
|
||||
'contact_id' => $contact->id,
|
||||
'carddata' => $card,
|
||||
'uri' => 'uuid3',
|
||||
'etag' => $etag,
|
||||
]);
|
||||
});
|
||||
|
||||
$client = (new DavTester())->fake()->client();
|
||||
|
||||
$batchs = (new AddressBookContactsPushMissed())
|
||||
->execute(new SyncDto($subscription, $client), [], collect([
|
||||
'uuid6' => new ContactDto('uuid6', $etag),
|
||||
]), collect([$contact]));
|
||||
|
||||
$this->assertCount(1, $batchs);
|
||||
$batch = $batchs->first();
|
||||
$this->assertInstanceOf(PushVCard::class, $batch);
|
||||
$dto = $this->getPrivateValue($batch, 'contact');
|
||||
$this->assertInstanceOf(ContactPushDto::class, $dto);
|
||||
$this->assertEquals('uuid3', $dto->uri);
|
||||
$this->assertEquals(2, $dto->mode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\DavClient\Utils;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Mockery\MockInterface;
|
||||
use App\Jobs\Dav\PushVCard;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\Api\DAV\CardEtag;
|
||||
use Tests\Helpers\DavTester;
|
||||
use App\Jobs\Dav\DeleteVCard;
|
||||
use App\Models\User\SyncToken;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Account\AddressBookSubscription;
|
||||
use App\Services\DavClient\Utils\Model\SyncDto;
|
||||
use App\Services\DavClient\Utils\Model\ContactDto;
|
||||
use App\Services\DavClient\Utils\Model\ContactPushDto;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Services\DavClient\Utils\AddressBookContactsPush;
|
||||
use App\Http\Controllers\DAV\Backend\CardDAV\CardDAVBackend;
|
||||
|
||||
class AddressBookContactsPushTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
use CardEtag;
|
||||
|
||||
/** @test */
|
||||
public function it_push_contacts_added()
|
||||
{
|
||||
$subscription = AddressBookSubscription::factory()->create();
|
||||
$token = factory(SyncToken::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'user_id' => $subscription->user_id,
|
||||
'name' => 'contacts1',
|
||||
'timestamp' => now()->addDays(-1),
|
||||
]);
|
||||
$subscription->localSyncToken = $token->id;
|
||||
$subscription->save();
|
||||
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'first_name' => 'Test',
|
||||
'uuid' => 'affacde9-b2fe-4371-9acb-6612aaee6971',
|
||||
]);
|
||||
$card = $this->getCard($contact);
|
||||
$etag = $this->getEtag($contact, true);
|
||||
|
||||
$this->mock(CardDAVBackend::class, function (MockInterface $mock) use ($contact, $card, $etag) {
|
||||
$mock->shouldReceive('init')->andReturn($mock);
|
||||
$mock->shouldReceive('getCard')
|
||||
->withArgs(function ($name, $uri) {
|
||||
$this->assertEquals($uri, 'uricontact2');
|
||||
|
||||
return true;
|
||||
})
|
||||
->andReturn([
|
||||
'contact_id' => $contact->id,
|
||||
'carddata' => $card,
|
||||
'etag' => $etag,
|
||||
'distant_etag' => $etag,
|
||||
]);
|
||||
$mock->shouldReceive('getUuid')
|
||||
->withArgs(function ($uri) {
|
||||
$this->assertEquals($uri, 'https://test/dav/uricontact1');
|
||||
|
||||
return true;
|
||||
})
|
||||
->andReturn('uricontact1');
|
||||
});
|
||||
|
||||
$client = (new DavTester())->fake()->client();
|
||||
|
||||
$batchs = (new AddressBookContactsPush())
|
||||
->execute(new SyncDto($subscription, $client), collect([
|
||||
'https://test/dav/uricontact1' => new ContactDto('https://test/dav/uricontact1', $etag),
|
||||
]), [
|
||||
'added' => ['uricontact2'],
|
||||
]);
|
||||
|
||||
$this->assertCount(1, $batchs);
|
||||
$batch = $batchs->first();
|
||||
$this->assertInstanceOf(PushVCard::class, $batch);
|
||||
$dto = $this->getPrivateValue($batch, 'contact');
|
||||
$this->assertInstanceOf(ContactPushDto::class, $dto);
|
||||
$this->assertEquals('uricontact2', $dto->uri);
|
||||
$this->assertEquals(ContactPushDto::MODE_MATCH_NONE, $dto->mode);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_push_contacts_modified()
|
||||
{
|
||||
$subscription = AddressBookSubscription::factory()->create();
|
||||
$token = factory(SyncToken::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'user_id' => $subscription->user_id,
|
||||
'name' => 'contacts1',
|
||||
'timestamp' => now()->addDays(-1),
|
||||
]);
|
||||
$subscription->localSyncToken = $token->id;
|
||||
$subscription->save();
|
||||
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'first_name' => 'Test',
|
||||
'uuid' => 'affacde9-b2fe-4371-9acb-6612aaee6971',
|
||||
]);
|
||||
$card = $this->getCard($contact);
|
||||
$etag = $this->getEtag($contact, true);
|
||||
|
||||
$this->mock(CardDAVBackend::class, function (MockInterface $mock) use ($contact, $card, $etag) {
|
||||
$mock->shouldReceive('init')->andReturn($mock);
|
||||
$mock->shouldReceive('getUuid')
|
||||
->withArgs(function ($uri) {
|
||||
$this->assertStringContainsString('uricontact', $uri);
|
||||
|
||||
return true;
|
||||
})
|
||||
->andReturnUsing(function ($uri) {
|
||||
return Str::contains($uri, 'uricontact1') ? 'uricontact1' : 'uricontact2';
|
||||
});
|
||||
$mock->shouldReceive('getCard')
|
||||
->withArgs(function ($name, $uri) {
|
||||
$this->assertEquals($uri, 'uricontact2');
|
||||
|
||||
return true;
|
||||
})
|
||||
->andReturn([
|
||||
'contact_id' => $contact->id,
|
||||
'carddata' => $card,
|
||||
'etag' => $etag,
|
||||
'distant_etag' => $etag,
|
||||
]);
|
||||
});
|
||||
|
||||
$client = (new DavTester())->fake()->client();
|
||||
|
||||
$batchs = (new AddressBookContactsPush())
|
||||
->execute(new SyncDto($subscription, $client), collect([
|
||||
'https://test/dav/uricontact1' => new ContactDto('https://test/dav/uricontact1', $etag),
|
||||
]), [
|
||||
'modified' => ['uricontact2'],
|
||||
]);
|
||||
|
||||
$this->assertCount(1, $batchs);
|
||||
$batch = $batchs->first();
|
||||
$this->assertInstanceOf(PushVCard::class, $batch);
|
||||
$dto = $this->getPrivateValue($batch, 'contact');
|
||||
$this->assertInstanceOf(ContactPushDto::class, $dto);
|
||||
$this->assertEquals('uricontact2', $dto->uri);
|
||||
$this->assertEquals(1, $dto->mode);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_delete_contacts_removed()
|
||||
{
|
||||
$subscription = AddressBookSubscription::factory()->create();
|
||||
$client = (new DavTester())->fake()->client();
|
||||
|
||||
$batchs = (new AddressBookContactsPush())
|
||||
->execute(new SyncDto($subscription, $client), collect(), [
|
||||
'deleted' => ['uricontact2'],
|
||||
]);
|
||||
|
||||
$this->assertCount(1, $batchs);
|
||||
$batch = $batchs->first();
|
||||
$this->assertInstanceOf(DeleteVCard::class, $batch);
|
||||
$uri = $this->getPrivateValue($batch, 'uri');
|
||||
$this->assertEquals('uricontact2', $uri);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\DavClient\Utils;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Mockery\MockInterface;
|
||||
use Tests\Api\DAV\CardEtag;
|
||||
use Tests\Helpers\DavTester;
|
||||
use App\Models\User\SyncToken;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Jobs\Dav\GetMultipleVCard;
|
||||
use App\Models\Account\AddressBookSubscription;
|
||||
use App\Services\DavClient\Utils\Model\SyncDto;
|
||||
use App\Services\DavClient\Utils\Model\ContactDto;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Http\Controllers\DAV\Backend\CardDAV\CardDAVBackend;
|
||||
use App\Services\DavClient\Utils\AddressBookContactsUpdaterMissed;
|
||||
|
||||
class AddressBookContactsUpdaterMissedTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
use CardEtag;
|
||||
|
||||
/** @test */
|
||||
public function it_sync_changes_missed()
|
||||
{
|
||||
$subscription = AddressBookSubscription::factory()->create();
|
||||
$token = factory(SyncToken::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'user_id' => $subscription->user_id,
|
||||
'name' => 'contacts1',
|
||||
'timestamp' => now()->addDays(-1),
|
||||
]);
|
||||
$subscription->localSyncToken = $token->id;
|
||||
$subscription->save();
|
||||
|
||||
$contact = new Contact();
|
||||
$contact->forceFill([
|
||||
'first_name' => 'Test',
|
||||
'uuid' => 'affacde9-b2fe-4371-9acb-6612aaee6971',
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
$card = $this->getCard($contact);
|
||||
$etag = $this->getEtag($contact, true);
|
||||
|
||||
$this->mock(CardDAVBackend::class, function (MockInterface $mock) use ($card, $etag) {
|
||||
$mock->shouldReceive('init')->andReturn($mock);
|
||||
$mock->shouldReceive('getUuid')
|
||||
->withArgs(function ($uri) {
|
||||
$this->assertEquals($uri, 'https://test/dav/uuid2');
|
||||
|
||||
return true;
|
||||
})
|
||||
->andReturn('uuid2');
|
||||
$mock->shouldReceive('updateCard')
|
||||
->withArgs(function ($addressBookId, $cardUri, $cardData) use ($card) {
|
||||
$this->assertEquals($card, $cardData);
|
||||
|
||||
return true;
|
||||
})
|
||||
->andReturn($etag);
|
||||
});
|
||||
|
||||
$client = (new DavTester())->fake()->client();
|
||||
|
||||
$batchs = (new AddressBookContactsUpdaterMissed())
|
||||
->execute(new SyncDto($subscription, $client), collect([
|
||||
[
|
||||
'uuid' => 'uuid1',
|
||||
],
|
||||
]), collect([
|
||||
'https://test/dav/uuid2' => new ContactDto('https://test/dav/uuid2', $etag),
|
||||
]));
|
||||
|
||||
$this->assertCount(2, $batchs);
|
||||
$batch = $batchs->first();
|
||||
$this->assertInstanceOf(GetMultipleVCard::class, $batch);
|
||||
$hrefs = $this->getPrivateValue($batch, 'hrefs');
|
||||
$this->assertEquals(['https://test/dav/uuid2'], $hrefs);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\DavClient\Utils;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Jobs\Dav\GetVCard;
|
||||
use Mockery\MockInterface;
|
||||
use Tests\Api\DAV\CardEtag;
|
||||
use Tests\Helpers\DavTester;
|
||||
use App\Jobs\Dav\DeleteVCard;
|
||||
use App\Models\User\SyncToken;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Jobs\Dav\GetMultipleVCard;
|
||||
use App\Jobs\Dav\DeleteMultipleVCard;
|
||||
use App\Models\Account\AddressBookSubscription;
|
||||
use App\Services\DavClient\Utils\Model\SyncDto;
|
||||
use App\Services\DavClient\Utils\Model\ContactDto;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Services\DavClient\Utils\Model\ContactDeleteDto;
|
||||
use App\Http\Controllers\DAV\Backend\CardDAV\CardDAVBackend;
|
||||
use App\Services\DavClient\Utils\AddressBookContactsUpdater;
|
||||
|
||||
class AddressBookContactsUpdaterTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
use CardEtag;
|
||||
|
||||
/** @test */
|
||||
public function it_sync_changes_multiget()
|
||||
{
|
||||
$subscription = AddressBookSubscription::factory()->create();
|
||||
$token = factory(SyncToken::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'user_id' => $subscription->user_id,
|
||||
'name' => 'contacts1',
|
||||
'timestamp' => now()->addDays(-1),
|
||||
]);
|
||||
$subscription->localSyncToken = $token->id;
|
||||
$subscription->save();
|
||||
|
||||
$contact = new Contact();
|
||||
$contact->forceFill([
|
||||
'first_name' => 'Test',
|
||||
'uuid' => 'affacde9-b2fe-4371-9acb-6612aaee6971',
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
$card = $this->getCard($contact);
|
||||
$etag = $this->getEtag($contact, true);
|
||||
|
||||
$this->mock(CardDAVBackend::class, function (MockInterface $mock) use ($card, $etag) {
|
||||
$mock->shouldReceive('updateCard')
|
||||
->withArgs(function ($addressBookId, $cardUri, $cardData) use ($card) {
|
||||
$this->assertEquals($card, $cardData);
|
||||
|
||||
return true;
|
||||
})
|
||||
->andReturn($etag);
|
||||
});
|
||||
|
||||
$client = (new DavTester())->fake()->client();
|
||||
|
||||
$batchs = (new AddressBookContactsUpdater())
|
||||
->execute(new SyncDto($subscription, $client), collect([
|
||||
'https://test/dav/uuid2' => new ContactDto('https://test/dav/uuid2', $etag),
|
||||
]));
|
||||
|
||||
$this->assertCount(2, $batchs);
|
||||
$batch = $batchs->first();
|
||||
$this->assertInstanceOf(GetMultipleVCard::class, $batch);
|
||||
$hrefs = $this->getPrivateValue($batch, 'hrefs');
|
||||
$this->assertEquals(['https://test/dav/uuid2'], $hrefs);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_sync_deleted_multiget()
|
||||
{
|
||||
$subscription = AddressBookSubscription::factory()->create();
|
||||
$token = factory(SyncToken::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'user_id' => $subscription->user_id,
|
||||
'name' => 'contacts1',
|
||||
'timestamp' => now()->addDays(-1),
|
||||
]);
|
||||
$subscription->localSyncToken = $token->id;
|
||||
$subscription->save();
|
||||
|
||||
$client = (new DavTester())->fake()->client();
|
||||
|
||||
$batchs = (new AddressBookContactsUpdater())
|
||||
->execute(new SyncDto($subscription, $client), collect([
|
||||
'https://test/dav/uuid2' => new ContactDeleteDto('https://test/dav/uuid2'),
|
||||
]));
|
||||
|
||||
$this->assertCount(2, $batchs);
|
||||
$batch = $batchs->first();
|
||||
$this->assertInstanceOf(GetMultipleVCard::class, $batch);
|
||||
$hrefs = $this->getPrivateValue($batch, 'hrefs');
|
||||
$this->assertEquals([], $hrefs);
|
||||
|
||||
$batch = $batchs[1];
|
||||
$this->assertInstanceOf(DeleteMultipleVCard::class, $batch);
|
||||
$hrefs = $this->getPrivateValue($batch, 'hrefs');
|
||||
$this->assertEquals(['https://test/dav/uuid2'], $hrefs);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_sync_changes_simple()
|
||||
{
|
||||
$subscription = AddressBookSubscription::factory()->create([
|
||||
'capabilities' => [
|
||||
'addressbookMultiget' => false,
|
||||
'addressbookQuery' => true,
|
||||
'syncCollection' => true,
|
||||
'addressData' => [
|
||||
'content-type' => 'text/vcard',
|
||||
'version' => '4.0',
|
||||
],
|
||||
],
|
||||
]);
|
||||
$token = factory(SyncToken::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'user_id' => $subscription->user_id,
|
||||
'name' => 'contacts1',
|
||||
'timestamp' => now()->addDays(-1),
|
||||
]);
|
||||
$subscription->localSyncToken = $token->id;
|
||||
$subscription->save();
|
||||
|
||||
$contact = new Contact();
|
||||
$contact->forceFill([
|
||||
'first_name' => 'Test',
|
||||
'uuid' => 'affacde9-b2fe-4371-9acb-6612aaee6971',
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
$card = $this->getCard($contact);
|
||||
$etag = $this->getEtag($contact, true);
|
||||
|
||||
$this->mock(CardDAVBackend::class, function (MockInterface $mock) use ($card, $etag) {
|
||||
$mock->shouldReceive('updateCard')
|
||||
->withArgs(function ($addressBookId, $cardUri, $cardData) use ($card) {
|
||||
$this->assertTrue(is_resource($cardData));
|
||||
|
||||
$data = '';
|
||||
while (! feof($cardData)) {
|
||||
$data .= fgets($cardData);
|
||||
}
|
||||
|
||||
fclose($cardData);
|
||||
|
||||
$this->assertEquals($card, $data);
|
||||
|
||||
return true;
|
||||
})
|
||||
->andReturn($etag);
|
||||
});
|
||||
|
||||
$client = (new DavTester())->fake()->client();
|
||||
|
||||
$batchs = (new AddressBookContactsUpdater())
|
||||
->execute(new SyncDto($subscription, $client), collect([
|
||||
'https://test/dav/uuid2' => new ContactDto('https://test/dav/uuid2', $etag),
|
||||
]));
|
||||
|
||||
$this->assertCount(1, $batchs);
|
||||
$batch = $batchs->first();
|
||||
$this->assertInstanceOf(GetVCard::class, $batch);
|
||||
$dto = $this->getPrivateValue($batch, 'contact');
|
||||
$this->assertInstanceOf(ContactDto::class, $dto);
|
||||
$this->assertEquals('https://test/dav/uuid2', $dto->uri);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_sync_deleted_simple()
|
||||
{
|
||||
$subscription = AddressBookSubscription::factory()->create([
|
||||
'capabilities' => [
|
||||
'addressbookMultiget' => false,
|
||||
'addressbookQuery' => true,
|
||||
'syncCollection' => true,
|
||||
'addressData' => [
|
||||
'content-type' => 'text/vcard',
|
||||
'version' => '4.0',
|
||||
],
|
||||
],
|
||||
]);
|
||||
$token = factory(SyncToken::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'user_id' => $subscription->user_id,
|
||||
'name' => 'contacts1',
|
||||
'timestamp' => now()->addDays(-1),
|
||||
]);
|
||||
$subscription->localSyncToken = $token->id;
|
||||
$subscription->save();
|
||||
|
||||
$client = (new DavTester())->fake()->client();
|
||||
|
||||
$batchs = (new AddressBookContactsUpdater())
|
||||
->execute(new SyncDto($subscription, $client), collect([
|
||||
'https://test/dav/uuid2' => new ContactDeleteDto('https://test/dav/uuid2'),
|
||||
]));
|
||||
|
||||
$this->assertCount(1, $batchs);
|
||||
$batch = $batchs->first();
|
||||
$this->assertInstanceOf(DeleteVCard::class, $batch);
|
||||
$uri = $this->getPrivateValue($batch, 'uri');
|
||||
$this->assertEquals('https://test/dav/uuid2', $uri);
|
||||
}
|
||||
}
|
||||
110
tests/Unit/Services/DavClient/Utils/AddressBookGetterTest.php
Normal file
110
tests/Unit/Services/DavClient/Utils/AddressBookGetterTest.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\DavClient\Utils;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Tests\Helpers\DavTester;
|
||||
use App\Services\DavClient\Utils\AddressBookGetter;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Services\DavClient\Utils\Dav\DavClientException;
|
||||
use App\Services\DavClient\Utils\Dav\DavServerNotCompliantException;
|
||||
|
||||
class AddressBookGetterTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_get_address_book_data()
|
||||
{
|
||||
$tester = (new DavTester())
|
||||
->addressBookBaseUri()
|
||||
->capabilities()
|
||||
->displayName()
|
||||
->fake();
|
||||
$client = $tester->client();
|
||||
$result = (new AddressBookGetter())
|
||||
->execute($client);
|
||||
|
||||
$tester->assert();
|
||||
$this->assertEquals([
|
||||
'uri' => 'https://test/dav/addressbooks/user@test.com/contacts/',
|
||||
'capabilities' => [
|
||||
'addressbookMultiget' => true,
|
||||
'addressbookQuery' => true,
|
||||
'syncCollection' => true,
|
||||
'addressData' => [
|
||||
'content-type' => 'text/vcard',
|
||||
'version' => '4.0',
|
||||
],
|
||||
],
|
||||
'name' => 'Test',
|
||||
], $result);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_on_server_not_compliant()
|
||||
{
|
||||
$tester = (new DavTester())
|
||||
->userPrincipalEmpty()
|
||||
->serviceUrl()
|
||||
->optionsFail()
|
||||
->fake();
|
||||
$client = $tester->client();
|
||||
|
||||
$this->expectException(DavServerNotCompliantException::class);
|
||||
(new AddressBookGetter())
|
||||
->execute($client);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_no_userprincipal()
|
||||
{
|
||||
$tester = (new DavTester())
|
||||
->userPrincipalEmpty()
|
||||
->serviceUrl()
|
||||
->optionsOk()
|
||||
->userPrincipalEmpty()
|
||||
->fake();
|
||||
$client = $tester->client();
|
||||
|
||||
$this->expectException(DavServerNotCompliantException::class);
|
||||
(new AddressBookGetter())
|
||||
->execute($client);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_no_addressbook()
|
||||
{
|
||||
$tester = (new DavTester())
|
||||
->userPrincipalEmpty()
|
||||
->serviceUrl()
|
||||
->optionsOk()
|
||||
->userPrincipal()
|
||||
->addressbookEmpty()
|
||||
->fake();
|
||||
$client = $tester->client();
|
||||
|
||||
$this->expectException(DavServerNotCompliantException::class);
|
||||
(new AddressBookGetter())
|
||||
->execute($client);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_no_addressbook_url()
|
||||
{
|
||||
$tester = (new DavTester())
|
||||
->userPrincipalEmpty()
|
||||
->serviceUrl()
|
||||
->optionsOk()
|
||||
->userPrincipal()
|
||||
->addressbookHome()
|
||||
->resourceTypeHomeOnly()
|
||||
->optionsOk()
|
||||
->fake();
|
||||
$client = $tester->client();
|
||||
|
||||
$this->expectException(DavClientException::class);
|
||||
(new AddressBookGetter())
|
||||
->execute($client);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,370 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\DavClient\Utils;
|
||||
|
||||
use Tests\TestCase;
|
||||
use Mockery\MockInterface;
|
||||
use Tests\Api\DAV\CardEtag;
|
||||
use Tests\Helpers\DavTester;
|
||||
use App\Models\User\SyncToken;
|
||||
use App\Models\Contact\Contact;
|
||||
use Illuminate\Bus\PendingBatch;
|
||||
use App\Jobs\Dav\GetMultipleVCard;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Jobs\Dav\DeleteMultipleVCard;
|
||||
use App\Models\Account\AddressBookSubscription;
|
||||
use App\Services\DavClient\Utils\Model\SyncDto;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Services\DavClient\Utils\AddressBookSynchronizer;
|
||||
use App\Services\DavClient\Utils\AddressBookContactsUpdater;
|
||||
use App\Services\DavClient\Utils\AddressBookContactsPushMissed;
|
||||
use App\Services\DavClient\Utils\AddressBookContactsUpdaterMissed;
|
||||
|
||||
class AddressBookSynchronizerTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
use CardEtag;
|
||||
|
||||
/** @test */
|
||||
public function it_sync_empty_changes()
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$this->mock(AddressBookContactsUpdater::class, function (MockInterface $mock) {
|
||||
$mock->shouldReceive('execute')
|
||||
->once()
|
||||
->andReturn(collect());
|
||||
});
|
||||
|
||||
$subscription = $this->getSubscription();
|
||||
|
||||
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'))
|
||||
->getSynctoken($subscription->syncToken)
|
||||
->fake();
|
||||
$client = $tester->client();
|
||||
|
||||
(new AddressBookSynchronizer())
|
||||
->execute(new SyncDto($subscription, $client));
|
||||
|
||||
$tester->assert();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_sync_no_changes()
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$this->mock(AddressBookContactsUpdater::class, function (MockInterface $mock) {
|
||||
$mock->shouldReceive('execute')
|
||||
->once()
|
||||
->andReturn(collect());
|
||||
});
|
||||
|
||||
$subscription = $this->getSubscription();
|
||||
|
||||
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'));
|
||||
$tester->getSynctoken('"test21"')
|
||||
->getSyncCollection('test20')
|
||||
->fake();
|
||||
|
||||
$client = $tester->client();
|
||||
|
||||
(new AddressBookSynchronizer())
|
||||
->execute(new SyncDto($subscription, $client));
|
||||
|
||||
$tester->assert();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_sync_changes_added_local_contact()
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$subscription = $this->getSubscription();
|
||||
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'address_book_id' => $subscription->address_book_id,
|
||||
'uuid' => 'd403af1c-8492-4e9b-9833-cf18c795dfa9',
|
||||
]);
|
||||
|
||||
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'));
|
||||
$tester->getSynctoken('"token"')
|
||||
->getSyncCollection('token', '"test2"')
|
||||
->fake();
|
||||
|
||||
$client = $tester->client();
|
||||
|
||||
$sync = new SyncDto($subscription, $client);
|
||||
$this->mock(AddressBookContactsUpdater::class, function (MockInterface $mock) use ($sync) {
|
||||
$mock->shouldReceive('execute')
|
||||
->once()
|
||||
->withArgs(function ($localSync, $contacts) use ($sync) {
|
||||
$this->assertEquals($sync, $localSync);
|
||||
$this->assertEquals('https://test/dav/addressbooks/user@test.com/contacts/uuid', $contacts->first()->uri);
|
||||
$this->assertEquals('"test2"', $contacts->first()->etag);
|
||||
|
||||
return true;
|
||||
})
|
||||
->andReturn(collect());
|
||||
});
|
||||
|
||||
(new AddressBookSynchronizer())
|
||||
->execute($sync);
|
||||
|
||||
$tester->assert();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_sync_changes_added_local_contact_batched()
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$subscription = $this->getSubscription();
|
||||
|
||||
factory(Contact::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'address_book_id' => $subscription->address_book_id,
|
||||
'uuid' => 'd403af1c-8492-4e9b-9833-cf18c795dfa9',
|
||||
]);
|
||||
|
||||
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'));
|
||||
$tester->getSynctoken('"token"')
|
||||
->getSyncCollection('token', '"test2"')
|
||||
->fake();
|
||||
|
||||
$client = $tester->client();
|
||||
|
||||
$sync = new SyncDto($subscription, $client);
|
||||
|
||||
(new AddressBookSynchronizer())
|
||||
->execute($sync);
|
||||
|
||||
$tester->assert();
|
||||
|
||||
Bus::assertBatched(function (PendingBatch $batch) {
|
||||
$this->assertCount(2, $batch->jobs);
|
||||
$job = $batch->jobs[0];
|
||||
$this->assertInstanceOf(GetMultipleVCard::class, $job);
|
||||
$this->assertEquals(['https://test/dav/addressbooks/user@test.com/contacts/uuid'], $this->getPrivateValue($job, 'hrefs'));
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_sync_changes_deleted_contact_batched()
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$subscription = $this->getSubscription();
|
||||
|
||||
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'));
|
||||
$tester->getSynctoken('"token"')
|
||||
->addResponse('https://test/dav/addressbooks/user@test.com/contacts/', Http::response(DavTester::multistatusHeader().
|
||||
'<d:response>'.
|
||||
'<d:status>HTTP/1.1 404 Not Found</d:status>'.
|
||||
'<d:href>https://test/dav/addressbooks/user@test.com/contacts/uuid</d:href>'.
|
||||
'<d:propstat>'.
|
||||
'<d:prop/>'.
|
||||
'<d:status>HTTP/1.1 418 I\'m a teapot</d:status>'.
|
||||
'</d:propstat>'.
|
||||
'</d:response>'.
|
||||
'<d:sync-token>token</d:sync-token>'.
|
||||
'</d:multistatus>'), null, 'REPORT')
|
||||
->fake();
|
||||
|
||||
$client = $tester->client();
|
||||
|
||||
$sync = new SyncDto($subscription, $client);
|
||||
|
||||
(new AddressBookSynchronizer())
|
||||
->execute($sync);
|
||||
|
||||
$tester->assert();
|
||||
|
||||
Bus::assertBatched(function (PendingBatch $batch) {
|
||||
$this->assertCount(2, $batch->jobs);
|
||||
$job = $batch->jobs[1];
|
||||
$this->assertInstanceOf(DeleteMultipleVCard::class, $job);
|
||||
$this->assertEquals(['https://test/dav/addressbooks/user@test.com/contacts/uuid'], $this->getPrivateValue($job, 'hrefs'));
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_forcesync_changes_added_local_contact()
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$subscription = $this->getSubscription();
|
||||
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'address_book_id' => $subscription->address_book_id,
|
||||
'uuid' => 'd403af1c-8492-4e9b-9833-cf18c795dfa9',
|
||||
]);
|
||||
$etag = $this->getEtag($contact, true);
|
||||
|
||||
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'))
|
||||
->fake();
|
||||
$tester->addResponse('https://test/dav/addressbooks/user@test.com/contacts/', Http::response(DavTester::multistatusHeader().
|
||||
'<d:response>'.
|
||||
'<d:href>https://test/dav/uuid1</d:href>'.
|
||||
'<d:propstat>'.
|
||||
'<d:prop>'.
|
||||
"<d:getetag>$etag</d:getetag>".
|
||||
'</d:prop>'.
|
||||
'<d:status>HTTP/1.1 200 OK</d:status>'.
|
||||
'</d:propstat>'.
|
||||
'</d:response>'.
|
||||
'</d:multistatus>'), '<?xml version="1.0" encoding="UTF-8"?>'."\n".
|
||||
'<card:addressbook-query xmlns:card="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">'.
|
||||
'<d:prop>'.
|
||||
'<d:getetag/>'.
|
||||
'</d:prop>'.
|
||||
"</card:addressbook-query>\n", 'REPORT');
|
||||
|
||||
$client = $tester->client();
|
||||
|
||||
$sync = new SyncDto($subscription, $client);
|
||||
$this->mock(AddressBookContactsUpdaterMissed::class, function (MockInterface $mock) use ($sync, $contact, $etag) {
|
||||
$mock->shouldReceive('execute')
|
||||
->once()
|
||||
->withArgs(function ($localSync, $localContacts, $distContacts) use ($sync, $contact, $etag) {
|
||||
$this->assertEquals($sync, $localSync);
|
||||
$this->assertEquals($contact->id, $localContacts->first()->id);
|
||||
$this->assertEquals('https://test/dav/uuid1', $distContacts->first()->uri);
|
||||
$this->assertEquals($etag, $distContacts->first()->etag);
|
||||
|
||||
return true;
|
||||
})
|
||||
->andReturn(collect());
|
||||
});
|
||||
$this->mock(AddressBookContactsPushMissed::class, function (MockInterface $mock) {
|
||||
$mock->shouldReceive('execute')
|
||||
->once()
|
||||
->andReturn(collect());
|
||||
});
|
||||
|
||||
(new AddressBookSynchronizer())
|
||||
->execute($sync, true);
|
||||
|
||||
$tester->assert();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_forcesync_changes_added_local_contact_batched()
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$subscription = $this->getSubscription();
|
||||
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'address_book_id' => $subscription->address_book_id,
|
||||
'uuid' => 'd403af1c-8492-4e9b-9833-cf18c795dfa9',
|
||||
]);
|
||||
$etag = $this->getEtag($contact, true);
|
||||
|
||||
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'))
|
||||
->fake();
|
||||
$tester->addResponse('https://test/dav/addressbooks/user@test.com/contacts/', Http::response(DavTester::multistatusHeader().
|
||||
'<d:response>'.
|
||||
'<d:href>https://test/dav/uuid1</d:href>'.
|
||||
'<d:propstat>'.
|
||||
'<d:prop>'.
|
||||
"<d:getetag>$etag</d:getetag>".
|
||||
'</d:prop>'.
|
||||
'<d:status>HTTP/1.1 200 OK</d:status>'.
|
||||
'</d:propstat>'.
|
||||
'</d:response>'.
|
||||
'</d:multistatus>'), '<?xml version="1.0" encoding="UTF-8"?>'."\n".
|
||||
'<card:addressbook-query xmlns:card="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">'.
|
||||
'<d:prop>'.
|
||||
'<d:getetag/>'.
|
||||
'</d:prop>'.
|
||||
"</card:addressbook-query>\n", 'REPORT');
|
||||
|
||||
$client = $tester->client();
|
||||
|
||||
$sync = new SyncDto($subscription, $client);
|
||||
|
||||
(new AddressBookSynchronizer())
|
||||
->execute($sync, true);
|
||||
|
||||
$tester->assert();
|
||||
|
||||
Bus::assertBatched(function (PendingBatch $batch) {
|
||||
$this->assertCount(2, $batch->jobs);
|
||||
$job = $batch->jobs[0];
|
||||
$this->assertInstanceOf(GetMultipleVCard::class, $job);
|
||||
$this->assertEquals(['https://test/dav/uuid1'], $this->getPrivateValue($job, 'hrefs'));
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_forcesync_changes_deleted_contact_batched()
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$subscription = $this->getSubscription();
|
||||
|
||||
$tester = (new DavTester('https://test/dav/addressbooks/user@test.com/contacts/'))
|
||||
->fake();
|
||||
$tester->addResponse('https://test/dav/addressbooks/user@test.com/contacts/', Http::response(DavTester::multistatusHeader().
|
||||
'<d:response>'.
|
||||
'<d:status>HTTP/1.1 404 Not Found</d:status>'.
|
||||
'<d:href>https://test/dav/uuid1</d:href>'.
|
||||
'<d:propstat>'.
|
||||
'<d:prop/>'.
|
||||
'<d:status>HTTP/1.1 418 I\'m a teapot</d:status>'.
|
||||
'</d:propstat>'.
|
||||
'</d:response>'.
|
||||
'</d:multistatus>'), '<?xml version="1.0" encoding="UTF-8"?>'."\n".
|
||||
'<card:addressbook-query xmlns:card="urn:ietf:params:xml:ns:carddav" xmlns:d="DAV:">'.
|
||||
'<d:prop>'.
|
||||
'<d:getetag/>'.
|
||||
'</d:prop>'.
|
||||
"</card:addressbook-query>\n", 'REPORT');
|
||||
|
||||
$client = $tester->client();
|
||||
|
||||
$sync = new SyncDto($subscription, $client);
|
||||
|
||||
(new AddressBookSynchronizer())
|
||||
->execute($sync, true);
|
||||
|
||||
$tester->assert();
|
||||
|
||||
Bus::assertBatched(function (PendingBatch $batch) {
|
||||
$this->assertCount(2, $batch->jobs);
|
||||
$job = $batch->jobs[1];
|
||||
$this->assertInstanceOf(DeleteMultipleVCard::class, $job);
|
||||
$this->assertEquals(['https://test/dav/uuid1'], $this->getPrivateValue($job, 'hrefs'));
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
private function getSubscription()
|
||||
{
|
||||
$subscription = AddressBookSubscription::factory()->create([
|
||||
'uri' => 'https://test/dav/addressbooks/user@test.com/contacts/',
|
||||
]);
|
||||
$token = factory(SyncToken::class)->create([
|
||||
'account_id' => $subscription->account_id,
|
||||
'user_id' => $subscription->user_id,
|
||||
'name' => 'contacts1',
|
||||
'timestamp' => now()->addDays(-1),
|
||||
]);
|
||||
$subscription->localSyncToken = $token->id;
|
||||
$subscription->save();
|
||||
|
||||
return $subscription;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user