]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Core/Console/AutomaticInstallationConsoleTest.php
Bugfixing executable (Mocking the executable)
[friendica.git] / tests / src / Core / Console / AutomaticInstallationConsoleTest.php
index 290d3ed0428dce6fe98f3725387441cc51d66560..bed3a578b6e40cda64c10b428dd5b21b4ecc1e21 100644 (file)
@@ -2,7 +2,11 @@
 
 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;
+use org\bovigo\vfs\vfsStreamFile;
 
 /**
  * @runTestsInSeparateProcesses
@@ -11,12 +15,24 @@ use org\bovigo\vfs\vfsStream;
  */
 class AutomaticInstallationConsoleTest extends ConsoleTest
 {
+       use DBAMockTrait;
+       use DBStructureMockTrait;
+
        private $db_host;
        private $db_port;
        private $db_data;
        private $db_user;
        private $db_pass;
 
+       /**
+        * @var vfsStreamFile Assert file without DB credentials
+        */
+       private $assertFile;
+       /**
+        * @var vfsStreamFile Assert file with DB credentials
+        */
+       private $assertFileDb;
+
        public function setUp()
        {
                parent::setUp();
@@ -31,12 +47,41 @@ class AutomaticInstallationConsoleTest extends ConsoleTest
                $this->db_data = getenv('MYSQL_DATABASE');
                $this->db_user = getenv('MYSQL_USERNAME') . getenv('MYSQL_USER');
                $this->db_pass = getenv('MYSQL_PASSWORD');
+
+               $this->mockConfigGet('config', 'php_path', false);
+
+               $assertFile  = dirname(__DIR__) . DIRECTORY_SEPARATOR .
+                       '..' . DIRECTORY_SEPARATOR .
+                       '..' . DIRECTORY_SEPARATOR .
+                       'datasets' . DIRECTORY_SEPARATOR .
+                       'ini' . DIRECTORY_SEPARATOR .
+                       'assert.ini.php';
+               $this->assertFile = vfsStream::newFile('assert.ini.php')
+                       ->at($this->root->getChild('test'))
+                       ->setContent($this->replaceEnvironmentSettings($assertFile, false));
+               $this->assertFileDb = vfsStream::newFile('assert_db.ini.php')
+                       ->at($this->root->getChild('test'))
+                       ->setContent($this->replaceEnvironmentSettings($assertFile, true));
        }
 
-       private function assertConfig($family, $key, $value)
+       /**
+        * Replacing environment specific variables in the assertion file
+        *
+        * @param string $file The file to compare in later tests
+        * @param bool $withDb If true, db settings are replaced too
+        * @return string The file content
+        */
+       private function replaceEnvironmentSettings($file, $withDb)
        {
-               $config = $this->execute(['config', $family, $key]);
-               $this->assertEquals($family . "." . $key . " => " . $value . "\n", $config);
+               $fileContent = file_get_contents($file);
+               $fileContent = str_replace("/usr/bin/php", trim(shell_exec('which php')), $fileContent);
+               if ($withDb) {
+                       $fileContent = str_replace("hostname = \"\"", "hostname = \"" . $this->db_host . (!empty($this->db_port) ? ":" . $this->db_port : "") . "\"", $fileContent);
+                       $fileContent = str_replace("username = \"\"", "username = \"" . $this->db_user . "\"", $fileContent);
+                       $fileContent = str_replace("password = \"\"", "password = \"" . $this->db_pass . "\"", $fileContent);
+                       $fileContent = str_replace("database = \"\"", "database = \"" . $this->db_data . "\"", $fileContent);
+               }
+               return $fileContent;
        }
 
        private function assertFinished($txt, $withconfig = false, $copyfile = false)
@@ -48,6 +93,8 @@ class AutomaticInstallationConsoleTest extends ConsoleTest
 
 
 Creating config file...
+
+ Complete!
 CFG;
                }
 
@@ -56,20 +103,23 @@ CFG;
 
 
 Copying config file...
+
+ Complete!
 CFG;
                }
 
                $finished = <<<FIN
-Initializing setup...{$cfg}
+Initializing setup...
 
  Complete!
 
 
-Checking basic setup...
+Checking environment...
 
  NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
 
  Complete!
+{$cfg}
 
 
 Checking database...
@@ -100,22 +150,25 @@ FIN;
                $finished = <<<FIN
 Initializing setup...
 
-Creating config file...
-
  Complete!
 
 
-Checking basic setup...
+Checking environment...
 
  NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
 
  Complete!
 
 
+Creating config file...
+
+ Complete!
+
+
 Checking database...
 
 [Error] --------
-MySQL Connection: Failed, please check your MySQL settings and credentials.
+: 
 
 
 FIN;
@@ -128,6 +181,11 @@ FIN;
         */
        public function testWithConfig()
        {
+               $this->mockConnect(true, 1);
+               $this->mockConnected(true, 1);
+               $this->mockExistsTable('user', false, 1);
+               $this->mockUpdate([false, true, true], null, 1);
+
                $config = <<<CONF
 <?php return <<<INI
 
@@ -166,7 +224,10 @@ CONF;
                        ->at($this->root)
                        ->setContent($config);
 
-               $txt = $this->execute(['autoinstall', '-f', 'prepared.ini.php']);
+               $console = new AutomaticInstallation($this->consoleArgv);
+               $console->setOption('f', 'prepared.ini.php');
+
+               $txt = $this->dumpExecute($console);
 
                $this->assertFinished($txt, false, true);
 
@@ -178,48 +239,56 @@ CONF;
         */
        public function testWithEnvironmentAndSave()
        {
+               $this->mockConnect(true, 1);
+               $this->mockConnected(true, 1);
+               $this->mockExistsTable('user', false, 1);
+               $this->mockUpdate([false, true, true], null, 1);
+
                $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
                $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
                $this->assertTrue(putenv('FRIENDICA_LANG=de'));
+               $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
 
-               $txt = $this->execute(['autoinstall', '--savedb']);
+               $console = new AutomaticInstallation($this->consoleArgv);
+               $console->setOption('savedb', true);
+
+               $txt = $this->dumpExecute($console);
 
                $this->assertFinished($txt, true);
 
                $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
 
-               $this->assertConfig('database', 'hostname', $this->db_host . (!empty($this->db_port) ? ':' . $this->db_port : ''));
-               $this->assertConfig('database', 'username', $this->db_user);
-               $this->assertConfig('database', 'database', $this->db_data);
-               $this->assertConfig('config', 'admin_email', 'admin@friendica.local');
-               $this->assertConfig('system', 'default_timezone', 'Europe/Berlin');
-               $this->assertConfig('system', 'language', 'de');
+               $this->assertFileEquals(
+                       $this->assertFileDb->url(),
+                       $this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php')->url());
        }
 
-
        /**
         * @medium
         */
        public function testWithEnvironmentWithoutSave()
        {
+               $this->mockConnect(true, 1);
+               $this->mockConnected(true, 1);
+               $this->mockExistsTable('user', false, 1);
+               $this->mockUpdate([false, true, true], null, 1);
+
                $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
                $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
                $this->assertTrue(putenv('FRIENDICA_LANG=de'));
                $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
 
-               $txt = $this->execute(['autoinstall']);
+               $console = new AutomaticInstallation($this->consoleArgv);
+
+               $txt = $this->dumpExecute($console);
 
                $this->assertFinished($txt, true);
 
                $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
 
-               $this->assertConfig('database', 'hostname', '');
-               $this->assertConfig('database', 'username', '');
-               $this->assertConfig('database', 'database', '');
-               $this->assertConfig('config', 'admin_email', 'admin@friendica.local');
-               $this->assertConfig('system', 'default_timezone', 'Europe/Berlin');
-               $this->assertConfig('system', 'language', 'de');
-               $this->assertConfig('system', 'urlpath', '/friendica');
+               $this->assertFileEquals(
+                       $this->assertFile->url(),
+                       $this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php')->url());
        }
 
        /**
@@ -227,58 +296,51 @@ CONF;
         */
        public function testWithArguments()
        {
-               $args = ['autoinstall'];
-               array_push($args, '--dbhost');
-               array_push($args, $this->db_host);
-               array_push($args, '--dbuser');
-               array_push($args, $this->db_user);
+               $this->mockConnect(true, 1);
+               $this->mockConnected(true, 1);
+               $this->mockExistsTable('user', false, 1);
+               $this->mockUpdate([false, true, true], null, 1);
+
+               $console = new AutomaticInstallation($this->consoleArgv);
+
+               $console->setOption('dbhost', $this->db_host);
+               $console->setOption('dbuser', $this->db_user);
                if (!empty($this->db_pass)) {
-                       array_push($args, '--dbpass');
-                       array_push($args, $this->db_pass);
+                       $console->setOption('dbpass', $this->db_pass);
                }
                if (!empty($this->db_port)) {
-                       array_push($args, '--dbport');
-                       array_push($args, $this->db_port);
+                       $console->setOption('dbport', $this->db_port);
                }
-               array_push($args, '--dbdata');
-               array_push($args, $this->db_data);
+               $console->setOption('dbdata', $this->db_data);
 
-               array_push($args, '--admin');
-               array_push($args, 'admin@friendica.local');
-               array_push($args, '--tz');
-               array_push($args, 'Europe/Berlin');
-               array_push($args, '--lang');
-               array_push($args, 'de');
+               $console->setOption('admin', 'admin@friendica.local');
+               $console->setOption('tz', 'Europe/Berlin');
+               $console->setOption('lang', 'de');
 
-               array_push($args, '--urlpath');
-               array_push($args, '/friendica');
+               $console->setOption('urlpath', '/friendica');
 
-               $txt = $this->execute($args);
+               $txt = $this->dumpExecute($console);
 
                $this->assertFinished($txt, true);
 
                $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
 
-               $this->assertConfig('database', 'hostname', $this->db_host . (!empty($this->db_port) ? ':' . $this->db_port : ''));
-               $this->assertConfig('database', 'username', $this->db_user);
-               $this->assertConfig('database', 'database', $this->db_data);
-               $this->assertConfig('config', 'admin_email', 'admin@friendica.local');
-               $this->assertConfig('system', 'default_timezone', 'Europe/Berlin');
-               $this->assertConfig('system', 'language', 'de');
-               $this->assertConfig('system', 'urlpath', '/friendica');
+               $this->assertFileEquals(
+                       $this->assertFileDb->url(),
+                       $this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php')->url());
        }
 
        /**
         * @runTestsInSeparateProcesses
+        * @preserveGlobalState disabled
         */
        public function testNoDatabaseConnection()
        {
-               $dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
-               $dbaMock
-                       ->shouldReceive('connected')
-                       ->andReturn(false);
+               $this->mockConnect(false, 1);
 
-               $txt = $this->execute(['autoinstall']);
+               $console = new AutomaticInstallation($this->consoleArgv);
+
+               $txt = $this->dumpExecute($console);
 
                $this->assertStuckDB($txt);
        }
@@ -338,7 +400,10 @@ Examples
 
 HELP;
 
-               $txt = $this->execute(['autoinstall', '-h']);
+               $console = new AutomaticInstallation($this->consoleArgv);
+               $console->setOption('help', true);
+
+               $txt = $this->dumpExecute($console);
 
                $this->assertEquals($txt, $theHelp);
        }