Skip to content

Instantly share code, notes, and snippets.

@pilec
Last active April 17, 2018 09:41
Show Gist options
  • Save pilec/2c17da4df16eb67d5b2f4ed71d3b637c to your computer and use it in GitHub Desktop.
Save pilec/2c17da4df16eb67d5b2f4ed71d3b637c to your computer and use it in GitHub Desktop.
Test if neons have same keys
<?php
declare(strict_types=1);
namespace UnitTests\Utils;
require_once __DIR__ . '/../../../bootstrap.php';
use Nette\Neon\Neon;
use Tester\Assert;
use Tester\TestCase;
final class NeonSynchronizerTest extends TestCase
{
private $filesToTest = [
__DIR__ . '/../../../../src/config/config.local.example.neon',
__DIR__ . '/../../../../src/config/config.devel.neon',
// __DIR__ . '/../../../../src/config/config.production.neon',
__DIR__ . '/../../../config/config.test.neon',
];
public function testIfNeonFilesHaveSameKeys(): void
{
foreach ($this->filesToTest as $fileA) {
foreach ($this->filesToTest as $fileB) {
if ($fileA === $fileB) {
continue;
}
$configFromFileA = Neon::decode(file_get_contents($fileA));
$configFromFileB = Neon::decode(file_get_contents($fileB));
$arrayKeysA = $this->array_keys_inner('', $configFromFileA);
$arrayKeysB = $this->array_keys_inner('', $configFromFileB);
$diff = array_diff($arrayKeysA, $arrayKeysB);
Assert::noError(function () use ($diff, $fileA, $fileB) {
if ($diff !== []) {
$fileObjectA = new \SplFileInfo($fileA);
$fileObjectB = new \SplFileInfo($fileB);
$diffAsString = implode(', ', $diff);
Assert::fail(sprintf(
'%s and %s do not have same keys, diff: %s',
$fileObjectA->getBasename(),
$fileObjectB->getBasename(),
$diffAsString
));
}
});
}
}
}
private function array_keys_inner($prefix, $array)
{
$resultArr = [];
foreach ($array as $key => $subArr) {
if (is_array($subArr)) {
$stringKey = ($prefix !== '') ? sprintf('%s.%s', $prefix, $key) : $key;
$resultArr = array_merge($resultArr, $this->array_keys_inner($stringKey, $subArr));
}
$resultArr[] = sprintf('%s.%s', $prefix, $key);
}
return $resultArr;
}
}
(new NeonSynchronizerTest())->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment