| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- /** @noinspection PhpUnhandledExceptionInspection */
- namespace App\Tests\Unit\Service\Mobyt;
- use App\Service\Mobyt\MobytService;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- use Symfony\Contracts\HttpClient\HttpClientInterface;
- class TestableMobytService extends MobytService
- {
- public function connect(string $login, string $password): void
- {
- parent::connect($login, $password);
- }
- public function getUserId(): string
- {
- return parent::getUserId();
- }
- public function getSessionKey(): string
- {
- return parent::getSessionKey();
- }
- }
- class MobytServiceTest extends TestCase
- {
- private HttpClientInterface $client;
- public function setUp(): void
- {
- $this->client = $this->getMockBuilder(HttpClientInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- }
- /**
- * @see MobytService::connect()
- * @see MobytService::getUserId()
- * @see MobytService::getSessionKey()
- */
- public function testConnect(): void
- {
- $mobytService = $this->getMockBuilder(TestableMobytService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['connect', 'getUserId', 'getSessionKey'])
- ->getMock();
- $login = 'foo';
- $password = 'bar';
- $mobytService
- ->expects(self::once())
- ->method('getContent')
- ->with(
- 'login',
- [],
- [
- 'headers' => [
- 'Content-Type: application/json',
- 'Authorization: Basic '.base64_encode($login.':'.$password),
- ],
- ]
- )
- ->willReturn('123;456');
- $mobytService->connect($login, $password);
- $this->assertEquals(
- '123',
- $mobytService->getUserId()
- );
- $this->assertEquals(
- '456',
- $mobytService->getSessionKey()
- );
- }
- /**
- * @see MobytService::getUserStatus()
- */
- public function testGetUserStatus(): void
- {
- $mobytService = $this->getMockBuilder(TestableMobytService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['getUserStatus'])
- ->getMock();
- $login = 'foo';
- $password = 'bar';
- $mobytService->expects(self::once())->method('connect')->with($login, $password);
- $mobytService->method('getUserId')->willReturn('123');
- $mobytService->method('getSessionKey')->willReturn('456');
- $mobytService
- ->expects(self::once())
- ->method('getJsonContent')
- ->with(
- 'status',
- ['getMoney' => 'true', 'typeAliases' => 'true'],
- [
- 'headers' => ['user_key' => '123', 'Session_key' => '456'],
- ]
- );
- $mobytService->getUserStatus($login, $password);
- }
- /**
- * @see MobytService::hasCredentialsCorrect()
- */
- public function testHasCredentialsCorrect(): void
- {
- $mobytService = $this->getMockBuilder(TestableMobytService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['hasCredentialsCorrect'])
- ->getMock();
- $login = 'foo';
- $password = 'bar';
- $mobytService->expects(self::once())->method('connect')->with($login, $password);
- $this->assertTrue($mobytService->hasCredentialsCorrect($login, $password));
- }
- /**
- * @see MobytService::hasCredentialsCorrect()
- */
- public function testHasCredentialsNotCorrect(): void
- {
- $mobytService = $this->getMockBuilder(TestableMobytService::class)
- ->setConstructorArgs([$this->client])
- ->setMethodsExcept(['hasCredentialsCorrect'])
- ->getMock();
- $login = 'foo';
- $password = 'bar';
- $mobytService->expects(self::once())->method('connect')->with($login, $password)->willThrowException(new NotFoundHttpException());
- $this->assertFalse($mobytService->hasCredentialsCorrect($login, $password));
- }
- }
|