TYPO3 CMS  TYPO3_7-6
QueryParserTest.php
Go to the documentation of this file.
1 <?php
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 
18 
20 {
24  protected $queryParser;
25 
29  protected $testExtensionsToLoad = ['typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example'];
30 
34  protected $coreExtensionsToLoad = ['extbase', 'fluid'];
35 
39  protected $objectManager;
40 
44  protected $blogRepository;
45 
49  protected function setUp()
50  {
51  parent::setUp();
52 
53  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/tags.xml');
54  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/tags-mm.xml');
55  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/persons.xml');
56  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/posts.xml');
57  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/post-tag-mm.xml');
58 
59  $this->objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
60  $this->queryParser = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class);
61  $this->blogRepository = $this->objectManager->get(\ExtbaseTeam\BlogExample\Domain\Repository\BlogRepository::class);
62  }
63 
68  {
69  $queryWithEquals = $this->blogRepository->createQuery();
70 
71  $queryWithEquals->matching(
72  $queryWithEquals->equals('uid', 1)
73  );
74 
75  list($hashWithEquals) = $this->queryParser->preparseQuery($queryWithEquals);
76 
77  $queryWithIn = $this->blogRepository->createQuery();
78 
79  $queryWithIn->matching(
80  $queryWithIn->in('uid', [1])
81  );
82 
83  list($hashWithIn) = $this->queryParser->preparseQuery($queryWithIn);
84 
85  $this->assertNotSame($hashWithEquals, $hashWithIn);
86  }
87 
92  {
93  $queryWithIsNull = $this->blogRepository->createQuery();
94 
95  $queryWithIsNull->matching(
96  $queryWithIsNull->equals('title', null)
97  );
98 
99  list($hashWithIsNull) = $this->queryParser->preparseQuery($queryWithIsNull);
100 
101  $queryWithoutIsNull = $this->blogRepository->createQuery();
102 
103  $queryWithoutIsNull->matching(
104  $queryWithoutIsNull->equals('title', '')
105  );
106 
107  list($hashWithoutIsNull) = $this->queryParser->preparseQuery($queryWithoutIsNull);
108 
109  $this->assertNotSame($hashWithIsNull, $hashWithoutIsNull);
110  }
111 
116  {
117  $queryCaseSensitiveFalse = $this->blogRepository->createQuery();
118 
119  $queryCaseSensitiveFalse->matching(
120  $queryCaseSensitiveFalse->equals('title', 'PoSt1', false)
121  );
122 
123  list($hashWithCaseSensitiveFalse) = $this->queryParser->preparseQuery($queryCaseSensitiveFalse);
124 
125  $queryCaseSensitiveTrue = $this->blogRepository->createQuery();
126 
127  $queryCaseSensitiveTrue->matching(
128  $queryCaseSensitiveTrue->equals('title', 'PoSt1', true)
129  );
130 
131  list($hashWithCaseSensitiveTrue) = $this->queryParser->preparseQuery($queryCaseSensitiveTrue);
132 
133  $this->assertNotSame($hashWithCaseSensitiveFalse, $hashWithCaseSensitiveTrue);
134  }
135 
139  public function queryWithMultipleRelationsToIdenticalTablesReturnsExpectedResultForOrQuery()
140  {
142  $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
143  $query = $postRepository->createQuery();
144  $query->matching(
145  $query->logicalAnd(
146  $query->equals('blog', 3),
147  $query->logicalOr(
148  $query->equals('tags.name', 'Tag12'),
149  $query->equals('author.tags.name', 'TagForAuthor1')
150  )
151  )
152  );
153  $result = $query->execute()->toArray();
154  $this->assertEquals(3, count($result));
155  }
156 
160  public function queryWithMultipleRelationsToIdenticalTablesReturnsExpectedResultForAndQuery()
161  {
163  $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
164  $query = $postRepository->createQuery();
165  $query->matching(
166  $query->logicalAnd(
167  $query->equals('blog', 3),
168  $query->equals('tags.name', 'Tag12'),
169  $query->equals('author.tags.name', 'TagForAuthor1')
170  )
171  );
172  $result = $query->execute()->toArray();
173  $this->assertEquals(1, count($result));
174  }
175 }