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