[TASK] Make BackendTest notice free
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / Generic / BackendTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17 use TYPO3\CMS\Core\Database\ReferenceIndex;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
20 use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
21 use TYPO3\CMS\Extbase\Persistence\Generic\Backend;
22 use TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy;
23 use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap;
24 use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap;
25 use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
26 use TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28
29 /**
30 * Test case
31 */
32 class BackendTest extends UnitTestCase
33 {
34 /**
35 * @test
36 */
37 public function insertRelationInRelationtableSetsMmMatchFieldsInRow()
38 {
39 /* \TYPO3\CMS\Extbase\Persistence\Generic\Backend|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface */
40 $fixture = $this->getAccessibleMock(Backend::class, ['dummy'], [], '', false);
41 /* \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper|\PHPUnit_Framework_MockObject_MockObject */
42 $dataMapper = $this->createMock(DataMapper::class);
43 /* \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap|\PHPUnit_Framework_MockObject_MockObject */
44 $dataMap = $this->createMock(DataMap::class);
45 /* \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap|\PHPUnit_Framework_MockObject_MockObject */
46 $columnMap = $this->createMock(ColumnMap::class);
47 /* \TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface|\PHPUnit_Framework_MockObject_MockObject */
48 $storageBackend = $this->createMock(BackendInterface::class);
49 /* \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
50 $domainObject = $this->createMock(DomainObjectInterface::class);
51
52 $mmMatchFields = [
53 'identifier' => 'myTable:myField',
54 ];
55
56 $expectedRow = [
57 'identifier' => 'myTable:myField',
58 '' => 0
59 ];
60
61 $columnMap
62 ->expects($this->once())
63 ->method('getRelationTableMatchFields')
64 ->will($this->returnValue($mmMatchFields));
65 $columnMap
66 ->expects($this->any())
67 ->method('getChildSortByFieldName')
68 ->will($this->returnValue(''));
69 $dataMap
70 ->expects($this->any())
71 ->method('getColumnMap')
72 ->will($this->returnValue($columnMap));
73 $dataMapper
74 ->expects($this->any())
75 ->method('getDataMap')
76 ->will($this->returnValue($dataMap));
77 $storageBackend
78 ->expects($this->once())
79 ->method('addRow')
80 ->with(null, $expectedRow, true);
81
82 $fixture->_set('dataMapper', $dataMapper);
83 $fixture->_set('storageBackend', $storageBackend);
84 $fixture->_call('insertRelationInRelationtable', $domainObject, $domainObject, '');
85 }
86
87 /**
88 * @test
89 */
90 public function getIdentifierByObjectReturnsIdentifierForNonLazyObject()
91 {
92 $fakeUuid = 'fakeUuid';
93 $configurationManager = $this->createMock(ConfigurationManagerInterface::class);
94 $session = $this->getMockBuilder('stdClass')
95 ->setMethods(['getIdentifierByObject'])
96 ->disableOriginalConstructor()
97 ->getMock();
98 $object = new \stdClass();
99
100 $referenceIndexProphecy = $this->prophesize(ReferenceIndex::class);
101 GeneralUtility::addInstance(ReferenceIndex::class, $referenceIndexProphecy->reveal());
102
103 $session->expects($this->once())->method('getIdentifierByObject')->with($object)->will($this->returnValue($fakeUuid));
104
105 /** @var Backend $backend */
106 $backend = $this->getAccessibleMock(Backend::class, ['dummy'], [$configurationManager]);
107 $backend->_set('session', $session);
108
109 $this->assertEquals($backend->getIdentifierByObject($object), $fakeUuid);
110 }
111
112 /**
113 * @test
114 */
115 public function getIdentifierByObjectReturnsIdentifierForLazyObject()
116 {
117 $fakeUuid = 'fakeUuid';
118 $configurationManager = $this->createMock(ConfigurationManagerInterface::class);
119 $parentObject = new \stdClass();
120 $proxy = $this->getMockBuilder(LazyLoadingProxy::class)
121 ->setMethods(['_loadRealInstance'])
122 ->setConstructorArgs([$parentObject, 'y', 'z'])
123 ->disableProxyingToOriginalMethods()
124 ->getMock();
125 $session = $this->getMockBuilder('stdClass')
126 ->setMethods(['getIdentifierByObject'])
127 ->disableOriginalConstructor()
128 ->getMock();
129 $object = new \stdClass();
130
131 $referenceIndexProphecy = $this->prophesize(ReferenceIndex::class);
132 GeneralUtility::addInstance(ReferenceIndex::class, $referenceIndexProphecy->reveal());
133
134 $proxy->expects($this->once())->method('_loadRealInstance')->will($this->returnValue($object));
135 $session->expects($this->once())->method('getIdentifierByObject')->with($object)->will($this->returnValue($fakeUuid));
136
137 /** @var Backend $backend */
138 $backend = $this->getAccessibleMock(Backend::class, ['dummy'], [$configurationManager]);
139 $backend->_set('session', $session);
140
141 $this->assertEquals($backend->getIdentifierByObject($proxy), $fakeUuid);
142 }
143 }