|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | +/** |
| 4 | + * Copyright © Magento, Inc. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Magento\AdobeIms\Test\Unit\Model; |
| 9 | + |
| 10 | +use Magento\AdobeIms\Model\LogIn; |
| 11 | +use Magento\AdobeIms\Model\OAuth\TokenResponse; |
| 12 | +use Magento\AdobeImsApi\Api\Data\TokenResponseInterfaceFactory; |
| 13 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 14 | +use Magento\Framework\App\ScopeResolverInterface; |
| 15 | +use Magento\Framework\Locale\ResolverInterface; |
| 16 | +use Magento\Framework\Stdlib\DateTime\Intl\DateFormatterFactory; |
| 17 | +use Magento\Framework\Stdlib\DateTime\Timezone; |
| 18 | +use Magento\Framework\Stdlib\DateTime\TimezoneInterface; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Magento\AdobeImsApi\Api\Data\TokenResponseInterface; |
| 21 | +use Magento\AdobeImsApi\Api\Data\UserProfileInterface; |
| 22 | +use Magento\AdobeImsApi\Api\Data\UserProfileInterfaceFactory; |
| 23 | +use Magento\AdobeImsApi\Api\UserProfileRepositoryInterface; |
| 24 | +use Magento\Framework\Encryption\EncryptorInterface; |
| 25 | +use Magento\Framework\Stdlib\DateTime\DateTime; |
| 26 | +use Magento\Framework\Stdlib\DateTime as StdlibDateTime; |
| 27 | +use Magento\AdobeImsApi\Api\GetImageInterface; |
| 28 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 29 | + |
| 30 | +/** |
| 31 | + * Unit tests for \Magento\AdobeIms\Model\LogIn class. |
| 32 | + * |
| 33 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 34 | + */ |
| 35 | +class LoginTest extends TestCase |
| 36 | +{ |
| 37 | + /** |
| 38 | + * @var UserProfileRepositoryInterface|MockObject |
| 39 | + */ |
| 40 | + protected $userProfileRepository; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var EncryptorInterface|MockObject |
| 44 | + */ |
| 45 | + protected $encryptor; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var UserProfileInterfaceFactory|MockObject |
| 49 | + */ |
| 50 | + protected $userProfileFactory; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var GetImageInterface|MockObject |
| 54 | + */ |
| 55 | + protected $getUserImage; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var string |
| 59 | + */ |
| 60 | + protected $scopeType; |
| 61 | + |
| 62 | + /** |
| 63 | + * @var string |
| 64 | + */ |
| 65 | + protected $defaultTimezonePath; |
| 66 | + |
| 67 | + /** |
| 68 | + * @var ScopeResolverInterface|MockObject |
| 69 | + */ |
| 70 | + protected $scopeResolver; |
| 71 | + |
| 72 | + /** |
| 73 | + * @var ResolverInterface|MockObject |
| 74 | + */ |
| 75 | + protected $localeResolver; |
| 76 | + |
| 77 | + /** |
| 78 | + * @var ScopeConfigInterface|MockObject |
| 79 | + */ |
| 80 | + protected $scopeConfig; |
| 81 | + |
| 82 | + /** |
| 83 | + * @var DateTime|MockObject |
| 84 | + */ |
| 85 | + protected $dateTime; |
| 86 | + |
| 87 | + /** |
| 88 | + * @var LogIn |
| 89 | + */ |
| 90 | + protected $model; |
| 91 | + |
| 92 | + /** |
| 93 | + * @inheritdoc |
| 94 | + */ |
| 95 | + protected function setUp(): void |
| 96 | + { |
| 97 | + $objectManager = new ObjectManager($this); |
| 98 | + $this->userProfileRepository = $this->createMock(UserProfileRepositoryInterface::class); |
| 99 | + $this->encryptor = $this->createMock(EncryptorInterface::class); |
| 100 | + $this->userProfileFactory = $this->createMock(UserProfileInterfaceFactory::class); |
| 101 | + $this->getUserImage = $this->createMock(GetImageInterface::class); |
| 102 | + $this->scopeType = 'default'; |
| 103 | + $this->defaultTimezonePath = 'general/locale/timezone'; |
| 104 | + $this->scopeResolver = $this->getMockBuilder(ScopeResolverInterface::class) |
| 105 | + ->getMock(); |
| 106 | + $this->localeResolver = $this->getMockBuilder(ResolverInterface::class) |
| 107 | + ->getMock(); |
| 108 | + $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class) |
| 109 | + ->getMock(); |
| 110 | + |
| 111 | + $this->dateTime = $objectManager->getObject( |
| 112 | + DateTime::class, |
| 113 | + ['localeDate' => $this->getTimezone()] |
| 114 | + ); |
| 115 | + |
| 116 | + $this->model = new LogIn( |
| 117 | + $this->userProfileRepository, |
| 118 | + $this->userProfileFactory, |
| 119 | + $this->getUserImage, |
| 120 | + $this->encryptor, |
| 121 | + $this->dateTime |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Test Login. |
| 127 | + * |
| 128 | + * @param int $userId |
| 129 | + * @param array $responseData |
| 130 | + * @dataProvider responseDataProvider |
| 131 | + */ |
| 132 | + public function testExecute( |
| 133 | + int $userId, |
| 134 | + array $responseData |
| 135 | + ): void { |
| 136 | + $userProfileMock = $this->createMock(UserProfileInterface::class); |
| 137 | + $this->userProfileRepository->expects($this->once())->method('save') |
| 138 | + ->with($userProfileMock)->willReturnSelf(); |
| 139 | + $this->userProfileRepository->expects($this->exactly(1)) |
| 140 | + ->method('getByUserId') |
| 141 | + ->willReturn($userProfileMock); |
| 142 | + $this->getUserImage->expects($this->once()) |
| 143 | + ->method('execute') |
| 144 | + ->with($responseData['access_token']) |
| 145 | + ->willReturn('adobe_user_image'); |
| 146 | + $this->encryptor->expects($this->any()) |
| 147 | + ->method('encrypt') |
| 148 | + ->with($responseData['access_token']) |
| 149 | + ->willReturn($responseData['access_token']); |
| 150 | + $tokenResponse = $this->createMock(TokenResponseInterface::class); |
| 151 | + $tokenResponse->expects($this->any()) |
| 152 | + ->method('getAccessToken') |
| 153 | + ->willReturn($responseData['access_token']); |
| 154 | + $tokenResponse->expects($this->once()) |
| 155 | + ->method('getRefreshToken') |
| 156 | + ->willReturn($responseData['refresh_token']); |
| 157 | + $tokenResponse->expects($this->once()) |
| 158 | + ->method('getName') |
| 159 | + ->willReturn($responseData['name']); |
| 160 | + $tokenResponse->expects($this->once()) |
| 161 | + ->method('getEmail') |
| 162 | + ->willReturn($responseData['email']); |
| 163 | + $tokenResponse->expects($this->once()) |
| 164 | + ->method('getExpiresIn') |
| 165 | + ->willReturn($responseData['expires_in']); |
| 166 | + $this->scopeConfig->expects($this->atLeastOnce()) |
| 167 | + ->method('getValue') |
| 168 | + ->with($this->defaultTimezonePath, $this->scopeType, null) |
| 169 | + ->willReturn('America/Chicago'); |
| 170 | + $this->localeResolver->expects($this->atLeastOnce()) |
| 171 | + ->method('getLocale') |
| 172 | + ->willReturn('en_US'); |
| 173 | + |
| 174 | + $this->model->execute($userId, $tokenResponse); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Data provider for response. |
| 179 | + * |
| 180 | + * @return array |
| 181 | + */ |
| 182 | + public function responseDataProvider(): array |
| 183 | + { |
| 184 | + return |
| 185 | + [ |
| 186 | + [ |
| 187 | + 'userId' => 10, |
| 188 | + 'tokenResponse' => [ |
| 189 | + 'name' => 'Test User', |
| 190 | + |
| 191 | + 'access_token' => 'kladjflakdjf3423rfzddsf', |
| 192 | + 'refresh_token' => 'kladjflakdjf3423rfzddsf', |
| 193 | + 'expires_in' => 1642259230998 |
| 194 | + ] |
| 195 | + ] |
| 196 | + ]; |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * @return Timezone |
| 201 | + */ |
| 202 | + private function getTimezone() |
| 203 | + { |
| 204 | + return new Timezone( |
| 205 | + $this->scopeResolver, |
| 206 | + $this->localeResolver, |
| 207 | + $this->createMock(StdlibDateTime::class), |
| 208 | + $this->scopeConfig, |
| 209 | + $this->scopeType, |
| 210 | + $this->defaultTimezonePath, |
| 211 | + new DateFormatterFactory() |
| 212 | + ); |
| 213 | + } |
| 214 | +} |
0 commit comments