Skip to content
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: 0 additions & 3 deletions .github/workflows/tests-functional.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ jobs:
POSTGRES_PASSWORD: b24phpLibTest
POSTGRES_DB: b24phpLibTest
DATABASE_HOST: localhost
DATABASE_USER: b24phpLibTest
DATABASE_PASSWORD: b24phpLibTest
DATABASE_NAME: b24phpLibTest

steps:
- name: "Checkout code"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,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 'testChangeDomainUrlWithHappyPath' tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\Pagination\PaginationInterface;
use Knp\Component\Pager\PaginatorInterface;
class Fetcher
class Bitrix24AccountFetcher
{

public function __construct(
Expand Down
17 changes: 8 additions & 9 deletions src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

public function handle(Command $command): void
{
$this->logger->debug('Bitrix24Accounts.ChangeDomainUrl.start', [
$this->logger->info('Bitrix24Accounts.ChangeDomainUrl.start', [
'b24_domain_url_old' => $command->oldDomainUrlHost,
'b24_domain_url_new' => $command->newDomainUrlHost,
]);
Expand All @@ -28,16 +28,15 @@
foreach ($accounts as $account) {
$account->changeDomainUrl($command->newDomainUrlHost);
$this->bitrix24AccountRepository->save($account);
// $this->flusher->flush();
// todo выяснить почему он не видит объединение типов
// @phpstan-ignore-next-line
/* foreach ($account->emitEvents() as $event) {
$this->eventDispatcher->dispatch($event);
}*/

}

//используется как оператор распаковки (splat operator) для передачи массива как отдельных аргументов:
$this->flusher->flush(...$accounts);

Check failure on line 34 in src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest, ubuntu-latest)

Parameter #1 ...$roots of method Bitrix24\Lib\Services\Flusher::flush() expects Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface, Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface given.

$this->logger->debug('Bitrix24Accounts.ChangeDomainUrl.Finish');
$this->logger->info('Bitrix24Accounts.ChangeDomainUrl.Finish',
[
'b24_domain_url_old' => $command->oldDomainUrlHost,
'b24_domain_url_new' => $command->newDomainUrlHost,
]);
}
}
20 changes: 19 additions & 1 deletion src/Bitrix24Accounts/UseCase/InstallFinish/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,30 @@

namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\InstallFinish;

use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException;
readonly class Command
{
public function __construct(
public string $applicationToken,
public string $memberId,
public string $domainUrl,
public ?int $bitrix24UserId,
) {}
) {
$this->validate();
}

private function validate(): void
{
if (empty($this->applicationToken)) {
throw new InvalidArgumentException('Application token cannot be empty.');
}

if (empty($this->memberId)) {
throw new InvalidArgumentException('Member ID cannot be empty.');
}

if (empty($this->domainUrl)) {
throw new InvalidArgumentException('Domain URL cannot be empty.');
}
}
}
20 changes: 10 additions & 10 deletions src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
readonly class Handler
{
public function __construct(
private EventDispatcherInterface $eventDispatcher,
private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository,
private Flusher $flusher,
private LoggerInterface $logger
Expand All @@ -31,27 +30,28 @@
*/
public function handle(Command $command): void
{
$this->logger->debug('Bitrix24Accounts.InstallFinish.start', [
$this->logger->info('Bitrix24Accounts.InstallFinish.start', [
'b24_domain_url' => $command->domainUrl,
'b24_member_id' => $command->memberId,
'b24_application_id' => $command->applicationToken,
'b24_user_id' => $command->bitrix24UserId,
]);

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

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

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

Check failure on line 45 in src/Bitrix24Accounts/UseCase/InstallFinish/Handler.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest, ubuntu-latest)

Parameter #1 ...$roots of method Bitrix24\Lib\Services\Flusher::flush() expects Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface, Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface given.


$this->logger->debug('Bitrix24Accounts.InstallFinish.Finish');
$this->logger->info('Bitrix24Accounts.InstallFinish.Finish',
[
'b24_domain_url' => $command->domainUrl,
'b24_member_id' => $command->memberId,
'b24_application_id' => $command->applicationToken,
'b24_user_id' => $command->bitrix24UserId,
]);
}

public function getSingleAccountByMemberId(string $domainUrl, string $memberId, Bitrix24AccountStatus $bitrix24AccountStatus, ?int $bitrix24UserId): Bitrix24AccountInterface
Expand Down
14 changes: 7 additions & 7 deletions src/Bitrix24Accounts/UseCase/InstallStart/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(

public function handle(Command $command): void
{
$this->logger->debug('Bitrix24Accounts.InstallStart.start', [
$this->logger->info('Bitrix24Accounts.InstallStart.start', [
'id' => $command->uuid->toRfc4122(),
'domain_url' => $command->domainUrl,
'member_id' => $command->memberId,
Expand All @@ -44,12 +44,12 @@ public function handle(Command $command): void
);
$this->bitrix24AccountRepository->save($bitrix24Account);
$this->flusher->flush($bitrix24Account);
// $this->flusher->flush();
/*foreach ($bitrix24Account->emitEvents() as $event) {

$this->eventDispatcher->dispatch($event);
}*/

$this->logger->debug('Bitrix24Accounts.InstallStart.Finish');
$this->logger->info('Bitrix24Accounts.InstallStart.Finish',
[
'id' => $command->uuid->toRfc4122(),
'domain_url' => $command->domainUrl,
'member_id' => $command->memberId,
]);
}
}
17 changes: 9 additions & 8 deletions src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface;
use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

readonly class Handler
{
Expand All @@ -26,7 +25,7 @@
*/
public function handle(Command $command): void
{
$this->logger->debug('Bitrix24Accounts.RenewAuthToken.start', [
$this->logger->info('Bitrix24Accounts.RenewAuthToken.start', [
'domain_url' => $command->renewedAuthToken->domain,
'member_id' => $command->renewedAuthToken->memberId,
'bitrix24_user_id' => $command->bitrix24UserId,
Expand All @@ -39,17 +38,19 @@
$command->bitrix24UserId
);

// Bitrix24Account extends AggregateRoot and implement AggregateRootEventsEmitterInterface
/** @var AggregateRootEventsEmitterInterface|Bitrix24AccountInterface $bitrix24Account */

$bitrix24Account->renewAuthToken($command->renewedAuthToken);

$this->bitrix24AccountRepository->save($bitrix24Account);

$this->flusher->flush($bitrix24Account);

Check failure on line 46 in src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest, ubuntu-latest)

Parameter #1 ...$roots of method Bitrix24\Lib\Services\Flusher::flush() expects Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface, Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface given.
/* foreach ($bitrix24Account->emitEvents() as $event) {
$this->eventDispatcher->dispatch($event);
}*/

$this->logger->debug('Bitrix24Accounts.RenewAuthToken.finish');
$this->logger->info('Bitrix24Accounts.RenewAuthToken.finish',
[
'domain_url' => $command->renewedAuthToken->domain,
'member_id' => $command->renewedAuthToken->memberId,
'bitrix24_user_id' => $command->bitrix24UserId,
]);
}

/**
Expand Down
15 changes: 7 additions & 8 deletions src/Bitrix24Accounts/UseCase/Uninstall/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,26 @@
) {}

/**
* @throws Bitrix24AccountNotFoundException
* @throws InvalidArgumentException
*/
public function handle(Command $command): void
{
$this->logger->debug('Bitrix24Accounts.Uninstall.start', [
$this->logger->info('Bitrix24Accounts.Uninstall.start', [
'b24_application_token' => $command->applicationToken,
]);

/**
* @var AggregateRootEventsEmitterInterface[]|Bitrix24AccountInterface[] $accounts
*/
$accounts = $this->bitrix24AccountRepository->findByApplicationToken($command->applicationToken);

$accountsCount = count($accounts);
foreach ($accounts as $account) {
$account->applicationUninstalled($command->applicationToken);
$this->bitrix24AccountRepository->save($account);
}
$this->flusher->flush(...$accounts);

Check failure on line 39 in src/Bitrix24Accounts/UseCase/Uninstall/Handler.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest, ubuntu-latest)

Parameter #1 ...$roots of method Bitrix24\Lib\Services\Flusher::flush() expects Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface, Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface given.


$this->logger->debug('Bitrix24Accounts.Uninstall.Finish');
$this->logger->info('Bitrix24Accounts.Uninstall.Finish',
[
'accountsCount' => $accountsCount,
'b24_application_token' => $command->applicationToken,
]);
}
}
9 changes: 4 additions & 5 deletions src/Services/Flusher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@


use Bitrix24\Lib\AggregateRoot;
use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class Flusher
{
private $em;
private $eventDispatcher;
public function __construct(EntityManagerInterface $em,EventDispatcher $eventDispatcher) {
public function __construct(EntityManagerInterface $em, EventDispatcherInterface $eventDispatcher) {
$this->em = $em;
$this->eventDispatcher = $eventDispatcher;
}

public function flush(AggregateRoot ...$roots): void
public function flush(AggregateRootEventsEmitterInterface ...$roots): void
{
$this->em->flush();

foreach ($roots as $root) {
$events = $root->emitEvents();
foreach ($events as $event) {
var_dump($event);
$this->eventDispatcher->dispatch($event);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Bitrix24\Lib\Tests\Functional\Bitrix24Accounts;

use Bitrix24\Lib\Bitrix24Accounts\Infrastructure\Doctrine\Bitrix24AccountRepository;
use Bitrix24\Lib\Bitrix24Accounts\ReadModel\Fetcher;
use Bitrix24\Lib\Bitrix24Accounts\ReadModel\Bitrix24AccountFetcher;
use Bitrix24\Lib\Services\Flusher;
use Bitrix24\Lib\Tests\EntityManagerFactory;
use Knp\Component\Pager\Event\Subscriber\Paginate\PaginationSubscriber;
Expand All @@ -21,15 +21,15 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Knp\Component\Pager\ArgumentAccess\RequestArgumentAccess;

class FetcherTest extends TestCase
class Bitrix24AccountsFetcherTest extends TestCase
{
private EntityManagerInterface $entityManager;

private PaginatorInterface $paginator;

private Bitrix24AccountRepositoryInterface $repository;

private Fetcher $fetcher;
private Bitrix24AccountFetcher $fetcher;

private Flusher $flusher;

Expand All @@ -42,7 +42,7 @@ protected function setUp(): void
$eventDispatcher->addSubscriber(new SortableSubscriber());
$requestArgumentAccess = new RequestArgumentAccess(new RequestStack());
$this->paginator = new Paginator($eventDispatcher, $requestArgumentAccess);
$this->fetcher = new Fetcher($this->entityManager, $this->paginator);
$this->fetcher = new Bitrix24AccountFetcher($this->entityManager, $this->paginator);
$this->flusher = new Flusher($this->entityManager,$eventDispatcher);
$this->repository = new Bitrix24AccountRepository($this->entityManager);
}
Expand All @@ -67,4 +67,36 @@ public function testListReturnsPaginatedResults(): void
$this->assertGreaterThan(0, $pagination->count()); // Проверяем, что есть хотя бы одна запись
}

public function testColumnNamesInBitrix24Accounts(): void
{
// Ожидаемые названия столбцов
$expectedColumns = [
'id',
'status',
'b24_user_id',
'is_b24_user_admin',
'member_id',
'domain_url',
'application_token',
'created_at_utc',
'updated_at_utc',
'application_version',
'authtoken_access_token',
'authtoken_refresh_token',
'authtoken_expires',
'authtoken_expires_in',
'applicationscope_current_scope'
];

// Получение фактических названий столбцов из базы данных
$connection = $this->entityManager->getConnection();
$schemaManager = $connection->createSchemaManager();
$columns = $schemaManager->listTableColumns('bitrix24account');
$actualColumns = array_keys($columns);

foreach ($expectedColumns as $column) {
$this->assertContains($column, $actualColumns, "Column '$column' is missing in table 'bitrix24account'.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Carbon\CarbonImmutable;
use Override;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Uid\Uuid;

#[CoversClass(Bitrix24AccountRepository::class)]
Expand Down Expand Up @@ -73,6 +74,8 @@ protected function createBitrix24AccountRepositoryImplementation(): Bitrix24Acco
#[Override]
protected function createRepositoryFlusherImplementation(): TestRepositoryFlusherInterface
{
return new FlusherDecorator(new Flusher(EntityManagerFactory::get()));
$entityManager = EntityManagerFactory::get();
$eventDispatcher = new EventDispatcher();
return new FlusherDecorator(new Flusher($entityManager, $eventDispatcher));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Uid\Uuid;

use Symfony\Component\EventDispatcher\EventDispatcher;
/**
* @internal
*/
Expand All @@ -50,8 +49,8 @@ protected function setUp(): void
$entityManager = EntityManagerFactory::get();
$eventDispatcher = new EventDispatcher();
$this->repository = new Bitrix24AccountRepository($entityManager);
$this->flusher = new Flusher($entityManager,$eventDispatcher);
$this->eventDispatcher = new TraceableEventDispatcher($eventDispatcher, new Stopwatch());
$this->flusher = new Flusher($entityManager,$this->eventDispatcher);
$this->handler = new Bitrix24Accounts\UseCase\ChangeDomainUrl\Handler(
$this->repository,
$this->flusher,
Expand Down
Loading
Loading