‪TYPO3CMS  11.5
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 
20 use Prophecy\PhpUnit\ProphecyTrait;
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33 
34 class ‪BackendTest extends UnitTestCase
35 {
36  use ProphecyTrait;
37 
38  protected function ‪tearDown(): void
39  {
40  GeneralUtility::purgeInstances();
41  parent::tearDown();
42  }
43 
48  {
49  /* \TYPO3\CMS\Extbase\Persistence\Generic\Backend|\PHPUnit\Framework\MockObject\MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface */
50  $fixture = $this->getAccessibleMock(Backend::class, ['dummy'], [], '', false);
51  /* \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper|\PHPUnit\Framework\MockObject\MockObject */
52  $dataMapFactory = $this->createMock(DataMapFactory::class);
53  /* \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap|\PHPUnit\Framework\MockObject\MockObject */
54  $dataMap = $this->createMock(DataMap::class);
55  /* \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap|\PHPUnit\Framework\MockObject\MockObject */
56  $columnMap = $this->createMock(ColumnMap::class);
57  /* \TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface|\PHPUnit\Framework\MockObject\MockObject */
58  $storageBackend = $this->createMock(BackendInterface::class);
59  /* \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface|\PHPUnit\Framework\MockObject\MockObject */
60  $domainObject = $this->createMock(DomainObjectInterface::class);
61 
62  $mmMatchFields = [
63  'identifier' => 'myTable:myField',
64  ];
65 
66  $expectedRow = [
67  'identifier' => 'myTable:myField',
68  '' => 0,
69  ];
70 
71  $columnMap
72  ->expects(self::once())
73  ->method('getRelationTableName')
74  ->willReturn('myTable');
75  $columnMap
76  ->expects(self::once())
77  ->method('getRelationTableMatchFields')
78  ->willReturn($mmMatchFields);
79  $columnMap
80  ->method('getChildSortByFieldName')
81  ->willReturn('');
82  $dataMap
83  ->method('getColumnMap')
84  ->willReturn($columnMap);
85  $dataMapFactory
86  ->method('buildDataMap')
87  ->willReturn($dataMap);
88  $storageBackend
89  ->expects(self::once())
90  ->method('addRow')
91  ->with('myTable', $expectedRow, true);
92 
93  $fixture->_set('dataMapFactory', $dataMapFactory);
94  $fixture->_set('storageBackend', $storageBackend);
95  $fixture->_call('insertRelationInRelationtable', $domainObject, $domainObject, '');
96  }
97 
102  {
103  $session = $this->createMock(Session::class);
104  $session->expects(self::never())->method('getIdentifierByObject');
105 
106  $backend = $this->getAccessibleMock(Backend::class, null, [$this->createMock(ConfigurationManagerInterface::class)], '', false);
107  $backend->_set('session', $session);
108 
109  self::assertNull($backend->getIdentifierByObject('invalidObject'));
110  }
111 
116  {
117  $fakeUuid = 'fakeUuid';
118  $configurationManager = $this->createMock(ConfigurationManagerInterface::class);
119  $session = $this->getMockBuilder(\stdClass::class)
120  ->addMethods(['getIdentifierByObject'])
121  ->disableOriginalConstructor()
122  ->getMock();
123  $object = new \stdClass();
124 
125  $referenceIndexProphecy = $this->prophesize(ReferenceIndex::class);
126  GeneralUtility::addInstance(ReferenceIndex::class, $referenceIndexProphecy->reveal());
127 
128  $session->expects(self::once())->method('getIdentifierByObject')->with($object)->willReturn($fakeUuid);
129 
130  $backend = $this->getAccessibleMock(Backend::class, ['dummy'], [$configurationManager], '', false);
131  $backend->_set('session', $session);
132 
133  self::assertEquals($backend->getIdentifierByObject($object), $fakeUuid);
134  }
135 
140  {
141  $fakeUuid = 'fakeUuid';
142  $configurationManager = $this->createMock(ConfigurationManagerInterface::class);
143  $proxy = $this->getMockBuilder(LazyLoadingProxy::class)
144  ->onlyMethods(['_loadRealInstance'])
145  ->disableOriginalConstructor()
146  ->disableProxyingToOriginalMethods()
147  ->getMock();
148  $session = $this->getMockBuilder(\stdClass::class)
149  ->addMethods(['getIdentifierByObject'])
150  ->disableOriginalConstructor()
151  ->getMock();
152  $object = new \stdClass();
153 
154  $referenceIndexProphecy = $this->prophesize(ReferenceIndex::class);
155  GeneralUtility::addInstance(ReferenceIndex::class, $referenceIndexProphecy->reveal());
156 
157  $proxy->expects(self::once())->method('_loadRealInstance')->willReturn($object);
158  $session->expects(self::once())->method('getIdentifierByObject')->with($object)->willReturn($fakeUuid);
159 
160  $backend = $this->getAccessibleMock(Backend::class, ['dummy'], [$configurationManager], '', false);
161  $backend->_set('session', $session);
162 
163  self::assertEquals($backend->getIdentifierByObject($proxy), $fakeUuid);
164  }
165 }
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\BackendTest\getIdentifierByObjectReturnsIdentifierForLazyObject
‪getIdentifierByObjectReturnsIdentifierForLazyObject()
Definition: BackendTest.php:138
‪TYPO3\CMS\Extbase\Persistence\Generic\Backend
Definition: Backend.php:49
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\BackendTest
Definition: BackendTest.php:35
‪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:42
‪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\Tests\Unit\Persistence\Generic\BackendTest\getIdentifierByObjectWithStringInsteadOfObjectReturnsNull
‪getIdentifierByObjectWithStringInsteadOfObjectReturnsNull()
Definition: BackendTest.php:100
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory
Definition: DataMapFactory.php:39
‪TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
Definition: DomainObjectInterface.php:29
‪TYPO3\CMS\Extbase\Persistence\Generic\Session
Definition: Session.php:27
‪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:50
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\BackendTest\getIdentifierByObjectReturnsIdentifierForNonLazyObject
‪getIdentifierByObjectReturnsIdentifierForNonLazyObject()
Definition: BackendTest.php:114
‪TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface
Definition: BackendInterface.php:27