Skip to content

Commit

Permalink
Create KernelTest and trait to test Config
Browse files Browse the repository at this point in the history
  • Loading branch information
mazarini committed Jan 21, 2024
1 parent 469e8ef commit f8ddce4
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/AbstractConfigBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ protected function loadConfig(ServicesConfigurator $services, array $configArray
->set($config::class)
->arg('$configData', $config->getData())
->public()
;
->alias(ConfigInterface::class, $config::class);
}
}
33 changes: 33 additions & 0 deletions src/KernelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* Copyright (C) 2024 Mazarini <mazarini@protonmail.com>.
* This file is part of mazarini/config.
*
* mazarini/config is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* mazarini/config is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
*/

namespace Mazarini\Config;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

abstract class KernelTest extends BaseKernel
{
use MicroKernelTrait;

protected function getConfigDir(): string
{
return $this->getProjectDir().'/tests/config';
}
}
50 changes: 50 additions & 0 deletions src/Test/ConfigTestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* Copyright (C) 2024 Mazarini <mazarini@protonmail.com>.
* This file is part of mazarini/config.
*
* mazarini/config is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* mazarini/config is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
*/

namespace Mazarini\Config\Test;

use Mazarini\Config\Config\ConfigInterface;

trait ConfigTestTrait
{
public function getConfig(): ConfigInterface
{
$config = $this->getContainer()->get(ConfigInterface::class);
if ($config instanceof ConfigInterface) {
return $config;
}
throw new \LogicException(sprintf('Container don\'t contain "%s"', ConfigInterface::class));
}

/**
* setConfig.
*
* @param array<string,mixed> $data
*/
public function setConfig(array $data): static
{
$config = $this->getConfig();
(new \ReflectionClass($config::class))
->getProperty('configData')
->setValue($config, array_merge($config->getData(), $data))
;

return $this;
}
}
11 changes: 2 additions & 9 deletions tests/src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,8 @@

namespace App;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Mazarini\Config\KernelTest;

class Kernel extends BaseKernel
class Kernel extends KernelTest
{
use MicroKernelTrait;

protected function getConfigDir(): string
{
return $this->getProjectDir().'/tests/config';
}
}
12 changes: 2 additions & 10 deletions tests/tests/01-Configuration/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@

use App\Config\Config;
use App\Config\ConfigTrait;
use Mazarini\Config\Config\ConfigInterface;
use Mazarini\Config\Test\ConfigTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class ValueTest extends KernelTestCase
{
use ConfigTestTrait;
use ConfigTrait;

public function testConfigExists(): void
Expand Down Expand Up @@ -60,13 +61,4 @@ public function valueProvider(): array
['getTypeBool', true],
];
}

public function getConfig(): ConfigInterface
{
$config = $this->getContainer()->get(Config::class);
if ($config instanceof ConfigInterface) {
return $config;
}
throw new \LogicException('Never occurs');
}
}
38 changes: 38 additions & 0 deletions tests/tests/02-Object/UpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* Copyright (C) 2024 Mazarini <mazarini@protonmail.com>.
* This file is part of mazarini/config.
*
* mazarini/config is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* mazarini/config is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
*/

namespace App\Tests\ConfigurationPager;

use Mazarini\Config\Test\ConfigTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class UpdateTest extends KernelTestCase
{
use ConfigTestTrait;

public function testModifiyConfig(): void
{
$this->setConfig([
'type' => [
'string' => 'set',
],
]);
$this->assertSame('set', $this->getConfig()->getValue('type.string'));
}
}

0 comments on commit f8ddce4

Please sign in to comment.