[FOLLOWUP] Add functional test for cache backend flushByTags 24/50924/9
authorClaus Due <claus@namelesscoder.net>
Sun, 11 Dec 2016 00:00:36 +0000 (01:00 +0100)
committerGeorg Ringer <georg.ringer@gmail.com>
Mon, 19 Dec 2016 06:26:01 +0000 (07:26 +0100)
Followup to https://review.typo3.org/#/c/50537/ to add
functional tests covering the new SQL operations.

Change-Id: I7a686fe2ed10d53506ff36d99a21f10379a9656d
Resolves: #78951
Releases: master
Reviewed-on: https://review.typo3.org/50924
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Claus Due <claus@phpmind.net>
Tested-by: Claus Due <claus@phpmind.net>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
typo3/sysext/core/Tests/Functional/Cache/Backend/Typo3DatabaseBackendTest.php

index e502cf5..a672cc8 100644 (file)
@@ -367,18 +367,11 @@ class Typo3DatabaseBackendTest extends FunctionalTestCase
      */
     public function findIdentifiersByTagReturnsIdentifierTaggedWithGivenTag()
     {
-        $frontendProphecy = $this->prophesize(FrontendInterface::class);
-        $frontendProphecy->getIdentifier()->willReturn('cache_pages');
-
-        $subject = new Typo3DatabaseBackend('Testing');
-        $subject->setCache($frontendProphecy->reveal());
-
-        $subject->set('idA', 'dataA', ['tagA', 'tagB']);
-        $subject->set('idB', 'dataB', ['tagB', 'tagC']);
+        $subject = $this->getSubjectObject();
 
         $this->assertSame(['idA' => 'idA'], $subject->findIdentifiersByTag('tagA'));
         $this->assertSame(['idA' => 'idA', 'idB' => 'idB'], $subject->findIdentifiersByTag('tagB'));
-        $this->assertSame(['idB' => 'idB'], $subject->findIdentifiersByTag('tagC'));
+        $this->assertSame(['idB' => 'idB', 'idC' => 'idC'], $subject->findIdentifiersByTag('tagC'));
     }
 
     /**
@@ -386,39 +379,25 @@ class Typo3DatabaseBackendTest extends FunctionalTestCase
      */
     public function flushByTagWorksWithEmptyCacheTablesWithMysql()
     {
-        $frontendProphecy = $this->prophesize(FrontendInterface::class);
-        $frontendProphecy->getIdentifier()->willReturn('cache_pages');
-
-        // Must be mocked here to test for "mysql" version implementation
-        $subject = $this->getMockBuilder(Typo3DatabaseBackend::class)
-            ->setMethods(['isConnectionMysql'])
-            ->setConstructorArgs(['Testing'])
-            ->getMock();
-        $subject->expects($this->once())->method('isConnectionMysql')->willReturn(true);
-        $subject->setCache($frontendProphecy->reveal());
-
+        $subject = $this->getSubjectObject(true);
         $subject->flushByTag('tagB');
     }
 
     /**
      * @test
      */
-    public function flushByTagRemovesCorrectRowsFromDatabaseWithMysql()
+    public function flushByTagsWorksWithEmptyCacheTablesWithMysql()
     {
-        $frontendProphecy = $this->prophesize(FrontendInterface::class);
-        $frontendProphecy->getIdentifier()->willReturn('cache_pages');
-
-        // Must be mocked here to test for "mysql" version implementation
-        $subject = $this->getMockBuilder(Typo3DatabaseBackend::class)
-            ->setMethods(['isConnectionMysql'])
-            ->setConstructorArgs(['Testing'])
-            ->getMock();
-        $subject->expects($this->once())->method('isConnectionMysql')->willReturn(true);
-        $subject->setCache($frontendProphecy->reveal());
+        $subject = $this->getSubjectObject(true);
+        $subject->flushByTags(['tagB']);
+    }
 
-        $subject->set('idA', 'dataA', ['tagA', 'tagB']);
-        $subject->set('idB', 'dataB', ['tagB', 'tagC']);
-        $subject->set('idC', 'dataC', ['tagC', 'tagD']);
+    /**
+     * @test
+     */
+    public function flushByTagRemovesCorrectRowsFromDatabaseWithMysql()
+    {
+        $subject = $this->getSubjectObject(true);
         $subject->flushByTag('tagB');
 
         $cacheTableConnection = (new ConnectionPool())->getConnectionForTable('cf_cache_pages');
@@ -434,41 +413,45 @@ class Typo3DatabaseBackendTest extends FunctionalTestCase
     /**
      * @test
      */
-    public function flushByTagWorksWithEmptyCacheTablesWithNonMysql()
+    public function flushByTagsRemovesCorrectRowsFromDatabaseWithMysql()
     {
-        $frontendProphecy = $this->prophesize(FrontendInterface::class);
-        $frontendProphecy->getIdentifier()->willReturn('cache_pages');
+        $subject = $this->getSubjectObject(true);
+        $subject->flushByTags(['tagC', 'tagD']);
 
-        // Must be mocked here to test for "mysql" version implementation
-        $subject = $this->getMockBuilder(Typo3DatabaseBackend::class)
-            ->setMethods(['isConnectionMysql'])
-            ->setConstructorArgs(['Testing'])
-            ->getMock();
-        $subject->expects($this->once())->method('isConnectionMysql')->willReturn(false);
-        $subject->setCache($frontendProphecy->reveal());
+        $cacheTableConnection = (new ConnectionPool())->getConnectionForTable('cf_cache_pages');
+        $this->assertSame(1, $cacheTableConnection->count('*', 'cf_cache_pages', ['identifier' => 'idA']));
+        $this->assertSame(0, $cacheTableConnection->count('*', 'cf_cache_pages', ['identifier' => 'idB']));
+        $this->assertSame(0, $cacheTableConnection->count('*', 'cf_cache_pages', ['identifier' => 'idC']));
+        $tagsTableConnection = (new ConnectionPool())->getConnectionForTable('cf_cache_pages_tags');
+        $this->assertSame(2, $tagsTableConnection->count('*', 'cf_cache_pages_tags', ['identifier' => 'idA']));
+        $this->assertSame(0, $tagsTableConnection->count('*', 'cf_cache_pages_tags', ['identifier' => 'idB']));
+        $this->assertSame(0, $tagsTableConnection->count('*', 'cf_cache_pages_tags', ['identifier' => 'idC']));
+    }
 
+    /**
+     * @test
+     */
+    public function flushByTagWorksWithEmptyCacheTablesWithNonMysql()
+    {
+        $subject = $this->getSubjectObject(true, false);
         $subject->flushByTag('tagB');
     }
 
     /**
      * @test
      */
-    public function flushByTagRemovesCorrectRowsFromDatabaseWithNonMysql()
+    public function flushByTagsWorksWithEmptyCacheTablesWithNonMysql()
     {
-        $frontendProphecy = $this->prophesize(FrontendInterface::class);
-        $frontendProphecy->getIdentifier()->willReturn('cache_pages');
-
-        // Must be mocked here to test for "mysql" version implementation
-        $subject = $this->getMockBuilder(Typo3DatabaseBackend::class)
-            ->setMethods(['isConnectionMysql'])
-            ->setConstructorArgs(['Testing'])
-            ->getMock();
-        $subject->expects($this->once())->method('isConnectionMysql')->willReturn(false);
-        $subject->setCache($frontendProphecy->reveal());
+        $subject = $this->getSubjectObject(true, false);
+        $subject->flushByTags(['tagB', 'tagC']);
+    }
 
-        $subject->set('idA', 'dataA', ['tagA', 'tagB']);
-        $subject->set('idB', 'dataB', ['tagB', 'tagC']);
-        $subject->set('idC', 'dataC', ['tagC', 'tagD']);
+    /**
+     * @test
+     */
+    public function flushByTagRemovesCorrectRowsFromDatabaseWithNonMysql()
+    {
+        $subject = $this->getSubjectObject(true, false);
         $subject->flushByTag('tagB');
 
         $cacheTableConnection = (new ConnectionPool())->getConnectionForTable('cf_cache_pages');
@@ -484,19 +467,27 @@ class Typo3DatabaseBackendTest extends FunctionalTestCase
     /**
      * @test
      */
-    public function collectGarbageWorksWithEmptyTableWithMysql()
+    public function flushByTagsRemovesCorrectRowsFromDatabaseWithNonMysql()
     {
-        $frontendProphecy = $this->prophesize(FrontendInterface::class);
-        $frontendProphecy->getIdentifier()->willReturn('cache_pages');
+        $subject = $this->getSubjectObject(true, false);
+        $subject->flushByTags(['tagC', 'tagD']);
 
-        // Must be mocked here to test for "mysql" version implementation
-        $subject = $this->getMockBuilder(Typo3DatabaseBackend::class)
-            ->setMethods(['isConnectionMysql'])
-            ->setConstructorArgs(['Testing'])
-            ->getMock();
-        $subject->expects($this->once())->method('isConnectionMysql')->willReturn(true);
-        $subject->setCache($frontendProphecy->reveal());
+        $cacheTableConnection = (new ConnectionPool())->getConnectionForTable('cf_cache_pages');
+        $this->assertSame(1, $cacheTableConnection->count('*', 'cf_cache_pages', ['identifier' => 'idA']));
+        $this->assertSame(0, $cacheTableConnection->count('*', 'cf_cache_pages', ['identifier' => 'idB']));
+        $this->assertSame(0, $cacheTableConnection->count('*', 'cf_cache_pages', ['identifier' => 'idC']));
+        $tagsTableConnection = (new ConnectionPool())->getConnectionForTable('cf_cache_pages_tags');
+        $this->assertSame(2, $tagsTableConnection->count('*', 'cf_cache_pages_tags', ['identifier' => 'idA']));
+        $this->assertSame(0, $tagsTableConnection->count('*', 'cf_cache_pages_tags', ['identifier' => 'idB']));
+        $this->assertSame(0, $tagsTableConnection->count('*', 'cf_cache_pages_tags', ['identifier' => 'idC']));
+    }
 
+    /**
+     * @test
+     */
+    public function collectGarbageWorksWithEmptyTableWithMysql()
+    {
+        $subject = $this->getSubjectObject(true);
         $subject->collectGarbage();
     }
 
@@ -749,4 +740,33 @@ class Typo3DatabaseBackendTest extends FunctionalTestCase
         $this->assertSame(0, $cacheTableConnection->count('*', 'cf_cache_pages', []));
         $this->assertSame(0, $tagsTableConnection->count('*', 'cf_cache_pages_tags', []));
     }
+
+    /**
+     * @param bool $returnMockObject
+     * @param bool $isConnectionMysql
+     *
+     * @return Typo3DatabaseBackend
+     */
+    protected function getSubjectObject($returnMockObject = false, $isConnectionMysql = true)
+    {
+        $frontendProphecy = $this->prophesize(FrontendInterface::class);
+        $frontendProphecy->getIdentifier()->willReturn('cache_pages');
+
+        if (!$returnMockObject) {
+            $subject = new Typo3DatabaseBackend('Testing');
+        } else {
+            $subject = $this->getMockBuilder(Typo3DatabaseBackend::class)
+                ->setMethods(['isConnectionMysql'])
+                ->setConstructorArgs(['Testing'])
+                ->getMock();
+            $subject->expects($this->once())->method('isConnectionMysql')->willReturn($isConnectionMysql);
+        }
+        $subject->setCache($frontendProphecy->reveal());
+
+        $subject->set('idA', 'dataA', ['tagA', 'tagB']);
+        $subject->set('idB', 'dataB', ['tagB', 'tagC']);
+        $subject->set('idC', 'dataC', ['tagC', 'tagD']);
+
+        return $subject;
+    }
 }