[TASK] Replace inject methods with @inject
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / Generic / QueryTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009 Christopher Hlubek <hlubek@networkteam.com>
8 * (c) 2010 Bastian Waidelich <bastian@typo3.org>
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 2 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 class QueryTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
28
29 /**
30 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Query
31 */
32 protected $query;
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
36 */
37 protected $querySettings;
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
41 */
42 protected $objectManager;
43
44 /**
45 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
46 */
47 protected $persistenceManager;
48
49 /**
50 * @var \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
51 */
52 protected $backend;
53
54 /**
55 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
56 */
57 protected $dataMapper;
58
59 /**
60 * Sets up this test case
61 *
62 * @return void
63 */
64 public function setUp() {
65 $this->objectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
66
67 $this->query = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\Query', array('dummy'), array('someType'));
68 $this->query->_set('objectManager', $this->objectManager);
69 $this->querySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QuerySettingsInterface');
70 $this->query->_set('querySettings', $this->querySettings);
71 $this->persistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
72 $this->backend = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\BackendInterface');
73 $this->backend->expects($this->any())->method('getQomFactory')->will($this->returnValue(NULL));
74 $this->persistenceManager->expects($this->any())->method('getBackend')->will($this->returnValue($this->backend));
75 $this->query->_set('persistenceManager', $this->persistenceManager);
76 $this->dataMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper');
77 $this->query->_set('dataMapper', $this->dataMapper);
78 }
79
80 /**
81 * @test
82 */
83 public function executeReturnsQueryResultInstanceAndInjectsItself() {
84 $queryResult = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QueryResult', array(), array(), '', FALSE);
85 $this->objectManager->expects($this->once())->method('get')->with('TYPO3\\CMS\\Extbase\\Persistence\\QueryResultInterface', $this->query)->will($this->returnValue($queryResult));
86 $actualResult = $this->query->execute();
87 $this->assertSame($queryResult, $actualResult);
88 }
89
90 /**
91 * @test
92 */
93 public function executeReturnsRawObjectDataIfRawQueryResultSettingIsTrue() {
94 $this->querySettings->expects($this->once())->method('getReturnRawQueryResult')->will($this->returnValue(TRUE));
95 $this->persistenceManager->expects($this->once())->method('getObjectDataByQuery')->with($this->query)->will($this->returnValue('rawQueryResult'));
96 $expectedResult = 'rawQueryResult';
97 $actualResult = $this->query->execute();
98 $this->assertEquals($expectedResult, $actualResult);
99 }
100
101 /**
102 * @test
103 * @expectedException \InvalidArgumentException
104 */
105 public function setLimitAcceptsOnlyIntegers() {
106 $this->query->setLimit(1.5);
107 }
108
109 /**
110 * @test
111 * @expectedException \InvalidArgumentException
112 */
113 public function setLimitRejectsIntegersLessThanOne() {
114 $this->query->setLimit(0);
115 }
116
117 /**
118 * @test
119 * @expectedException \InvalidArgumentException
120 */
121 public function setOffsetAcceptsOnlyIntegers() {
122 $this->query->setOffset(1.5);
123 }
124
125 /**
126 * @test
127 * @expectedException \InvalidArgumentException
128 */
129 public function setOffsetRejectsIntegersLessThanZero() {
130 $this->query->setOffset(-1);
131 }
132
133 /**
134 * @return array
135 */
136 public function equalsForCaseSensitiveFalseLowercasesOperandProvider() {
137 return array(
138 'Polish alphabet' => array('name', 'ĄĆĘŁŃÓŚŹŻABCDEFGHIJKLMNOPRSTUWYZQXVąćęłńóśźżabcdefghijklmnoprstuwyzqxv', 'ąćęłńóśźżabcdefghijklmnoprstuwyzqxvąćęłńóśźżabcdefghijklmnoprstuwyzqxv'),
139 'German alphabet' => array('name', 'ßÜÖÄüöä', 'ßüöäüöä'),
140 'Greek alphabet' => array('name', 'Τάχιστη αλώπηξ βαφής ψημένη γη', 'τάχιστη αλώπηξ βαφής ψημένη γη'),
141 'Russian alphabet' => array('name', 'АВСТРАЛИЯавстралия', 'австралияавстралия')
142 );
143 }
144
145 /**
146 * Checks if equals condition makes utf-8 argument lowercase correctly
147 *
148 * @test
149 * @dataProvider equalsForCaseSensitiveFalseLowercasesOperandProvider
150 * @param string $propertyName The name of the property to compare against
151 * @param mixed $operand The value to compare with
152 * @param string $expectedOperand
153 */
154 public function equalsForCaseSensitiveFalseLowercasesOperand($propertyName, $operand, $expectedOperand) {
155 /** @var $qomFactory \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory */
156 $qomFactory = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Qom\\QueryObjectModelFactory', array('comparison'));
157 $qomFactory->_set('objectManager', \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'));
158 $qomFactory->expects($this->once())->method('comparison')->with($this->anything(), $this->anything(), $expectedOperand);
159 $this->query->_set('qomFactory', $qomFactory);
160 $this->query->equals($propertyName, $operand, FALSE);
161 }
162 }
163
164 ?>