2017-10-31 13:40:13 +01:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-10-31 14:23:23 +01:00
|
|
|
namespace Engelsystem\Test\Unit\Config;
|
2017-10-31 13:40:13 +01:00
|
|
|
|
2019-04-24 10:45:00 +02:00
|
|
|
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
|
2017-10-31 13:40:13 +01:00
|
|
|
use Engelsystem\Application;
|
|
|
|
use Engelsystem\Config\Config;
|
|
|
|
use Engelsystem\Config\ConfigServiceProvider;
|
2018-09-25 17:33:31 +02:00
|
|
|
use Engelsystem\Models\EventConfig;
|
2017-10-31 14:15:19 +01:00
|
|
|
use Engelsystem\Test\Unit\ServiceProviderTest;
|
2018-08-10 15:02:07 +02:00
|
|
|
use Exception;
|
2018-09-25 17:33:31 +02:00
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-10-31 13:40:13 +01:00
|
|
|
|
2017-10-31 14:15:19 +01:00
|
|
|
class ConfigServiceProviderTest extends ServiceProviderTest
|
2017-10-31 13:40:13 +01:00
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
use ArraySubsetAsserts;
|
|
|
|
|
2023-09-24 21:42:44 +02:00
|
|
|
private array $configVarsWhereNullIsPruned =
|
|
|
|
['themes', 'tshirt_sizes', 'headers', 'header_items', 'footer_items', 'locales'];
|
|
|
|
|
2017-10-31 13:40:13 +01:00
|
|
|
/**
|
2018-09-25 17:33:31 +02:00
|
|
|
* @covers \Engelsystem\Config\ConfigServiceProvider::getConfigPath
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Config\ConfigServiceProvider::register
|
2017-10-31 13:40:13 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testRegister(): void
|
2017-10-31 13:40:13 +01:00
|
|
|
{
|
2019-11-06 13:16:00 +01:00
|
|
|
/** @var Application|MockObject $app */
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Config|MockObject $config */
|
2019-11-06 13:16:00 +01:00
|
|
|
list($app, $config) = $this->getConfiguredApp(__DIR__ . '/../../../config');
|
2017-10-31 13:40:13 +01:00
|
|
|
|
2023-09-24 21:42:44 +02:00
|
|
|
$config
|
|
|
|
->expects($this->exactly(4 + sizeof($this->configVarsWhereNullIsPruned)))
|
2018-08-10 15:02:07 +02:00
|
|
|
->method('get')
|
2023-09-24 21:42:44 +02:00
|
|
|
->with($this->callback(function (mixed $arg) {
|
|
|
|
return is_null($arg) || in_array($arg, $this->configVarsWhereNullIsPruned);
|
|
|
|
}))
|
|
|
|
->will($this->returnCallback(function (mixed $arg) {
|
|
|
|
if (in_array($arg, $this->configVarsWhereNullIsPruned)) {
|
|
|
|
return [$arg . '_foo' => $arg . '_bar', $arg . '_willBePruned' => null];
|
|
|
|
} elseif (is_null($arg)) {
|
|
|
|
return ['some' => 'value'];
|
|
|
|
} else {
|
|
|
|
throw new Exception('Unexpected arg: ' . $arg);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
$config
|
|
|
|
->expects($this->exactly(3 + sizeof($this->configVarsWhereNullIsPruned)))
|
|
|
|
->method('set')
|
|
|
|
//With does not support a callback funtion with multiple args ...
|
|
|
|
//Therefore, we misuse will
|
|
|
|
->will($this->returnCallback(function (mixed $key, mixed $value = null) {
|
|
|
|
if (is_array($key)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (in_array($key, $this->configVarsWhereNullIsPruned)) {
|
|
|
|
if ($value == [$key . '_foo' => $key . '_bar']) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
throw new Exception('Value for key ' . print_r($key, true) .
|
|
|
|
'is not as expected: ' . print_r($value, true));
|
|
|
|
}
|
|
|
|
throw new Exception('Unexpected key: ' . print_r($key, true));
|
|
|
|
}));
|
2017-10-31 13:40:13 +01:00
|
|
|
|
2017-10-31 15:13:37 +01:00
|
|
|
$configFile = __DIR__ . '/../../../config/config.php';
|
|
|
|
$configExists = file_exists($configFile);
|
|
|
|
if (!$configExists) {
|
2018-08-10 15:02:07 +02:00
|
|
|
file_put_contents($configFile, '<?php return ["lor"=>"em"];');
|
2017-10-31 15:13:37 +01:00
|
|
|
}
|
|
|
|
|
2017-10-31 13:40:13 +01:00
|
|
|
$serviceProvider = new ConfigServiceProvider($app);
|
|
|
|
$serviceProvider->register();
|
2017-10-31 15:13:37 +01:00
|
|
|
|
|
|
|
if (!$configExists) {
|
|
|
|
unlink($configFile);
|
|
|
|
}
|
2017-10-31 13:40:13 +01:00
|
|
|
}
|
2018-08-10 15:02:07 +02:00
|
|
|
|
|
|
|
/**
|
2018-09-25 17:33:31 +02:00
|
|
|
* @covers \Engelsystem\Config\ConfigServiceProvider::register
|
2018-08-10 15:02:07 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testRegisterException(): void
|
2018-08-10 15:02:07 +02:00
|
|
|
{
|
2019-11-06 13:16:00 +01:00
|
|
|
/** @var Application|MockObject $app */
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Config|MockObject $config */
|
2019-11-06 13:16:00 +01:00
|
|
|
list($app, $config) = $this->getConfiguredApp(__DIR__ . '/not_existing');
|
2018-08-10 15:02:07 +02:00
|
|
|
|
|
|
|
$this->setExpects($config, 'set', null, null, $this->never());
|
|
|
|
$this->setExpects($config, 'get', [null], []);
|
|
|
|
|
|
|
|
$this->expectException(Exception::class);
|
|
|
|
|
|
|
|
$serviceProvider = new ConfigServiceProvider($app);
|
|
|
|
$serviceProvider->register();
|
|
|
|
}
|
2018-09-25 17:33:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Config\ConfigServiceProvider::__construct
|
|
|
|
* @covers \Engelsystem\Config\ConfigServiceProvider::boot
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testBoot(): void
|
2018-09-25 17:33:31 +02:00
|
|
|
{
|
|
|
|
$app = $this->getApp(['get']);
|
|
|
|
|
|
|
|
/** @var EventConfig|MockObject $eventConfig */
|
|
|
|
$eventConfig = $this->createMock(EventConfig::class);
|
|
|
|
/** @var EloquentBuilder|MockObject $eloquentBuilder */
|
|
|
|
$eloquentBuilder = $this->getMockBuilder(EloquentBuilder::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$config = new Config(['foo' => 'bar', 'lorem' => ['ipsum' => 'dolor', 'bla' => 'foo']]);
|
|
|
|
|
|
|
|
$configs = [
|
|
|
|
new EventConfig(['name' => 'test', 'value' => 'testing']),
|
|
|
|
new EventConfig(['name' => 'lorem', 'value' => ['ipsum' => 'tester']]),
|
|
|
|
];
|
|
|
|
|
|
|
|
$returnValue = $eloquentBuilder;
|
|
|
|
$eventConfig
|
|
|
|
->expects($this->exactly(3))
|
|
|
|
->method('newQuery')
|
|
|
|
->willReturnCallback(function () use (&$returnValue) {
|
|
|
|
if ($returnValue instanceof EloquentBuilder) {
|
|
|
|
$return = $returnValue;
|
|
|
|
$returnValue = null;
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($returnValue)) {
|
|
|
|
throw new QueryException('', [], new Exception());
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->setExpects($eloquentBuilder, 'get', [['name', 'value']], $configs);
|
|
|
|
$this->setExpects($app, 'get', ['config'], $config, $this->exactly(3));
|
|
|
|
|
|
|
|
$serviceProvider = new ConfigServiceProvider($app);
|
|
|
|
$serviceProvider->boot();
|
|
|
|
|
|
|
|
$serviceProvider = new ConfigServiceProvider($app, $eventConfig);
|
|
|
|
$serviceProvider->boot();
|
|
|
|
$serviceProvider->boot();
|
|
|
|
$serviceProvider->boot();
|
|
|
|
|
|
|
|
$this->assertArraySubset(
|
|
|
|
[
|
|
|
|
'foo' => 'bar',
|
|
|
|
'lorem' => [
|
|
|
|
'ipsum' => 'tester',
|
|
|
|
'bla' => 'foo',
|
|
|
|
],
|
|
|
|
'test' => 'testing',
|
|
|
|
],
|
|
|
|
$config->get(null)
|
|
|
|
);
|
|
|
|
}
|
2019-11-06 13:16:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Application[]|Config[]
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function getConfiguredApp(string $configPath): array
|
2019-11-06 13:16:00 +01:00
|
|
|
{
|
|
|
|
/** @var Config|MockObject $config */
|
|
|
|
$config = $this->getMockBuilder(Config::class)
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$app = $this->getApp(['make', 'instance', 'get']);
|
|
|
|
Application::setInstance($app);
|
|
|
|
|
|
|
|
$this->setExpects($app, 'make', [Config::class], $config);
|
|
|
|
$this->setExpects($app, 'get', ['path.config'], $configPath, $this->atLeastOnce());
|
|
|
|
$app->expects($this->exactly(2))
|
|
|
|
->method('instance')
|
|
|
|
->withConsecutive(
|
|
|
|
[Config::class, $config],
|
|
|
|
['config', $config]
|
|
|
|
);
|
|
|
|
|
|
|
|
return [$app, $config];
|
|
|
|
}
|
2017-10-31 13:40:13 +01:00
|
|
|
}
|