/
ConfigManagerTest.php
117 lines (99 loc) · 4.01 KB
/
ConfigManagerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Configuration;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Yaml\Yaml;
class ConfigManagerTest extends TestCase
{
public static function tearDownAfterClass()
{
// this is important because this test generates a different Symfony
// kernel for each configuration to avoid cache issues
self::deleteDirectory(__DIR__.'/../../build/cache/test');
}
/**
* @dataProvider provideConfigFilePaths
*/
public function testLoadConfig($backendConfigFilePath, $expectedConfigFilePath)
{
$backendConfig = $this->loadConfig($backendConfigFilePath);
$expectedConfig = Yaml::parse(file_get_contents($expectedConfigFilePath));
$this->assertArraySubset($expectedConfig['easy_admin'], $backendConfig);
}
/**
* @dataProvider provideConfigExceptionFilePaths
*/
public function testBackendExceptions($backendConfigFilePath)
{
$backendConfig = Yaml::parse(file_get_contents($backendConfigFilePath));
if (isset($backendConfig['expected_exception']['class'])) {
$this->expectException($backendConfig['expected_exception']['class']);
if (isset($backendConfig['expected_exception']['message_string'])) {
$this->expectException($backendConfig['expected_exception']['class']);
$this->expectExceptionMessage($backendConfig['expected_exception']['message_string']);
} elseif (isset($backendConfig['expected_exception']['message_regexp'])) {
$this->expectException($backendConfig['expected_exception']['class']);
$this->expectExceptionMessageRegExp($backendConfig['expected_exception']['message_regexp']);
}
}
$this->loadConfig($backendConfigFilePath);
}
public function provideConfigFilePaths()
{
$inputs = array_merge(
glob(__DIR__.'/fixtures/configurations/input/admin_*.yml'),
glob(__DIR__.'/fixtures/templates/*/input/admin_*.yml')
);
$outputs = array_merge(
glob(__DIR__.'/fixtures/configurations/output/config_*.yml'),
glob(__DIR__.'/fixtures/templates/*/output/config_*.yml')
);
return array_map(null, $inputs, $outputs);
}
public function provideConfigExceptionFilePaths()
{
// glob() returns an array of strings and fixtures require an array of arrays
return array_map(
function ($filePath) {
return [$filePath];
},
glob(__DIR__.'/fixtures/exceptions/*.yml')
);
}
/**
* Given the path of the YAML file which defines the backend config, it
* fully processes it to generate the real and complete config used by
* the application.
*
* @param string $backendConfigFilePath
*
* @return array
*/
private function loadConfig($backendConfigFilePath)
{
$configuration = Yaml::parse(file_get_contents($backendConfigFilePath));
// to get the processed config, boot a special Symfony kernel to load
// the backend config dynamically
include_once __DIR__.'/../Fixtures/App/DynamicConfigLoadingKernel.php';
$app = new \DynamicConfigLoadingKernel($configuration['easy_admin']);
$app->boot();
$backendConfig = $app->getContainer()->get('easyadmin.config.manager')->getBackendConfig();
return $backendConfig;
}
/**
* Utility method because PHP doesn't allow to delete non-empty directories.
*/
private static function deleteDirectory($dir)
{
if (!is_dir($dir)) {
return;
}
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileinfo) {
$fileinfo->isDir() ? rmdir($fileinfo->getRealPath()) : unlink($fileinfo->getRealPath());
}
rmdir($dir);
}
}