From 4b7cc618b4c5bfdf8806148357fc3da8470e9b1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sun, 1 Jan 2017 03:23:27 -0300 Subject: [PATCH] [Nostromo] Tests --- README.md | 2 +- .../BuiltIn/Config/DumpCommandTest.php | 27 +++++ .../Config/EnvironmentsCommandTest.php | 93 ++++++++++++++++ .../Command/BuiltIn/DeployCommandTest.php | 105 ++++++++++++++++++ src/Mage/Tests/UtilsTest.php | 72 ++++++++++++ src/Mage/Utils.php | 11 +- 6 files changed, 305 insertions(+), 5 deletions(-) create mode 100644 src/Mage/Tests/Command/BuiltIn/Config/DumpCommandTest.php create mode 100644 src/Mage/Tests/Command/BuiltIn/Config/EnvironmentsCommandTest.php create mode 100644 src/Mage/Tests/UtilsTest.php diff --git a/README.md b/README.md index 8097021..e0a7a3d 100644 --- a/README.md +++ b/README.md @@ -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)*. diff --git a/src/Mage/Tests/Command/BuiltIn/Config/DumpCommandTest.php b/src/Mage/Tests/Command/BuiltIn/Config/DumpCommandTest.php new file mode 100644 index 0000000..bb526a4 --- /dev/null +++ b/src/Mage/Tests/Command/BuiltIn/Config/DumpCommandTest.php @@ -0,0 +1,27 @@ +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); + } +} diff --git a/src/Mage/Tests/Command/BuiltIn/Config/EnvironmentsCommandTest.php b/src/Mage/Tests/Command/BuiltIn/Config/EnvironmentsCommandTest.php new file mode 100644 index 0000000..bf1c689 --- /dev/null +++ b/src/Mage/Tests/Command/BuiltIn/Config/EnvironmentsCommandTest.php @@ -0,0 +1,93 @@ +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); + } +} diff --git a/src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php b/src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php index 0867d15..89cab7c 100644 --- a/src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php +++ b/src/Mage/Tests/Command/BuiltIn/DeployCommandTest.php @@ -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); + } } diff --git a/src/Mage/Tests/UtilsTest.php b/src/Mage/Tests/UtilsTest.php new file mode 100644 index 0000000..85372dd --- /dev/null +++ b/src/Mage/Tests/UtilsTest.php @@ -0,0 +1,72 @@ +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), ''); + } +} diff --git a/src/Mage/Utils.php b/src/Mage/Utils.php index e82dc44..eb6b866 100644 --- a/src/Mage/Utils.php +++ b/src/Mage/Utils.php @@ -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; } }