]> git.mxchange.org Git - friendica.git/commitdiff
Restructure tests
authorPhilipp <admin@philipp.info>
Mon, 13 Feb 2023 19:52:24 +0000 (20:52 +0100)
committerPhilipp <admin@philipp.info>
Mon, 13 Feb 2023 19:52:24 +0000 (20:52 +0100)
- Avoid database leftovers

tests/DatabaseTestTrait.php
tests/DiceHttpMockHandlerTrait.php
tests/FixtureTest.php
tests/FixtureTestTrait.php [new file with mode: 0644]
tests/datasets/api.fixture.php
tests/functional/DependencyCheckTest.php
tests/src/Console/ServerBlockConsoleTest.php
tests/src/Content/PageInfoTest.php
tests/src/Network/HTTPClient/Client/HTTPClientTest.php
tests/src/Network/ProbeTest.php
tests/src/Util/ImagesTest.php

index 4badf85e903d0218ac8da2f273ddeb3ca3383b9d..6d3a75ab1be203b4c8d6e99e9a29ad21c81ee1cf 100644 (file)
@@ -71,7 +71,11 @@ trait DatabaseTestTrait
                        }
 
                        foreach ($rows as $row) {
-                               $dba->insert($tableName, $row, true);
+                               if (is_array($row)) {
+                                       $dba->insert($tableName, $row, true);
+                               } else {
+                                       throw new \Exception('row isn\'t an array');
+                               }
                        }
                }
        }
index 581687a024739a261c7788aa7e2b34824e3dd87b..56250817d64a5bf6d64facfee277c6b0014d6f9a 100644 (file)
@@ -32,6 +32,8 @@ use GuzzleHttp\HandlerStack;
  */
 trait DiceHttpMockHandlerTrait
 {
+       use FixtureTestTrait;
+
        /**
         * Handler for mocking requests anywhere for testing purpose
         *
@@ -41,9 +43,7 @@ trait DiceHttpMockHandlerTrait
 
        protected function setupHttpMockHandler(): void
        {
-               if (!empty($this->httpRequestHandler) && $this->httpRequestHandler instanceof HandlerStack) {
-                       return;
-               }
+               $this->setUpFixtures();
 
                $this->httpRequestHandler = HandlerStack::create();
 
@@ -59,10 +59,8 @@ trait DiceHttpMockHandlerTrait
                DI::init($newDice);
        }
 
-       protected function tearDown(): void
+       protected function tearDownHandler(): void
        {
-               \Mockery::close();
-
-               parent::tearDown();
+               $this->tearDownFixtures();
        }
 }
index 8483aee089a3130a6d53650d781fc5ce1c5c3a90..e2e8ad725a393ff6cd94331673dba163839dfede 100644 (file)
@@ -39,66 +39,21 @@ use Friendica\Test\Util\VFSTrait;
 /**
  * Parent class for test cases requiring fixtures
  */
-abstract class FixtureTest extends DatabaseTest
+abstract class FixtureTest extends MockedTest
 {
-       use VFSTrait;
+       use FixtureTestTrait;
 
-       /** @var Dice */
-       protected $dice;
-
-       /**
-        * Create variables used by tests.
-        */
        protected function setUp(): void
        {
-               $this->setUpVfsDir();
-
                parent::setUp();
 
-               $server                   = $_SERVER;
-               $server['REQUEST_METHOD'] = Router::GET;
-
-               $this->dice = (new Dice())
-                       ->addRules(include __DIR__ . '/../static/dependencies.config.php')
-                       ->addRule(ConfigFileManager::class, [
-                               'instanceOf' => Config::class,
-                               'call'       => [['createConfigFileManager', [$this->root->url(), $server,],
-                                                                 Dice::CHAIN_CALL]]])
-                       ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
-                       ->addRule(IHandleSessions::class, ['instanceOf' => Memory::class, 'shared' => true, 'call' => null])
-                       ->addRule(Arguments::class, [
-                               'instanceOf' => Arguments::class,
-                               'call'       => [
-                                       ['determine', [$server, $_GET], Dice::CHAIN_CALL],
-                               ],
-                       ]);
-               DI::init($this->dice);
-
-               $config = $this->dice->create(IManageConfigValues::class);
-               $config->set('database', 'disable_pdo', true);
-
-               /** @var Database $dba */
-               $dba = $this->dice->create(Database::class);
-
-               $dba->setTestmode(true);
-
-               // Load the API dataset for the whole API
-               $this->loadFixture(__DIR__ . '/datasets/api.fixture.php', $dba);
+               $this->setUpFixtures();
        }
 
-       protected function useHttpMethod(string $method = Router::GET)
+       protected function tearDown(): void
        {
-               $server                   = $_SERVER;
-               $server['REQUEST_METHOD'] = $method;
-
-               $this->dice = $this->dice
-                       ->addRule(Arguments::class, [
-                               'instanceOf' => Arguments::class,
-                               'call'       => [
-                                       ['determine', [$server, $_GET], Dice::CHAIN_CALL],
-                               ],
-                       ]);
+               $this->tearDownFixtures();
 
-               DI::init($this->dice);
+               parent::tearDown();
        }
 }
diff --git a/tests/FixtureTestTrait.php b/tests/FixtureTestTrait.php
new file mode 100644 (file)
index 0000000..d8524ba
--- /dev/null
@@ -0,0 +1,87 @@
+<?php
+
+namespace Friendica\Test;
+
+use Dice\Dice;
+use Friendica\App\Arguments;
+use Friendica\App\Router;
+use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Config\Factory\Config;
+use Friendica\Core\Config\Util\ConfigFileManager;
+use Friendica\Core\Session\Capability\IHandleSessions;
+use Friendica\Core\Session\Type\Memory;
+use Friendica\Database\Database;
+use Friendica\Database\DBStructure;
+use Friendica\DI;
+use Friendica\Test\Util\Database\StaticDatabase;
+use Friendica\Test\Util\VFSTrait;
+
+trait FixtureTestTrait
+{
+       use VFSTrait;
+       use DatabaseTestTrait;
+
+       /** @var Dice */
+       protected $dice;
+
+       /**
+        * Create variables used by tests.
+        */
+       protected function setUpFixtures(): void
+       {
+               $this->setUpVfsDir();
+               $this->setUpDb();
+
+               $server                   = $_SERVER;
+               $server['REQUEST_METHOD'] = Router::GET;
+
+               $this->dice = (new Dice())
+                       ->addRules(include __DIR__ . '/../static/dependencies.config.php')
+                       ->addRule(ConfigFileManager::class, [
+                               'instanceOf' => Config::class,
+                               'call'       => [['createConfigFileManager', [$this->root->url(), $server,],
+                                                                 Dice::CHAIN_CALL]]])
+                       ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
+                       ->addRule(IHandleSessions::class, ['instanceOf' => Memory::class, 'shared' => true, 'call' => null])
+                       ->addRule(Arguments::class, [
+                               'instanceOf' => Arguments::class,
+                               'call'       => [
+                                       ['determine', [$server, $_GET], Dice::CHAIN_CALL],
+                               ],
+                       ]);
+               DI::init($this->dice, true);
+
+               $config = $this->dice->create(IManageConfigValues::class);
+               $config->set('database', 'disable_pdo', true);
+
+               /** @var Database $dba */
+               $dba = $this->dice->create(Database::class);
+               $dba->setTestmode(true);
+
+               DBStructure::checkInitialValues();
+
+               // Load the API dataset for the whole API
+               $this->loadFixture(__DIR__ . '/datasets/api.fixture.php', $dba);
+       }
+
+       protected function tearDownFixtures(): void
+       {
+               $this->tearDownDb();
+       }
+
+       protected function useHttpMethod(string $method = Router::GET)
+       {
+               $server                   = $_SERVER;
+               $server['REQUEST_METHOD'] = $method;
+
+               $this->dice = $this->dice
+                       ->addRule(Arguments::class, [
+                               'instanceOf' => Arguments::class,
+                               'call'       => [
+                                       ['determine', [$server, $_GET], Dice::CHAIN_CALL],
+                               ],
+                       ]);
+
+               DI::init($this->dice);
+       }
+}
index b50f706251e446111567f8ebfd9c1413459938c1..f1e5d012330ab9f4cb515fe59336535980457b6e 100644 (file)
@@ -35,6 +35,22 @@ return [
        'workerqueue',
        'mail',
        'post-delivery-data',
+       'gserver' => [
+               [
+                       'id'                    => 42,
+                       'url'                   => 'https://friendica.test',
+                       'info'                  => 'test_node',
+                       'active-week-users'     => 1,
+                       'active-month-users'    => 1,
+                       'active-halfyear-users' => 1,
+                       'local-posts'           => 42,
+                       'local-comments'        => 42,
+                       'protocol'              => 'apub',
+                       'detection-method'      => 'none',
+                       'blocked'               => 0,
+                       'failed'                => 0,
+               ],
+       ],
        // Base test config to avoid notice messages
        'user' => [
                [
index 27e693295ecd4af55311dfb57d24fb4c92e6d77c..89b8f79a542d85c11f76c0f3f9fbfcc5f1edfa3f 100644 (file)
@@ -21,7 +21,6 @@
 
 namespace Friendica\Test\functional;
 
-use Dice\Dice;
 use Friendica\App;
 use Friendica\Core\Cache\Capability\ICanCache;
 use Friendica\Core\Cache\Capability\ICanCacheInMemory;
@@ -29,37 +28,17 @@ use Friendica\Core\Config\ValueObject\Cache;
 use Friendica\Core\Config\Capability\IManageConfigValues;
 use Friendica\Core\Lock\Capability\ICanLock;
 use Friendica\Database\Database;
-use Friendica\Test\Util\VFSTrait;
+use Friendica\Test\FixtureTest;
 use Friendica\Util\BasePath;
 use Friendica\Core\Config\Util\ConfigFileManager;
-use PHPUnit\Framework\TestCase;
 use Psr\Log\LoggerInterface;
 
-class DependencyCheckTest extends TestCase
+class DependencyCheckTest extends FixtureTest
 {
-       use VFSTrait;
-
-       /**
-        * @var Dice
-        */
-       private $dice;
-
        protected function setUp() : void
        {
                parent::setUp();
 
-               $this->setUpVfsDir();
-
-               $this->dice = (new Dice())
-                       ->addRules(include __DIR__ . '/../../static/dependencies.config.php')
-                       ->addRule(BasePath::class, [
-                               'constructParams' => [
-                                       $this->root->url(),
-                                       [],
-                               ],
-                       ])
-                       ->addRule(LoggerInterface::class, ['constructParams' => ['test']]);
-
                /** @var IManageConfigValues $config */
                $config = $this->dice->create(IManageConfigValues::class);
                $config->set('system', 'logfile', $this->root->url() . '/logs/friendica.log');
@@ -75,6 +54,9 @@ class DependencyCheckTest extends TestCase
 
                self::assertInstanceOf(BasePath::class, $basePath);
                self::assertEquals($this->root->url(), $basePath->getPath());
+
+               /** @var Database $dba */
+               $dba = $this->dice->create(Database::class);
        }
 
        /**
index e8d75863c31760025c227d0f17cf18abb2c9cdf7..6179cf733e606b331e216f55f1d0ffd3f1c223dc 100644 (file)
 
 namespace Friendica\Test\src\Console;
 
-use Dice\Dice;
 use Friendica\Console\ServerBlock;
-use Friendica\Core\Config\Capability\IManageConfigValues;
-use Friendica\DI;
 use Friendica\Moderation\DomainPatternBlocklist;
+use Friendica\Test\FixtureTestTrait;
 use Mockery;
 
 class ServerBlockConsoleTest extends ConsoleTest
 {
+       use FixtureTestTrait;
+
        protected $defaultBlockList = [
                [
                        'domain' => 'social.nobodyhasthe.biz',
@@ -49,9 +49,18 @@ class ServerBlockConsoleTest extends ConsoleTest
        {
                parent::setUp();
 
+               $this->setUpFixtures();
+
                $this->blocklistMock = Mockery::mock(DomainPatternBlocklist::class);
        }
 
+       protected function tearDown(): void
+       {
+               $this->tearDownFixtures();
+
+               parent::tearDown();
+       }
+
        /**
         * Test to list the default blocked servers
         */
@@ -80,11 +89,6 @@ CONS;
         */
        public function testAddBlockedServer()
        {
-               $dice = new Dice();
-               $dice = $dice->addRules(include  __DIR__ . '/../../../static/dependencies.config.php');
-
-               DI::init($dice, true);
-
                $this->blocklistMock
                        ->shouldReceive('addPattern')
                        ->with('testme.now', 'I like it!')
@@ -105,11 +109,6 @@ CONS;
         */
        public function testUpdateBlockedServer()
        {
-               $dice = new Dice();
-               $dice = $dice->addRules(include  __DIR__ . '/../../../static/dependencies.config.php');
-
-               DI::init($dice, true);
-
                $this->blocklistMock
                        ->shouldReceive('addPattern')
                        ->with('pod.ordoevangelistarum.com', 'Other reason')
@@ -130,11 +129,6 @@ CONS;
         */
        public function testRemoveBlockedServer()
        {
-               $dice = new Dice();
-               $dice = $dice->addRules(include  __DIR__ . '/../../../static/dependencies.config.php');
-
-               DI::init($dice, true);
-
                $this->blocklistMock
                        ->shouldReceive('removePattern')
                        ->with('pod.ordoevangelistarum.com')
index 98ceeecbed7d92e59e581c1af3ce2f7a6c75bdd9..701771049e2b50fe209b2f112d4ca1c36df10a1a 100644 (file)
@@ -21,9 +21,9 @@
 
 namespace Friendica\Test\src\Content;
 
-use Friendica\Test\MockedTest;
+use Friendica\Test\DatabaseTest;
 
-class PageInfoTest extends MockedTest
+class PageInfoTest extends DatabaseTest
 {
        public function dataGetRelevantUrlFromBody()
        {
index cd3d3328d5045f810401981cdb6bfd706a267687..2e6afc841ea53ba98bc1984aea69dc90a0816ab7 100644 (file)
@@ -38,6 +38,13 @@ class HTTPClientTest extends MockedTest
                $this->setupHttpMockHandler();
        }
 
+       protected function tearDown(): void
+       {
+               $this->tearDownHandler();
+
+               parent::tearDown();
+       }
+
        /**
         * Test for issue https://github.com/friendica/friendica/issues/10473#issuecomment-907749093
         */
index 013126b4d3ce1396748ea30478e7ccca632fd58c..d9c0f074ec9419c13cd641e88b5ba22ca09b805c 100644 (file)
@@ -23,10 +23,10 @@ namespace Friendica\Test\src\Network;
 
 use Friendica\Network\Probe;
 use Friendica\Test\DiceHttpMockHandlerTrait;
-use Friendica\Test\FixtureTest;
+use Friendica\Test\MockedTest;
 use GuzzleHttp\Middleware;
 
-class ProbeTest extends FixtureTest
+class ProbeTest extends MockedTest
 {
        use DiceHttpMockHandlerTrait;
 
@@ -37,6 +37,13 @@ class ProbeTest extends FixtureTest
                $this->setupHttpMockHandler();
        }
 
+       protected function tearDown(): void
+       {
+               $this->tearDownHandler();
+
+               parent::tearDown();
+       }
+
        const TEMPLATENOBASE = '
 <!DOCTYPE html>
 <html lang="en-us">
index 36a1afde01fef9adefebd536c7a64b7f8c3aee61..384e90936483de8c594d8d51d7f0d01e3051f4f8 100644 (file)
@@ -40,6 +40,13 @@ class ImagesTest extends MockedTest
                $this->setupHttpMockHandler();
        }
 
+       protected function tearDown(): void
+       {
+               $this->tearDownFixtures();
+
+               parent::tearDown();
+       }
+
        public function dataImages()
        {
                return [