Skip to content

Commit

Permalink
Refactor event dispatching mechanism
Browse files Browse the repository at this point in the history
Revised the event dispatching mechanism to handle emitted events directly from the Bitrix24Account entity. Streamlined constructor formatting and updated doc annotations for consistency.

Signed-off-by: mesilov <[email protected]>
  • Loading branch information
mesilov committed Nov 17, 2024
1 parent 57153ae commit 9ad0581
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 72 deletions.
17 changes: 12 additions & 5 deletions src/Bitrix24Accounts/Entity/Bitrix24Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,20 @@ public function __construct(
private CarbonImmutable $updatedAt,
private int $applicationVersion,
Scope $applicationScope,
bool $isEmitBitrix24AccountCreatedEvent = false
) {
$this->authToken = $authToken;
$this->accessToken = $authToken->accessToken;
$this->refreshToken = $authToken->refreshToken;
$this->expires = $authToken->expires;
$this->applicationScope = $applicationScope->getScopeCodes();

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

#[\Override]
Expand Down Expand Up @@ -113,10 +116,14 @@ public function getStatus(): Bitrix24AccountStatus
return $this->status;
}

/**
* @throws InvalidArgumentException
*/
#[\Override]
public function getAuthToken(): AuthToken
{
return new AuthToken($this->accessToken, $this->refreshToken, $this->expires);
$this->authToken = new AuthToken($this->accessToken, $this->refreshToken, $this->expires);
return $this->authToken;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Bitrix24Accounts/UseCase/InstallStart/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public function handle(Command $command): void
new CarbonImmutable(),
new CarbonImmutable(),
$command->applicationVersion,
$command->applicationScope
$command->applicationScope,
true
);
$this->bitrix24AccountRepository->save($newAccount);
$this->flusher->flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,17 @@

namespace Bitrix24\Lib\Tests\Functional\Bitrix24Accounts\UseCase\InstallStart;

use Bitrix24\Lib\Bitrix24Accounts\Entity\Bitrix24Account;
use Bitrix24\Lib\Bitrix24Accounts;
use Bitrix24\Lib\Bitrix24Accounts\Infrastructure\Doctrine\Bitrix24AccountRepository;
use Bitrix24\Lib\Services\Flusher;
use Bitrix24\Lib\Bitrix24Accounts;
use Bitrix24\Lib\Tests\EntityManagerFactory;
use Bitrix24\SDK\Application\ApplicationStatus;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Events\Bitrix24AccountCreatedEvent;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Events\Bitrix24AccountDomainUrlChangedEvent;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Exceptions\Bitrix24AccountNotFoundException;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface;
use Bitrix24\SDK\Core\Credentials\AuthToken;
use Bitrix24\SDK\Core\Credentials\Scope;
use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException;
use Bitrix24\SDK\Core\Exceptions\UnknownScopeCodeException;
use Bitrix24\SDK\Core\Response\DTO\RenewedAuthToken;
use Carbon\CarbonImmutable;
use Override;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
Expand All @@ -41,6 +34,9 @@
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Uid\Uuid;

/**
* @internal
*/
#[CoversClass(Bitrix24Accounts\UseCase\InstallStart\Handler::class)]
class HandlerTest extends TestCase
{
Expand All @@ -49,6 +45,21 @@ class HandlerTest extends TestCase
private Bitrix24AccountRepositoryInterface $repository;
private TraceableEventDispatcher $eventDispatcher;

#[\Override]
protected function setUp(): void
{
$entityManager = EntityManagerFactory::get();
$this->repository = new Bitrix24AccountRepository($entityManager);
$this->flusher = new Flusher($entityManager);
$this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
$this->handler = new Bitrix24Accounts\UseCase\InstallStart\Handler(
$this->eventDispatcher,
$this->repository,
$this->flusher,
new NullLogger()
);
}

/**
* @throws InvalidArgumentException
* @throws Bitrix24AccountNotFoundException
Expand All @@ -62,7 +73,7 @@ public function testInstallStartHappyPath(): void
$b24UserId = random_int(1, 100_000);
$isB24UserAdmin = true;
$b24MemberId = Uuid::v7()->toRfc4122();
$b24DomainUrl = Uuid::v7()->toRfc4122() . '-test.bitrix24.com';
$b24DomainUrl = Uuid::v7()->toRfc4122().'-test.bitrix24.com';
$authToken = new AuthToken('old_1', 'old_2', 3600);
$appVersion = 1;
$scope = new Scope(['crm']);
Expand All @@ -88,24 +99,13 @@ public function testInstallStartHappyPath(): void
$this->assertEquals($appVersion, $account->getApplicationVersion());
$this->assertEquals($scope, $account->getApplicationScope());


$this->assertTrue(in_array(
$this->assertContains(
Bitrix24AccountCreatedEvent::class,
$this->eventDispatcher->getOrphanedEvents()));
}

#[Override]
protected function setUp(): void
{
$entityManager = EntityManagerFactory::get();
$this->repository = new Bitrix24AccountRepository($entityManager);
$this->flusher = new Flusher($entityManager);
$this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
$this->handler = new Bitrix24Accounts\UseCase\InstallStart\Handler(
$this->eventDispatcher,
$this->repository,
$this->flusher,
new NullLogger()
$this->eventDispatcher->getOrphanedEvents(),
sprintf(
'not found expected domain event «%s»',
Bitrix24AccountCreatedEvent::class
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,27 @@

namespace Bitrix24\Lib\Tests\Functional\Bitrix24Accounts\UseCase\RenewAuthToken;

use Bitrix24\Lib\Bitrix24Accounts\Entity\Bitrix24Account;
use Bitrix24\Lib\Bitrix24Accounts\Infrastructure\Doctrine\Bitrix24AccountRepository;
use Bitrix24\Lib\Bitrix24Accounts\UseCase\RenewAuthToken\Command;
use Bitrix24\Lib\Bitrix24Accounts\UseCase\RenewAuthToken\Handler;
use Bitrix24\Lib\Services\Flusher;
use Bitrix24\Lib\Tests\EntityManagerFactory;
use Bitrix24\Lib\Tests\Functional\Bitrix24Accounts\Builders\Bitrix24AccountBuilder;
use Bitrix24\SDK\Application\ApplicationStatus;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface;
use Bitrix24\SDK\Core\Credentials\AuthToken;
use Bitrix24\SDK\Core\Credentials\Scope;
use Bitrix24\SDK\Core\Response\DTO\RenewedAuthToken;
use Carbon\CarbonImmutable;
use Override;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
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;

/**
* @internal
*/
#[CoversClass(Handler::class)]
class HandlerTest extends TestCase
{
Expand All @@ -44,25 +42,28 @@ class HandlerTest extends TestCase
private Bitrix24AccountRepositoryInterface $repository;
private TraceableEventDispatcher $eventDispatcher;

#[Test]
public function testRenewAuthTokenWithoutBitrix24UserId(): void
#[\Override]
protected function setUp(): void
{
$bitrix24Account = new Bitrix24Account(
Uuid::v7(),
1,
true,
Uuid::v7()->toRfc4122(),
Uuid::v7()->toRfc4122() . '-test.bitrix24.com',
Bitrix24AccountStatus::active,
new AuthToken('old_1', 'old_2', 3600),
new CarbonImmutable(),
new CarbonImmutable(),
1,
new Scope()
$entityManager = EntityManagerFactory::get();
$this->repository = new Bitrix24AccountRepository($entityManager);
$this->flusher = new Flusher($entityManager);
$this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
$this->handler = new Handler(
$this->eventDispatcher,
$this->repository,
$this->flusher,
new NullLogger()
);
}

#[Test]
public function testRenewAuthTokenWithoutBitrix24UserId(): void
{
$bitrix24Account = (new Bitrix24AccountBuilder())
->build()
;
$this->repository->save($bitrix24Account);

$this->flusher->flush();

$newAuthToken = new AuthToken('new_1', 'new_2', 3600);
Expand All @@ -78,28 +79,18 @@ public function testRenewAuthTokenWithoutBitrix24UserId(): void
)
)
);

$updated = $this->repository->getById($bitrix24Account->getId());
$this->assertEquals($newAuthToken->accessToken, $updated->getAuthToken()->accessToken);
$this->assertEquals($newAuthToken->refreshToken, $updated->getAuthToken()->refreshToken);

// на продлении токенов событий не бросаем
$this->assertCount(0, $this->eventDispatcher->getOrphanedEvents());
}


#[Override]
protected function setUp(): void
{
$entityManager = EntityManagerFactory::get();
$this->repository = new Bitrix24AccountRepository($entityManager);
$this->flusher = new Flusher($entityManager);
$this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
$this->handler = new Handler(
$this->eventDispatcher,
$this->repository,
$this->flusher,
new NullLogger()
$this->assertCount(
0,
$this->eventDispatcher->getOrphanedEvents(),
sprintf(
'get unexpected domain events count %s',
count($this->eventDispatcher->getOrphanedEvents()),
)
);
}
}
}

0 comments on commit 9ad0581

Please sign in to comment.