From 2dd7b9c994ebf2876b8526c09a1c842dfd14280f Mon Sep 17 00:00:00 2001
From: Sebastian Wagner <sebastian.wagner@tritum.de>
Date: Sat, 5 Mar 2016 22:29:57 +0100
Subject: [PATCH] [TASK] EXT:form - add unitTests for class
 TypoScriptToJsonConverter

This patch introduces a first test case for TypoScriptToJsonConverter
of EXT:form. The test case asserts that a protected method internally
triggers adding elements to a given and mocked parent element in the
nested form hierarchy.

Resolves: #74337
Releases: master, 7.6
Change-Id: I191ba8fe41a5829e23303447f86303ae8c6dd081
Reviewed-on: https://review.typo3.org/47122
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
Reviewed-by: Andreas Wolf <andreas.wolf@typo3.org>
Tested-by: Andreas Wolf <andreas.wolf@typo3.org>
---
 .../Utility/TypoScriptToJsonConverterTest.php | 80 +++++++++++++++++++
 1 file changed, 80 insertions(+)
 create mode 100644 typo3/sysext/form/Tests/Unit/Utility/TypoScriptToJsonConverterTest.php

diff --git a/typo3/sysext/form/Tests/Unit/Utility/TypoScriptToJsonConverterTest.php b/typo3/sysext/form/Tests/Unit/Utility/TypoScriptToJsonConverterTest.php
new file mode 100644
index 000000000000..257b1b539588
--- /dev/null
+++ b/typo3/sysext/form/Tests/Unit/Utility/TypoScriptToJsonConverterTest.php
@@ -0,0 +1,80 @@
+<?php
+namespace TYPO3\CMS\Form\Tests\Unit\Utility;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Tests\AccessibleObjectInterface;
+use TYPO3\CMS\Core\Tests\UnitTestCase;
+use TYPO3\CMS\Form\Utility\TypoScriptToJsonConverter;
+use TYPO3\CMS\Form\Domain\Model\Json\FormJsonElement;
+
+/**
+ * Test case for \TYPO3\CMS\Form\Utility\TypoScriptToJsonConverter
+ */
+class TypoScriptToJsonConverterTest extends UnitTestCase
+{
+    /**
+     * Checks if calling protected method getChildElementsByIntegerKey with different data
+     * calls the addMethod in the mocked FormJsonElement for an expected method count.
+     *
+     * @dataProvider getChildElementsByIntegerKeyCallsAddElementDataProvider
+     * @param array $typoScript
+     * @param int $methodCount
+     * @test
+     */
+    public function getChildElementsByIntegerKeyCallsAddElement(array $typoScript, $methodCount)
+    {
+        /** @var FormJsonElement|\PHPUnit_Framework_MockObject_MockObject $mockSubject */
+        $parentElement = $this->getMock(FormJsonElement::class, ['addElement',]);
+        // check if method gets called exactly X times
+        $parentElement->expects($this->exactly($methodCount))->method('addElement');
+
+        /** @var TypoScriptToJsonConverter|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface $subjectAccessible */
+        $accessibleSubject = $this->getAccessibleMock(TypoScriptToJsonConverter::class, ['dummy',]);
+        $accessibleSubject->_call('getChildElementsByIntegerKey', $parentElement, $typoScript);
+    }
+
+    /**
+     * Data provider for test method getChildElementsByIntegerKeyCallsAddElement.
+     *
+     * @return array
+     */
+    public function getChildElementsByIntegerKeyCallsAddElementDataProvider()
+    {
+        return [
+            [
+                'typoscript' => [
+                    'prefix' => 'tx_form',
+                    'confirmation' => '1',
+                    'postProcessor.' => [
+                        '1' => 'mail',
+                        '1.' => [
+                            'recipientEmail' => '',
+                            'senderEmail' => '',
+                        ],
+                    ],
+                    '10' => 'FILEUPLOAD',
+                    '10.' => [
+                        'name' => 'foo',
+                        'type' => 'file',
+                        'label.' => [
+                            'value' => 'Edit this label',
+                        ],
+                    ],
+                ],
+                1
+            ],
+        ];
+    }
+}
-- 
GitLab