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