3ff7cd1a4e1714d62983ad9d6e1bbb306e707e73
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / DatabaseEditRowTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
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
17 use Prophecy\Prophecy\ObjectProphecy;
18 use TYPO3\CMS\Backend\Form\Exception\DatabaseRecordException;
19 use TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseEditRow;
20 use TYPO3\CMS\Core\Database\DatabaseConnection;
21
22 /**
23 * Test case
24 */
25 class DatabaseEditRowTest extends \TYPO3\Components\TestingFramework\Core\UnitTestCase
26 {
27 /**
28 * @var DatabaseEditRow|\PHPUnit_Framework_MockObject_MockObject
29 */
30 protected $subject;
31
32 /**
33 * @var DatabaseConnection | ObjectProphecy
34 */
35 protected $dbProphecy;
36
37 protected function setUp()
38 {
39 $this->subject = $this->getMockBuilder(DatabaseEditRow::class)
40 ->setMethods(['getDatabaseRow'])
41 ->getMock();
42 }
43
44 /**
45 * @test
46 */
47 public function addDataRetrievesRecordInformationFromDatabase()
48 {
49 $input = [
50 'tableName' => 'tt_content',
51 'command' => 'edit',
52 'vanillaUid' => 10,
53 ];
54 $resultRow = [
55 'uid' => 10,
56 'pid' => 123
57 ];
58 $this->subject->expects($this->once())->method('getDatabaseRow')->willReturn($resultRow);
59
60 $result = $this->subject->addData($input);
61
62 $this->assertSame($resultRow, $result['databaseRow']);
63 }
64
65 /**
66 * @test
67 */
68 public function addDataThrowsExceptionIfRetrievedRowHasNoPid()
69 {
70 $input = [
71 'tableName' => 'tt_content',
72 'command' => 'edit',
73 'vanillaUid' => 10,
74 ];
75 $resultRow = [
76 'uid' => 10,
77 ];
78 $this->subject->expects($this->once())->method('getDatabaseRow')->willReturn($resultRow);
79
80 $this->expectException(\UnexpectedValueException::class);
81 $this->expectExceptionCode(1437663061);
82
83 $this->subject->addData($input);
84 }
85
86 /**
87 * @test
88 */
89 public function addDataThrowsExceptionIfGivenUidIsNotPositive()
90 {
91 $input = [
92 'tableName' => 'tt_content',
93 'command' => 'edit',
94 'vanillaUid' => -10,
95 ];
96
97 $this->expectException(\InvalidArgumentException::class);
98 $this->expectExceptionCode(1437656456);
99
100 $this->subject->addData($input);
101 }
102
103 /**
104 * @test
105 */
106 public function addDataThrowsExceptionIfDatabaseFetchingReturnsNoRow()
107 {
108 $input = [
109 'tableName' => 'tt_content',
110 'command' => 'edit',
111 'vanillaUid' => 10,
112 ];
113 $this->subject->expects($this->once())->method('getDatabaseRow')->willReturn([]);
114
115 $this->expectException(DatabaseRecordException::class);
116 $this->expectExceptionCode(1437656081);
117
118 $this->subject->addData($input);
119 }
120
121 /**
122 * @test
123 */
124 public function addDataThrowsExceptionDatabaseRecordExceptionWithAdditionalInformationSet()
125 {
126 $input = [
127 'tableName' => 'tt_content',
128 'command' => 'edit',
129 'vanillaUid' => 10,
130 ];
131 $this->subject->expects($this->once())->method('getDatabaseRow')->willReturn([]);
132
133 try {
134 $this->subject->addData($input);
135 } catch (DatabaseRecordException $e) {
136 $this->assertSame('tt_content', $e->getTableName());
137 $this->assertSame(10, $e->getUid());
138 }
139 }
140 }