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

Commit

Permalink
#2 Add ParameterProvider service
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieu-ducrot committed Jul 2, 2021
1 parent e1bfa3e commit 1db2615
Show file tree
Hide file tree
Showing 19 changed files with 369 additions and 64 deletions.
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"symfony/http-kernel": "^4.4",
"symfony/config": "^4.4",
"symfony/yaml": "^4.4",
"symfony/dependency-injection": "^4.4"
"symfony/dependency-injection": "^4.4",
"doctrine/orm": "^2.9",
"doctrine/doctrine-bundle": "^2.4"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.6",
Expand All @@ -26,7 +28,12 @@
"phpstan/phpstan": "^0.12.90",
"phpunit/phpunit": "^9.5",
"phpstan/phpstan-symfony": "^0.12.37",
"symfony/framework-bundle": "^4.4"
"symfony/framework-bundle": "^4.4",
"phpstan/phpstan-doctrine": "^0.12.39",
"dama/doctrine-test-bundle": "^6.6",
"liip/test-fixtures-bundle": "^1.11",
"theofidry/alice-data-fixtures": "^1.4",
"doctrine/doctrine-fixtures-bundle": "^3.4"
},
"autoload": {
"psr-4": {
Expand Down
Empty file removed config/.gitkeep
Empty file.
10 changes: 10 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
smart_parameter.parameter_provider:
class: Smart\ParameterBundle\Provider\ParameterProvider
public: true
arguments:
- '@Doctrine\ORM\EntityManagerInterface'

Smart\ParameterBundle\Provider\ParameterProvider:
public: false
alias: smart_parameter.parameter_provider
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
includes:
- vendor/phpstan/phpstan-symfony/extension.neon
- vendor/phpstan/phpstan-symfony/rules.neon
- vendor/phpstan/phpstan-doctrine/extension.neon
- vendor/phpstan/phpstan-doctrine/rules.neon

parameters:
checkMissingIterableValueType: false
4 changes: 4 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<php>
<ini name="error_reporting" value="-1"/>
<ini name="memory_limit" value="-1"/>
<server name="KERNEL_CLASS" value="Smart\ParameterBundle\Tests\AppKernel"/>
</php>
<testsuites>
<testsuite name="Test suite">
Expand All @@ -22,4 +23,7 @@
<directory>./src/SmartParameterBundle.php</directory>
</exclude>
</coverage>
<extensions>
<extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension" />
</extensions>
</phpunit>
5 changes: 5 additions & 0 deletions src/DependencyInjection/SmartParameterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Smart\ParameterBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;

Expand All @@ -19,5 +21,8 @@ class SmartParameterExtension extends Extension
public function load(array $configs, ContainerBuilder $container)
{
$this->processConfiguration(new Configuration(), $configs);

$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
$loader->load('services.yaml');
}
}
94 changes: 94 additions & 0 deletions src/Entity/Parameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Smart\ParameterBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @author Mathieu Ducrot <[email protected]>
*
* @ORM\Table(name="smart_parameter")
* @ORM\Entity(repositoryClass="Smart\ParameterBundle\Repository\ParameterRepository")
*/
class Parameter
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(name="id", type="integer")
*/
private int $id;

/**
* @ORM\Column(name="code", type="string", nullable=false, unique=true)
*/
private string $code;

/**
* @ORM\Column(name="value", type="text", nullable=false)
*/
private string $value;

/**
* @ORM\Column(name="help", type="text", nullable=true)
*/
private ?string $help;

public function __toString()
{
return (string) $this->getCode();
}

public function getId(): ?int
{
return $this->id;
}

/**
* @return string
*/
public function getCode(): string
{
return $this->code;
}

/**
* @param string $code
*/
public function setCode(string $code): void
{
$this->code = $code;
}

/**
* @return string
*/
public function getValue(): string
{
return $this->value;
}

/**
* @param string $value
*/
public function setValue(string $value): void
{
$this->value = $value;
}

/**
* @return string|null
*/
public function getHelp(): ?string
{
return $this->help;
}

/**
* @param string|null $help
*/
public function setHelp(?string $help): void
{
$this->help = $help;
}
}
51 changes: 51 additions & 0 deletions src/Provider/ParameterProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Smart\ParameterBundle\Provider;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityNotFoundException;
use Smart\ParameterBundle\Entity\Parameter;

/**
* @author Mathieu Ducrot <[email protected]>
*/
class ParameterProvider
{
private EntityManagerInterface $entityManager;
private array $parameters;

public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
$this->parameters = [];
}

/**
* Get a Parameter instance
*
* @throws EntityNotFoundException
*/
public function get(string $code): Parameter
{
if (isset($this->parameters[$code])) {
return $this->parameters[$code];
}

$parameter = $this->entityManager->getRepository(Parameter::class)->findOneBy(['code' => $code]);

if ($parameter == null) {
throw new EntityNotFoundException("The parameter with code \"$code\" was not found.");
}
$this->parameters[$code] = $parameter;

return $parameter;
}

/**
* Get a Parameter value
*/
public function getValue(string $code): string
{
return $this->get($code)->getValue();
}
}
16 changes: 16 additions & 0 deletions src/Repository/ParameterRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Smart\ParameterBundle\Repository;

use Doctrine\ORM\EntityRepository;
use Smart\ParameterBundle\Entity\Parameter;

/**
* @author Mathieu Ducrot <[email protected]>
*
* @extends \Doctrine\ORM\EntityRepository<Parameter>
*/
class ParameterRepository extends EntityRepository
{

}
29 changes: 29 additions & 0 deletions tests/AbstractWebTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Smart\ParameterBundle\Tests;

use Liip\TestFixturesBundle\Test\FixturesTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
* @author Mathieu Ducrot <[email protected]>
*/
abstract class AbstractWebTestCase extends WebTestCase
{
use FixturesTrait;

public function setUp(): void
{
parent::setUp();

self::bootKernel();

// Empty load to guarantee that the base will always be available
$this->loadFixtureFiles([]);
}

protected function getFixtureDir(): string
{
return __DIR__ . '/fixtures';
}
}
26 changes: 0 additions & 26 deletions tests/App/AppKernel.php

This file was deleted.

26 changes: 0 additions & 26 deletions tests/App/AppKernelTest.php

This file was deleted.

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

namespace Smart\ParameterBundle\Tests;

use Psr\Log\NullLogger;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;

/**
* Kernel used by PHPUnit to ease services testing
*
* @author Mathieu Ducrot <[email protected]>
*/
class AppKernel extends Kernel
{
public function registerBundles()
{
return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new \Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
new \DAMA\DoctrineTestBundle\DAMADoctrineTestBundle(),
new \Liip\TestFixturesBundle\LiipTestFixturesBundle(),
new \Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle(),
new \Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle(),
new \Smart\ParameterBundle\SmartParameterBundle(),
];
}

public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(__DIR__ . '/config_test.yml');
}

// https://github.com/dmaicher/doctrine-test-bundle/blob/master/tests/Functional/app/AppKernel.php
protected function build(ContainerBuilder $container): void
{
// remove logger info
$container->register('logger', NullLogger::class);
}
}
11 changes: 3 additions & 8 deletions tests/DependencyInjection/DependencyInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Smart\ParameterBundle\Tests\DependencyInjection;

use Doctrine\ORM\EntityManagerInterface;
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\Definition;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

/**
Expand All @@ -23,13 +24,7 @@ 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->setDefinition('Doctrine\ORM\EntityManagerInterface', new Definition(EntityManagerInterface::class));
$this->container->registerExtension($bundle->getContainerExtension());
$bundle->build($this->container);
}
Expand Down
Loading

0 comments on commit 1db2615

Please sign in to comment.