From 70e240691e56a0b84e069658378b4d6593064632 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Wed, 31 Oct 2018 10:24:07 +0100 Subject: [PATCH] Moved Mocking usage Adding more documentation --- tests/Util/AppMockTrait.php | 6 +++--- tests/Util/ConfigMockTrait.php | 5 +++++ tests/Util/DBAMockTrait.php | 17 +++++++++++++++++ tests/Util/DBStructureMockTrait.php | 19 +++++++++++++++++++ tests/Util/VFSTrait.php | 13 +++++++++++++ .../AutomaticInstallationConsoleTest.php | 14 ++++++++------ 6 files changed, 65 insertions(+), 9 deletions(-) diff --git a/tests/Util/AppMockTrait.php b/tests/Util/AppMockTrait.php index c04b5d7dc..cdd5aedd5 100644 --- a/tests/Util/AppMockTrait.php +++ b/tests/Util/AppMockTrait.php @@ -5,6 +5,7 @@ namespace Friendica\Test\Util; use Friendica\App; use Friendica\BaseObject; use Friendica\Render\FriendicaSmartyEngine; +use Mockery\MockInterface; use org\bovigo\vfs\vfsStreamDirectory; /** @@ -13,10 +14,9 @@ use org\bovigo\vfs\vfsStreamDirectory; trait AppMockTrait { use ConfigMockTrait; - use DBAMockTrait; /** - * @var App The Friendica global App Mock + * @var MockInterface|App The mocked Friendica\App */ protected $app; @@ -35,7 +35,7 @@ trait AppMockTrait $this->mockConfigGet('system', 'theme', 'testtheme'); // Mocking App and most used functions - $this->app = \Mockery::mock('Friendica\App'); + $this->app = \Mockery::mock(App::class); $this->app ->shouldReceive('getBasePath') ->andReturn($root->url()); diff --git a/tests/Util/ConfigMockTrait.php b/tests/Util/ConfigMockTrait.php index 8e285f392..d2867a589 100644 --- a/tests/Util/ConfigMockTrait.php +++ b/tests/Util/ConfigMockTrait.php @@ -2,11 +2,16 @@ namespace Friendica\Test\Util; +use Mockery\MockInterface; + /** * Trait to Mock Config settings */ trait ConfigMockTrait { + /** + * @var MockInterface The mocking interface of Friendica\Core\Config + */ private $configMock; /** diff --git a/tests/Util/DBAMockTrait.php b/tests/Util/DBAMockTrait.php index 77746f7d9..a076ac23d 100644 --- a/tests/Util/DBAMockTrait.php +++ b/tests/Util/DBAMockTrait.php @@ -2,13 +2,24 @@ namespace Friendica\Test\Util; +use Mockery\MockInterface; + /** * Trait to mock the DBA connection status */ trait DBAMockTrait { + /** + * @var MockInterface The mocking interface of Friendica\Database\DBA + */ private $dbaMock; + /** + * Mocking DBA::connect() + * + * @param bool $return True, if the connect was successful, otherwise false + * @param null|int $times How often the method will get used + */ public function mockConnect($return = true, $times = null) { if (!isset($this->dbaMock)) { @@ -21,6 +32,12 @@ trait DBAMockTrait ->andReturn($return); } + /** + * Mocking DBA::connected() + * + * @param bool $return True, if the DB is connected, otherwise false + * @param null|int $times How often the method will get used + */ public function mockConnected($return = true, $times = null) { if (!isset($this->dbaMock)) { diff --git a/tests/Util/DBStructureMockTrait.php b/tests/Util/DBStructureMockTrait.php index 3298107eb..87c120d3f 100644 --- a/tests/Util/DBStructureMockTrait.php +++ b/tests/Util/DBStructureMockTrait.php @@ -2,13 +2,25 @@ namespace Friendica\Test\Util; +use Mockery\MockInterface; + /** * Trait to mock the DBStructure connection status */ trait DBStructureMockTrait { + /** + * @var MockInterface The mocking interface of Friendica\Database\DBStructure + */ private $dbStructure; + /** + * Mocking DBStructure::update() + * + * @param array $args The arguments for the update call + * @param bool $return True, if the connect was successful, otherwise false + * @param null|int $times How often the method will get used + */ public function mockUpdate($args = [], $return = true, $times = null) { if (!isset($this->dbStructure)) { @@ -22,6 +34,13 @@ trait DBStructureMockTrait ->andReturn($return); } + /** + * Mocking DBStructure::existsTable() + * + * @param string $tableName The name of the table to check + * @param bool $return True, if the connect was successful, otherwise false + * @param null|int $times How often the method will get used + */ public function mockExistsTable($tableName, $return = true, $times = null) { if (!isset($this->dbStructure)) { diff --git a/tests/Util/VFSTrait.php b/tests/Util/VFSTrait.php index d51ba5b6a..34763b138 100644 --- a/tests/Util/VFSTrait.php +++ b/tests/Util/VFSTrait.php @@ -13,6 +13,9 @@ trait VFSTrait */ protected $root; + /** + * Sets up the Virtual File System for Friendica with common files (config, dbstructure) + */ protected function setUpVfsDir() { // the used directories inside the App class $structure = [ @@ -29,6 +32,11 @@ trait VFSTrait $this->setConfigFile('dbstructure.php'); } + /** + * Copying a config file from the file system to the Virtual File System + * + * @param string $filename The filename of the config file + */ protected function setConfigFile($filename) { $file = dirname(__DIR__) . DIRECTORY_SEPARATOR . @@ -43,6 +51,11 @@ trait VFSTrait } } + /** + * Delets a config file from the Virtual File System + * + * @param string $filename The filename of the config file + */ protected function delConfigFile($filename) { if ($this->root->hasChild('config/' . $filename)) { diff --git a/tests/src/Core/Console/AutomaticInstallationConsoleTest.php b/tests/src/Core/Console/AutomaticInstallationConsoleTest.php index 78f4e14e9..1c371f9ba 100644 --- a/tests/src/Core/Console/AutomaticInstallationConsoleTest.php +++ b/tests/src/Core/Console/AutomaticInstallationConsoleTest.php @@ -3,6 +3,7 @@ namespace Friendica\Test\src\Core\Console; use Friendica\Core\Console\AutomaticInstallation; +use Friendica\Test\Util\DBAMockTrait; use Friendica\Test\Util\DBStructureMockTrait; use org\bovigo\vfs\vfsStream; @@ -13,6 +14,7 @@ use org\bovigo\vfs\vfsStream; */ class AutomaticInstallationConsoleTest extends ConsoleTest { + use DBAMockTrait; use DBStructureMockTrait; private $db_host; @@ -251,9 +253,9 @@ CONF; $console = new AutomaticInstallation(); - $returnStr = $this->dumpExecute($console); + $txt = $this->dumpExecute($console); - $this->assertFinished($returnStr, true); + $this->assertFinished($txt, true); $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php')); @@ -290,9 +292,9 @@ CONF; $console->setOption('urlpath', '/friendica'); - $returnStr = $this->dumpExecute($console); + $txt = $this->dumpExecute($console); - $this->assertFinished($returnStr, true); + $this->assertFinished($txt, true); $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php')); @@ -311,9 +313,9 @@ CONF; $console = new AutomaticInstallation(); - $returnStr = $this->dumpExecute($console); + $txt = $this->dumpExecute($console); - $this->assertStuckDB($returnStr); + $this->assertStuckDB($txt); } public function testGetHelp()