[TASK] Tests for Persistence\Generic\Backend::getIdentifierByObject
[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 * Copyright notice
6 *
7 * (c) 2012 Kai Vogel <kai.vogel@speedprogs.de>, Speedprogs.de
8 *
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 **********************************************************************/
27
28 /**
29 * Test case
30 */
31 class BackendTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
32
33 /**
34 * @test
35 */
36 public function insertRelationInRelationtableSetsMmMatchFieldsInRow() {
37 /* \TYPO3\CMS\Extbase\Persistence\Generic\Backend|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
38 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend', array('dummy'), array(), '', FALSE);
39 /* \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper|\PHPUnit_Framework_MockObject_MockObject */
40 $dataMapper = $this->getMock('TYPO3\\CMS\Extbase\\Persistence\\Generic\\Mapper\\DataMapper');
41 /* \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap|\PHPUnit_Framework_MockObject_MockObject */
42 $dataMap = $this->getMock('TYPO3\\CMS\Extbase\\Persistence\\Generic\\Mapper\\DataMap', array(), array(), '', FALSE);
43 /* \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap|\PHPUnit_Framework_MockObject_MockObject */
44 $columnMap = $this->getMock('TYPO3\\CMS\Extbase\\Persistence\\Generic\\Mapper\\ColumnMap', array(), array(), '', FALSE);
45 /* \TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface|\PHPUnit_Framework_MockObject_MockObject */
46 $storageBackend = $this->getMock('TYPO3\\CMS\Extbase\\Persistence\\Generic\\Storage\\BackendInterface');
47 /* \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface|\PHPUnit_Framework_MockObject_MockObject */
48 $domainObject = $this->getMock('TYPO3\\CMS\Extbase\\DomainObject\\DomainObjectInterface');
49
50 $mmMatchFields = array(
51 'identifier' => 'myTable:myField',
52 );
53
54 $expectedRow = array(
55 'identifier' => 'myTable:myField',
56 '' => 0
57 );
58
59 $columnMap
60 ->expects($this->once())
61 ->method('getRelationTableMatchFields')
62 ->will($this->returnValue($mmMatchFields));
63 $columnMap
64 ->expects($this->any())
65 ->method('getChildSortByFieldName')
66 ->will($this->returnValue(''));
67 $dataMap
68 ->expects($this->any())
69 ->method('getColumnMap')
70 ->will($this->returnValue($columnMap));
71 $dataMapper
72 ->expects($this->any())
73 ->method('getDataMap')
74 ->will($this->returnValue($dataMap));
75 $storageBackend
76 ->expects($this->once())
77 ->method('addRow')
78 ->with(NULL, $expectedRow, TRUE);
79
80 $fixture->_set('dataMapper', $dataMapper);
81 $fixture->_set('storageBackend', $storageBackend);
82 $fixture->_call('insertRelationInRelationtable', $domainObject, $domainObject, '');
83 }
84
85 /**
86 * @test
87 */
88 public function getPlainValueReturnsCorrectDateTimeFormat() {
89 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend', array('dummy'), array(), '', FALSE);
90 $columnMap = new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('column_name', 'propertyName');
91 $columnMap->setDateTimeStorageFormat('datetime');
92 $datetimeAsString = '2013-04-15 09:30:00';
93 $input = new \DateTime($datetimeAsString);
94 $this->assertEquals('2013-04-15 09:30:00', $fixture->_call('getPlainValue', $input, $columnMap));
95 $columnMap->setDateTimeStorageFormat('date');
96 $this->assertEquals('2013-04-15', $fixture->_call('getPlainValue', $input, $columnMap));
97 }
98
99 /**
100 * @test
101 */
102 public function getIdentifierByObjectReturnsIdentifierForNonlazyObject() {
103 $fakeUuid = 'fakeUuid';
104 $configurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
105 $session = $this->getMock('stdClass', array('getIdentifierByObject'), array(), '', FALSE);
106 $object = new \stdClass();
107
108 $session->expects($this->once())->method('getIdentifierByObject')->with($object)->will($this->returnValue($fakeUuid));
109
110 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Backend $backend */
111 $backend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend', array('dummy'), array($configurationManager));
112 $backend->_set('session', $session);
113
114 $this->assertEquals($backend->getIdentifierByObject($object), $fakeUuid);
115 }
116
117 /**
118 * @test
119 */
120 public function getIdentifierByObjectReturnsIdentifierForLazyObject() {
121 $fakeUuid = 'fakeUuid';
122 $configurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
123 $parentObject = new \stdClass();
124 $proxy = $this->getMock('TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy', array('_loadRealInstance'), array($parentObject, 'y', 'z'), '', FALSE);
125 $session = $this->getMock('stdClass', array('getIdentifierByObject'), array(), '', FALSE);
126 $object = new \stdClass();
127
128 $proxy->expects($this->once())->method('_loadRealInstance')->will($this->returnValue($object));
129 $session->expects($this->once())->method('getIdentifierByObject')->with($object)->will($this->returnValue($fakeUuid));
130
131 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Backend $backend */
132 $backend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend', array('dummy'), array($configurationManager));
133 $backend->_set('session', $session);
134
135 $this->assertEquals($backend->getIdentifierByObject($proxy), $fakeUuid);
136 }
137
138 }