cec193076aec5a2128ad8b7637f85c93c2bef4f0
[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->dbProphecy = $this->prophesize(DatabaseConnection::class);
41 $GLOBALS['TYPO3_DB'] = $this->dbProphecy->reveal();
42
43 $this->subject = $this->getMockBuilder(DatabaseEditRow::class)
44 ->setMethods(['getDatabaseRow'])
45 ->getMock();
46 }
47
48 /**
49 * @test
50 */
51 public function addDataRetrievesRecordInformationFromDatabase()
52 {
53 $input = [
54 'tableName' => 'tt_content',
55 'command' => 'edit',
56 'vanillaUid' => 10,
57 ];
58 $resultRow = [
59 'uid' => 10,
60 'pid' => 123
61 ];
62 $this->subject->expects($this->once())->method('getDatabaseRow')->willReturn($resultRow);
63
64 $result = $this->subject->addData($input);
65
66 $this->assertSame($resultRow, $result['databaseRow']);
67 }
68
69 /**
70 * @test
71 */
72 public function addDataThrowsExceptionIfRetrievedRowHasNoPid()
73 {
74 $input = [
75 'tableName' => 'tt_content',
76 'command' => 'edit',
77 'vanillaUid' => 10,
78 ];
79 $resultRow = [
80 'uid' => 10,
81 ];
82 $this->subject->expects($this->once())->method('getDatabaseRow')->willReturn($resultRow);
83
84 $this->expectException(\UnexpectedValueException::class);
85 $this->expectExceptionCode(1437663061);
86
87 $this->subject->addData($input);
88 }
89
90 /**
91 * @test
92 */
93 public function addDataThrowsExceptionIfGivenUidIsNotPositive()
94 {
95 $input = [
96 'tableName' => 'tt_content',
97 'command' => 'edit',
98 'vanillaUid' => -10,
99 ];
100
101 $this->expectException(\InvalidArgumentException::class);
102 $this->expectExceptionCode(1437656456);
103
104 $this->subject->addData($input);
105 }
106
107 /**
108 * @test
109 */
110 public function addDataThrowsExceptionIfDatabaseFetchingReturnsNoRow()
111 {
112 $input = [
113 'tableName' => 'tt_content',
114 'command' => 'edit',
115 'vanillaUid' => 10,
116 ];
117 $this->subject->expects($this->once())->method('getDatabaseRow')->willReturn([]);
118
119 $this->expectException(DatabaseRecordException::class);
120 $this->expectExceptionCode(1437656081);
121
122 $this->subject->addData($input);
123 }
124
125 /**
126 * @test
127 */
128 public function addDataThrowsExceptionDatabaseRecordExceptionWithAdditionalInformationSet()
129 {
130 $input = [
131 'tableName' => 'tt_content',
132 'command' => 'edit',
133 'vanillaUid' => 10,
134 ];
135 $this->subject->expects($this->once())->method('getDatabaseRow')->willReturn([]);
136
137 try {
138 $this->subject->addData($input);
139 } catch (DatabaseRecordException $e) {
140 $this->assertSame('tt_content', $e->getTableName());
141 $this->assertSame(10, $e->getUid());
142 }
143 }
144 }