[TASK] EXT:form - add unitTests for class TypoScriptToJsonConverter 30/47130/3
authorSebastian Wagner <sebastian.wagner@tritum.de>
Sat, 5 Mar 2016 21:29:57 +0000 (22:29 +0100)
committerAndreas Wolf <andreas.wolf@typo3.org>
Sun, 6 Mar 2016 01:08:28 +0000 (02:08 +0100)
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/47130
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>
typo3/sysext/form/Tests/Unit/Utility/TypoScriptToJsonConverterTest.php [new file with mode: 0644]

diff --git a/typo3/sysext/form/Tests/Unit/Utility/TypoScriptToJsonConverterTest.php b/typo3/sysext/form/Tests/Unit/Utility/TypoScriptToJsonConverterTest.php
new file mode 100644 (file)
index 0000000..257b1b5
--- /dev/null
@@ -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
+            ],
+        ];
+    }
+}