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
							
								
									8090ebf65b
								
							
						
					
					
						commit
						b4f2b9b224
					
				@ -505,6 +505,110 @@ class DeployCommandTest extends TestCase
 | 
			
		||||
        $this->assertEquals(0, $tester->getStatusCode());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testDeploymentWithBranchOverwrite()
 | 
			
		||||
    {
 | 
			
		||||
        $application = new MageTestApplication();
 | 
			
		||||
        $application->add(new DeployCommand());
 | 
			
		||||
 | 
			
		||||
        $runtime = new RuntimeMockup();
 | 
			
		||||
        $runtime->setConfiguration(array(
 | 
			
		||||
                'environments' =>
 | 
			
		||||
                    array(
 | 
			
		||||
                        'test' =>
 | 
			
		||||
                            array(
 | 
			
		||||
                                'user' => 'tester',
 | 
			
		||||
                                'branch' => 'test',
 | 
			
		||||
                                'host_path' => '/var/www/test',
 | 
			
		||||
                                '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('deploy');
 | 
			
		||||
        $command->setRuntime($runtime);
 | 
			
		||||
 | 
			
		||||
        $tester = new CommandTester($command);
 | 
			
		||||
        $tester->execute(['command' => $command->getName(), 'environment' => 'test', '--branch' => 'maintenance']);
 | 
			
		||||
 | 
			
		||||
        $ranCommands = $runtime->getRanCommands();
 | 
			
		||||
 | 
			
		||||
        $testCase = array(
 | 
			
		||||
            0 => 'git branch | grep "*"',
 | 
			
		||||
            1 => 'git checkout maintenance',
 | 
			
		||||
            2 => 'git pull',
 | 
			
		||||
            3 => 'composer install --dev',
 | 
			
		||||
            4 => 'composer dumpautoload --optimize',
 | 
			
		||||
            5 => 'rsync -e "ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" -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 \\&\\& 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($testCase), count($ranCommands));
 | 
			
		||||
 | 
			
		||||
        // Check Generated Commands
 | 
			
		||||
        foreach ($testCase as $index => $command) {
 | 
			
		||||
            $this->assertEquals($command, $ranCommands[$index]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals(0, $tester->getStatusCode());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testDeploymentWithSkippingTask()
 | 
			
		||||
    {
 | 
			
		||||
        $application = new MageTestApplication();
 | 
			
		||||
@ -609,4 +713,73 @@ class DeployCommandTest extends TestCase
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals(0, $tester->getStatusCode());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testDeploymentWithCustomTask()
 | 
			
		||||
    {
 | 
			
		||||
        $application = new MageTestApplication();
 | 
			
		||||
        $application->add(new DeployCommand());
 | 
			
		||||
 | 
			
		||||
        $runtime = new RuntimeMockup();
 | 
			
		||||
        $runtime->setConfiguration(array(
 | 
			
		||||
                'environments' =>
 | 
			
		||||
                    array(
 | 
			
		||||
                        'test' =>
 | 
			
		||||
                            array(
 | 
			
		||||
                                'user' => 'tester',
 | 
			
		||||
                                'host_path' => '/var/www/test',
 | 
			
		||||
                                'exclude' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'vendor',
 | 
			
		||||
                                        1 => 'app/cache',
 | 
			
		||||
                                        2 => 'app/log',
 | 
			
		||||
                                        3 => 'web/app_dev.php',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'hosts' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'testhost',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'pre-deploy' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        1 => 'composer/install',
 | 
			
		||||
                                        2 => 'composer/generate-autoload',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'on-deploy' =>
 | 
			
		||||
                                    array(
 | 
			
		||||
                                        0 => 'Mage\\Tests\\Task\\CustomTask',
 | 
			
		||||
                                    ),
 | 
			
		||||
                                'on-release' => null,
 | 
			
		||||
                                'post-release' => null,
 | 
			
		||||
                                'post-deploy' => null,
 | 
			
		||||
                            ),
 | 
			
		||||
                    ),
 | 
			
		||||
            )
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        /** @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 => 'composer install --dev',
 | 
			
		||||
            1 => 'composer dumpautoload --optimize',
 | 
			
		||||
            2 => 'rsync -e "ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" -avz --exclude=.git --exclude=vendor --exclude=app/cache --exclude=app/log --exclude=web/app_dev.php ./ tester@testhost:/var/www/test',
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        // Check total of Executed Commands
 | 
			
		||||
        $this->assertEquals(count($testCase), count($ranCommands));
 | 
			
		||||
 | 
			
		||||
        // Check Generated Commands
 | 
			
		||||
        foreach ($testCase as $index => $command) {
 | 
			
		||||
            $this->assertEquals($command, $ranCommands[$index]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->assertTrue(strpos($tester->getDisplay(), '[Custom] Dummy Task') !== false);
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals(0, $tester->getStatusCode());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										49
									
								
								src/Mage/Tests/MageApplicationTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								src/Mage/Tests/MageApplicationTest.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,49 @@
 | 
			
		||||
<?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\MageApplication;
 | 
			
		||||
use Mage\Runtime\Exception\RuntimeException;
 | 
			
		||||
use Exception;
 | 
			
		||||
use PHPUnit_Framework_TestCase as TestCase;
 | 
			
		||||
 | 
			
		||||
class MageApplicationTest extends TestCase
 | 
			
		||||
{
 | 
			
		||||
    public function testValidConfiguration()
 | 
			
		||||
    {
 | 
			
		||||
        $application = new MageApplication();
 | 
			
		||||
        $application->configure(__DIR__ . '/Resources/basic.yml');
 | 
			
		||||
        $this->assertTrue($application instanceof MageApplication);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testInValidConfiguration()
 | 
			
		||||
    {
 | 
			
		||||
        try {
 | 
			
		||||
            $application = new MageApplication();
 | 
			
		||||
            $application->configure(__DIR__ . '/Resources/invalid.yml');
 | 
			
		||||
        } catch (Exception $exception) {
 | 
			
		||||
            $this->assertTrue($exception instanceof RuntimeException);
 | 
			
		||||
            $this->assertEquals(sprintf('The file "%s" does not have a valid Magallanes configuration.', __DIR__ . '/Resources/invalid.yml'), $exception->getMessage());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testInValidFile()
 | 
			
		||||
    {
 | 
			
		||||
        try {
 | 
			
		||||
            $application = new MageApplication();
 | 
			
		||||
            $application->configure(__DIR__ . '/Resources/this-does-not-exists.yml');
 | 
			
		||||
        } catch (Exception $exception) {
 | 
			
		||||
            $this->assertTrue($exception instanceof RuntimeException);
 | 
			
		||||
            $this->assertEquals(sprintf('The file "%s" does not exists or is not readable.', __DIR__ . '/Resources/this-does-not-exists.yml'), $exception->getMessage());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								src/Mage/Tests/Resources/basic.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/Mage/Tests/Resources/basic.yml
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
			
		||||
magephp:
 | 
			
		||||
    log_dir: /tmp
 | 
			
		||||
    environments:
 | 
			
		||||
        production:
 | 
			
		||||
            user: app
 | 
			
		||||
            branch: master
 | 
			
		||||
            host_path: /var/www/myapp
 | 
			
		||||
            releases: 4
 | 
			
		||||
            exclude:
 | 
			
		||||
                - ./var/cache/*
 | 
			
		||||
                - ./var/log/*
 | 
			
		||||
                - ./web/app_dev.php
 | 
			
		||||
            hosts:
 | 
			
		||||
                - webserver1
 | 
			
		||||
                - webserver2
 | 
			
		||||
                - webserver3
 | 
			
		||||
            pre-deploy:
 | 
			
		||||
                - git/update
 | 
			
		||||
                - composer/install
 | 
			
		||||
                - composer/generate-autoload
 | 
			
		||||
            on-deploy:
 | 
			
		||||
                - symfony/cache-warmup: { env: 'dev' }
 | 
			
		||||
                - symfony/assets-install: { env: 'dev' }
 | 
			
		||||
                - symfony/assetic-dump: { env: 'dev' }
 | 
			
		||||
            on-release:
 | 
			
		||||
            post-release:
 | 
			
		||||
            post-deploy:
 | 
			
		||||
							
								
								
									
										1
									
								
								src/Mage/Tests/Resources/invalid.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/Mage/Tests/Resources/invalid.yml
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
			
		||||
mage:
 | 
			
		||||
							
								
								
									
										36
									
								
								src/Mage/Tests/Task/CustomTask.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/Mage/Tests/Task/CustomTask.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;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Custom Task for Testing
 | 
			
		||||
 *
 | 
			
		||||
 * @author Andrés Montañez <andresmontanez@gmail.com>
 | 
			
		||||
 */
 | 
			
		||||
class CustomTask extends AbstractTask
 | 
			
		||||
{
 | 
			
		||||
    public function getName()
 | 
			
		||||
    {
 | 
			
		||||
        return 'custom';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getDescription()
 | 
			
		||||
    {
 | 
			
		||||
        return '[Custom] Dummy Task';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function execute()
 | 
			
		||||
    {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user