[TASK] Namespace classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / QueryTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence;
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 $this->query = new \TYPO3\CMS\Extbase\Persistence\Generic\Query('someType');
67 $this->query->injectObjectManager($this->objectManager);
68 $this->querySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QuerySettingsInterface');
69 $this->query->setQuerySettings($this->querySettings);
70 $this->persistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
71 $this->backend = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\BackendInterface');
72 $this->backend->expects($this->any())->method('getQomFactory')->will($this->returnValue(NULL));
73 $this->persistenceManager->expects($this->any())->method('getBackend')->will($this->returnValue($this->backend));
74 $this->query->injectPersistenceManager($this->persistenceManager);
75 $this->dataMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper');
76 $this->query->injectDataMapper($this->dataMapper);
77 }
78
79 /**
80 * @test
81 */
82 public function executeReturnsQueryResultInstanceAndInjectsItself() {
83 $queryResult = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QueryResult', array(), array(), '', FALSE);
84 $this->objectManager->expects($this->once())->method('create')->with('TYPO3\\CMS\\Extbase\\Persistence\\QueryResultInterface', $this->query)->will($this->returnValue($queryResult));
85 $actualResult = $this->query->execute();
86 $this->assertSame($queryResult, $actualResult);
87 }
88
89 /**
90 * @test
91 */
92 public function executeReturnsRawObjectDataIfRawQueryResultSettingIsTrue() {
93 $this->querySettings->expects($this->once())->method('getReturnRawQueryResult')->will($this->returnValue(TRUE));
94 $this->persistenceManager->expects($this->once())->method('getObjectDataByQuery')->with($this->query)->will($this->returnValue('rawQueryResult'));
95 $expectedResult = 'rawQueryResult';
96 $actualResult = $this->query->execute();
97 $this->assertEquals($expectedResult, $actualResult);
98 }
99
100 /**
101 * @test
102 * @expectedException InvalidArgumentException
103 */
104 public function setLimitAcceptsOnlyIntegers() {
105 $this->query->setLimit(1.5);
106 }
107
108 /**
109 * @test
110 * @expectedException InvalidArgumentException
111 */
112 public function setLimitRejectsIntegersLessThanOne() {
113 $this->query->setLimit(0);
114 }
115
116 /**
117 * @test
118 * @expectedException InvalidArgumentException
119 */
120 public function setOffsetAcceptsOnlyIntegers() {
121 $this->query->setOffset(1.5);
122 }
123
124 /**
125 * @test
126 * @expectedException InvalidArgumentException
127 */
128 public function setOffsetRejectsIntegersLessThanZero() {
129 $this->query->setOffset(-1);
130 }
131
132 /**
133 * @return array
134 */
135 public function equalsForCaseSensitiveFalseLowercasesOperandProvider() {
136 return array(
137 'Polish alphabet' => array('name', 'ĄĆĘŁŃÓŚŹŻABCDEFGHIJKLMNOPRSTUWYZQXVąćęłńóśźżabcdefghijklmnoprstuwyzqxv', 'ąćęłńóśźżabcdefghijklmnoprstuwyzqxvąćęłńóśźżabcdefghijklmnoprstuwyzqxv'),
138 'German alphabet' => array('name', 'ßÜÖÄüöä', 'ßüöäüöä'),
139 'Greek alphabet' => array('name', 'Τάχιστη αλώπηξ βαφής ψημένη γη', 'τάχιστη αλώπηξ βαφής ψημένη γη'),
140 'Russian alphabet' => array('name', 'АВСТРАЛИЯавстралия', 'австралияавстралия')
141 );
142 }
143
144 /**
145 * Checks if equals condition makes utf-8 argument lowercase correctly
146 *
147 * @test
148 * @dataProvider equalsForCaseSensitiveFalseLowercasesOperandProvider
149 * @param string $propertyName The name of the property to compare against
150 * @param mixed $operand The value to compare with
151 * @param string $expectedOperand
152 */
153 public function equalsForCaseSensitiveFalseLowercasesOperand($propertyName, $operand, $expectedOperand) {
154 /** @var $qomFactory \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory */
155 $qomFactory = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Qom\\QueryObjectModelFactory', array('comparison'));
156 $qomFactory->injectObjectManager(\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'));
157 $qomFactory->expects($this->once())->method('comparison')->with($this->anything(), $this->anything(), $expectedOperand);
158 $this->query->injectQomFactory($qomFactory);
159 $this->query->equals($propertyName, $operand, FALSE);
160 }
161
162 }
163
164
165 ?>