diff --git a/Makefile b/Makefile index 03001c5..0e4c481 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ ENV_LOCAL := $(PWD)/.env.local include $(ENV) -include $(ENV_LOCAL) + default: @echo "make needs target:" @egrep -e '^\S+' ./Makefile | grep -v default | sed -r 's/://' | sed -r 's/^/ - /' @@ -96,10 +97,15 @@ test-run-functional: debug-print-env 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 bin/doctrine orm:schema-tool:update --dump-sql - docker-compose run --rm php-cli php vendor/bin/phpunit --testsuite=functional_tests --display-warnings --testdox + docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --testsuite=functional_tests --display-warnings --testdox + +# Запустить один функциональный тест с дебагером +run-one-functional-test: debug-print-env + docker-compose run --rm php-cli php -dxdebug.start_with_request=yes vendor/bin/phpunit --filter 'testFindByApplicationToken' tests/Functional/Bitrix24Accounts/Infrastructure/Doctrine/Bitrix24AccountRepositoryTest.php schema-drop: docker-compose run --rm php-cli php bin/doctrine orm:schema-tool:drop --force schema-create: - docker-compose run --rm php-cli php bin/doctrine orm:schema-tool:create \ No newline at end of file + docker-compose run --rm php-cli php bin/doctrine orm:schema-tool:create + diff --git a/docker-compose.yaml b/docker-compose.yaml index beb38fc..fb520fb 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -6,6 +6,10 @@ services: - database volumes: - .:/var/www/html + extra_hosts: + - "host.docker.internal:host-gateway" + environment: + PHP_IDE_CONFIG: "serverName=docker-cli" working_dir: /var/www/html database: image: postgres:${POSTGRES_VERSION:-16}-alpine diff --git a/docker/php-cli/Dockerfile b/docker/php-cli/Dockerfile index 256b9c8..a154dfd 100644 --- a/docker/php-cli/Dockerfile +++ b/docker/php-cli/Dockerfile @@ -1,9 +1,17 @@ FROM php:8.3-cli-alpine -RUN apk add unzip libpq-dev git icu-dev \ +RUN apk add unzip libpq-dev git icu-dev autoconf build-base linux-headers \ && docker-php-ext-install bcmath pdo pdo_pgsql intl \ && docker-php-ext-enable bcmath pdo pdo_pgsql intl +RUN pecl install xdebug \ + && docker-php-ext-enable xdebug \ + && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "xdebug.client_host = host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \ + && echo "xdebug.client_port = 9003" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini + +RUN git config --global --add safe.directory /var/www/html + RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/bin --filename=composer --quiet -ENV COMPOSER_ALLOW_SUPERUSER 1 +ENV COMPOSER_ALLOW_SUPERUSER=1 diff --git a/tests/EntityManagerFactory.php b/tests/EntityManagerFactory.php index 2475265..783cae0 100644 --- a/tests/EntityManagerFactory.php +++ b/tests/EntityManagerFactory.php @@ -11,13 +11,14 @@ use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Exception\ORMException; use Doctrine\ORM\OptimisticLockException; +use Monolog\Logger; use Symfony\Bridge\Doctrine\Types\UuidType; use Doctrine\DBAL\DriverManager; use Doctrine\ORM\EntityManager; use Doctrine\ORM\ORMSetup; use Doctrine\DBAL\Types\Type; - - +use Monolog\Handler\StreamHandler; +use Monolog\Level; class EntityManagerFactory { /** @@ -26,54 +27,62 @@ class EntityManagerFactory * @throws ORMException * @throws Exception */ + + private static ?EntityManager $entityManager = null; + public static function get(): EntityManagerInterface { /* $paths = [ dirname(__DIR__) . '/src/Bitrix24Accounts/Entity' ];*/ - $paths = [ - dirname(__DIR__) . '/config/xml' - ]; - $isDevMode = true; - if (!array_key_exists('DATABASE_HOST', $_ENV)) { - throw new WrongConfigurationException('DATABASE_HOST not defined in $_ENV'); - } + if (self::$entityManager === null) { + $paths = [ + dirname(__DIR__) . '/config/xml' + ]; + $isDevMode = true; - if (!array_key_exists('DATABASE_USER', $_ENV)) { - throw new WrongConfigurationException('DATABASE_USER not defined in $_ENV'); - } + if (!array_key_exists('DATABASE_HOST', $_ENV)) { + throw new WrongConfigurationException('DATABASE_HOST not defined in $_ENV'); + } - if (!array_key_exists('DATABASE_PASSWORD', $_ENV)) { - throw new WrongConfigurationException('DATABASE_PASSWORD not defined in $_ENV'); - } + if (!array_key_exists('DATABASE_USER', $_ENV)) { + throw new WrongConfigurationException('DATABASE_USER not defined in $_ENV'); + } - if (!array_key_exists('DATABASE_NAME', $_ENV)) { - throw new WrongConfigurationException('DATABASE_NAME not defined in $_ENV'); - } + if (!array_key_exists('DATABASE_PASSWORD', $_ENV)) { + throw new WrongConfigurationException('DATABASE_PASSWORD not defined in $_ENV'); + } - $connectionParams = [ - 'driver' => 'pdo_pgsql', - 'host' => $_ENV['DATABASE_HOST'], - 'user' => $_ENV['DATABASE_USER'], - 'password' => $_ENV['DATABASE_PASSWORD'], - 'dbname' => $_ENV['DATABASE_NAME'], - ]; - if (!Type::hasType(UuidType::NAME)) { - Type::addType(UuidType::NAME, UuidType::class); - } + if (!array_key_exists('DATABASE_NAME', $_ENV)) { + throw new WrongConfigurationException('DATABASE_NAME not defined in $_ENV'); + } - if (!Type::hasType('carbon_immutable')) { - Type::addType('carbon_immutable', CarbonImmutableType::class); - } + $connectionParams = [ + 'driver' => 'pdo_pgsql', + 'host' => $_ENV['DATABASE_HOST'], + 'user' => $_ENV['DATABASE_USER'], + 'password' => $_ENV['DATABASE_PASSWORD'], + 'dbname' => $_ENV['DATABASE_NAME'], + ]; + + if (!Type::hasType(UuidType::NAME)) { + Type::addType(UuidType::NAME, UuidType::class); + } - // $configuration = ORMSetup::createAttributeMetadataConfiguration($paths, $isDevMode); - $configuration = ORMSetup::createXMLMetadataConfiguration($paths, $isDevMode); - $connection = DriverManager::getConnection($connectionParams, $configuration); - $entityManager = new EntityManager($connection, $configuration); - // todo разобраться, почему так, без этого объекты оставались в кеше и при find мы получали старые значения - $entityManager->clear(); - $entityManager->flush(); - return $entityManager; + if (!Type::hasType('carbon_immutable')) { + Type::addType('carbon_immutable', CarbonImmutableType::class); + } + + // $configuration = ORMSetup::createAttributeMetadataConfiguration($paths, $isDevMode); + $configuration = ORMSetup::createXMLMetadataConfiguration($paths, $isDevMode); + // $log = new Logger('name'); + // $log->pushHandler(new StreamHandler('log.txt', Level::Debug)); + + // $configuration->setMiddlewares([new \Doctrine\DBAL\Logging\Middleware($log)]); + $connection = DriverManager::getConnection($connectionParams, $configuration); + self::$entityManager = new EntityManager($connection, $configuration); + } + return self::$entityManager; } } \ No newline at end of file