mirror of
				https://github.com/hauke68/Magallanes.git
				synced 2025-11-04 00:50:18 +01:00 
			
		
		
		
	[Nostromo] Tests
This commit is contained in:
		
							parent
							
								
									6d6b50b81a
								
							
						
					
					
						commit
						4b7cc618b4
					
				@ -22,5 +22,5 @@ $ vendor/bin/mage version
 | 
			
		||||
### What happend to version 2? ###
 | 
			
		||||
There is no version 2. I've skipped it and jumpped stright from v1 to v3. This new version of **Magallanes** is quite radical and different from it's successor. The whole application has been rewritten using **_Symfony3 Components_**, so naming it v3 makes a lot of sense.
 | 
			
		||||
 | 
			
		||||
### Codename... Nostromo? ###
 | 
			
		||||
### Codename Nostromo ###
 | 
			
		||||
Each new mayor version of **Magallanes** will have a codename (like Ubuntu), and in the current version it is **_Nostromo_**, like the spaceship from the movie *Alien (1979)*.
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										27
									
								
								src/Mage/Tests/Command/BuiltIn/Config/DumpCommandTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/Mage/Tests/Command/BuiltIn/Config/DumpCommandTest.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Mage\Tests\Command\BuiltIn\Config;
 | 
			
		||||
 | 
			
		||||
use Mage\Command\BuiltIn\Config\DumpCommand;
 | 
			
		||||
use Mage\Command\AbstractCommand;
 | 
			
		||||
use Mage\Tests\MageTestApplication;
 | 
			
		||||
use Mage\Tests\Runtime\RuntimeMockup;
 | 
			
		||||
use Symfony\Component\Console\Tester\CommandTester;
 | 
			
		||||
use PHPUnit_Framework_TestCase as TestCase;
 | 
			
		||||
 | 
			
		||||
class DumpCommandTest extends TestCase
 | 
			
		||||
{
 | 
			
		||||
    public function testConfigDumpTermination()
 | 
			
		||||
    {
 | 
			
		||||
        $application = new MageTestApplication();
 | 
			
		||||
        $application->add(new DumpCommand());
 | 
			
		||||
        
 | 
			
		||||
        /** @var AbstractCommand $command */
 | 
			
		||||
        $command = $application->find('config:dump');
 | 
			
		||||
        $command->setRuntime(new RuntimeMockup());
 | 
			
		||||
 | 
			
		||||
        $tester = new CommandTester($command);
 | 
			
		||||
        $tester->execute(['command' => $command->getName()]);
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals($tester->getStatusCode(), 0);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,93 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Mage\Tests\Command\BuiltIn\Config;
 | 
			
		||||
 | 
			
		||||
use Mage\Command\BuiltIn\Config\EnvironmentsCommand;
 | 
			
		||||
use Mage\Command\AbstractCommand;
 | 
			
		||||
use Mage\Tests\MageTestApplication;
 | 
			
		||||
use Mage\Tests\Runtime\RuntimeMockup;
 | 
			
		||||
use Symfony\Component\Console\Tester\CommandTester;
 | 
			
		||||
use PHPUnit_Framework_TestCase as TestCase;
 | 
			
		||||
 | 
			
		||||
class EnvironmentsCommandTest extends TestCase
 | 
			
		||||
{
 | 
			
		||||
    public function testConfigDumpTermination()
 | 
			
		||||
    {
 | 
			
		||||
        $application = new MageTestApplication();
 | 
			
		||||
        $application->add(new EnvironmentsCommand());
 | 
			
		||||
 | 
			
		||||
        $runtime = new RuntimeMockup();
 | 
			
		||||
        $runtime->setConfiguration(array(
 | 
			
		||||
                'environments' =>
 | 
			
		||||
                    array(
 | 
			
		||||
                        'test' =>
 | 
			
		||||
                            array(
 | 
			
		||||
                                'user' => 'tester',
 | 
			
		||||
                                'branch' => 'test',
 | 
			
		||||
                                'host_path' => '/var/www/test',
 | 
			
		||||
                                'releases' => 4,
 | 
			
		||||
                                'exclude' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'vendor',
 | 
			
		||||
                                        1 => 'app/cache',
 | 
			
		||||
                                        2 => 'app/log',
 | 
			
		||||
                                        3 => 'web/app_dev.php',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'hosts' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'testhost',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'pre-deploy' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'git/update',
 | 
			
		||||
                                        1 => 'composer/install',
 | 
			
		||||
                                        2 => 'composer/generate-autoload',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'on-deploy' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 =>
 | 
			
		||||
                                            array(
 | 
			
		||||
                                                'symfony/cache-clear' =>
 | 
			
		||||
                                                    array(
 | 
			
		||||
                                                        'env' => 'dev',
 | 
			
		||||
                                                    ),
 | 
			
		||||
                                            ),
 | 
			
		||||
                                        1 =>
 | 
			
		||||
                                            array(
 | 
			
		||||
                                                'symfony/cache-warmup' =>
 | 
			
		||||
                                                    array(
 | 
			
		||||
                                                        'env' => 'dev',
 | 
			
		||||
                                                    ),
 | 
			
		||||
                                            ),
 | 
			
		||||
                                        2 =>
 | 
			
		||||
                                            array(
 | 
			
		||||
                                                'symfony/assets-install' =>
 | 
			
		||||
                                                    array(
 | 
			
		||||
                                                        'env' => 'dev',
 | 
			
		||||
                                                    ),
 | 
			
		||||
                                            ),
 | 
			
		||||
                                        3 =>
 | 
			
		||||
                                            array(
 | 
			
		||||
                                                'symfony/assetic-dump' =>
 | 
			
		||||
                                                    array(
 | 
			
		||||
                                                        'env' => 'dev',
 | 
			
		||||
                                                    ),
 | 
			
		||||
                                            ),
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'on-release' => null,
 | 
			
		||||
                                'post-release' => null,
 | 
			
		||||
                                'post-deploy' => null,
 | 
			
		||||
                            ),
 | 
			
		||||
                    ),
 | 
			
		||||
            )
 | 
			
		||||
        );
 | 
			
		||||
        
 | 
			
		||||
        /** @var AbstractCommand $command */
 | 
			
		||||
        $command = $application->find('config:environments');
 | 
			
		||||
        $command->setRuntime($runtime);
 | 
			
		||||
 | 
			
		||||
        $tester = new CommandTester($command);
 | 
			
		||||
        $tester->execute(['command' => $command->getName()]);
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals($tester->getStatusCode(), 0);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -229,4 +229,109 @@ class DeployCommandTest extends TestCase
 | 
			
		||||
            $this->assertEquals($ranCommands[$index], $command);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testDeploymentWithSkippingTask()
 | 
			
		||||
    {
 | 
			
		||||
        $application = new MageTestApplication();
 | 
			
		||||
        $application->add(new DeployCommand());
 | 
			
		||||
 | 
			
		||||
        $runtime = new RuntimeMockup();
 | 
			
		||||
        $runtime->setConfiguration(array(
 | 
			
		||||
                'environments' =>
 | 
			
		||||
                    array(
 | 
			
		||||
                        'test' =>
 | 
			
		||||
                            array(
 | 
			
		||||
                                'user' => 'tester',
 | 
			
		||||
                                'host_path' => '/var/www/test',
 | 
			
		||||
                                'branch' => 'master',
 | 
			
		||||
                                'exclude' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'vendor',
 | 
			
		||||
                                        1 => 'app/cache',
 | 
			
		||||
                                        2 => 'app/log',
 | 
			
		||||
                                        3 => 'web/app_dev.php',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'hosts' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'testhost',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'pre-deploy' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'git/update',
 | 
			
		||||
                                        1 => 'composer/install',
 | 
			
		||||
                                        2 => 'composer/generate-autoload',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'on-deploy' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 =>
 | 
			
		||||
                                            array(
 | 
			
		||||
                                                'symfony/cache-clear' =>
 | 
			
		||||
                                                    array(
 | 
			
		||||
                                                        'env' => 'dev',
 | 
			
		||||
                                                    ),
 | 
			
		||||
                                            ),
 | 
			
		||||
                                        1 =>
 | 
			
		||||
                                            array(
 | 
			
		||||
                                                'symfony/cache-warmup' =>
 | 
			
		||||
                                                    array(
 | 
			
		||||
                                                        'env' => 'dev',
 | 
			
		||||
                                                    ),
 | 
			
		||||
                                            ),
 | 
			
		||||
                                        2 =>
 | 
			
		||||
                                            array(
 | 
			
		||||
                                                'symfony/assets-install' =>
 | 
			
		||||
                                                    array(
 | 
			
		||||
                                                        'env' => 'dev',
 | 
			
		||||
                                                    ),
 | 
			
		||||
                                            ),
 | 
			
		||||
                                        3 =>
 | 
			
		||||
                                            array(
 | 
			
		||||
                                                'symfony/assetic-dump' =>
 | 
			
		||||
                                                    array(
 | 
			
		||||
                                                        'env' => 'dev',
 | 
			
		||||
                                                    ),
 | 
			
		||||
                                            ),
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'on-release' => null,
 | 
			
		||||
                                'post-release' => null,
 | 
			
		||||
                                'post-deploy' => null,
 | 
			
		||||
                            ),
 | 
			
		||||
                    ),
 | 
			
		||||
            )
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $runtime->setReleaseId('1234567890');
 | 
			
		||||
 | 
			
		||||
        /** @var AbstractCommand $command */
 | 
			
		||||
        $command = $application->find('deploy');
 | 
			
		||||
        $command->setRuntime($runtime);
 | 
			
		||||
 | 
			
		||||
        $tester = new CommandTester($command);
 | 
			
		||||
        $tester->execute(['command' => $command->getName(), 'environment' => 'test']);
 | 
			
		||||
 | 
			
		||||
        $ranCommands = $runtime->getRanCommands();
 | 
			
		||||
 | 
			
		||||
        $testCase = array(
 | 
			
		||||
            0 => 'git branch | grep "*"',
 | 
			
		||||
            1 => 'git pull',
 | 
			
		||||
            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 \\"',
 | 
			
		||||
            9 => 'git branch | grep "*"',
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        // Check total of Executed Commands
 | 
			
		||||
        $this->assertEquals(count($ranCommands), count($testCase));
 | 
			
		||||
 | 
			
		||||
        // Check Generated Commands
 | 
			
		||||
        foreach ($testCase as $index => $command) {
 | 
			
		||||
            $this->assertEquals($ranCommands[$index], $command);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(strpos($tester->getDisplay(), 'SKIPPED') !== false);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										72
									
								
								src/Mage/Tests/UtilsTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								src/Mage/Tests/UtilsTest.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,72 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Mage\Tests;
 | 
			
		||||
 | 
			
		||||
use Mage\Utils;
 | 
			
		||||
use Mage\Runtime\Runtime;
 | 
			
		||||
use DateTime;
 | 
			
		||||
use PHPUnit_Framework_TestCase as TestCase;
 | 
			
		||||
 | 
			
		||||
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');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testReleaseDate()
 | 
			
		||||
    {
 | 
			
		||||
        $releaseId = '20170102031530';
 | 
			
		||||
        $dateTime = Utils::getReleaseDate($releaseId);
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue($dateTime instanceof DateTime);
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals($dateTime->format('Y-m-d H:i:s'), '2017-01-02 03:15:30');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testTimeDiffs()
 | 
			
		||||
    {
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-1 second');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), 'just now');
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-45 seconds');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), '45 seconds ago');
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-90 seconds');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), 'one minute ago');
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-30 minutes');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), '30 minutes ago');
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-1 hour');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), 'one hour ago');
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-10 hours');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), '10 hours ago');
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-1 day');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), 'one day ago');
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-3 days');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), '3 days ago');
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-7 days');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), 'a week ago');
 | 
			
		||||
 | 
			
		||||
        $dateTime = new DateTime();
 | 
			
		||||
        $dateTime->modify('-10 days');
 | 
			
		||||
        $this->assertEquals(Utils::getTimeDiff($dateTime), '');
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -67,7 +67,8 @@ class Utils
 | 
			
		||||
            $releaseId[6], $releaseId[7],
 | 
			
		||||
            $releaseId[8], $releaseId[9],
 | 
			
		||||
            $releaseId[10], $releaseId[11],
 | 
			
		||||
            $releaseId[12], $releaseId[13]);
 | 
			
		||||
            $releaseId[12], $releaseId[13]
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        return new DateTime($formatted);
 | 
			
		||||
    }
 | 
			
		||||
@ -83,6 +84,7 @@ class Utils
 | 
			
		||||
        $textDiff = '';
 | 
			
		||||
        $now = new DateTime();
 | 
			
		||||
        $diff = $now->diff($releaseDate);
 | 
			
		||||
 | 
			
		||||
        if ($diff->format('%a') <= 7) {
 | 
			
		||||
            if ($diff->format('%d') == 7) {
 | 
			
		||||
                $textDiff = 'a week ago';
 | 
			
		||||
@ -100,9 +102,9 @@ class Utils
 | 
			
		||||
                } else {
 | 
			
		||||
                    $textDiff = $hours . ' hours ago';
 | 
			
		||||
                }
 | 
			
		||||
            } elseif ($diff->format('%d') == 0 && $diff->format('%h') == 0) {
 | 
			
		||||
            } elseif ($diff->format('%d') == 0 && $diff->format('%h') == 0 && $diff->format('%i') > 0) {
 | 
			
		||||
                $minutes = $diff->format('%i');
 | 
			
		||||
                if ($minutes <= 1) {
 | 
			
		||||
                if ($minutes == 1) {
 | 
			
		||||
                    $textDiff = 'one minute ago';
 | 
			
		||||
                } else {
 | 
			
		||||
                    $textDiff = $minutes . ' minutes ago';
 | 
			
		||||
@ -110,12 +112,13 @@ class Utils
 | 
			
		||||
            } elseif ($diff->format('%d') == 0 && $diff->format('%h') == 0 && $diff->format('%i') == 0) {
 | 
			
		||||
                $seconds = $diff->format('%s');
 | 
			
		||||
                if ($seconds < 10) {
 | 
			
		||||
                    $textDiff = 'just now!';
 | 
			
		||||
                    $textDiff = 'just now';
 | 
			
		||||
                } else {
 | 
			
		||||
                    $textDiff = $seconds . ' seconds ago';
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $textDiff;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user