TYPO3 CMS  TYPO3_8-7
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 
25 class SaveToDatabaseFinisherTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
26 {
27 
32  {
33  $this->expectException(FinisherException::class);
34  $this->expectExceptionCode(1480469086);
35 
36  $mockSaveToDatabaseFinisher = $this->getAccessibleMock(SaveToDatabaseFinisher::class, [
37  'dummy'
38  ], [], '', false);
39 
40  $mockSaveToDatabaseFinisher->_set('options', [
41  'mode' => 'update',
42  'whereClause' => '',
43  ]);
44 
45  $mockSaveToDatabaseFinisher->_call('throwExceptionOnInconsistentConfiguration');
46  }
47 
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 
76  {
77  $saveToDatabaseFinisher = $this->getMockBuilder(SaveToDatabaseFinisher::class)
78  ->setMethods(['process'])
79  ->getMock();
80  $this->inject($saveToDatabaseFinisher, 'options', [
81  'table' => 'tx_foo',
82  'databaseColumnMappings' => [
83  'foo' => 1,
84  ],
85  ]);
86 
87  $saveToDatabaseFinisher->expects($this->once())->method('process')->with(0);
88 
89  $saveToDatabaseFinisher->execute($this->prophesize(FinisherContext::class)->reveal());
90  }
91 
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)
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:self::assertSame($expectedEmpty, empty($databaseData));
148  }
149 
154  {
155  $saveToDatabaseFinisher = $this->getMockBuilder(SaveToDatabaseFinisher::class)
156  ->setMethods(['process'])
157  ->getMock();
158  $this->inject($saveToDatabaseFinisher, 'options', [
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($this->exactly(2))->method('process')->withConsecutive([0], [1]);
174 
175  $saveToDatabaseFinisher->execute($this->prophesize(FinisherContext::class)->reveal());
176  }
177 
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  $this->assertEquals(1, preg_match($expected, $databaseData['date']));
198  }
199 
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 }