Skip to content

Commit

Permalink
add test for suggest avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
digitlimit committed Jan 5, 2024
1 parent 5f31807 commit 9060a44
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 59 deletions.
42 changes: 31 additions & 11 deletions app/Domains/Contact/ManageAvatar/Services/SuggestAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
use App\Interfaces\ServiceInterface;
use App\Services\BaseService;
use App\Services\GooglePhotoService;
use Exception;

class SuggestAvatar extends BaseService implements ServiceInterface
{
private array $data;
/**
* The contact instance.
*/
private string $search_term;

/**
* Get the validation rules that apply to the service.
*
* @return array<string,string>
*/
public function rules(): array
{
Expand All @@ -26,6 +32,8 @@ public function rules(): array

/**
* Get the permissions that apply to the user calling the service.
*
* @return array<string>
*/
public function permissions(): array
{
Expand All @@ -39,30 +47,42 @@ public function permissions(): array

/**
* Remove the current file used as avatar and put the default avatar back.
*
* @throws Exception
*/
public function execute(array $data): array
{
$this->data = $data;
$this->validate();

$search_term = $data['search_term'] ?? $this->contact->name;
$this->validate($data);
$this->setSearchTerm($data);

if (empty($search_term)) {
if (empty($this->getSearchTerm())) {
return [];
}

try {
return (new GooglePhotoService())->search($search_term);
} catch (\Exception $e) {
return (new GooglePhotoService())->search($this->getSearchTerm());
} catch (Exception $e) {
return [];
}
}

public function getSearchTerm(): string
{
return $this->search_term;
}

public function setSearchTerm(array $data): self
{
$this->search_term = $data['search_term'] ?? $this->contact->name;

return $this;
}

/**
* @throws \Exception
* @throws Exception
*/
private function validate(): void
private function validate(array $data): void
{
$this->validateRules($this->data);
$this->validateRules($data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,16 @@ public function destroy(Request $request, string $vaultId, string $contactId)

public function suggest(Request $request, string $vaultId, string $contactId): JsonResponse
{
$accountId = Auth::user()->account_id;
$authorId = Auth::id();
$searchTerm = $request->search_term;

$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::id(),
'account_id' => $accountId,
'author_id' => $authorId,
'vault_id' => $vaultId,
'contact_id' => $contactId,
'search_term' => $request->input('search_term'),
'search_term' => $searchTerm,
];

$imageUrls = (new SuggestAvatar())->execute($data);
Expand Down
11 changes: 10 additions & 1 deletion app/Services/GooglePhotoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DOMDocument;
use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

class GooglePhotoService
{
Expand All @@ -13,6 +14,11 @@ class GooglePhotoService
*/
public const GOOGLE_SEARCH_URL = 'https://www.google.com/search';

/**
* Google image URL.
*/
public const GOOGLE_IMAGE_URL = 'https://encrypted-tbn0.gstatic.com';

/**
* The params for Google search.
*/
Expand Down Expand Up @@ -48,7 +54,10 @@ public function imageUrls(string $html): array

foreach ($imgTags as $imgTag) {
$src = $imgTag->getAttribute('src');
if (filter_var($src, FILTER_VALIDATE_URL)) {

// Add only full-size images (exclude thumbnails, etc.)
if (filter_var($src, FILTER_VALIDATE_URL)
&& Str::startsWith($src, self::GOOGLE_IMAGE_URL)) {
$imageUrls[] = $src;
}
}
Expand Down
107 changes: 107 additions & 0 deletions tests/Unit/Domains/Contact/ManageAvatar/Services/SuggestAvatarTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Tests\Unit\Domains\Contact\ManageAvatar\Services;

use App\Domains\Contact\ManageAvatar\Services\SuggestAvatar;
use App\Models\Contact;
use App\Models\Vault;
use Exception;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;

class SuggestAvatarTest extends TestCase
{
use DatabaseTransactions;

private array $suggestions = [
'https://encrypted-tbn0.gstatic.com/images?q=tbn:A1nnWw7Tai5mXqXIS_NN3jA0&s',
'https://encrypted-tbn0.gstatic.com/images?q=tbn:A2nnWw7Tai5mXqXIS_NN3jA0&s',
'https://www.google.com/logos/doodles/2023/seasonal-holidays-2023-6753651837110165.2-s.png',
];

/**
* @test
*
* @group suggest-avatar
* @group it_suggests_list_based_on_contact_name
*
* @throws Exception
*/
public function it_suggests_list_based_on_contact_name(): void
{
$this->fakeHttpResponse();
$request = $this->requestParams();

$suggestAvatar = new SuggestAvatar();
$suggestions = $suggestAvatar->execute($request);

$this->assertCount(2, $suggestions);
$this->assertEquals(
$suggestions,
array_slice($this->suggestions, 0, 2)
);

$contact = Contact::find($request['contact_id']);

$this->assertSame($suggestAvatar->getSearchTerm(), $contact->name);
}

/**
* @test
*
* @group suggest-avatar
* @group it_suggests_list_based_on_search_term
*
* @throws Exception
*/
public function it_suggests_list_based_on_search_term(): void
{
$this->fakeHttpResponse();
$request = $this->requestParams();
$request['search_term'] = 'John Doe';

$suggestAvatar = new SuggestAvatar();
$suggestions = $suggestAvatar->execute($request);

$this->assertCount(2, $suggestions);
$this->assertEquals(
$suggestions,
array_slice($this->suggestions, 0, 2)
);

$this->assertSame($suggestAvatar->getSearchTerm(), $request['search_term']);
}

private function requestParams(): array
{
$author = $this->createUser();
$vault = $this->createVault($author->account);
$vault = $this->setPermissionInVault($author, Vault::PERMISSION_EDIT, $vault);
$contact = Contact::factory()->create(['vault_id' => $vault->id]);

return [
'account_id' => $author->account->id,
'vault_id' => $vault->id,
'author_id' => $author->id,
'contact_id' => $contact->id,
];
}

/**
* @throws Exception
*/
private function fakeHttpResponse(): void
{
Http::fake(function () {
return Http::response('
<html lang="en" dir="ltr" itemscope itemtype="http://schema.org/SearchResultsPage">
<body>
<img src="'.$this->suggestions[0].'" alt="">
<img src="'.$this->suggestions[1].'" alt="">
<img src="'.$this->suggestions[2].'" alt="">
</body>
</html>');
});
}
}

This file was deleted.

0 comments on commit 9060a44

Please sign in to comment.