diff --git a/app/src/Service/Auth/GoogleAuthService.php b/app/src/Service/Auth/GoogleAuthService.php index 5b4cf33..ca1e925 100644 --- a/app/src/Service/Auth/GoogleAuthService.php +++ b/app/src/Service/Auth/GoogleAuthService.php @@ -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); diff --git a/tests/Factories/GoogleAccountFactory.php b/tests/Factories/GoogleAccountFactory.php new file mode 100644 index 0000000..4a335ee --- /dev/null +++ b/tests/Factories/GoogleAccountFactory.php @@ -0,0 +1,53 @@ +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; + } +} diff --git a/tests/Factories/UserFactory.php b/tests/Factories/UserFactory.php index c72a99b..73fe329 100644 --- a/tests/Factories/UserFactory.php +++ b/tests/Factories/UserFactory.php @@ -5,7 +5,6 @@ namespace Tests\Factories; use App\Database\Currency; -use App\Database\GoogleAccount; use App\Database\User; use Tests\Fixtures; @@ -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([ diff --git a/tests/Feature/Controller/Auth/GoogleProviderControllerTest.php b/tests/Feature/Controller/Auth/GoogleProviderControllerTest.php index e88d5bf..a7fdccb 100644 --- a/tests/Feature/Controller/Auth/GoogleProviderControllerTest.php +++ b/tests/Feature/Controller/Auth/GoogleProviderControllerTest.php @@ -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; @@ -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 @@ -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([ @@ -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, diff --git a/tests/Feature/Controller/Profile/ProfileControllerTest.php b/tests/Feature/Controller/Profile/ProfileControllerTest.php index ab8dfa0..1cbda92 100644 --- a/tests/Feature/Controller/Profile/ProfileControllerTest.php +++ b/tests/Feature/Controller/Profile/ProfileControllerTest.php @@ -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 @@ -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);