‪TYPO3CMS  10.4
BackendTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
30 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31 
35 class ‪BackendTest extends UnitTestCase
36 {
37  protected function ‪tearDown(): void
38  {
39  GeneralUtility::purgeInstances();
40  parent::tearDown();
41  }
42 
47  {
48  /* \TYPO3\CMS\Extbase\Persistence\Generic\Backend|\PHPUnit\Framework\MockObject\MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface */
49  $fixture = $this->getAccessibleMock(Backend::class, ['dummy'], [], '', false);
50  /* \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper|\PHPUnit\Framework\MockObject\MockObject */
51  $dataMapFactory = $this->createMock(DataMapFactory::class);
52  /* \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap|\PHPUnit\Framework\MockObject\MockObject */
53  $dataMap = $this->createMock(DataMap::class);
54  /* \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap|\PHPUnit\Framework\MockObject\MockObject */
55  $columnMap = $this->createMock(ColumnMap::class);
56  /* \TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface|\PHPUnit\Framework\MockObject\MockObject */
57  $storageBackend = $this->createMock(BackendInterface::class);
58  /* \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface|\PHPUnit\Framework\MockObject\MockObject */
59  $domainObject = $this->createMock(DomainObjectInterface::class);
60 
61  $mmMatchFields = [
62  'identifier' => 'myTable:myField',
63  ];
64 
65  $expectedRow = [
66  'identifier' => 'myTable:myField',
67  '' => 0
68  ];
69 
70  $columnMap
71  ->expects(self::once())
72  ->method('getRelationTableName')
73  ->willReturn('myTable');
74  $columnMap
75  ->expects(self::once())
76  ->method('getRelationTableMatchFields')
77  ->willReturn($mmMatchFields);
78  $columnMap
79  ->expects(self::any())
80  ->method('getChildSortByFieldName')
81  ->willReturn('');
82  $dataMap
83  ->expects(self::any())
84  ->method('getColumnMap')
85  ->willReturn($columnMap);
86  $dataMapFactory
87  ->expects(self::any())
88  ->method('buildDataMap')
89  ->willReturn($dataMap);
90  $storageBackend
91  ->expects(self::once())
92  ->method('addRow')
93  ->with('myTable', $expectedRow, true);
94 
95  $fixture->_set('dataMapFactory', $dataMapFactory);
96  $fixture->_set('storageBackend', $storageBackend);
97  $fixture->_call('insertRelationInRelationtable', $domainObject, $domainObject, '');
98  }
99 
104  {
105  $fakeUuid = 'fakeUuid';
106  $configurationManager = $this->createMock(ConfigurationManagerInterface::class);
107  $session = $this->getMockBuilder('stdClass')
108  ->setMethods(['getIdentifierByObject'])
109  ->disableOriginalConstructor()
110  ->getMock();
111  $object = new \stdClass();
112 
113  $referenceIndexProphecy = $this->prophesize(ReferenceIndex::class);
114  GeneralUtility::addInstance(ReferenceIndex::class, $referenceIndexProphecy->reveal());
115 
116  $session->expects(self::once())->method('getIdentifierByObject')->with($object)->willReturn($fakeUuid);
117 
119  $backend = $this->getAccessibleMock(Backend::class, ['dummy'], [$configurationManager], '', false);
120  $backend->_set('session', $session);
121 
122  self::assertEquals($backend->getIdentifierByObject($object), $fakeUuid);
123  }
124 
129  {
130  $fakeUuid = 'fakeUuid';
131  $configurationManager = $this->createMock(ConfigurationManagerInterface::class);
132  $parentObject = new \stdClass();
133  $proxy = $this->getMockBuilder(LazyLoadingProxy::class)
134  ->setMethods(['_loadRealInstance'])
135  ->disableOriginalConstructor()
136  ->disableProxyingToOriginalMethods()
137  ->getMock();
138  $session = $this->getMockBuilder('stdClass')
139  ->setMethods(['getIdentifierByObject'])
140  ->disableOriginalConstructor()
141  ->getMock();
142  $object = new \stdClass();
143 
144  $referenceIndexProphecy = $this->prophesize(ReferenceIndex::class);
145  GeneralUtility::addInstance(ReferenceIndex::class, $referenceIndexProphecy->reveal());
146 
147  $proxy->expects(self::once())->method('_loadRealInstance')->willReturn($object);
148  $session->expects(self::once())->method('getIdentifierByObject')->with($object)->willReturn($fakeUuid);
149 
151  $backend = $this->getAccessibleMock(Backend::class, ['dummy'], [$configurationManager], '', false);
152  $backend->_set('session', $session);
153 
154  self::assertEquals($backend->getIdentifierByObject($proxy), $fakeUuid);
155  }
156 }
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\BackendTest\getIdentifierByObjectReturnsIdentifierForLazyObject
‪getIdentifierByObjectReturnsIdentifierForLazyObject()
Definition: BackendTest.php:128
‪TYPO3\CMS\Extbase\Persistence\Generic\Backend
Definition: Backend.php:51
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\BackendTest
Definition: BackendTest.php:36
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap
Definition: DataMap.php:23
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\BackendTest\tearDown
‪tearDown()
Definition: BackendTest.php:37
‪TYPO3\CMS\Core\Database\ReferenceIndex
Definition: ReferenceIndex.php:48
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\BackendTest\insertRelationInRelationtableSetsMmMatchFieldsInRow
‪insertRelationInRelationtableSetsMmMatchFieldsInRow()
Definition: BackendTest.php:46
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap
Definition: ColumnMap.php:28
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:28
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory
Definition: DataMapFactory.php:42
‪TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
Definition: DomainObjectInterface.php:29
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic
Definition: BackendTest.php:18
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy
Definition: LazyLoadingProxy.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\BackendTest\getIdentifierByObjectReturnsIdentifierForNonLazyObject
‪getIdentifierByObjectReturnsIdentifierForNonLazyObject()
Definition: BackendTest.php:103
‪TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface
Definition: BackendInterface.php:27