Skip to content

Commit

Permalink
Refactor Docker setup and Bitrix24Account entity
Browse files Browse the repository at this point in the history
Removed custom Docker configurations for nginx, PHP-FPM, and PostgreSQL to streamline the Docker environment. Introduced a centralized environment configuration using .env and Symfony Dotenv. Additionally, refactored the Bitrix24Account entity and its repository to enhance code maintainability and align with best practices.

Signed-off-by: mesilov <[email protected]>
  • Loading branch information
mesilov committed Oct 6, 2024
1 parent 6d8d5bd commit 85dafa1
Show file tree
Hide file tree
Showing 18 changed files with 379 additions and 162 deletions.
18 changes: 18 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#
# * .env contains default values for the environment variables needed by the app
# * .env.local uncommitted file with local overrides
# * .env.$APP_ENV committed environment-specific defaults
# * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
#
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
#
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration
DATABASE_HOST=bitrix24-php-lib-test-database
DATABASE_USER=
DATABASE_PASSWORD=
DATABASE_NAME=
67 changes: 20 additions & 47 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,8 @@ default:
@echo "make needs target:"
@egrep -e '^\S+' ./Makefile | grep -v default | sed -r 's/://' | sed -r 's/^/ - /'

# linters
lint-phpstan:
vendor/bin/phpstan --memory-limit=1G analyse
lint-rector:
vendor/bin/rector process --dry-run
lint-rector-fix:
vendor/bin/rector process

# unit tests
test-unit:
vendor/bin/phpunit --testsuite unit_tests --display-warnings

test-integration:
vendor/bin/phpunit --testsuite integration_tests --display-warnings


# docker file

docker-up:
docker compose up --build -d

docker-down:
docker compose down --remove-orphans

#======================================
restart: down up
%:
@: # silence

init:
@echo "remove all containers"
Expand All @@ -47,17 +23,14 @@ init:
docker-compose build
@echo "install dependencies"
docker-compose run --rm php-cli composer install
# @echo "run database migrations…"
# docker-compose run --rm php-cli php bin/doctrine doctrine:migrations:migrate --no-interaction
@echo "change owner of var folder for access from container"
docker-compose run --rm php-cli chown -R www-data:www-data /var/www/html/var/
@echo "run application…"
docker-compose up -d


up:
@echo "run application…"
docker-compose up -d
docker-compose up --build -d

down:
@echo "stop application and remove containers"
Expand All @@ -67,42 +40,42 @@ down-clear:
@echo "stop application and remove containers with volumes"
docker-compose down -v --remove-orphans

restart: down up

# работа с контейнерами
php-cli-bash:
docker-compose run --rm php-cli sh $(filter-out $@,$(MAKECMDGOALS))

# работа с composer
composer-install:
@echo "install dependencies…"
docker-compose run --rm php-cli composer install

composer-update:
@echo "update dependencies…"
docker-compose run --rm php-cli composer update

composer-dumpautoload:
docker-compose run --rm php-cli composer dumpautoload
# вызов composer с любыми параметрами
# Примеры:
# make composer install
# make composer "install --no-dev"
composer:
docker-compose run --rm php-cli composer $(filter-out $@,$(MAKECMDGOALS))

%:
@: # silence

dev-dump-cache:
composer dumpautoload

#======================================
cli-bash:
docker-compose run --rm php-cli sh $(filter-out $@,$(MAKECMDGOALS))

# static code analysis
test-run-phpstan:
# linters
lint-phpstan:
docker-compose run --rm php-cli php vendor/bin/phpstan analyse --memory-limit 2G
lint-rector:
docker-compose run --rm php-cli php vendor/bin/rector process --dry-run
lint-rector-fix:
docker-compose run --rm php-cli php vendor/bin/rector process

# unit-tests
test-run-unit-tests:
docker-compose run --rm php-cli php vendor/bin/phpunit --testsuite=unit_tests --testdox
test-run-unit:
docker-compose run --rm php-cli php vendor/bin/phpunit --testsuite=unit_tests --display-warnings --testdox

# functional-tests, work with test database
test-run-functional-tests:
test-run-functional:
docker-compose run --rm php-cli php bin/doctrine orm:schema-tool:drop --force
docker-compose run --rm php-cli php bin/doctrine orm:schema-tool:create
docker-compose run --rm php-cli php vendor/bin/phpunit --testsuite=functional --testdox
13 changes: 13 additions & 0 deletions bin/doctrine
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env php
<?php

use Bitrix24\SDK\Lib\Tests\EntityManagerFactory;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;

require_once 'tests/bootstrap.php';

ConsoleRunner::run(
new SingleManagerProvider(EntityManagerFactory::get()),
[]
);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"fakerphp/faker": "^1",
"phpstan/phpstan": "^1",
"phpunit/phpunit": "^11",
"doctrine/migrations":"^3",
"psalm/phar": "^5",
"rector/rector": "^1",
"roave/security-advisories": "dev-master",
Expand Down
19 changes: 19 additions & 0 deletions config/cli-config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

require 'vendor/autoload.php';

use Doctrine\DBAL\DriverManager;
use Doctrine\Migrations\Configuration\Connection\ExistingConnection;
use Doctrine\Migrations\Configuration\Migration\PhpFile;
use Doctrine\Migrations\DependencyFactory;

$config = new PhpFile('migrations.php');

$conn = DriverManager::getConnection(
[
'driver' => 'pdo_pgsql',
'memory' => true,
'dbname' => 'b24AccountTest',
]);

return DependencyFactory::fromConnection($config, new ExistingConnection($conn));
31 changes: 4 additions & 27 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,45 +1,22 @@
services:
php-fpm:
build:
context: ./docker/php-fpm
volumes:
- ./:/var/www/bitrix24-php-lib
nginx:
build:
context: ./docker/nginx
volumes:
- ./:/var/www/bitrix24-php-lib
ports:
- "8081:80"
depends_on:
- php-fpm
postgres-container:
image: postgres
build:
context: ./docker/postgres
ports:
- "5432:5432"
volumes:
- ./docker/postgres/data:/var/lib/postgresql/data
restart: always
php-cli:
build:
context: ./docker/php-cli
links:
- b24-account-test-database
- database
volumes:
- .:/var/www/html
working_dir: /var/www/html
b24-account-test-database:
image: postgres:${POSTGRES_VERSION:-15}-alpine
database:
image: postgres:${POSTGRES_VERSION:-16}-alpine
restart: unless-stopped
environment:
POSTGRES_DB: ${POSTGRES_DB:-b24AccountTest}
# You should definitely change the password in production
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-b24AccountTest}
POSTGRES_USER: ${POSTGRES_USER:-b24AccountTest}
PGDATA: "/var/lib/postgresql/data/pgdata"
container_name: b24-account-test-database
container_name: bitrix24-php-lib-test-database
ports:
- '5432:5432'
expose:
Expand Down
5 changes: 0 additions & 5 deletions docker/nginx/Dockerfile

This file was deleted.

20 changes: 0 additions & 20 deletions docker/nginx/conf.d/nginx.conf

This file was deleted.

13 changes: 0 additions & 13 deletions docker/php-fpm/Dockerfile

This file was deleted.

12 changes: 0 additions & 12 deletions docker/postgres/Dockerfile

This file was deleted.

17 changes: 0 additions & 17 deletions index.php

This file was deleted.

20 changes: 20 additions & 0 deletions migrations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

return [
'table_storage' => [
'table_name' => 'b24AccountsDoctrineMigrationVersions',
'version_column_name' => 'version',
'version_column_length' => 1024,
'executed_at_column_name' => 'executed_at',
'execution_time_column_name' => 'execution_time',
],
'migrations_paths' => [
'B24io\Core\Bitrix24\Account\Migrations' => '/migrations/',
],
'all_or_nothing' => true,
'transactional' => true,
'check_database_platform' => true,
'organize_migrations' => 'none',
'connection' => null,
'em' => null,
];
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<testsuite name="unit_tests">
<directory>./tests/Unit</directory>
</testsuite>
<testsuite name="integration_tests">
<directory>./tests/Integration</directory>
<testsuite name="functional_tests">
<directory>./tests/Functional</directory>
</testsuite>
</testsuites>
<source>
Expand Down
31 changes: 13 additions & 18 deletions src/Bitrix24Accounts/Entity/Bitrix24Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,13 @@
use Carbon\CarbonImmutable;
use Override;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Serializer\Annotation\Ignore;
use Symfony\Component\Serializer\Annotation\SerializedName;
use Symfony\Component\Uid\Uuid;
use Doctrine\ORM\Mapping as ORM;

class Bitrix24Account implements Bitrix24AccountInterface
{
#[ORM\Id]
#[ORM\Column(type: UuidType::NAME, unique: true)]
private readonly Uuid $id;
#[ORM\Column(name: 'b24_user_id', type: 'integer', nullable: false)]
#[SerializedName('b24_user_id')]
private readonly int $bitrix24UserId;
/** bitrix24 portal unique id */
#[ORM\Column(name: 'member_id', type: 'string', nullable: false)]
#[SerializedName('member_id')]
private readonly string $memberId;

private string $accessToken;

private string $refreshToken;
Expand All @@ -52,23 +42,28 @@ class Bitrix24Account implements Bitrix24AccountInterface
private ?string $comment = null;

public function __construct(
Uuid $id,
int $bitrix24UserId,
#[ORM\Id]
#[ORM\Column(type: UuidType::NAME, unique: true)]
private readonly Uuid $id,
#[ORM\Column(name: 'b24_user_id', type: 'integer', nullable: false)]
#[SerializedName('b24_user_id')]
private readonly int $bitrix24UserId,
private readonly bool $isBitrix24UserAdmin,
string $memberId,
/** bitrix24 portal unique id */
#[ORM\Column(name: 'member_id', type: 'string', nullable: false)]
#[SerializedName('member_id')]
private readonly string $memberId,
private string $domainUrl,
private Bitrix24AccountStatus $accountStatus,
AuthToken $authToken,
#[ORM\Column(name: 'created_at_utc', type: 'carbon_immutable', precision: 3, nullable: false)]
#[Ignore]
private readonly CarbonImmutable $createdAt,
private CarbonImmutable $updatedAt,
private int $applicationVersion,
Scope $applicationScope,
)
{
$this->id = $id;
$this->bitrix24UserId = $bitrix24UserId;
$this->memberId = $memberId;

$this->accessToken = $authToken->accessToken;
$this->refreshToken = $authToken->refreshToken;
$this->expires = $authToken->expires;
Expand Down
Loading

0 comments on commit 85dafa1

Please sign in to comment.