From a7a384b3eb94c40838fd8c9539431160b4d66a4e Mon Sep 17 00:00:00 2001
From: Christian Dangl <cd@dasistweb.de>
Date: Sun, 24 Dec 2023 10:24:01 +0100
Subject: [PATCH] add configuration validator test

---
 .../Services/ConfigurationValidatorTest.php   | 168 ++++++++++++++++++
 1 file changed, 168 insertions(+)
 create mode 100644 tests/phpunit/Components/Configuration/Services/ConfigurationValidatorTest.php

diff --git a/tests/phpunit/Components/Configuration/Services/ConfigurationValidatorTest.php b/tests/phpunit/Components/Configuration/Services/ConfigurationValidatorTest.php
new file mode 100644
index 00000000..d9adb7c2
--- /dev/null
+++ b/tests/phpunit/Components/Configuration/Services/ConfigurationValidatorTest.php
@@ -0,0 +1,168 @@
+<?php
+
+namespace phpunit\Components\Configuration\Services;
+
+use PHPUnit\Framework\TestCase;
+use phpunit\Utils\Traits\TranslationSetBuilderTrait;
+use PHPUnuhi\Configuration\Services\ConfigurationValidator;
+use PHPUnuhi\Exceptions\ConfigurationException;
+use PHPUnuhi\Models\Configuration\Configuration;
+use PHPUnuhi\Models\Configuration\Filter;
+use PHPUnuhi\Models\Configuration\Protection;
+use PHPUnuhi\Models\Translation\Locale;
+use PHPUnuhi\Models\Translation\TranslationSet;
+
+class ConfigurationValidatorTest extends TestCase
+{
+    use TranslationSetBuilderTrait;
+
+
+    /**
+     * @var ConfigurationValidator
+     */
+    private $validator;
+
+    /**
+     * @return void
+     */
+    public function setUp(): void
+    {
+        $this->validator = new ConfigurationValidator();
+    }
+
+    /**
+     * @throws ConfigurationException
+     * @return void
+     */
+    public function testEmptySetsLeadToException(): void
+    {
+        $this->expectException(ConfigurationException::class);
+
+        $configuration = new Configuration([]);
+
+        $this->validator->validateConfig($configuration);
+    }
+
+    /**
+     * @throws ConfigurationException
+     * @return void
+     */
+    public function testSetsWithoutNameLeadToException(): void
+    {
+        $this->expectException(ConfigurationException::class);
+
+        $set = new TranslationSet(
+            '',
+            '',
+            new Protection(),
+            [],
+            new Filter(),
+            [],
+            [],
+            []
+        );
+
+        $configuration = new Configuration([$set]);
+
+        $this->validator->validateConfig($configuration);
+    }
+
+    /**
+     * @throws ConfigurationException
+     * @return void
+     */
+    public function testSetsWithoutFormatLeadToException(): void
+    {
+        $this->expectException(ConfigurationException::class);
+
+        $set = new TranslationSet(
+            'Storefront',
+            '',
+            new Protection(),
+            [],
+            new Filter(),
+            [],
+            [],
+            []
+        );
+
+        $configuration = new Configuration([$set]);
+
+        $this->validator->validateConfig($configuration);
+    }
+
+    /**
+     * @throws ConfigurationException
+     * @return void
+     */
+    public function testSetsWithSameNameLeadToException(): void
+    {
+        $this->expectException(ConfigurationException::class);
+
+        $set = new TranslationSet(
+            'Storefront',
+            'json',
+            new Protection(),
+            [],
+            new Filter(),
+            [],
+            [],
+            []
+        );
+
+        $configuration = new Configuration([$set, $set]);
+
+        $this->validator->validateConfig($configuration);
+    }
+
+    /**
+     * @throws ConfigurationException
+     * @return void
+     */
+    public function testLocalesWithoutNameLeadToException(): void
+    {
+        $this->expectException(ConfigurationException::class);
+
+        $locale = new Locale('', '', '');
+
+        $set = $this->buildTranslationSet([$locale], []);
+
+        $configuration = new Configuration([$set, $set]);
+
+        $this->validator->validateConfig($configuration);
+    }
+
+    /**
+     * @throws ConfigurationException
+     * @return void
+     */
+    public function testLocalesWithSameNameLeadToException(): void
+    {
+        $this->expectException(ConfigurationException::class);
+
+        $locale = new Locale('DE', '', '');
+
+        $set = $this->buildTranslationSet([$locale, $locale], []);
+
+        $configuration = new Configuration([$set, $set]);
+
+        $this->validator->validateConfig($configuration);
+    }
+
+    /**
+     * @throws ConfigurationException
+     * @return void
+     */
+    public function testLocalesWithNotExistingFileLeadToException(): void
+    {
+        $this->expectException(ConfigurationException::class);
+
+        $locale = new Locale('DE', 'not-existing.json', '');
+
+        $set = $this->buildTranslationSet([$locale], []);
+
+        $configuration = new Configuration([$set, $set]);
+
+        $this->validator->validateConfig($configuration);
+    }
+}