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