From 15711748ee76ff46a866c6f4432ae0b992be41c3 Mon Sep 17 00:00:00 2001
From: Yohan Giarelli <yohan@un-zero-un.fr>
Date: Mon, 13 Jan 2025 21:08:38 +0100
Subject: [PATCH] feat(symfony): Added symfony bundle

---
 .dockerignore                                 | 10 ++----
 .gitignore                                    | 10 ++----
 Dockerfile                                    | 18 +++++-----
 Makefile                                      |  2 +-
 composer.json                                 |  8 +++--
 phpunit.xml.dist                              |  2 +-
 .../DependencyInjection/FiniteExtension.php   | 23 ++++++++++++
 src/Extension/Symfony/Bundle/FiniteBundle.php | 10 ++++++
 tests/Extension/Symfony/ServiceTest.php       | 25 +++++++++++++
 .../Symfony/fixtures/app/AppKernel.php        | 35 +++++++++++++++++++
 .../Symfony/fixtures/app/bootstrap.php        |  7 ++++
 tests/bootstrap.php                           |  5 ---
 12 files changed, 120 insertions(+), 35 deletions(-)
 create mode 100644 src/Extension/Symfony/Bundle/DependencyInjection/FiniteExtension.php
 create mode 100644 src/Extension/Symfony/Bundle/FiniteBundle.php
 create mode 100644 tests/Extension/Symfony/ServiceTest.php
 create mode 100644 tests/Extension/Symfony/fixtures/app/AppKernel.php
 create mode 100644 tests/Extension/Symfony/fixtures/app/bootstrap.php
 delete mode 100644 tests/bootstrap.php

diff --git a/.dockerignore b/.dockerignore
index 4f79572..a878b84 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -1,8 +1,4 @@
-vendor
-build
-bin
-composer.phar
+tests/Extension/Symfony/fixtures/app/var/
+vendor/
+.phpunit.result.cache
 composer.lock
-cache.properties
-coverage.clover
-docs/_build
diff --git a/.gitignore b/.gitignore
index ff369a0..a878b84 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,10 +1,4 @@
+tests/Extension/Symfony/fixtures/app/var/
+vendor/
 .phpunit.result.cache
-vendor
-build
-bin
-composer.phar
 composer.lock
-cache.properties
-coverage.clover
-docs/_build
-.phpunit.result.cache
diff --git a/Dockerfile b/Dockerfile
index a846a7f..1418bb9 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -6,14 +6,12 @@ ARG DEPENDENCIES=highest
 
 RUN set -eux; \
     apk add --no-cache acl libzip; \
-    apk add --no-cache --virtual .build-deps ${PHPIZE_DEPS} zlib-dev libzip-dev linux-headers; \
+    apk add --no-cache --virtual .build-deps ${PHPIZE_DEPS} zlib-dev libzip-dev; \
     docker-php-ext-install zip; \
-    pecl install xdebug;\
-    docker-php-ext-enable xdebug; \
+    pecl install pcov;\
+    docker-php-ext-enable pcov; \
     apk del .build-deps;
 
-RUN echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
-
 COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
 
 WORKDIR /app
@@ -25,8 +23,8 @@ RUN set -eux; \
     if [ "${DEPENDENCIES}" = "lowest" ]; then COMPOSER_MEMORY_LIMIT=-1 composer update --prefer-lowest --no-interaction; fi; \
     if [ "${DEPENDENCIES}" = "highest" ]; then COMPOSER_MEMORY_LIMIT=-1 composer update --no-interaction; fi
 
-COPY ./examples /app/examples
-COPY ./src /app/src
-COPY ./tests /app/tests
-COPY ./phpunit.xml.dist /app/
-COPY ./psalm.xml /app/
+COPY --link ./examples /app/examples
+COPY --link ./src /app/src
+COPY --link ./tests /app/tests
+COPY --link ./phpunit.xml.dist /app/
+COPY --link ./psalm.xml /app/
diff --git a/Makefile b/Makefile
index 0c5a92e..d3e54f7 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ cli:
 	docker run -it --rm -v${PWD}:/app -w/app yohang/finite ash
 
 test:
-	docker run -it --rm -v${PWD}:/app -w/app yohang/finite php ./vendor/bin/phpunit
+	docker run -it --rm -v${PWD}:/app -w/app yohang/finite php ./vendor/bin/phpunit --coverage-text
 
 test_all_targets:
 	docker build -t yohang/finite:php-8.1 --build-arg PHP_VERSION=8.1 .
diff --git a/composer.json b/composer.json
index 9708cf2..f63af81 100644
--- a/composer.json
+++ b/composer.json
@@ -21,13 +21,15 @@
     ],
     "require": {
         "php": ">=8.1",
-        "symfony/property-access": "^6.1|^7.0"
+        "symfony/property-access": ">=5.4,<8"
     },
     "require-dev": {
         "phpunit/phpunit": "^10.5.40",
-        "symfony/var-dumper": "^6.1|^7.0",
+        "symfony/var-dumper": ">=5.4,<8",
         "twig/twig": "^3.4",
-        "vimeo/psalm": "dev-master@dev"
+        "vimeo/psalm": "dev-master@dev",
+        "symfony/http-kernel": ">=5.4,<8",
+        "symfony/framework-bundle": ">=5.4,<8"
     },
     "autoload": {
         "psr-4": {
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 2044d9f..5986202 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -2,7 +2,7 @@
 <!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
 <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          colors="true"
-         bootstrap="tests/bootstrap.php"
+         bootstrap="tests/Extension/Symfony/fixtures/app/bootstrap.php"
          xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd">
     <source>
         <include>
diff --git a/src/Extension/Symfony/Bundle/DependencyInjection/FiniteExtension.php b/src/Extension/Symfony/Bundle/DependencyInjection/FiniteExtension.php
new file mode 100644
index 0000000..39c5caf
--- /dev/null
+++ b/src/Extension/Symfony/Bundle/DependencyInjection/FiniteExtension.php
@@ -0,0 +1,23 @@
+<?php
+declare(strict_types=1);
+
+namespace Finite\Extension\Symfony\Bundle\DependencyInjection;
+
+use Finite\Extension\Twig\FiniteExtension as TwigExtension;
+use Finite\StateMachine;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Definition;
+use Symfony\Component\DependencyInjection\Extension\Extension;
+
+final class FiniteExtension extends Extension
+{
+    public function load(array $configs, ContainerBuilder $container): void
+    {
+        $container->addDefinitions(
+            [
+                StateMachine::class => (new Definition(StateMachine::class))->setPublic(true),
+                TwigExtension::class => new Definition(TwigExtension::class),
+            ]
+        );
+    }
+}
diff --git a/src/Extension/Symfony/Bundle/FiniteBundle.php b/src/Extension/Symfony/Bundle/FiniteBundle.php
new file mode 100644
index 0000000..a995b08
--- /dev/null
+++ b/src/Extension/Symfony/Bundle/FiniteBundle.php
@@ -0,0 +1,10 @@
+<?php
+declare(strict_types=1);
+
+namespace Finite\Extension\Symfony\Bundle;
+
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+
+final class FiniteBundle extends Bundle
+{
+}
diff --git a/tests/Extension/Symfony/ServiceTest.php b/tests/Extension/Symfony/ServiceTest.php
new file mode 100644
index 0000000..ae08da2
--- /dev/null
+++ b/tests/Extension/Symfony/ServiceTest.php
@@ -0,0 +1,25 @@
+<?php
+declare(strict_types=1);
+
+namespace Finite\Tests\Extension\Symfony;
+
+use Finite\Extension\Twig\FiniteExtension;
+use Finite\StateMachine;
+use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
+use Symfony\Bundle\FrameworkBundle\Test\TestContainer;
+
+class ServiceTest extends KernelTestCase
+{
+    public function test_services_are_registered(): void
+    {
+        /** @var TestContainer $container */
+        $container = static::getContainer();
+
+        $this->assertInstanceOf(StateMachine::class, $container->get(StateMachine::class));
+    }
+
+    protected static function getKernelClass(): string
+    {
+        return \Finite\Tests\Extension\Symfony\fixtures\app\AppKernel::class;
+    }
+}
diff --git a/tests/Extension/Symfony/fixtures/app/AppKernel.php b/tests/Extension/Symfony/fixtures/app/AppKernel.php
new file mode 100644
index 0000000..c1aa91b
--- /dev/null
+++ b/tests/Extension/Symfony/fixtures/app/AppKernel.php
@@ -0,0 +1,35 @@
+<?php
+declare(strict_types=1);
+
+namespace Finite\Tests\Extension\Symfony\fixtures\app;
+
+use Finite;
+use Symfony;
+use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
+use Symfony\Component\Config\Loader\LoaderInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\HttpKernel\Kernel;
+
+class AppKernel extends Kernel
+{
+    use MicroKernelTrait;
+
+    public function registerBundles(): iterable
+    {
+        return [
+            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
+            new Finite\Extension\Symfony\Bundle\FiniteBundle(),
+        ];
+    }
+
+    protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void
+    {
+        $c->prependExtensionConfig('framework', ['test' => true]);
+    }
+
+    public function getProjectDir(): string
+    {
+        return __DIR__;
+    }
+
+}
diff --git a/tests/Extension/Symfony/fixtures/app/bootstrap.php b/tests/Extension/Symfony/fixtures/app/bootstrap.php
new file mode 100644
index 0000000..b58eb5b
--- /dev/null
+++ b/tests/Extension/Symfony/fixtures/app/bootstrap.php
@@ -0,0 +1,7 @@
+<?php
+declare(strict_types=1);
+
+$loader = require __DIR__ . '/../../../../../vendor/autoload.php';
+require __DIR__.'/AppKernel.php';
+
+return $loader;
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
deleted file mode 100644
index 88abca9..0000000
--- a/tests/bootstrap.php
+++ /dev/null
@@ -1,5 +0,0 @@
-<?php
-
-$loader = require __DIR__.'/../vendor/autoload.php';
-
-$loader->add('Finite\Test', __DIR__);