‪TYPO3CMS  11.5
Typo3DatabaseBackendTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Prophecy\Argument;
21 use Prophecy\PhpUnit\ProphecyTrait;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
34 class ‪Typo3DatabaseBackendTest extends UnitTestCase
35 {
36  use ProphecyTrait;
37 
38  protected ‪$resetSingletonInstances = true;
39 
43  public function ‪setCacheCalculatesCacheTableName(): void
44  {
45  $frontendProphecy = $this->prophesize(FrontendInterface::class);
46  $frontendProphecy->getIdentifier()->willReturn('test');
47 
48  $subject = new ‪Typo3DatabaseBackend('Testing');
49  $subject->setCache($frontendProphecy->reveal());
50 
51  self::assertEquals('cache_test', $subject->getCacheTable());
52  }
53 
57  public function ‪setCacheCalculatesTagsTableName(): void
58  {
59  $frontendProphecy = $this->prophesize(FrontendInterface::class);
60  $frontendProphecy->getIdentifier()->willReturn('test');
61 
62  $subject = new ‪Typo3DatabaseBackend('Testing');
63  $subject->setCache($frontendProphecy->reveal());
64 
65  self::assertEquals('cache_test_tags', $subject->getTagsTable());
66  }
67 
71  public function ‪setThrowsExceptionIfFrontendWasNotSet(): void
72  {
73  $subject = new ‪Typo3DatabaseBackend('Testing');
74  $this->expectException(Exception::class);
75  $this->expectExceptionCode(1236518288);
76  $subject->set('identifier', 'data');
77  }
78 
82  public function ‪setThrowsExceptionIfDataIsNotAString(): void
83  {
84  $frontendProphecy = $this->prophesize(FrontendInterface::class);
85  $frontendProphecy->getIdentifier()->willReturn('test');
86 
87  $subject = new Typo3DatabaseBackend('Testing');
88  $subject->setCache($frontendProphecy->reveal());
89 
90  $this->expectException(InvalidDataException::class);
91  $this->expectExceptionCode(1236518298);
92 
93  $subject->set('identifier', ['iAmAnArray']);
94  }
95 
99  public function ‪getThrowsExceptionIfFrontendWasNotSet(): void
100  {
101  $subject = new Typo3DatabaseBackend('Testing');
102  $this->expectException(Exception::class);
103  $this->expectExceptionCode(1236518288);
104  $subject->get('identifier');
105  }
106 
110  public function ‪hasThrowsExceptionIfFrontendWasNotSet(): void
111  {
112  $subject = new Typo3DatabaseBackend('Testing');
113  $this->expectException(Exception::class);
114  $this->expectExceptionCode(1236518288);
115  $subject->has('identifier');
116  }
117 
121  public function ‪removeThrowsExceptionIfFrontendWasNotSet(): void
122  {
123  $subject = new Typo3DatabaseBackend('Testing');
124  $this->expectException(Exception::class);
125  $this->expectExceptionCode(1236518288);
126  $subject->remove('identifier');
127  }
128 
133  {
134  $subject = new Typo3DatabaseBackend('Testing');
135  $this->expectException(Exception::class);
136  $this->expectExceptionCode(1236518288);
137  $subject->collectGarbage();
138  }
139 
144  {
145  $subject = new Typo3DatabaseBackend('Testing');
146  $this->expectException(Exception::class);
147  $this->expectExceptionCode(1236518288);
148  $subject->findIdentifiersByTag('identifier');
149  }
150 
154  public function ‪flushThrowsExceptionIfFrontendWasNotSet(): void
155  {
156  $subject = new Typo3DatabaseBackend('Testing');
157  $this->expectException(Exception::class);
158  $this->expectExceptionCode(1236518288);
159  $subject->flush();
160  }
161 
165  public function ‪flushRemovesAllCacheEntries(): void
166  {
167  $frontendProphecy = $this->prophesize(FrontendInterface::class);
168  $frontendProphecy->getIdentifier()->willReturn('test');
169 
170  $subject = new Typo3DatabaseBackend('Testing');
171  $subject->setCache($frontendProphecy->reveal());
172 
173  $connectionProphet = $this->prophesize(Connection::class);
174  $connectionProphet->truncate('cache_test')->shouldBeCalled()->willReturn(0);
175  $connectionProphet->truncate('cache_test_tags')->shouldBeCalled()->willReturn(0);
176 
177  $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
178  $connectionPoolProphet->getConnectionForTable(Argument::cetera())->willReturn($connectionProphet->reveal());
179 
180  // Two instances are required as there are different tables being cleared
181  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
182  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
183 
184  $subject->flush();
185  }
186 
187  public function ‪flushByTagCallsDeleteOnConnection(): void
188  {
189  $frontendProphecy = $this->prophesize(FrontendInterface::class);
190  $frontendProphecy->getIdentifier()->willReturn('test');
191 
192  $subject = new ‪Typo3DatabaseBackend('Testing');
193  $subject->setCache($frontendProphecy->reveal());
194 
195  $connectionProphet = $this->prophesize(Connection::class);
196  $connectionProphet->delete('cache_test')->shouldBeCalled()->willReturn(0);
197  $connectionProphet->delete('cache_test_tags')->shouldBeCalled()->willReturn(0);
198 
199  $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
200  $connectionPoolProphet->getConnectionForTable(Argument::cetera())->willReturn($connectionProphet->reveal());
201 
202  // Two instances are required as there are different tables being cleared
203  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
204  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
205 
206  $subject->flushByTag('Tag');
207  }
208 
209  public function ‪flushByTagsCallsDeleteOnConnection(): void
210  {
211  $frontendProphecy = $this->prophesize(FrontendInterface::class);
212  $frontendProphecy->getIdentifier()->willReturn('test');
213 
214  $subject = new ‪Typo3DatabaseBackend('Testing');
215  $subject->setCache($frontendProphecy->reveal());
216 
217  $connectionProphet = $this->prophesize(Connection::class);
218  $connectionProphet->delete('cache_test')->shouldBeCalled()->willReturn(0);
219  $connectionProphet->delete('cache_test_tags')->shouldBeCalled()->willReturn(0);
220 
221  $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
222  $connectionPoolProphet->getConnectionForTable(Argument::cetera())->willReturn($connectionProphet->reveal());
223 
224  // Two instances are required as there are different tables being cleared
225  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
226  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
227 
228  $subject->flushByTag(['Tag1', 'Tag2']);
229  }
230 
235  {
236  $subject = new ‪Typo3DatabaseBackend('Testing');
237  $this->expectException(Exception::class);
238  $this->expectExceptionCode(1236518288);
239  $subject->flushByTag('Tag');
240  }
245  {
246  $subject = new ‪Typo3DatabaseBackend('Testing');
247  $this->expectException(Exception::class);
248  $this->expectExceptionCode(1236518288);
249  $subject->flushByTags([]);
250  }
251 }
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: Typo3DatabaseBackendTest.php:37
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend
Definition: AbstractBackendTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\setCacheCalculatesCacheTableName
‪setCacheCalculatesCacheTableName()
Definition: Typo3DatabaseBackendTest.php:42
‪TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend
Definition: Typo3DatabaseBackend.php:30
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest
Definition: Typo3DatabaseBackendTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\setCacheCalculatesTagsTableName
‪setCacheCalculatesTagsTableName()
Definition: Typo3DatabaseBackendTest.php:56
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\flushThrowsExceptionIfFrontendWasNotSet
‪flushThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:153
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\flushByTagCallsDeleteOnConnection
‪flushByTagCallsDeleteOnConnection()
Definition: Typo3DatabaseBackendTest.php:186
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\findIdentifiersByTagThrowsExceptionIfFrontendWasNotSet
‪findIdentifiersByTagThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:142
‪TYPO3\CMS\Core\Cache\Exception
Definition: DuplicateIdentifierException.php:16
‪TYPO3\CMS\Core\Cache\Exception\InvalidDataException
Definition: InvalidDataException.php:23
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\flushByTagsThrowsExceptionIfFrontendWasNotSet
‪flushByTagsThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:243
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\removeThrowsExceptionIfFrontendWasNotSet
‪removeThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:120
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\hasThrowsExceptionIfFrontendWasNotSet
‪hasThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:109
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\setThrowsExceptionIfFrontendWasNotSet
‪setThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:70
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\flushRemovesAllCacheEntries
‪flushRemovesAllCacheEntries()
Definition: Typo3DatabaseBackendTest.php:164
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\flushByTagsCallsDeleteOnConnection
‪flushByTagsCallsDeleteOnConnection()
Definition: Typo3DatabaseBackendTest.php:208
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\setThrowsExceptionIfDataIsNotAString
‪setThrowsExceptionIfDataIsNotAString()
Definition: Typo3DatabaseBackendTest.php:81
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\getThrowsExceptionIfFrontendWasNotSet
‪getThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:98
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\flushByTagThrowsExceptionIfFrontendWasNotSet
‪flushByTagThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:233
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\collectGarbageThrowsExceptionIfFrontendWasNotSet
‪collectGarbageThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:131