faker = Factory::create(); } public function testSearchingForContacts() { $account = Account::factory()->create(); $this->user = User::factory()->create([ 'account_id' => $account->id, 'email' => $this->faker->safeEmail(), ]); $this->company = Company::factory()->create([ 'account_id' => $account->id, ]); $this->client = Client::factory()->create([ 'user_id' => $this->user->id, 'company_id' => $this->company->id, ]); $this->primary_contact = ClientContact::factory()->create([ 'user_id' => $this->user->id, 'client_id' => $this->client->id, 'company_id' => $this->company->id, 'is_primary' => 1, ]); ClientContact::factory()->count(2)->create([ 'user_id' => $this->user->id, 'client_id' => $this->client->id, 'company_id' => $this->company->id, ]); ClientContact::factory()->create([ 'user_id' => $this->user->id, 'client_id' => $this->client->id, 'company_id' => $this->company->id, 'email' => 'search@gmail.com', ]); $this->assertEquals(4, $this->client->contacts->count()); $this->assertTrue($this->client->contacts->contains(function ($contact) { return $contact->email == 'search@gmail.com'; })); $this->assertFalse($this->client->contacts->contains(function ($contact) { return $contact->email == 'false@gmail.com'; })); } public function testMergeClients() { $account = Account::factory()->create(); $user = User::factory()->create([ 'account_id' => $account->id, 'email' => $this->faker->safeEmail(), ]); $company = Company::factory()->create([ 'account_id' => $account->id, ]); $client = Client::factory()->create([ 'user_id' => $user->id, 'company_id' => $company->id, ]); $primary_contact = ClientContact::factory()->create([ 'user_id' => $user->id, 'client_id' => $client->id, 'company_id' => $company->id, 'is_primary' => 1, ]); ClientContact::factory()->count(2)->create([ 'user_id' => $user->id, 'client_id' => $client->id, 'company_id' => $company->id, ]); ClientContact::factory()->create([ 'user_id' => $user->id, 'client_id' => $client->id, 'company_id' => $company->id, 'email' => 'search@gmail.com', ]); //4contacts $mergable_client = Client::factory()->create([ 'user_id' => $user->id, 'company_id' => $company->id, ]); $primary_contact = ClientContact::factory()->create([ 'user_id' => $user->id, 'client_id' => $mergable_client->id, 'company_id' => $company->id, 'is_primary' => 1, ]); ClientContact::factory()->count(2)->create([ 'user_id' => $user->id, 'client_id' => $mergable_client->id, 'company_id' => $company->id, ]); ClientContact::factory()->create([ 'user_id' => $user->id, 'client_id' => $mergable_client->id, 'company_id' => $company->id, 'email' => 'search@gmail.com', ]); //4 contacts $this->assertEquals(4, $client->contacts->count()); $this->assertEquals(4, $mergable_client->contacts->count()); $client = $client->service()->merge($mergable_client)->save(); // nlog($client->contacts->fresh()->toArray()); // $this->assertEquals(7, $client->fresh()->contacts->count()); } }