‪TYPO3CMS  11.5
SaveToDatabaseFinisherTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪SaveToDatabaseFinisherTest extends UnitTestCase
30 {
31  use \Prophecy\PhpUnit\ProphecyTrait;
36  {
37  $this->expectException(FinisherException::class);
38  $this->expectExceptionCode(1480469086);
39  $mockSaveToDatabaseFinisher = $this->getAccessibleMock(SaveToDatabaseFinisher::class, ['parseOption'], [], '', false);
40  $mockSaveToDatabaseFinisher->method('parseOption')->willReturnMap([
41  ['mode', 'update'],
42  ['whereClause', ''],
43  ]);
44 
45  $mockSaveToDatabaseFinisher->_call('throwExceptionOnInconsistentConfiguration');
46  }
47 
51  public function ‪prepareDataConvertsArrayValuesToCsv(): void
52  {
53  $elementsConfiguration = [
54  'foo' => [
55  'mapOnDatabaseColumn' => 'bar',
56  ],
57  ];
58 
59  $saveToDatabaseFinisher = $this->getAccessibleMock(SaveToDatabaseFinisher::class, ['getFormValues', 'getElementByIdentifier']);
60  $saveToDatabaseFinisher->method('getFormValues')->willReturn([
61  'foo' => [
62  'one',
63  'two',
64  ],
65  ]);
66  $saveToDatabaseFinisher->method('getElementByIdentifier')->willReturn($this->prophesize(FormElementInterface::class)->reveal());
67  $databaseData = $saveToDatabaseFinisher->_call('prepareData', $elementsConfiguration, []);
68 
69  self::assertSame('one,two', $databaseData['bar']);
70  }
71 
75  public function ‪executeInternalProcessesSingleTable(): void
76  {
77  $saveToDatabaseFinisher = $this->getMockBuilder(SaveToDatabaseFinisher::class)
78  ->onlyMethods(['process'])
79  ->getMock();
80  $saveToDatabaseFinisher->setOptions([
81  'table' => 'tx_foo',
82  'databaseColumnMappings' => [
83  'foo' => 1,
84  ],
85  ]);
86 
87  $saveToDatabaseFinisher->expects(self::once())->method('process')->with(0);
88 
89  $saveToDatabaseFinisher->execute($this->prophesize(FinisherContext::class)->reveal());
90  }
91 
95  public function ‪skipIfValueIsEmptyDataProvider(): array
96  {
97  return [
98  'null value' => [
99  'value' => null,
100  'expectedEmpty' => true,
101  ],
102  'empty string' => [
103  'value' => '',
104  'expectedEmpty' => true,
105  ],
106  'false value' => [
107  'value' => false,
108  'expectedEmpty' => false,
109  ],
110  'space character' => [
111  'value' => ' ',
112  'expectedEmpty' => false,
113  ],
114  'zero' => [
115  'value' => 0,
116  'expectedEmpty' => false,
117  ],
118  'zero float' => [
119  'value' => 0.0,
120  'expectedEmpty' => false,
121  ],
122  ];
123  }
124 
131  public function ‪skipIfValueIsEmptyDetectsEmptyValues($value, bool $expectedEmpty): void
132  {
133  $elementsConfiguration = [
134  'foo' => [
135  'mapOnDatabaseColumn' => 'bar',
136  'skipIfValueIsEmpty' => true,
137  ],
138  ];
139 
140  $saveToDatabaseFinisher = $this->getAccessibleMock(SaveToDatabaseFinisher::class, ['getFormValues', 'getElementByIdentifier']);
141  $saveToDatabaseFinisher->method('getFormValues')->willReturn([
142  'foo' => $value,
143  ]);
144  $saveToDatabaseFinisher->method('getElementByIdentifier')->willReturn($this->prophesize(FormElementInterface::class)->reveal());
145  $databaseData = $saveToDatabaseFinisher->_call('prepareData', $elementsConfiguration, []);
146 
147  self::assertSame($expectedEmpty, empty($databaseData));
148  }
149 
153  public function ‪executeInternalProcessesMultipleTables(): void
154  {
155  $saveToDatabaseFinisher = $this->getMockBuilder(SaveToDatabaseFinisher::class)
156  ->onlyMethods(['process'])
157  ->getMock();
158  $saveToDatabaseFinisher->setOptions([
159  [
160  'table' => 'tx_foo',
161  'databaseColumnMappings' => [
162  'foo' => 1,
163  ],
164  ],
165  [
166  'table' => 'tx_bar',
167  'databaseColumnMappings' => [
168  'bar' => 1,
169  ],
170  ],
171  ]);
172 
173  $saveToDatabaseFinisher->expects(self::exactly(2))->method('process')->withConsecutive([0], [1]);
174 
175  $saveToDatabaseFinisher->execute($this->prophesize(FinisherContext::class)->reveal());
176  }
177 
181  public function ‪prepareDataConvertsDateTimeToUnixTimestamp(): void
182  {
183  $elementsConfiguration = [
184  'date' => [
185  'mapOnDatabaseColumn' => 'date',
186  ],
187  ];
188 
189  $saveToDatabaseFinisher = $this->getAccessibleMock(SaveToDatabaseFinisher::class, ['getFormValues', 'getElementByIdentifier']);
190  $saveToDatabaseFinisher->method('getFormValues')->willReturn([
191  'date' => new \DateTime(),
192  ]);
193  $saveToDatabaseFinisher->method('getElementByIdentifier')->willReturn($this->prophesize(FormElementInterface::class)->reveal());
194  $databaseData = $saveToDatabaseFinisher->_call('prepareData', $elementsConfiguration, []);
195 
196  $expected = '#^([0-9]{10})$#';
197  self::assertMatchesRegularExpression($expected, $databaseData['date']);
198  }
199 
203  public function ‪prepareDataConvertsDateTimeToFormat(): void
204  {
205  $elementsConfiguration = [
206  'date' => [
207  'mapOnDatabaseColumn' => 'date',
208  'dateFormat' => 'Y.m.d',
209  ],
210  ];
211 
212  $saveToDatabaseFinisher = $this->getAccessibleMock(SaveToDatabaseFinisher::class, ['getFormValues', 'getElementByIdentifier']);
213  $saveToDatabaseFinisher->method('getFormValues')->willReturn([
214  'date' => new \DateTime('2018-06-12'),
215  ]);
216  $saveToDatabaseFinisher->method('getElementByIdentifier')->willReturn($this->prophesize(FormElementInterface::class)->reveal());
217  $databaseData = $saveToDatabaseFinisher->_call('prepareData', $elementsConfiguration, []);
218 
219  self::assertSame('2018.06.12', $databaseData['date']);
220  }
221 }
‪TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher
Definition: SaveToDatabaseFinisher.php:175
‪TYPO3\CMS\Form\Tests\Unit\Domain\Finishers\SaveToDatabaseFinisherTest\executeInternalProcessesSingleTable
‪executeInternalProcessesSingleTable()
Definition: SaveToDatabaseFinisherTest.php:74
‪TYPO3\CMS\Form\Tests\Unit\Domain\Finishers\SaveToDatabaseFinisherTest
Definition: SaveToDatabaseFinisherTest.php:30
‪TYPO3\CMS\Form\Tests\Unit\Domain\Finishers\SaveToDatabaseFinisherTest\skipIfValueIsEmptyDataProvider
‪array skipIfValueIsEmptyDataProvider()
Definition: SaveToDatabaseFinisherTest.php:94
‪TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface
Definition: FormElementInterface.php:40
‪TYPO3\CMS\Form\Tests\Unit\Domain\Finishers\SaveToDatabaseFinisherTest\skipIfValueIsEmptyDetectsEmptyValues
‪skipIfValueIsEmptyDetectsEmptyValues($value, bool $expectedEmpty)
Definition: SaveToDatabaseFinisherTest.php:130
‪TYPO3\CMS\Form\Tests\Unit\Domain\Finishers\SaveToDatabaseFinisherTest\prepareDataConvertsDateTimeToFormat
‪prepareDataConvertsDateTimeToFormat()
Definition: SaveToDatabaseFinisherTest.php:202
‪TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException
Definition: FinisherException.php:25
‪TYPO3\CMS\Form\Tests\Unit\Domain\Finishers\SaveToDatabaseFinisherTest\prepareDataConvertsDateTimeToUnixTimestamp
‪prepareDataConvertsDateTimeToUnixTimestamp()
Definition: SaveToDatabaseFinisherTest.php:180
‪TYPO3\CMS\Form\Domain\Finishers\FinisherContext
Definition: FinisherContext.php:38
‪TYPO3\CMS\Form\Tests\Unit\Domain\Finishers\SaveToDatabaseFinisherTest\prepareDataConvertsArrayValuesToCsv
‪prepareDataConvertsArrayValuesToCsv()
Definition: SaveToDatabaseFinisherTest.php:50
‪TYPO3\CMS\Form\Tests\Unit\Domain\Finishers\SaveToDatabaseFinisherTest\executeInternalProcessesMultipleTables
‪executeInternalProcessesMultipleTables()
Definition: SaveToDatabaseFinisherTest.php:152
‪TYPO3\CMS\Form\Tests\Unit\Domain\Finishers
Definition: AbstractFinisherTest.php:18
‪TYPO3\CMS\Form\Tests\Unit\Domain\Finishers\SaveToDatabaseFinisherTest\throwExceptionOnInconsistentConfigurationThrowsExceptionOnInconsistentConfiguration
‪throwExceptionOnInconsistentConfigurationThrowsExceptionOnInconsistentConfiguration()
Definition: SaveToDatabaseFinisherTest.php:34