Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4 from smartbooster/2_bundle_configuration
Browse files Browse the repository at this point in the history
#2 Setup Bundle Configuration.php
  • Loading branch information
mathieu-ducrot authored Jun 25, 2021
2 parents d8536d7 + 2a60013 commit e1bfa3e
Show file tree
Hide file tree
Showing 21 changed files with 306 additions and 6 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@ jobs:
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install
- name: Install Composer dependencies
run: composer install --prefer-dist

# —— QA ✔️‍️ ———————————————————————————————————————————————————————————————
- name: Qualimetry
run: make qa

# —— Tests 🧪 ————————————————————————————————————————————————————————————
- name: Phpunit tests
run: XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-clover coverage.xml
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

- name: "Upload coverage to Codecov"
uses: codecov/codecov-action@v1
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ composer.lock
/phpmetrics/
coverage.*
.php_cs.cache
.phpunit.result.cache
/var/
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,16 @@ metrics: ## Build static analysis from the php in src. Repports available in ./p

.PHONY: phpstan
phpstan: ## Launch PHP Static Analysis
vendor/bin/phpstan analyse src --level=6
vendor/bin/phpstan analyse src tests --level=6 -c phpstan.neon

.PHONY: qa qualimetry
qa: qualimetry ## Launch all qualimetry rules
qualimetry: checkstyle lint-php cpd composer-validate metrics phpstan

# ====================
## Testing
phpunit:
vendor/bin/phpunit

phpunit-coverage:
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[![License](http://poser.pugx.org/smartbooster/parameter-bundle/license)](https://packagist.org/packages/smartbooster/parameter-bundle)

![CI workflow](https://github.com/smartbooster/parameter-bundle/actions/workflows/ci.yml/badge.svg)
[![codecov](https://codecov.io/gh/smartbooster/parameter-bundle/branch/master/graph/badge.svg?token=QQZPRVXGL8)](https://codecov.io/gh/smartbooster/parameter-bundle)

## Installation

Expand Down
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@
"homepage": "https://www.smartbooster.io"
}
],
"require": {
"php": "^7.4",
"symfony/http-kernel": "^4.4",
"symfony/config": "^4.4",
"symfony/yaml": "^4.4",
"symfony/dependency-injection": "^4.4"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.6",
"sebastian/phpcpd": "^6.0",
"phpmetrics/phpmetrics": "^2.7",
"phpstan/phpstan": "^0.12.90"
"phpstan/phpstan": "^0.12.90",
"phpunit/phpunit": "^9.5",
"phpstan/phpstan-symfony": "^0.12.37",
"symfony/framework-bundle": "^4.4"
},
"autoload": {
"psr-4": {
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
includes:
- vendor/phpstan/phpstan-symfony/extension.neon
- vendor/phpstan/phpstan-symfony/rules.neon

parameters:
checkMissingIterableValueType: false
25 changes: 25 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="./vendor/autoload.php">
<php>
<ini name="error_reporting" value="-1"/>
<ini name="memory_limit" value="-1"/>
</php>
<testsuites>
<testsuite name="Test suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory>./src</directory>
</include>
<exclude>
<directory>./src/SmartParameterBundle.php</directory>
</exclude>
</coverage>
</phpunit>
41 changes: 41 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Smart\ParameterBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* @author Mathieu Ducrot <[email protected]>
*/
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('smart_parameter');
$root = $treeBuilder->getRootNode();

$root
->children()
->append($this->getParametersNode())
->end()
;

return $treeBuilder;
}

private function getParametersNode(): NodeDefinition
{
return (new TreeBuilder('parameters'))->getRootNode()
->requiresAtLeastOneElement()
->useAttributeAsKey('name')
->arrayPrototype()
->children()
->scalarNode('value')->isRequired()->end()
->scalarNode('help')->end()
->end()
->end()
;
}
}
23 changes: 23 additions & 0 deletions src/DependencyInjection/SmartParameterExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Smart\ParameterBundle\DependencyInjection;

use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @author Mathieu Ducrot <[email protected]>
*/
class SmartParameterExtension extends Extension
{
/**
* @param array<array> $configs
* @param ContainerBuilder $container
* @throws \Exception
* @return void
*/
public function load(array $configs, ContainerBuilder $container)
{
$this->processConfiguration(new Configuration(), $configs);
}
}
16 changes: 16 additions & 0 deletions src/SmartParameterBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Smart\ParameterBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* @author Mathieu Ducrot <[email protected]>
*/
class SmartParameterBundle extends Bundle
{
public function getPath(): string
{
return \dirname(__DIR__);
}
}
3 changes: 0 additions & 3 deletions src/tmp.php

This file was deleted.

Empty file removed tests/.gitkeep
Empty file.
26 changes: 26 additions & 0 deletions tests/App/AppKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Smart\ParameterBundle\Tests\App;

use Smart\ParameterBundle\SmartParameterBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;

/**
* @author Mathieu Ducrot <[email protected]>
*/
class AppKernel extends Kernel
{
public function registerBundles()
{
return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new SmartParameterBundle(),
];
}

public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(__DIR__ . '/../fixtures/config/full.yml');
}
}
26 changes: 26 additions & 0 deletions tests/App/AppKernelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Smart\ParameterBundle\Tests\App;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* @author Mathieu Ducrot <[email protected]>
*
* vendor/bin/phpunit tests/App/AppKernelTest.php
*/
class AppKernelTest extends WebTestCase
{
protected static function getKernelClass()
{
return AppKernel::class;
}

public function testBootingKernel(): void
{
self::bootKernel();

$this->assertInstanceOf(KernelInterface::class, self::$kernel);
}
}
84 changes: 84 additions & 0 deletions tests/DependencyInjection/DependencyInjectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Smart\ParameterBundle\Tests\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Smart\ParameterBundle\SmartParameterBundle;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\FileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

/**
* @author Mathieu Ducrot <[email protected]>
*
* vendor/bin/phpunit tests/DependencyInjection/DependencyInjectionTest.php
*/
class DependencyInjectionTest extends TestCase
{
private ContainerBuilder $container;

protected function setUp(): void
{
$bundle = new SmartParameterBundle();
$this->container = new ContainerBuilder();

$this->container->setParameter('kernel.debug', true);
$this->container->setParameter('kernel.bundles', [
'FrameworkBundle' => \Symfony\Bundle\FrameworkBundle\FrameworkBundle::class,
]);
$this->container->setParameter('kernel.environment', 'test');

$this->container->registerExtension($bundle->getContainerExtension());
$bundle->build($this->container);
}

/**
* @dataProvider invalidConfigurationProvider
*/
public function testInvalidConfigurationParsing(string $resource, string $message): void
{
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage($message);

$loader = new YamlFileLoader($this->container, new FileLocator(__DIR__ . '/../fixtures/config/'));
$loader->load($resource . ".yml");
$this->container->compile();
}

public function invalidConfigurationProvider(): array
{
return [
'invalid_no_parameter_defined' => [
'ressource' => 'invalid_no_parameter_defined',
'message' => 'The path "smart_parameter.parameters" should have at least 1 element(s) defined.',
],
'invalid_missing_parameter_value' => [
'ressource' => 'invalid_missing_parameter_value',
'message' => 'The child node "value" at path "smart_parameter.parameters.parameter_without_value" must be configured.',
],
];
}

/**
* @dataProvider configurationProvider
*/
public function testValidConfigurationParsing(string $resource): void
{
$loader = new YamlFileLoader($this->container, new FileLocator(__DIR__ . '/../fixtures/config/'));
$loader->load($resource . ".yml");
$this->container->compile();
// This assertion is already true with the setUp but it valid the fact that the container did compile with the given configuration
$this->assertNotNull($this->container->getExtension("smart_parameter"));
}

public function configurationProvider(): array
{
return [
'full' => ['full'],
'minimal' => ['minimal'],
'none' => ['none'],
];
}
}
11 changes: 11 additions & 0 deletions tests/fixtures/config/full.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Full config fixture
smart_parameter:
parameters:
# Textual paramter
dummy_support_emails:
value: "[email protected], [email protected]"
help: "This parameter is used by the mailer for support recipients. Format : Separate email by comma."
# Number parameter
dummy_homepage_cache_duration:
value: 3600
help: "This parameter is used by the backend to set the duration of cache applied to the Homepage. Set the duration in second."
4 changes: 4 additions & 0 deletions tests/fixtures/config/invalid_missing_parameter_value.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#
smart_parameter:
parameters:
parameter_without_value:
3 changes: 3 additions & 0 deletions tests/fixtures/config/invalid_no_parameter_defined.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#
smart_parameter:
parameters:
5 changes: 5 additions & 0 deletions tests/fixtures/config/minimal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Config with only minimal parameter configuration
smart_parameter:
parameters:
dummy_parameter:
value: "some value"
2 changes: 2 additions & 0 deletions tests/fixtures/config/none.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Empty config
smart_parameter: ~

0 comments on commit e1bfa3e

Please sign in to comment.