Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Добавил fetcher тест и снял еще коменты #22

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/tests-functional.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ jobs:
- name: "Run functional tests"
run: |
php bin/doctrine orm:schema-tool:drop --force
php bin/doctrine orm:schema-tool:create
php bin/doctrine orm:schema-tool:create --dump-sql
php bin/doctrine orm:schema-tool:update --dump-sql
php bin/doctrine orm:info
php vendor/bin/phpunit --testsuite=functional_tests --display-warnings --testdox
- name: "is functional tests succeeded"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ test-run-functional: debug-print-env

# Запустить один функциональный тест с дебагером
run-one-functional-test: debug-print-env
docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testFindByApplicationToken' tests/Functional/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepositoryTest.php
docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testRenewAuthTokenWithoutBitrix24UserId' tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php

schema-drop:
docker-compose run --rm php-cli php bin/doctrine orm:schema-tool:drop --force
Expand Down
2 changes: 1 addition & 1 deletion config/cli-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[
'driver' => 'pdo_pgsql',
'memory' => true,
'dbname' => 'b24phpLibTest',
'dbname' => $_ENV['DATABASE_NAME'],
]);

return DependencyFactory::fromConnection($config, new ExistingConnection($conn));
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<field name="createdAt" type="carbon_immutable" column="created_at_utc" precision="3" nullable="false"/>

<field name="updatedAt" type="carbon_immutable" column="update_at_utc" precision="3" nullable="false"/>
<field name="updatedAt" type="carbon_immutable" column="updated_at_utc" precision="3" nullable="false"/>

<field name="applicationVersion" type="integer" column="application_version" nullable="false"/>

Expand Down
12 changes: 0 additions & 12 deletions config/xml/Bitrix24.Lib.Bitrix24Accounts.Entity.TestUser.dcm.xml

This file was deleted.

38 changes: 18 additions & 20 deletions src/Bitrix24Accounts/Entity/Bitrix24Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@

class Bitrix24Account implements Bitrix24AccountInterface, AggregateRootEventsEmitterInterface
{
private string $accessToken;

private string $refreshToken;

private int $expires;

private array $applicationScope;

private ?string $applicationToken = null;
Expand Down Expand Up @@ -67,17 +61,8 @@ public function __construct(
bool $isEmitBitrix24AccountCreatedEvent = false
) {
$this->authToken = $authToken;
$this->accessToken = $authToken->accessToken;
$this->refreshToken = $authToken->refreshToken;
$this->expires = $authToken->expires;
$this->applicationScope = $applicationScope->getScopeCodes();

if ($isEmitBitrix24AccountCreatedEvent) {
$this->events[] = new Bitrix24AccountCreatedEvent(
$this->id,
$this->createdAt
);
}
$this->addAccountCreatedEventIfNeeded($isEmitBitrix24AccountCreatedEvent);
}

#[\Override]
Expand Down Expand Up @@ -122,7 +107,6 @@ public function getStatus(): Bitrix24AccountStatus
#[\Override]
public function getAuthToken(): AuthToken
{
$this->authToken = new AuthToken($this->accessToken, $this->refreshToken, $this->expires);
return $this->authToken;
}

Expand All @@ -144,9 +128,12 @@ public function renewAuthToken(RenewedAuthToken $renewedAuthToken): void
);
}

$this->accessToken = $renewedAuthToken->authToken->accessToken;
$this->refreshToken = $renewedAuthToken->authToken->refreshToken;
$this->expires = $renewedAuthToken->authToken->expires;
$this->authToken = new AuthToken(
$renewedAuthToken->authToken->accessToken,
$renewedAuthToken->authToken->refreshToken,
$renewedAuthToken->authToken->expires
);

$this->updatedAt = new CarbonImmutable();
}

Expand Down Expand Up @@ -370,4 +357,15 @@ public function emitEvents(): array

return $events;
}

public function addAccountCreatedEventIfNeeded(bool $isEmitCreatedEvent): void
{
if ($isEmitCreatedEvent) {
// Создание события и добавление его в массив событий
$this->events[] = new Bitrix24AccountCreatedEvent(
$this->id,
$this->createdAt
);
}
}
}
44 changes: 0 additions & 44 deletions src/Bitrix24Accounts/Entity/TestUser.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public function __construct(
#[Override]
public function getById(Uuid $uuid): Bitrix24AccountInterface
{
// print_r($uuid);
// exit();
$res = $this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid);
if (null === $res) {
throw new Bitrix24AccountNotFoundException(
Expand Down Expand Up @@ -106,12 +104,15 @@ public function delete(Uuid $uuid): void
$this->getEntityManager()->remove($bitrix24Account);
}

public function findAllActive(): array
public function findAllActive(int|null $limit = null, int|null $offset = null): array
{
return $this->getEntityManager()->getRepository(Bitrix24Account::class)->findBy(
[
'status' => Bitrix24AccountStatus::active,
]
],
null,
$limit,
$offset
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Bitrix24Accounts/ReadModel/Fetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Bitrix24\Lib\Bitrix24Accounts\ReadModel;

use Bitrix24\Lib\Bitrix24Accounts\Infrastructure\Doctrine\Bitrix24AccountRepository;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\Pagination\PaginationInterface;
use Knp\Component\Pager\PaginatorInterface;
Expand All @@ -21,13 +22,14 @@ public function list(
int $size
): PaginationInterface
{

$queryBuilder = $this->em->getConnection()->createQueryBuilder()
->select(
'b24account.id as id',
'b24account.status as status',
'b24account.member_id as member_id',
'b24account.domain_url as domain_url',
'b24account.app_version as application_version',
'b24account.application_version as application_version',
'b24account.created_at_utc as created_at',
'b24account.updated_at_utc as updated_at',
)
Expand Down
49 changes: 29 additions & 20 deletions src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\InstallFinish;

use Bitrix24\Lib\Bitrix24Accounts\Entity\Bitrix24Account;
use Bitrix24\Lib\Services\Flusher;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus;
Expand Down Expand Up @@ -38,17 +39,37 @@ public function handle(Command $command): void
'b24_user_id' => $command->bitrix24UserId,
]);


/**
* @var AggregateRootEventsEmitterInterface|Bitrix24AccountInterface $targetAccount
*/
$targetAccount = $this->getSingleAccountByMemberId($command->domainUrl, $command->memberId,Bitrix24AccountStatus::new, $command->bitrix24UserId);

$targetAccount->applicationInstalled($command->applicationToken);

$this->bitrix24AccountRepository->save($targetAccount);
$this->flusher->flush();
foreach ($targetAccount->emitEvents() as $event) {
$this->eventDispatcher->dispatch($event);
}

$this->logger->debug('Bitrix24Accounts.InstallFinish.Finish');
}

public function getSingleAccountByMemberId(string $domainUrl, string $memberId, Bitrix24AccountStatus $status, int|null $bitrix24UserId): Bitrix24AccountInterface
{
$accounts = $this->bitrix24AccountRepository->findByMemberId(
$command->memberId,
Bitrix24AccountStatus::new,
$command->bitrix24UserId
$memberId,
$status,
$bitrix24UserId
);

if ([] === $accounts) {
throw new Bitrix24AccountNotFoundException(
sprintf(
'bitrix24 account for domain %s with member id %s in status «new» not found',
$command->domainUrl,
$command->memberId
$domainUrl,
$memberId
)
);
}
Expand All @@ -57,24 +78,12 @@ public function handle(Command $command): void
throw new MultipleBitrix24AccountsFoundException(
sprintf(
'multiple bitrix24 accounts for domain %s with member id %s in status «new» found',
$command->domainUrl,
$command->memberId
$domainUrl,
$memberId
)
);
}

/**
* @var AggregateRootEventsEmitterInterface|Bitrix24AccountInterface $targetAccount
*/
$targetAccount = $accounts[0];
$targetAccount->applicationInstalled($command->applicationToken);

$this->bitrix24AccountRepository->save($targetAccount);
$this->flusher->flush();
foreach ($targetAccount->emitEvents() as $event) {
$this->eventDispatcher->dispatch($event);
}

$this->logger->debug('Bitrix24Accounts.InstallFinish.Finish');
return $accounts[0];
}
}
51 changes: 28 additions & 23 deletions src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,48 @@ public function handle(Command $command): void
]);

// get all active bitrix24 accounts
$targetAccount = $this->getSingleAccountByMemberId($command->renewedAuthToken->domain, $command->renewedAuthToken->memberId,Bitrix24AccountStatus::active,$command->bitrix24UserId);

/**
* @var Bitrix24AccountInterface|AggregateRootEventsEmitterInterface $targetAccount
*/
$targetAccount->renewAuthToken($command->renewedAuthToken);
$this->bitrix24AccountRepository->save($targetAccount);
$this->flusher->flush();
foreach ($targetAccount->emitEvents() as $event) {
$this->eventDispatcher->dispatch($event);
}

$this->logger->debug('Bitrix24Accounts.RenewAuthToken.finish');
}

public function getSingleAccountByMemberId(string $domainUrl, string $memberId, Bitrix24AccountStatus $status, int|null $bitrix24UserId): Bitrix24AccountInterface
{
$accounts = $this->bitrix24AccountRepository->findByMemberId(
$command->renewedAuthToken->memberId,
Bitrix24AccountStatus::active,
$command->bitrix24UserId
$memberId,
$status,
$bitrix24UserId
);

if ($command->bitrix24UserId === null && count($accounts) > 1) {
if ($bitrix24UserId === null && count($accounts) > 1) {
throw new MultipleBitrix24AccountsFoundException(
sprintf('updating auth token failure - for domain %s with member id %s found multiple active accounts, try pass bitrix24_user_id in command',
$command->renewedAuthToken->domain,
$command->renewedAuthToken->memberId
$domainUrl,
$memberId
)
);
}

if ($command->bitrix24UserId !== null && count($accounts) > 1) {
if ($bitrix24UserId !== null && count($accounts) > 1) {
throw new MultipleBitrix24AccountsFoundException(
sprintf('updating auth token failure - for domain %s with member id %s and bitrix24 user id %s found multiple active accounts',
$command->renewedAuthToken->domain,
$command->renewedAuthToken->memberId,
$command->bitrix24UserId
$domainUrl,
$memberId,
$bitrix24UserId
)
);
}

$targetAccount = $accounts[0];

/**
* @var Bitrix24AccountInterface|AggregateRootEventsEmitterInterface $targetAccount
*/
$targetAccount->renewAuthToken($command->renewedAuthToken);
$this->bitrix24AccountRepository->save($targetAccount);
$this->flusher->flush();
foreach ($targetAccount->emitEvents() as $event) {
$this->eventDispatcher->dispatch($event);
}

$this->logger->debug('Bitrix24Accounts.RenewAuthToken.finish');
return $accounts[0];
}
}
8 changes: 0 additions & 8 deletions tests/EntityManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ class EntityManagerFactory

public static function get(): EntityManagerInterface
{
/* $paths = [
dirname(__DIR__) . '/src/Bitrix24Accounts/Entity'
];*/

if (self::$entityManager === null) {
$paths = [
dirname(__DIR__) . '/config/xml'
Expand Down Expand Up @@ -74,12 +70,8 @@ public static function get(): EntityManagerInterface
Type::addType('carbon_immutable', CarbonImmutableType::class);
}

// $configuration = ORMSetup::createAttributeMetadataConfiguration($paths, $isDevMode);
$configuration = ORMSetup::createXMLMetadataConfiguration($paths, $isDevMode);
// $log = new Logger('name');
// $log->pushHandler(new StreamHandler('log.txt', Level::Debug));

// $configuration->setMiddlewares([new \Doctrine\DBAL\Logging\Middleware($log)]);
$connection = DriverManager::getConnection($connectionParams, $configuration);
self::$entityManager = new EntityManager($connection, $configuration);
}
Expand Down
Loading
Loading