Skip to content

Commit

Permalink
Merge pull request #25 from KarlsonComplete/add-docker-structure
Browse files Browse the repository at this point in the history
Update fetcher and flusher
  • Loading branch information
mesilov authored Dec 8, 2024
2 parents bd63bba + be68b22 commit 65413da
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 61 deletions.
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 'testListReturnsPaginatedResults' tests/Functional/Bitrix24Accounts/FetcherTest.php
docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testChangeDomainUrlWithHappyPath' tests/Functional/Bitrix24Accounts/UseCase/ChangeDomainUrl/HandlerTest.php

schema-drop:
docker-compose run --rm php-cli php bin/doctrine orm:schema-tool:drop --force
Expand Down
5 changes: 0 additions & 5 deletions src/Bitrix24Accounts/Entity/Bitrix24Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ public function getStatus(): Bitrix24AccountStatus
return $this->status;
}

public function setStatus(Bitrix24AccountStatus $bitrix24AccountStatus): void
{
$this->status = $bitrix24AccountStatus;
}

/**
* @throws InvalidArgumentException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public function delete(Uuid $uuid): void
);
}

$bitrix24Account->setStatus(Bitrix24AccountStatus::deleted);
$this->save($bitrix24Account);
}

Expand Down
24 changes: 10 additions & 14 deletions src/Bitrix24Accounts/ReadModel/Fetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\Pagination\PaginationInterface;
use Knp\Component\Pager\PaginatorInterface;

class Fetcher
{

public function __construct(
private readonly EntityManagerInterface $em,
private readonly PaginatorInterface $paginator
Expand All @@ -19,23 +19,19 @@ public function list(
int $page,
int $size
): PaginationInterface {
/* $queryBuilder = $this->em->getConnection()->createQueryBuilder()
$qb = $this->em->createQueryBuilder()
->select(
'b24account.id as id',
'b24account.status as status',
'b24account.member_id as member_id',
'b24account.domain_url as domain_url',
'b24account.application_version as application_version',
'b24account.created_at_utc as created_at',
'b24account.updated_at_utc as updated_at',
'b24account.memberId as member_id',
'b24account.domainUrl as domain_url',
'b24account.applicationVersion as application_version',
'b24account.createdAt as created_at_utc',
'b24account.updatedAt as updated_at_utc',
)
->from('bitrix24account', 'b24account')
->orderBy('b24account.created_at_utc', 'DESC');
*/
// var_dump($queryBuilder->getMaxResults());
$query = $this->em->createQuery('SELECT b24account FROM Bitrix24\Lib\Bitrix24Accounts\Entity\Bitrix24Account b24account');
->from('Bitrix24\Lib\Bitrix24Accounts\Entity\Bitrix24Account', 'b24account')
->orderBy('b24account.createdAt', 'DESC');

return $this->paginator->paginate($query, $page, $size);
// return $this->paginator->paginate($queryBuilder->getSQL(), $page, $size);
return $this->paginator->paginate($qb, $page, $size);
}
}
9 changes: 5 additions & 4 deletions src/Bitrix24Accounts/UseCase/ChangeDomainUrl/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
readonly class Handler
{
public function __construct(
private EventDispatcherInterface $eventDispatcher,
private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository,
private Flusher $flusher,
private LoggerInterface $logger
Expand All @@ -29,13 +28,15 @@ public function handle(Command $command): void
foreach ($accounts as $account) {
$account->changeDomainUrl($command->newDomainUrlHost);
$this->bitrix24AccountRepository->save($account);
$this->flusher->flush();
// $this->flusher->flush();
// todo выяснить почему он не видит объединение типов
// @phpstan-ignore-next-line
foreach ($account->emitEvents() as $event) {
/* foreach ($account->emitEvents() as $event) {

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)

No error to ignore is reported on line 34.

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)

No error to ignore is reported on line 34.
$this->eventDispatcher->dispatch($event);
}
}*/

}
$this->flusher->flush(...$accounts);

Check failure on line 39 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\Lib\AggregateRoot, Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface given.

Check failure on line 39 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\Lib\AggregateRoot, Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface given.

$this->logger->debug('Bitrix24Accounts.ChangeDomainUrl.Finish');
}
Expand Down
8 changes: 4 additions & 4 deletions src/Bitrix24Accounts/UseCase/InstallStart/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
readonly class Handler
{
public function __construct(
private EventDispatcherInterface $eventDispatcher,
private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository,
private Flusher $flusher,
private LoggerInterface $logger
Expand Down Expand Up @@ -44,11 +43,12 @@ public function handle(Command $command): void
true
);
$this->bitrix24AccountRepository->save($bitrix24Account);
$this->flusher->flush();
$this->flusher->flush($bitrix24Account);
// $this->flusher->flush();
/*foreach ($bitrix24Account->emitEvents() as $event) {
foreach ($bitrix24Account->emitEvents() as $event) {
$this->eventDispatcher->dispatch($event);
}
}*/

$this->logger->debug('Bitrix24Accounts.InstallStart.Finish');
}
Expand Down
7 changes: 3 additions & 4 deletions src/Bitrix24Accounts/UseCase/RenewAuthToken/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
readonly class Handler
{
public function __construct(
private EventDispatcherInterface $eventDispatcher,
private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository,
private Flusher $flusher,
private LoggerInterface $logger
Expand Down Expand Up @@ -45,10 +44,10 @@ public function handle(Command $command): void
$bitrix24Account->renewAuthToken($command->renewedAuthToken);

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

Check failure on line 47 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\Lib\AggregateRoot, Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface|Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface given.

Check failure on line 47 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\Lib\AggregateRoot, Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface|Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface given.
/* foreach ($bitrix24Account->emitEvents() as $event) {
$this->eventDispatcher->dispatch($event);
}
}*/

$this->logger->debug('Bitrix24Accounts.RenewAuthToken.finish');
}
Expand Down
8 changes: 2 additions & 6 deletions src/Bitrix24Accounts/UseCase/Uninstall/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
readonly class Handler
{
public function __construct(
private EventDispatcherInterface $eventDispatcher,
private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository,
private Flusher $flusher,
private LoggerInterface $logger,
Expand All @@ -40,12 +39,9 @@ public function handle(Command $command): void
foreach ($accounts as $account) {
$account->applicationUninstalled($command->applicationToken);
$this->bitrix24AccountRepository->save($account);
$this->flusher->flush();

foreach ($account->emitEvents() as $event) {
$this->eventDispatcher->dispatch($event);
}
}
$this->flusher->flush(...$accounts);

Check failure on line 43 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\Lib\AggregateRoot, Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface|Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface given.

Check failure on line 43 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\Lib\AggregateRoot, Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface|Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface given.


$this->logger->debug('Bitrix24Accounts.Uninstall.Finish');
}
Expand Down
23 changes: 19 additions & 4 deletions src/Services/Flusher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,29 @@

namespace Bitrix24\Lib\Services;

use Doctrine\ORM\EntityManagerInterface;

final readonly class Flusher
use Bitrix24\Lib\AggregateRoot;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
class Flusher
{
public function __construct(private EntityManagerInterface $em) {}
private $em;
private $eventDispatcher;
public function __construct(EntityManagerInterface $em,EventDispatcher $eventDispatcher) {
$this->em = $em;
$this->eventDispatcher = $eventDispatcher;
}

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

foreach ($roots as $root) {
$events = $root->emitEvents();
foreach ($events as $event) {
var_dump($event);
$this->eventDispatcher->dispatch($event);
}
}
}
}
9 changes: 2 additions & 7 deletions tests/Functional/Bitrix24Accounts/FetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;
use Knp\Component\Pager\ArgumentAccess\RequestArgumentAccess;

class FetcherTest extends TestCase
Expand All @@ -41,13 +40,10 @@ protected function setUp(): void
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber(new PaginationSubscriber());
$eventDispatcher->addSubscriber(new SortableSubscriber());
// dd(Request::createFromGlobals());
new RequestStack();
// Request::createFromGlobals()
$requestArgumentAccess = new RequestArgumentAccess(new RequestStack());
$this->paginator = new Paginator($eventDispatcher, $requestArgumentAccess);
$this->fetcher = new Fetcher($this->entityManager, $this->paginator);
$this->flusher = new Flusher($this->entityManager);
$this->flusher = new Flusher($this->entityManager,$eventDispatcher);
$this->repository = new Bitrix24AccountRepository($this->entityManager);
}

Expand All @@ -63,8 +59,7 @@ public function testListReturnsPaginatedResults(): void
$size = 10;
// Вызов метода list
$pagination = $this->fetcher->list($page, $size);
// var_dump($result->getItems());
// var_dump($result->count());

// Проверка, что результат является экземпляром PaginationInterface
$this->assertInstanceOf(PaginationInterface::class, $pagination);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class HandlerTest extends TestCase
protected function setUp(): void
{
$entityManager = EntityManagerFactory::get();
$eventDispatcher = new EventDispatcher();
$this->repository = new Bitrix24AccountRepository($entityManager);
$this->flusher = new Flusher($entityManager);
$this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
$this->flusher = new Flusher($entityManager,$eventDispatcher);
$this->eventDispatcher = new TraceableEventDispatcher($eventDispatcher, new Stopwatch());
$this->handler = new Bitrix24Accounts\UseCase\ChangeDomainUrl\Handler(
$this->eventDispatcher,
$this->repository,
$this->flusher,
new NullLogger()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ public function testFinishInstallationWithHappyPath(): void
protected function setUp(): void
{
$entityManager = EntityManagerFactory::get();
$eventDispatcher = new EventDispatcher();
$this->repository = new Bitrix24AccountRepository($entityManager);
$this->flusher = new Flusher($entityManager);
$this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
$this->flusher = new Flusher($entityManager,$eventDispatcher);
$this->eventDispatcher = new TraceableEventDispatcher($eventDispatcher, new Stopwatch());
$this->handler = new Bitrix24Accounts\UseCase\InstallFinish\Handler(
$this->eventDispatcher,
$this->repository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ class HandlerTest extends TestCase
protected function setUp(): void
{
$entityManager = EntityManagerFactory::get();
$eventDispatcher = new EventDispatcher();
$this->repository = new Bitrix24AccountRepository($entityManager);
$this->flusher = new Flusher($entityManager);
$this->flusher = new Flusher($entityManager,$eventDispatcher);
$this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
$this->handler = new Bitrix24Accounts\UseCase\InstallStart\Handler(
$this->eventDispatcher,
$this->repository,
$this->flusher,
new NullLogger()
Expand Down Expand Up @@ -156,6 +156,7 @@ public function testInstallStartHappyPath(): void
'Object not equals'
);

// var_dump($this->eventDispatcher->getOrphanedEvents());
$this->assertContains(
Bitrix24AccountCreatedEvent::class,
$this->eventDispatcher->getOrphanedEvents(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class HandlerTest extends TestCase
protected function setUp(): void
{
$entityManager = EntityManagerFactory::get();
$eventDispatcher = new EventDispatcher();
$this->repository = new Bitrix24AccountRepository($entityManager);
$this->flusher = new Flusher($entityManager);
$this->flusher = new Flusher($entityManager,$eventDispatcher);
$this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
$this->handler = new Handler(
$this->eventDispatcher,
$this->repository,
$this->flusher,
new NullLogger()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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 Carbon\CarbonImmutable;
use Override;
use PHPUnit\Framework\Attributes\CoversClass;
Expand All @@ -47,6 +48,10 @@ class HandlerTest extends TestCase

private TraceableEventDispatcher $eventDispatcher;

/**
* @throws InvalidArgumentException
* @throws Bitrix24AccountNotFoundException
*/
#[Test]
public function testUninstallWithHappyPath(): void
{
Expand Down Expand Up @@ -97,12 +102,12 @@ protected function setUp(): void
{

$entityManager = EntityManagerFactory::get();
$eventDispatcher = new EventDispatcher();
$this->repository = new Bitrix24AccountRepository($entityManager);
$this->flusher = new Flusher($entityManager);
$this->flusher = new Flusher($entityManager, $eventDispatcher);
$this->eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());

$this->handler = new Bitrix24Accounts\UseCase\Uninstall\Handler(
$this->eventDispatcher,
$this->repository,
$this->flusher,
new NullLogger(),
Expand Down

0 comments on commit 65413da

Please sign in to comment.