‪TYPO3CMS  ‪main
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 PHPUnit\Framework\Attributes\Test;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
30 final class ‪Typo3DatabaseBackendTest extends UnitTestCase
31 {
32  protected bool ‪$resetSingletonInstances = true;
33 
34  #[Test]
35  public function ‪setCacheCalculatesCacheTableName(): void
36  {
37  $frontend = new ‪NullFrontend('test');
38  $subject = new ‪Typo3DatabaseBackend('Testing');
39  $subject->setCache($frontend);
40  self::assertEquals('cache_test', $subject->getCacheTable());
41  }
42 
43  #[Test]
44  public function ‪setCacheCalculatesTagsTableName(): void
45  {
46  $frontend = new ‪NullFrontend('test');
47  $subject = new ‪Typo3DatabaseBackend('Testing');
48  $subject->setCache($frontend);
49 
50  self::assertEquals('cache_test_tags', $subject->getTagsTable());
51  }
52 
53  #[Test]
55  {
56  $subject = new ‪Typo3DatabaseBackend('Testing');
57  $this->expectException(Exception::class);
58  $this->expectExceptionCode(1236518288);
59  $subject->set('identifier', 'data');
60  }
61 
62  #[Test]
64  {
65  $this->expectException(InvalidDataException::class);
66  $this->expectExceptionCode(1236518298);
67  $frontend = new ‪NullFrontend('test');
68  $subject = new ‪Typo3DatabaseBackend('Testing');
69  $subject->setCache($frontend);
70  $subject->set('identifier', ['iAmAnArray']);
71  }
72 
73  #[Test]
75  {
76  $this->expectException(Exception::class);
77  $this->expectExceptionCode(1236518288);
78  $subject = new ‪Typo3DatabaseBackend('Testing');
79  $subject->get('identifier');
80  }
81 
82  #[Test]
84  {
85  $this->expectException(Exception::class);
86  $this->expectExceptionCode(1236518288);
87  $subject = new ‪Typo3DatabaseBackend('Testing');
88  $subject->has('identifier');
89  }
90 
91  #[Test]
93  {
94  $this->expectException(Exception::class);
95  $this->expectExceptionCode(1236518288);
96  $subject = new ‪Typo3DatabaseBackend('Testing');
97  $subject->remove('identifier');
98  }
99 
100  #[Test]
102  {
103  $this->expectException(Exception::class);
104  $this->expectExceptionCode(1236518288);
105  $subject = new ‪Typo3DatabaseBackend('Testing');
106  $subject->collectGarbage();
107  }
108 
109  #[Test]
111  {
112  $this->expectException(Exception::class);
113  $this->expectExceptionCode(1236518288);
114  $subject = new ‪Typo3DatabaseBackend('Testing');
115  $subject->findIdentifiersByTag('identifier');
116  }
117 
118  #[Test]
120  {
121  $this->expectException(Exception::class);
122  $this->expectExceptionCode(1236518288);
123  $subject = new ‪Typo3DatabaseBackend('Testing');
124  $subject->flush();
125  }
126 
127  #[Test]
128  public function ‪flushRemovesAllCacheEntries(): void
129  {
130  $frontend = new ‪NullFrontend('test');
131  $subject = new ‪Typo3DatabaseBackend('Testing');
132  $subject->setCache($frontend);
133 
134  $connectionMock = $this->createMock(Connection::class);
135  $series = [
136  ['cache_test'],
137  ['cache_test_tags'],
138  ];
139  $connectionMock->expects(self::exactly(2))->method('truncate')
140  ->willReturnCallback(function (string $table) use (&$series): int {
141  $arguments = array_shift($series);
142  self::assertSame($arguments[0], $table);
143  return 0;
144  });
145  $connectionPoolMock = $this->createMock(ConnectionPool::class);
146  $connectionPoolMock->method('getConnectionForTable')->with(self::anything())->willReturn($connectionMock);
147 
148  // Two instances are required as there are different tables being cleared
149  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolMock);
150  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolMock);
151 
152  $subject->flush();
153  }
154 
155  public function ‪flushByTagCallsDeleteOnConnection(): void
156  {
157  $frontend = new ‪NullFrontend('test');
158  $subject = new ‪Typo3DatabaseBackend('Testing');
159  $subject->setCache($frontend);
160 
161  $connectionMock = $this->createMock(Connection::class);
162  $connectionMock->expects(self::exactly(2))
163  ->method('delete')
164  ->willReturnMap(
165  [
166  ['cache_test', 0],
167  ['cache_test_tags', 0],
168  ]
169  );
170 
171  $connectionPoolMock = $this->createMock(ConnectionPool::class);
172  $connectionPoolMock->method('getConnectionForTable')->with(self::anything())->willReturn($connectionMock);
173 
174  // Two instances are required as there are different tables being cleared
175  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolMock);
176  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolMock);
177 
178  $subject->flushByTag('Tag');
179  }
180 
182  {
183  $frontend = new ‪NullFrontend('test');
184  $subject = new ‪Typo3DatabaseBackend('Testing');
185  $subject->setCache($frontend);
186 
187  $connectionMock = $this->createMock(Connection::class);
188  $connectionMock->expects(self::exactly(2))
189  ->method('delete')
190  ->willReturnMap(
191  [
192  ['cache_test', 0],
193  ['cache_test_tags', 0],
194  ]
195  );
196 
197  $connectionPoolMock = $this->createMock(ConnectionPool::class);
198  $connectionPoolMock->method('getConnectionForTable')->with(self::anything())->willReturn($connectionMock);
199 
200  // Two instances are required as there are different tables being cleared
201  GeneralUtility::addInstance(ConnectionPool::class, $connectionMock);
202  GeneralUtility::addInstance(ConnectionPool::class, $connectionMock);
203 
204  $subject->flushByTags(['Tag1', 'Tag2']);
205  }
206 
207  #[Test]
209  {
210  $this->expectException(Exception::class);
211  $this->expectExceptionCode(1236518288);
212  $subject = new ‪Typo3DatabaseBackend('Testing');
213  $subject->flushByTag('Tag');
214  }
215 
216  #[Test]
218  {
219  $this->expectException(Exception::class);
220  $this->expectExceptionCode(1236518288);
221  $subject = new ‪Typo3DatabaseBackend('Testing');
222  $subject->flushByTags([]);
223  }
224 }
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: Typo3DatabaseBackendTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend
Definition: AbstractBackendTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\setCacheCalculatesCacheTableName
‪setCacheCalculatesCacheTableName()
Definition: Typo3DatabaseBackendTest.php:35
‪TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend
Definition: Typo3DatabaseBackend.php:30
‪TYPO3\CMS\Core\Cache\Frontend\NullFrontend
Definition: NullFrontend.php:30
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest
Definition: Typo3DatabaseBackendTest.php:31
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\setCacheCalculatesTagsTableName
‪setCacheCalculatesTagsTableName()
Definition: Typo3DatabaseBackendTest.php:44
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\flushThrowsExceptionIfFrontendWasNotSet
‪flushThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:119
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\flushByTagCallsDeleteOnConnection
‪flushByTagCallsDeleteOnConnection()
Definition: Typo3DatabaseBackendTest.php:155
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\findIdentifiersByTagThrowsExceptionIfFrontendWasNotSet
‪findIdentifiersByTagThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:110
‪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:217
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\removeThrowsExceptionIfFrontendWasNotSet
‪removeThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:92
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\hasThrowsExceptionIfFrontendWasNotSet
‪hasThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:83
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\setThrowsExceptionIfFrontendWasNotSet
‪setThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:54
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\flushRemovesAllCacheEntries
‪flushRemovesAllCacheEntries()
Definition: Typo3DatabaseBackendTest.php:128
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\flushByTagsCallsDeleteOnConnection
‪flushByTagsCallsDeleteOnConnection()
Definition: Typo3DatabaseBackendTest.php:181
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\setThrowsExceptionIfDataIsNotAString
‪setThrowsExceptionIfDataIsNotAString()
Definition: Typo3DatabaseBackendTest.php:63
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\getThrowsExceptionIfFrontendWasNotSet
‪getThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:74
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\flushByTagThrowsExceptionIfFrontendWasNotSet
‪flushByTagThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:208
‪TYPO3\CMS\Core\Tests\Unit\Cache\Backend\Typo3DatabaseBackendTest\collectGarbageThrowsExceptionIfFrontendWasNotSet
‪collectGarbageThrowsExceptionIfFrontendWasNotSet()
Definition: Typo3DatabaseBackendTest.php:101