mirror of
				https://github.com/hauke68/Magallanes.git
				synced 2025-11-04 00:50:18 +01:00 
			
		
		
		
	[Nostromo] Add more and tweak Tests
This commit is contained in:
		
							parent
							
								
									616258e91e
								
							
						
					
					
						commit
						3c21cfcce9
					
				@ -31,7 +31,7 @@ class Runtime
 | 
			
		||||
    /**
 | 
			
		||||
     * @var array Magallanes configuration
 | 
			
		||||
     */
 | 
			
		||||
    protected $configuration;
 | 
			
		||||
    protected $configuration = [];
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @var string|null Environment being deployed
 | 
			
		||||
@ -214,6 +214,10 @@ class Runtime
 | 
			
		||||
     */
 | 
			
		||||
    public function getEnvironmentConfig($key = null, $default = null)
 | 
			
		||||
    {
 | 
			
		||||
        if (!array_key_exists('environments', $this->configuration) || !is_array($this->configuration['environments'])) {
 | 
			
		||||
            return [];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!array_key_exists($this->environment, $this->configuration['environments'])) {
 | 
			
		||||
            return [];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -19,7 +19,7 @@ use Exception;
 | 
			
		||||
 */
 | 
			
		||||
class ErrorException extends Exception
 | 
			
		||||
{
 | 
			
		||||
    public function getTrimmedMessage($maxLength = 80)
 | 
			
		||||
    public function getTrimmedMessage($maxLength = 60)
 | 
			
		||||
    {
 | 
			
		||||
        $message = $this->getMessage();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,13 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of the Magallanes package.
 | 
			
		||||
 *
 | 
			
		||||
 * (c) Andrés Montañez <andres@andresmontanez.com>
 | 
			
		||||
 *
 | 
			
		||||
 * For the full copyright and license information, please view the LICENSE
 | 
			
		||||
 * file that was distributed with this source code.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace Mage\Tests\Command\BuiltIn\Config;
 | 
			
		||||
 | 
			
		||||
use Mage\Command\BuiltIn\Config\DumpCommand;
 | 
			
		||||
@ -22,6 +31,6 @@ class DumpCommandTest extends TestCase
 | 
			
		||||
        $tester = new CommandTester($command);
 | 
			
		||||
        $tester->execute(['command' => $command->getName()]);
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals($tester->getStatusCode(), 0);
 | 
			
		||||
        $this->assertEquals(0, $tester->getStatusCode());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,13 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of the Magallanes package.
 | 
			
		||||
 *
 | 
			
		||||
 * (c) Andrés Montañez <andres@andresmontanez.com>
 | 
			
		||||
 *
 | 
			
		||||
 * For the full copyright and license information, please view the LICENSE
 | 
			
		||||
 * file that was distributed with this source code.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace Mage\Tests\Command\BuiltIn\Config;
 | 
			
		||||
 | 
			
		||||
use Mage\Command\BuiltIn\Config\EnvironmentsCommand;
 | 
			
		||||
@ -88,6 +97,6 @@ class EnvironmentsCommandTest extends TestCase
 | 
			
		||||
        $tester = new CommandTester($command);
 | 
			
		||||
        $tester->execute(['command' => $command->getName()]);
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals($tester->getStatusCode(), 0);
 | 
			
		||||
        $this->assertEquals(0, $tester->getStatusCode());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,13 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of the Magallanes package.
 | 
			
		||||
 *
 | 
			
		||||
 * (c) Andrés Montañez <andres@andresmontanez.com>
 | 
			
		||||
 *
 | 
			
		||||
 * For the full copyright and license information, please view the LICENSE
 | 
			
		||||
 * file that was distributed with this source code.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace Mage\Tests\Command\BuiltIn;
 | 
			
		||||
 | 
			
		||||
use Mage\Command\BuiltIn\DeployCommand;
 | 
			
		||||
@ -118,14 +127,14 @@ class DeployCommandTest extends TestCase
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        // Check total of Executed Commands
 | 
			
		||||
        $this->assertEquals(count($ranCommands), count($testCase));
 | 
			
		||||
        $this->assertEquals(count($testCase), count($ranCommands));
 | 
			
		||||
 | 
			
		||||
        // Check Generated Commands
 | 
			
		||||
        foreach ($testCase as $index => $command) {
 | 
			
		||||
            $this->assertEquals($ranCommands[$index], $command);
 | 
			
		||||
            $this->assertEquals($command, $ranCommands[$index]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals($tester->getStatusCode(), 0);
 | 
			
		||||
        $this->assertEquals(0, $tester->getStatusCode());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testDeploymentWithErrorTaskCommands()
 | 
			
		||||
@ -197,16 +206,16 @@ class DeployCommandTest extends TestCase
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        // Check total of Executed Commands
 | 
			
		||||
        $this->assertEquals(count($ranCommands), count($testCase));
 | 
			
		||||
        $this->assertEquals(count($testCase), count($ranCommands));
 | 
			
		||||
 | 
			
		||||
        // Check Generated Commands
 | 
			
		||||
        foreach ($testCase as $index => $command) {
 | 
			
		||||
            $this->assertEquals($ranCommands[$index], $command);
 | 
			
		||||
            $this->assertEquals($command, $ranCommands[$index]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(strpos($tester->getDisplay(), 'ERROR') !== false);
 | 
			
		||||
 | 
			
		||||
        $this->assertNotEquals($tester->getStatusCode(), 0);
 | 
			
		||||
        $this->assertNotEquals(0, $tester->getStatusCode());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testDeploymentWithFailingPostDeployTaskCommands()
 | 
			
		||||
@ -255,8 +264,6 @@ class DeployCommandTest extends TestCase
 | 
			
		||||
            )
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $runtime->setReleaseId('20170101015120');
 | 
			
		||||
 | 
			
		||||
        /** @var AbstractCommand $command */
 | 
			
		||||
        $command = $application->find('deploy');
 | 
			
		||||
        $command->setRuntime($runtime);
 | 
			
		||||
@ -277,16 +284,16 @@ class DeployCommandTest extends TestCase
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        // Check total of Executed Commands
 | 
			
		||||
        $this->assertEquals(count($ranCommands), count($testCase));
 | 
			
		||||
        $this->assertEquals(count($testCase), count($ranCommands));
 | 
			
		||||
 | 
			
		||||
        // Check Generated Commands
 | 
			
		||||
        foreach ($testCase as $index => $command) {
 | 
			
		||||
            $this->assertEquals($ranCommands[$index], $command);
 | 
			
		||||
            $this->assertEquals($command, $ranCommands[$index]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(strpos($tester->getDisplay(), 'ERROR') !== false);
 | 
			
		||||
 | 
			
		||||
        $this->assertNotEquals($tester->getStatusCode(), 0);
 | 
			
		||||
        $this->assertNotEquals(0, $tester->getStatusCode());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testDeploymentWithoutReleasesCommands()
 | 
			
		||||
@ -359,8 +366,6 @@ class DeployCommandTest extends TestCase
 | 
			
		||||
            )
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $runtime->setReleaseId('1234567890');
 | 
			
		||||
 | 
			
		||||
        /** @var AbstractCommand $command */
 | 
			
		||||
        $command = $application->find('deploy');
 | 
			
		||||
        $command->setRuntime($runtime);
 | 
			
		||||
@ -377,22 +382,22 @@ class DeployCommandTest extends TestCase
 | 
			
		||||
            3 => 'composer install --dev',
 | 
			
		||||
            4 => 'composer dumpautoload --optimize',
 | 
			
		||||
            5 => 'rsync -avz --exclude=.git --exclude=vendor --exclude=app/cache --exclude=app/log --exclude=web/app_dev.php ./ tester@testhost:/var/www/test',
 | 
			
		||||
            6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console cache:clear --env=dev \\"',
 | 
			
		||||
            7 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console cache:warmup --env=dev \\"',
 | 
			
		||||
            8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console assets:install --env=dev --symlink --relative web\\"',
 | 
			
		||||
            9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console assetic:dump --env=dev \\"',
 | 
			
		||||
            6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console cache:clear --env=dev \\"',
 | 
			
		||||
            7 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console cache:warmup --env=dev \\"',
 | 
			
		||||
            8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assets:install --env=dev --symlink --relative web\\"',
 | 
			
		||||
            9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assetic:dump --env=dev \\"',
 | 
			
		||||
            10 => 'git checkout master',
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        // Check total of Executed Commands
 | 
			
		||||
        $this->assertEquals(count($ranCommands), count($testCase));
 | 
			
		||||
        $this->assertEquals(count($testCase), count($ranCommands));
 | 
			
		||||
 | 
			
		||||
        // Check Generated Commands
 | 
			
		||||
        foreach ($testCase as $index => $command) {
 | 
			
		||||
            $this->assertEquals($ranCommands[$index], $command);
 | 
			
		||||
            $this->assertEquals($command, $ranCommands[$index]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals($tester->getStatusCode(), 0);
 | 
			
		||||
        $this->assertEquals(0, $tester->getStatusCode());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testDeploymentWithSkippingTask()
 | 
			
		||||
@ -465,8 +470,6 @@ class DeployCommandTest extends TestCase
 | 
			
		||||
            )
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $runtime->setReleaseId('1234567890');
 | 
			
		||||
 | 
			
		||||
        /** @var AbstractCommand $command */
 | 
			
		||||
        $command = $application->find('deploy');
 | 
			
		||||
        $command->setRuntime($runtime);
 | 
			
		||||
@ -482,23 +485,23 @@ class DeployCommandTest extends TestCase
 | 
			
		||||
            2 => 'composer install --dev',
 | 
			
		||||
            3 => 'composer dumpautoload --optimize',
 | 
			
		||||
            4 => 'rsync -avz --exclude=.git --exclude=vendor --exclude=app/cache --exclude=app/log --exclude=web/app_dev.php ./ tester@testhost:/var/www/test',
 | 
			
		||||
            5 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console cache:clear --env=dev \\"',
 | 
			
		||||
            6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console cache:warmup --env=dev \\"',
 | 
			
		||||
            7 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console assets:install --env=dev --symlink --relative web\\"',
 | 
			
		||||
            8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test/releases/1234567890 \\&\\& bin/console assetic:dump --env=dev \\"',
 | 
			
		||||
            5 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console cache:clear --env=dev \\"',
 | 
			
		||||
            6 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console cache:warmup --env=dev \\"',
 | 
			
		||||
            7 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assets:install --env=dev --symlink --relative web\\"',
 | 
			
		||||
            8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \\"cd /var/www/test \\&\\& bin/console assetic:dump --env=dev \\"',
 | 
			
		||||
            9 => 'git branch | grep "*"',
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        // Check total of Executed Commands
 | 
			
		||||
        $this->assertEquals(count($ranCommands), count($testCase));
 | 
			
		||||
        $this->assertEquals(count($testCase), count($ranCommands));
 | 
			
		||||
 | 
			
		||||
        // Check Generated Commands
 | 
			
		||||
        foreach ($testCase as $index => $command) {
 | 
			
		||||
            $this->assertEquals($ranCommands[$index], $command);
 | 
			
		||||
            $this->assertEquals($command, $ranCommands[$index]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(strpos($tester->getDisplay(), 'SKIPPED') !== false);
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals($tester->getStatusCode(), 0);
 | 
			
		||||
        $this->assertEquals(0, $tester->getStatusCode());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,13 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of the Magallanes package.
 | 
			
		||||
 *
 | 
			
		||||
 * (c) Andrés Montañez <andres@andresmontanez.com>
 | 
			
		||||
 *
 | 
			
		||||
 * For the full copyright and license information, please view the LICENSE
 | 
			
		||||
 * file that was distributed with this source code.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace Mage\Tests\Command\BuiltIn\Releases;
 | 
			
		||||
 | 
			
		||||
use Mage\Command\BuiltIn\Releases\ListCommand;
 | 
			
		||||
@ -96,11 +105,11 @@ class ListCommandTest extends TestCase
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        // Check total of Executed Commands
 | 
			
		||||
        $this->assertEquals(count($ranCommands), count($testCase));
 | 
			
		||||
        $this->assertEquals(count($testCase), count($ranCommands));
 | 
			
		||||
 | 
			
		||||
        // Check Generated Commands
 | 
			
		||||
        foreach ($testCase as $index => $command) {
 | 
			
		||||
            $this->assertEquals($ranCommands[$index], $command);
 | 
			
		||||
            $this->assertEquals($command, $ranCommands[$index]);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,13 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of the Magallanes package.
 | 
			
		||||
 *
 | 
			
		||||
 * (c) Andrés Montañez <andres@andresmontanez.com>
 | 
			
		||||
 *
 | 
			
		||||
 * For the full copyright and license information, please view the LICENSE
 | 
			
		||||
 * file that was distributed with this source code.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace Mage\Tests\Command\BuiltIn\Releases;
 | 
			
		||||
 | 
			
		||||
use Mage\Command\BuiltIn\Releases\RollbackCommand;
 | 
			
		||||
@ -96,11 +105,11 @@ class RollbackCommandTest extends TestCase
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        // Check total of Executed Commands
 | 
			
		||||
        $this->assertEquals(count($ranCommands), count($testCase));
 | 
			
		||||
        $this->assertEquals(count($testCase), count($ranCommands));
 | 
			
		||||
 | 
			
		||||
        // Check Generated Commands
 | 
			
		||||
        foreach ($testCase as $index => $command) {
 | 
			
		||||
            $this->assertEquals($ranCommands[$index], $command);
 | 
			
		||||
            $this->assertEquals($command, $ranCommands[$index]);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,13 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of the Magallanes package.
 | 
			
		||||
 *
 | 
			
		||||
 * (c) Andrés Montañez <andres@andresmontanez.com>
 | 
			
		||||
 *
 | 
			
		||||
 * For the full copyright and license information, please view the LICENSE
 | 
			
		||||
 * file that was distributed with this source code.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace Mage\Tests\Command\BuiltIn;
 | 
			
		||||
 | 
			
		||||
use Mage\Command\BuiltIn\VersionCommand;
 | 
			
		||||
 | 
			
		||||
@ -10,23 +10,8 @@
 | 
			
		||||
 | 
			
		||||
namespace Mage\Tests;
 | 
			
		||||
 | 
			
		||||
use Mage\Command\AbstractCommand;
 | 
			
		||||
use Mage\Runtime\Runtime;
 | 
			
		||||
use Symfony\Component\Console\Input\InputInterface;
 | 
			
		||||
use Symfony\Component\Console\Output\OutputInterface;
 | 
			
		||||
use Symfony\Component\Finder\Finder;
 | 
			
		||||
use Symfony\Component\Finder\SplFileInfo;
 | 
			
		||||
use Monolog\Logger;
 | 
			
		||||
use Monolog\Handler\StreamHandler;
 | 
			
		||||
use Symfony\Component\Console\Application;
 | 
			
		||||
use Symfony\Component\Yaml\Yaml;
 | 
			
		||||
use Mage\Runtime\Exception\RuntimeException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * The Console Application for launching the Mage command in a standalone instance
 | 
			
		||||
 *
 | 
			
		||||
 * @author Andrés Montañez <andresmontanez@gmail.com>
 | 
			
		||||
 */
 | 
			
		||||
class MageTestApplication extends Application
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,13 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of the Magallanes package.
 | 
			
		||||
 *
 | 
			
		||||
 * (c) Andrés Montañez <andres@andresmontanez.com>
 | 
			
		||||
 *
 | 
			
		||||
 * For the full copyright and license information, please view the LICENSE
 | 
			
		||||
 * file that was distributed with this source code.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace Mage\Tests\Runtime;
 | 
			
		||||
 | 
			
		||||
use Symfony\Component\Process\Process;
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,13 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of the Magallanes package.
 | 
			
		||||
 *
 | 
			
		||||
 * (c) Andrés Montañez <andres@andresmontanez.com>
 | 
			
		||||
 *
 | 
			
		||||
 * For the full copyright and license information, please view the LICENSE
 | 
			
		||||
 * file that was distributed with this source code.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace Mage\Tests\Runtime;
 | 
			
		||||
 | 
			
		||||
use Mage\Runtime\Runtime;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										145
									
								
								src/Mage/Tests/Runtime/RuntimeTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										145
									
								
								src/Mage/Tests/Runtime/RuntimeTest.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,145 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of the Magallanes package.
 | 
			
		||||
 *
 | 
			
		||||
 * (c) Andrés Montañez <andres@andresmontanez.com>
 | 
			
		||||
 *
 | 
			
		||||
 * For the full copyright and license information, please view the LICENSE
 | 
			
		||||
 * file that was distributed with this source code.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace Mage\Tests\Runtime;
 | 
			
		||||
 | 
			
		||||
use Mage\Runtime\Runtime;
 | 
			
		||||
use Mage\Runtime\Exception\InvalidEnvironmentException;
 | 
			
		||||
use Exception;
 | 
			
		||||
use Monolog\Logger;
 | 
			
		||||
use Monolog\Handler\TestHandler;
 | 
			
		||||
use PHPUnit_Framework_TestCase as TestCase;
 | 
			
		||||
use Psr\Log\LogLevel;
 | 
			
		||||
use Symfony\Component\Process\Process;
 | 
			
		||||
 | 
			
		||||
class RuntimeTest extends TestCase
 | 
			
		||||
{
 | 
			
		||||
    public function testReleaseIdGeneration()
 | 
			
		||||
    {
 | 
			
		||||
        $releaseId = date('YmdHis');
 | 
			
		||||
 | 
			
		||||
        $runtime = new Runtime();
 | 
			
		||||
        $runtime->generateReleaseId();
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals($releaseId, $runtime->getReleaseId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testEmptyEnvironmentConfig()
 | 
			
		||||
    {
 | 
			
		||||
        $runtime = new Runtime();
 | 
			
		||||
        $config = $runtime->getEnvironmentConfig();
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(is_array($config));
 | 
			
		||||
        $this->assertEquals(0, count($config));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testInvalidEnvironments()
 | 
			
		||||
    {
 | 
			
		||||
        try {
 | 
			
		||||
            $runtime = new Runtime();
 | 
			
		||||
            $runtime->setEnvironment('invalid');
 | 
			
		||||
        } catch (Exception $exception) {
 | 
			
		||||
            $this->assertTrue($exception instanceof InvalidEnvironmentException);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            $runtime = new Runtime();
 | 
			
		||||
            $runtime->setConfiguration(['environments' => ['valid' => []]]);
 | 
			
		||||
            $runtime->setEnvironment('invalid');
 | 
			
		||||
        } catch (Exception $exception) {
 | 
			
		||||
            $this->assertTrue($exception instanceof InvalidEnvironmentException);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testTempFile()
 | 
			
		||||
    {
 | 
			
		||||
        $runtime = new Runtime();
 | 
			
		||||
        $tempFile = $runtime->getTempFile();
 | 
			
		||||
 | 
			
		||||
        $this->assertNotEquals('', $tempFile);
 | 
			
		||||
        $this->assertTrue(file_exists($tempFile));
 | 
			
		||||
        $this->assertEquals(0, filesize($tempFile));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testSSHConfigUndefinedOptions()
 | 
			
		||||
    {
 | 
			
		||||
        $runtime = new Runtime();
 | 
			
		||||
        $sshConfig = $runtime->getSSHConfig();
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(is_array($sshConfig));
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(array_key_exists('port', $sshConfig));
 | 
			
		||||
        $this->assertEquals('22', $sshConfig['port']);
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(array_key_exists('flags', $sshConfig));
 | 
			
		||||
        $this->assertEquals('-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no', $sshConfig['flags']);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testSSHConfigEmptyOptions()
 | 
			
		||||
    {
 | 
			
		||||
        $runtime = new Runtime();
 | 
			
		||||
        $runtime->setConfiguration(['environments' => ['test' => ['ssh' => []]]]);
 | 
			
		||||
        $runtime->setEnvironment('test');
 | 
			
		||||
        $sshConfig = $runtime->getSSHConfig();
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(is_array($sshConfig));
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(array_key_exists('port', $sshConfig));
 | 
			
		||||
        $this->assertEquals('22', $sshConfig['port']);
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(array_key_exists('flags', $sshConfig));
 | 
			
		||||
        $this->assertEquals('-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no', $sshConfig['flags']);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testSSHConfigCustomOptions()
 | 
			
		||||
    {
 | 
			
		||||
        $runtime = new Runtime();
 | 
			
		||||
        $runtime->setConfiguration(['environments' => ['test' => ['ssh' => ['port' => '2222', 'flags' => '-q']]]]);
 | 
			
		||||
        $runtime->setEnvironment('test');
 | 
			
		||||
        $sshConfig = $runtime->getSSHConfig();
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(is_array($sshConfig));
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(array_key_exists('port', $sshConfig));
 | 
			
		||||
        $this->assertEquals('2222', $sshConfig['port']);
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(array_key_exists('flags', $sshConfig));
 | 
			
		||||
        $this->assertEquals('-q', $sshConfig['flags']);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testLogger()
 | 
			
		||||
    {
 | 
			
		||||
        $logger = new Logger('test');
 | 
			
		||||
        $handler = new TestHandler();
 | 
			
		||||
        $logger->pushHandler($handler);
 | 
			
		||||
 | 
			
		||||
        $runtime = new Runtime();
 | 
			
		||||
        $runtime->setLogger($logger);
 | 
			
		||||
 | 
			
		||||
        $runtime->log('Test Message', LogLevel::INFO);
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue($handler->hasInfoRecords());
 | 
			
		||||
        $this->assertTrue($handler->hasInfo('Test Message'));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testLocalCommand()
 | 
			
		||||
    {
 | 
			
		||||
        $runtime = new Runtime();
 | 
			
		||||
 | 
			
		||||
        /** @var Process $process */
 | 
			
		||||
        $process = $runtime->runLocalCommand('date +%s');
 | 
			
		||||
        $this->assertTrue($process->isSuccessful());
 | 
			
		||||
        $this->assertEquals(time(), trim($process->getOutput()));
 | 
			
		||||
 | 
			
		||||
        /** @var Process $process */
 | 
			
		||||
        $process = $runtime->runLocalCommand('false');
 | 
			
		||||
        $this->assertFalse($process->isSuccessful());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										44
									
								
								src/Mage/Tests/Task/AbstractTaskTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								src/Mage/Tests/Task/AbstractTaskTest.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,44 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of the Magallanes package.
 | 
			
		||||
 *
 | 
			
		||||
 * (c) Andrés Montañez <andres@andresmontanez.com>
 | 
			
		||||
 *
 | 
			
		||||
 * For the full copyright and license information, please view the LICENSE
 | 
			
		||||
 * file that was distributed with this source code.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace Mage\Tests\Task;
 | 
			
		||||
 | 
			
		||||
use Mage\Task\ErrorException;
 | 
			
		||||
use Exception;
 | 
			
		||||
use PHPUnit_Framework_TestCase as TestCase;
 | 
			
		||||
 | 
			
		||||
class AbstractTaskTest extends TestCase
 | 
			
		||||
{
 | 
			
		||||
    public function testNotArrayOptions()
 | 
			
		||||
    {
 | 
			
		||||
        $task = new TestCaseTask();
 | 
			
		||||
        $task->setOptions('not an array');
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(is_array($task->getOptions()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testFailingTask()
 | 
			
		||||
    {
 | 
			
		||||
        $task = new TestCaseFailTask();
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            $task->execute();
 | 
			
		||||
            $this->assertTrue(false, 'TestCaseFailTask did not throw exception');
 | 
			
		||||
        } catch (Exception $exception) {
 | 
			
		||||
            $this->assertTrue($exception instanceof ErrorException);
 | 
			
		||||
 | 
			
		||||
            if ($exception instanceof ErrorException) {
 | 
			
		||||
                $this->assertEquals('This is a text...', $exception->getTrimmedMessage(14));
 | 
			
		||||
                $this->assertEquals('This is a text with a lot of characters', $exception->getTrimmedMessage());
 | 
			
		||||
                $this->assertEquals('This is a text with a lot of characters', $exception->getMessage());
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										32
									
								
								src/Mage/Tests/Task/TestCaseFailTask.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/Mage/Tests/Task/TestCaseFailTask.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,32 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of the Magallanes package.
 | 
			
		||||
 *
 | 
			
		||||
 * (c) Andrés Montañez <andres@andresmontanez.com>
 | 
			
		||||
 *
 | 
			
		||||
 * For the full copyright and license information, please view the LICENSE
 | 
			
		||||
 * file that was distributed with this source code.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace Mage\Tests\Task;
 | 
			
		||||
 | 
			
		||||
use Mage\Task\ErrorException;
 | 
			
		||||
use Mage\Task\AbstractTask;
 | 
			
		||||
 | 
			
		||||
class TestCaseFailTask extends AbstractTask
 | 
			
		||||
{
 | 
			
		||||
    public function getName()
 | 
			
		||||
    {
 | 
			
		||||
        return 'test-fail';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getDescription()
 | 
			
		||||
    {
 | 
			
		||||
        return '[Test] This is a Test Task which Fails';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function execute()
 | 
			
		||||
    {
 | 
			
		||||
        throw new ErrorException('This is a text with a lot of characters');
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										36
									
								
								src/Mage/Tests/Task/TestCaseTask.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/Mage/Tests/Task/TestCaseTask.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,36 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of the Magallanes package.
 | 
			
		||||
 *
 | 
			
		||||
 * (c) Andrés Montañez <andres@andresmontanez.com>
 | 
			
		||||
 *
 | 
			
		||||
 * For the full copyright and license information, please view the LICENSE
 | 
			
		||||
 * file that was distributed with this source code.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace Mage\Tests\Task;
 | 
			
		||||
 | 
			
		||||
use Mage\Task\AbstractTask;
 | 
			
		||||
 | 
			
		||||
class TestCaseTask extends AbstractTask
 | 
			
		||||
{
 | 
			
		||||
    public function getName()
 | 
			
		||||
    {
 | 
			
		||||
        return 'test';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getDescription()
 | 
			
		||||
    {
 | 
			
		||||
        return '[Test] This is a Test Task';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function execute()
 | 
			
		||||
    {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getOptions()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->options;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,4 +1,13 @@
 | 
			
		||||
<?php
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of the Magallanes package.
 | 
			
		||||
 *
 | 
			
		||||
 * (c) Andrés Montañez <andres@andresmontanez.com>
 | 
			
		||||
 *
 | 
			
		||||
 * For the full copyright and license information, please view the LICENSE
 | 
			
		||||
 * file that was distributed with this source code.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace Mage\Tests;
 | 
			
		||||
 | 
			
		||||
use Mage\Utils;
 | 
			
		||||
@ -10,11 +19,11 @@ class UtilsTest extends TestCase
 | 
			
		||||
{
 | 
			
		||||
    public function testStageNames()
 | 
			
		||||
    {
 | 
			
		||||
        $this->assertEquals(Utils::getStageName(Runtime::PRE_DEPLOY), 'Pre Deployment');
 | 
			
		||||
        $this->assertEquals(Utils::getStageName(Runtime::ON_DEPLOY), 'On Deployment');
 | 
			
		||||
        $this->assertEquals(Utils::getStageName(Runtime::POST_DEPLOY), 'Post Deployment');
 | 
			
		||||
        $this->assertEquals(Utils::getStageName(Runtime::ON_RELEASE), 'On Release');
 | 
			
		||||
        $this->assertEquals(Utils::getStageName(Runtime::POST_RELEASE), 'Post Release');
 | 
			
		||||
        $this->assertEquals('Pre Deployment', Utils::getStageName(Runtime::PRE_DEPLOY));
 | 
			
		||||
        $this->assertEquals('On Deployment', Utils::getStageName(Runtime::ON_DEPLOY));
 | 
			
		||||
        $this->assertEquals('Post Deployment', Utils::getStageName(Runtime::POST_DEPLOY));
 | 
			
		||||
        $this->assertEquals('On Release', Utils::getStageName(Runtime::ON_RELEASE));
 | 
			
		||||
        $this->assertEquals('Post Release', Utils::getStageName(Runtime::POST_RELEASE));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testReleaseDate()
 | 
			
		||||
@ -24,49 +33,49 @@ class UtilsTest extends TestCase
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue($dateTime instanceof DateTime);
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals($dateTime->format('Y-m-d H:i:s'), '2017-01-02 03:15:30');
 | 
			
		||||
        $this->assertEquals('2017-01-02 03:15:30', $dateTime->format('Y-m-d H:i:s'));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testTimeDiffs()
 | 
			
		||||
    {
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-1 second');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), 'just now');
 | 
			
		||||
        $this->assertEquals('just now', Utils::getTimeDiff($dateTime));
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-45 seconds');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), '45 seconds ago');
 | 
			
		||||
        $this->assertEquals('45 seconds ago', Utils::getTimeDiff($dateTime));
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-90 seconds');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), 'one minute ago');
 | 
			
		||||
        $this->assertEquals('one minute ago', Utils::getTimeDiff($dateTime));
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-30 minutes');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), '30 minutes ago');
 | 
			
		||||
        $this->assertEquals('30 minutes ago', Utils::getTimeDiff($dateTime));
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-1 hour');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), 'one hour ago');
 | 
			
		||||
        $this->assertEquals('one hour ago', Utils::getTimeDiff($dateTime));
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-10 hours');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), '10 hours ago');
 | 
			
		||||
        $this->assertEquals('10 hours ago', Utils::getTimeDiff($dateTime));
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-1 day');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), 'one day ago');
 | 
			
		||||
        $this->assertEquals('one day ago', Utils::getTimeDiff($dateTime));
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-3 days');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), '3 days ago');
 | 
			
		||||
        $this->assertEquals('3 days ago', Utils::getTimeDiff($dateTime));
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-7 days');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), 'a week ago');
 | 
			
		||||
        $this->assertEquals('a week ago', Utils::getTimeDiff($dateTime));
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-10 days');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), '');
 | 
			
		||||
        $this->assertEquals('', Utils::getTimeDiff($dateTime));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user