Skip to content

Commit

Permalink
Update unit tests for detached googleAccount relation
Browse files Browse the repository at this point in the history
  • Loading branch information
vokomarov committed Sep 1, 2024
1 parent 2bc2a8e commit ec03e22
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/src/Service/Auth/GoogleAuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected function makeUser(array $data): User
protected function makeGoogleAccount(User $user, array $data): GoogleAccount
{
$account = new GoogleAccount();
$account->userId = $user->id;
$account->userId = (int) $user->id;
$account->accountId = $data['sub'] ?? null;
$account->pictureUrl = $data['picture'] ?? null;
$account->setData($data);
Expand Down
53 changes: 53 additions & 0 deletions tests/Factories/GoogleAccountFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Tests\Factories;

use App\Database\GoogleAccount;
use App\Database\User;
use Tests\Fixtures;

class GoogleAccountFactory extends AbstractFactory
{
public function create(GoogleAccount $googleAccount = null): GoogleAccount
{
$googleAccount = $googleAccount ?? self::make();

$this->persist($googleAccount);

return $googleAccount;
}

public static function make(): GoogleAccount
{
$googleAccount = new GoogleAccount;

$googleAccount->accountId = Fixtures::string();
$googleAccount->pictureUrl = Fixtures::url();
$googleAccount->setData([
'sub' => $googleAccount->accountId,
'picture' => $googleAccount->pictureUrl,
]);

return $googleAccount;
}

public static function withUser(User $user, ?array $data = null): GoogleAccount
{
$googleAccount = $data === null ? self::make() : self::withData($data);
$googleAccount->userId = $user->id;

return $googleAccount;
}

public static function withData(array $data = []): GoogleAccount
{
$googleAccount = self::make();
$googleAccount->accountId = $data['sub'] ?? null;
$googleAccount->pictureUrl = $data['picture'] ?? null;
$googleAccount->setData($data);

return $googleAccount;
}
}
15 changes: 0 additions & 15 deletions tests/Factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Tests\Factories;

use App\Database\Currency;
use App\Database\GoogleAccount;
use App\Database\User;
use Tests\Fixtures;

Expand Down Expand Up @@ -91,20 +90,6 @@ public static function emailNotConfirmed(User $user = null): User
return self::emailConfirmed($user, false);
}

public static function withGoogleAccount(array $data = [], User $user = null): User
{
if ($user === null) {
$user = self::make();
}

$user->googleAccount = new GoogleAccount();
$user->googleAccount->accountId = $data['sub'] ?? null;
$user->googleAccount->pictureUrl = $data['picture'] ?? null;
$user->googleAccount->setData($data);

return $user;
}

public static function invalidNickNames(): array
{
return array_merge([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Service\PhotoStorageService;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\DatabaseTransaction;
use Tests\Factories\GoogleAccountFactory;
use Tests\Factories\UserFactory;
use Tests\Fixtures;
use Tests\TestCase;
Expand All @@ -16,11 +17,14 @@ class GoogleProviderControllerTest extends TestCase implements DatabaseTransacti
{
protected UserFactory $userFactory;

protected GoogleAccountFactory $googleAccountFactory;

protected function setUp(): void
{
parent::setUp();

$this->userFactory = $this->getContainer()->get(UserFactory::class);
$this->googleAccountFactory = $this->getContainer()->get(GoogleAccountFactory::class);
}

protected function googleAccountInfo(): array
Expand Down Expand Up @@ -167,7 +171,7 @@ public function testLoggedInExistingUserExistingGoogleAccount(): void
];

$user = $this->userFactory->create($user);
$user = $this->userFactory->create(UserFactory::withGoogleAccount($existingData, $user));
$this->googleAccountFactory->create(GoogleAccountFactory::withUser($user, $existingData));

$googleClient = $this->getMockBuilder(\Google\Client::class)->onlyMethods(['verifyIdToken'])->disableOriginalConstructor()->getMock();
$googleClient->expects($this->once())->method('verifyIdToken')->with($token)->willReturn([
Expand Down Expand Up @@ -232,7 +236,7 @@ public function testFailedExistingUserAlreadyHaveDifferentGoogleAccount(): void
];

$user = $this->userFactory->create($user);
$user = $this->userFactory->create(UserFactory::withGoogleAccount($existingData, $user));
$this->googleAccountFactory->create(GoogleAccountFactory::withUser($user, $existingData));

[
'token' => $token,
Expand Down
9 changes: 5 additions & 4 deletions tests/Feature/Controller/Profile/ProfileControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@
use App\Service\UserService;
use Spiral\Testing\Http\TestResponse;
use Tests\DatabaseTransaction;
use Tests\Factories\GoogleAccountFactory;
use Tests\Factories\UserFactory;
use Tests\Fixtures;
use Tests\TestCase;

class ProfileControllerTest extends TestCase implements DatabaseTransaction
{
/**
* @var \Tests\Factories\UserFactory
*/
protected UserFactory $userFactory;

protected GoogleAccountFactory $googleAccountFactory;

protected function setUp(): void
{
parent::setUp();

$this->userFactory = $this->getContainer()->get(UserFactory::class);
$this->googleAccountFactory = $this->getContainer()->get(GoogleAccountFactory::class);
}

private function userFields(): array
Expand Down Expand Up @@ -487,7 +488,7 @@ public function testSocialAccountsGoogle(): void
];

$user = $this->userFactory->create($user);
$user = $this->userFactory->create(UserFactory::withGoogleAccount($existingData, $user));
$this->googleAccountFactory->create(GoogleAccountFactory::withUser($user, $existingData));

$auth = $this->makeAuth($user);

Expand Down

0 comments on commit ec03e22

Please sign in to comment.