Skip to content

Commit

Permalink
Merge pull request #23 from KarlsonComplete/add-docker-structure
Browse files Browse the repository at this point in the history
Правки
  • Loading branch information
mesilov authored Dec 1, 2024
2 parents e9817e1 + 57a3f51 commit a27c75e
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 39 deletions.
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 'testRenewAuthTokenWithoutBitrix24UserId' tests/Functional/Bitrix24Accounts/UseCase/RenewAuthToken/HandlerTest.php
docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testListReturnsPaginatedResults' tests/Functional/Bitrix24Accounts/FetcherTest.php

schema-drop:
docker-compose run --rm php-cli php bin/doctrine orm:schema-tool:drop --force
Expand Down
19 changes: 19 additions & 0 deletions src/AggregateRoot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Bitrix24\Lib;

use Bitrix24\SDK\Application\Contracts\Events\AggregateRootEventsEmitterInterface;
use Symfony\Contracts\EventDispatcher\Event;

class AggregateRoot implements AggregateRootEventsEmitterInterface
{
protected array $events = [];
public function emitEvents(): array
{
$events = $this->events;
$this->events = [];

return $events;
}

}
35 changes: 14 additions & 21 deletions src/Bitrix24Accounts/Entity/Bitrix24Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Bitrix24\Lib\Bitrix24Accounts\Entity;

use Bitrix24\Lib\AggregateRoot;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Events\Bitrix24AccountApplicationInstalledEvent;
Expand All @@ -30,21 +31,16 @@
use Symfony\Component\Uid\Uuid;
use Symfony\Contracts\EventDispatcher\Event;

class Bitrix24Account implements Bitrix24AccountInterface, AggregateRootEventsEmitterInterface
class Bitrix24Account extends AggregateRoot implements Bitrix24AccountInterface
{
private array $applicationScope;
private array|Scope $applicationScope;

private ?string $applicationToken = null;

private ?string $comment = null;

private AuthToken $authToken;

/**
* @var Event[]
*/
private array $events = [];

public function __construct(
private Uuid $id,
private readonly int $bitrix24UserId,
Expand All @@ -61,7 +57,8 @@ public function __construct(
bool $isEmitBitrix24AccountCreatedEvent = false
) {
$this->authToken = $authToken;
$this->applicationScope = $applicationScope->getScopeCodes();
$array = $applicationScope->getScopeCodes();
$this->applicationScope = $array;
$this->addAccountCreatedEventIfNeeded($isEmitBitrix24AccountCreatedEvent);
}

Expand Down Expand Up @@ -101,6 +98,11 @@ public function getStatus(): Bitrix24AccountStatus
return $this->status;
}

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

/**
* @throws InvalidArgumentException
*/
Expand Down Expand Up @@ -143,13 +145,11 @@ public function getApplicationVersion(): int
return $this->applicationVersion;
}

/**
* @throws UnknownScopeCodeException
*/
#[\Override]
public function getApplicationScope(): Scope
{
return new Scope($this->applicationScope);
// return $this->applicationScope;
}

/**
Expand Down Expand Up @@ -346,19 +346,12 @@ public function getComment(): ?string
return $this->comment;
}

/**
* @return Event[]
*/
#[\Override]
public function emitEvents(): array
public function getEvents(): array
{
$events = $this->events;
$this->events = [];

return $events;
return $this->events;
}

public function addAccountCreatedEventIfNeeded(bool $isEmitCreatedEvent): void
private function addAccountCreatedEventIfNeeded(bool $isEmitCreatedEvent): void
{
if ($isEmitCreatedEvent) {
// Создание события и добавление его в массив событий
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ public function __construct(
#[Override]
public function getById(Uuid $uuid): Bitrix24AccountInterface
{
$res = $this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid);
if (null === $res) {
$account = $this->getEntityManager()->getRepository(Bitrix24Account::class)->find($uuid);
if (null === $account || $account->getStatus() === Bitrix24AccountStatus::deleted) {
throw new Bitrix24AccountNotFoundException(
sprintf('bitrix24 account not found by id %s', $uuid->toRfc4122())
);
}

return $res;

return $account;
}

#[Override]
Expand Down Expand Up @@ -100,8 +101,8 @@ public function delete(Uuid $uuid): void
)
);
}

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

public function findAllActive(int|null $limit = null, int|null $offset = null): array
Expand Down
10 changes: 7 additions & 3 deletions src/Bitrix24Accounts/ReadModel/Fetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use Bitrix24\Lib\Bitrix24Accounts\Infrastructure\Doctrine\Bitrix24AccountRepository;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\Event\Subscriber\Paginate\Callback\CallbackPagination;
use Knp\Component\Pager\Pagination\PaginationInterface;
use Knp\Component\Pager\PaginatorInterface;

class Fetcher
{
public function __construct(
Expand All @@ -23,7 +23,7 @@ public function list(
): PaginationInterface
{

$queryBuilder = $this->em->getConnection()->createQueryBuilder()
/* $queryBuilder = $this->em->getConnection()->createQueryBuilder()
->select(
'b24account.id as id',
'b24account.status as status',
Expand All @@ -35,7 +35,11 @@ public function list(
)
->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");

return $this->paginator->paginate($queryBuilder, $page, $size);
return $this->paginator->paginate($query, $page, $size);
// return $this->paginator->paginate($queryBuilder->getSQL(), $page, $size);
}
}
4 changes: 3 additions & 1 deletion src/Bitrix24Accounts/UseCase/Uninstall/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Bitrix24\Lib\Bitrix24Accounts\UseCase\Uninstall;

use Bitrix24\Lib\AggregateRoot;
use Bitrix24\Lib\Services\Flusher;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountInterface;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Exceptions\Bitrix24AccountNotFoundException;
Expand All @@ -19,7 +20,7 @@ public function __construct(
private EventDispatcherInterface $eventDispatcher,
private Bitrix24AccountRepositoryInterface $bitrix24AccountRepository,
private Flusher $flusher,
private LoggerInterface $logger
private LoggerInterface $logger,
) {}

/**
Expand All @@ -41,6 +42,7 @@ public function handle(Command $command): void
$account->applicationUninstalled($command->applicationToken);
$this->bitrix24AccountRepository->save($account);
$this->flusher->flush();

foreach ($account->emitEvents() as $event) {
$this->eventDispatcher->dispatch($event);
}
Expand Down
19 changes: 13 additions & 6 deletions tests/Functional/Bitrix24Accounts/FetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Bitrix24\Lib\Bitrix24Accounts\ReadModel\Fetcher;
use Bitrix24\Lib\Services\Flusher;
use Bitrix24\Lib\Tests\EntityManagerFactory;
use Knp\Component\Pager\Event\Subscriber\Paginate\PaginationSubscriber;
use Knp\Component\Pager\Event\Subscriber\Sortable\SortableSubscriber;
use Knp\Component\Pager\Paginator;
use PHPUnit\Framework\TestCase;
use Doctrine\ORM\EntityManagerInterface;
Expand All @@ -16,8 +18,9 @@
use Bitrix24\Lib\Tests\Functional\Bitrix24Accounts\Builders\Bitrix24AccountBuilder;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Repository\Bitrix24AccountRepositoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Knp\Component\Pager\ArgumentAccess;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;
use Knp\Component\Pager\ArgumentAccess\RequestArgumentAccess;

class FetcherTest extends TestCase
{
Expand All @@ -34,9 +37,13 @@ protected function setUp(): void
{
$this->entityManager = EntityManagerFactory::get();
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber(new PaginationSubscriber());
$eventDispatcher->addSubscriber(new SortableSubscriber());
// dd(Request::createFromGlobals());
$requestStack = new RequestStack();
$argumentAccess = new ArgumentAccess\RequestArgumentAccess($requestStack);
$this->paginator = new Paginator($eventDispatcher, $argumentAccess);
// Request::createFromGlobals()
$accessor = new RequestArgumentAccess(new RequestStack());
$this->paginator = new Paginator($eventDispatcher, $accessor);
$this->fetcher = new Fetcher($this->entityManager, $this->paginator);
$this->flusher = new Flusher($this->entityManager);
$this->repository = new Bitrix24AccountRepository($this->entityManager);
Expand All @@ -55,14 +62,14 @@ public function testListReturnsPaginatedResults(): void
$page = 1;
$size = 10;
// Вызов метода list
/** @var PaginationInterface $result */
$result = $this->fetcher->list($page, $size);

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

// Проверка, что данные возвращаются корректно
$this->assertGreaterThan(0, count($result)); // Проверяем, что есть хотя бы одна запись
$this->assertGreaterThan(0, $result->count()); // Проверяем, что есть хотя бы одна запись
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@

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

use Bitrix24\Lib\AggregateRoot;
use Bitrix24\Lib\Bitrix24Accounts\Entity\Bitrix24Account;
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\Contracts\Bitrix24Accounts\Entity\Bitrix24AccountStatus;
use Bitrix24\SDK\Application\Contracts\Bitrix24Accounts\Events\Bitrix24AccountApplicationUninstalledEvent;
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;
Expand Down Expand Up @@ -65,10 +67,11 @@ public function testUninstallWithHappyPath(): void
$this->repository->save($bitrix24Account);
$this->flusher->flush();


$this->handler->handle(new Bitrix24Accounts\UseCase\Uninstall\Command($applicationToken));

$this->expectException(Bitrix24AccountNotFoundException::class);
$updated = $this->repository->getById($bitrix24Account->getId());

$this->assertEquals(
Bitrix24AccountStatus::deleted,
$updated->getStatus(),
Expand All @@ -89,15 +92,17 @@ public function testUninstallWithHappyPath(): void
#[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\Uninstall\Handler(
$this->eventDispatcher,
$this->repository,
$this->flusher,
new NullLogger()
new NullLogger(),
);
}
}

0 comments on commit a27c75e

Please sign in to comment.