b8910743a47b0731cf166eef70dbb38ccb531b14
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / Generic / BackendTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic;
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 /**
18 * Test case
19 */
20 class BackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
21
22 /**
23 * @test
24 */
25 public function insertRelationInRelationtableSetsMmMatchFieldsInRow() {
26 /* \TYPO3\CMS\Extbase\Persistence\Generic\Backend|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
27 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend', array('dummy'), array(), '', FALSE);
28 /* \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper|\PHPUnit_Framework_MockObject_MockObject */
29 $dataMapper = $this->getMock('TYPO3\\CMS\Extbase\\Persistence\\Generic\\Mapper\\DataMapper');
30 /* \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap|\PHPUnit_Framework_MockObject_MockObject */
31 $dataMap = $this->getMock('TYPO3\\CMS\Extbase\\Persistence\\Generic\\Mapper\\DataMap', array(), array(), '', FALSE);
32 /* \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap|\PHPUnit_Framework_MockObject_MockObject */
33 $columnMap = $this->getMock('TYPO3\\CMS\Extbase\\Persistence\\Generic\\Mapper\\ColumnMap', array(), array(), '', FALSE);
34 /* \TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface|\PHPUnit_Framework_MockObject_MockObject */
35 $storageBackend = $this->getMock('TYPO3\\CMS\Extbase\\Persistence\\Generic\\Storage\\BackendInterface');
36 /* \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
37 $domainObject = $this->getMock('TYPO3\\CMS\Extbase\\DomainObject\\DomainObjectInterface');
38
39 $mmMatchFields = array(
40 'identifier' => 'myTable:myField',
41 );
42
43 $expectedRow = array(
44 'identifier' => 'myTable:myField',
45 '' => 0
46 );
47
48 $columnMap
49 ->expects($this->once())
50 ->method('getRelationTableMatchFields')
51 ->will($this->returnValue($mmMatchFields));
52 $columnMap
53 ->expects($this->any())
54 ->method('getChildSortByFieldName')
55 ->will($this->returnValue(''));
56 $dataMap
57 ->expects($this->any())
58 ->method('getColumnMap')
59 ->will($this->returnValue($columnMap));
60 $dataMapper
61 ->expects($this->any())
62 ->method('getDataMap')
63 ->will($this->returnValue($dataMap));
64 $storageBackend
65 ->expects($this->once())
66 ->method('addRow')
67 ->with(NULL, $expectedRow, TRUE);
68
69 $fixture->_set('dataMapper', $dataMapper);
70 $fixture->_set('storageBackend', $storageBackend);
71 $fixture->_call('insertRelationInRelationtable', $domainObject, $domainObject, '');
72 }
73
74 /**
75 * @test
76 */
77 public function getPlainValueReturnsCorrectDateTimeFormat() {
78 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend', array('dummy'), array(), '', FALSE);
79 $columnMap = new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('column_name', 'propertyName');
80 $columnMap->setDateTimeStorageFormat('datetime');
81 $datetimeAsString = '2013-04-15 09:30:00';
82 $input = new \DateTime($datetimeAsString);
83 $this->assertEquals('2013-04-15 09:30:00', $fixture->_call('getPlainValue', $input, $columnMap));
84 $columnMap->setDateTimeStorageFormat('date');
85 $this->assertEquals('2013-04-15', $fixture->_call('getPlainValue', $input, $columnMap));
86 }
87
88 /**
89 * @test
90 */
91 public function getIdentifierByObjectReturnsIdentifierForNonlazyObject() {
92 $fakeUuid = 'fakeUuid';
93 $configurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
94 $session = $this->getMock('stdClass', array('getIdentifierByObject'), array(), '', FALSE);
95 $object = new \stdClass();
96
97 $session->expects($this->once())->method('getIdentifierByObject')->with($object)->will($this->returnValue($fakeUuid));
98
99 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Backend $backend */
100 $backend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend', array('dummy'), array($configurationManager));
101 $backend->_set('session', $session);
102
103 $this->assertEquals($backend->getIdentifierByObject($object), $fakeUuid);
104 }
105
106 /**
107 * @test
108 */
109 public function getIdentifierByObjectReturnsIdentifierForLazyObject() {
110 $fakeUuid = 'fakeUuid';
111 $configurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
112 $parentObject = new \stdClass();
113 $proxy = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy', array('_loadRealInstance'), array($parentObject, 'y', 'z'), '', FALSE);
114 $session = $this->getMock('stdClass', array('getIdentifierByObject'), array(), '', FALSE);
115 $object = new \stdClass();
116
117 $proxy->expects($this->once())->method('_loadRealInstance')->will($this->returnValue($object));
118 $session->expects($this->once())->method('getIdentifierByObject')->with($object)->will($this->returnValue($fakeUuid));
119
120 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Backend $backend */
121 $backend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend', array('dummy'), array($configurationManager));
122 $backend->_set('session', $session);
123
124 $this->assertEquals($backend->getIdentifierByObject($proxy), $fakeUuid);
125 }
126
127 }