diff --git a/README.md b/README.md index 103ac2af..e973fed9 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,8 @@ task('clear', function () { run('php bin/clear'); }); -task('prod', ['prod_server', 'upload', 'clear']); -task('test', ['test_server', 'upload', 'clear']); +task('prod', 'Deploy on production.', ['prod_server', 'upload', 'clear']); +task('test', 'Deploy on test server.', ['test_server', 'upload', 'clear']); start(); ``` diff --git a/test/Deployer/TaskTest.php b/test/Deployer/TaskTest.php index 6b396733..b2054008 100644 --- a/test/Deployer/TaskTest.php +++ b/test/Deployer/TaskTest.php @@ -11,31 +11,6 @@ use Symfony\Component\Console\Tester\ApplicationTester; class TaskTest extends \PHPUnit_Framework_TestCase { - public function testTask() - { - $tool = deployer(); - - $str = ''; - - task('first', function () use (&$str) { - $str .= 'first'; - }); - - task('second', function () use (&$str) { - $str .= 'second'; - }); - - task('all', 'Run all', ['second', 'first']); - - $tool->getApp()->addCommands($tool->getCommands()); - $tool->getApp()->setAutoExit(false); - $tool->getApp()->setCatchExceptions(false); - $app = new ApplicationTester($tool->getApp()); - $app->run(array('command' => 'all')); - - $this->assertEquals('secondfirst', $str); - } - public function testRun() { $called = false; diff --git a/test/Deployer/Tool/ContextTest.php b/test/Deployer/Tool/ContextTest.php index 86e1d74b..54ab2517 100644 --- a/test/Deployer/Tool/ContextTest.php +++ b/test/Deployer/Tool/ContextTest.php @@ -8,7 +8,6 @@ namespace Deployer\Tool; use Deployer\Tool\Context; -use Deployer\Tool; class ContextTest extends \PHPUnit_Framework_TestCase { @@ -16,11 +15,11 @@ class ContextTest extends \PHPUnit_Framework_TestCase { Context::clear(); - $t1 = new Tool(); + $t1 = $this->getMockBuilder('Deployer\Tool')->disableOriginalConstructor()->getMock(); Context::push($t1); $this->assertInstanceOf('Deployer\Tool', Context::get()); - $t2 = new Tool(); + $t2 = $this->getMockBuilder('Deployer\Tool')->disableOriginalConstructor()->getMock(); Context::push($t2); $this->assertInstanceOf('Deployer\Tool', Context::get()); diff --git a/test/Deployer/ToolTest.php b/test/Deployer/ToolTest.php new file mode 100644 index 00000000..89e24b00 --- /dev/null +++ b/test/Deployer/ToolTest.php @@ -0,0 +1,243 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Deployer; + +use Deployer\Tool\Context; +use Deployer\Utils\Local; +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Input\ArgvInput; +use Symfony\Component\Console\Output\ConsoleOutput; +use Symfony\Component\Console\Tester\ApplicationTester; + +class ToolTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Application + */ + private $app; + + /** + * @var Tool + */ + private $tool; + + public function testCreate() + { + $tool = deployer(false); + $this->assertInstanceOf('Deployer\Tool', $tool); + } + + public function testIncludeFunctions() + { + deployer(); + $this->assertTrue(function_exists('start'), 'Functions does not included.'); + } + + public function testTask() + { + $this->deployer(); + + $called = false; + task('one', 'One task', function () use (&$called) { + $called = true; + }); + + $this->start('one'); + + $this->assertTrue($called, 'Task was not called.'); + } + + public function testTaskCreateWithAndWithoutDesc() + { + $this->deployer(); + + task('one', function () { + }); + + task('two', 'Task two', function () { + }); + + $this->assertArrayHasKey('one', $this->tool->getTasks()); + $this->assertArrayHasKey('two', $this->tool->getTasks()); + } + + public function testTaskArrayDefinition() + { + $this->deployer(); + + $calls = []; + + task('one', function () use (&$calls) { + $calls[] = 'one'; + }); + + task('two', function () use (&$calls) { + $calls[] = 'two'; + }); + + task('comp', 'Call one and two', ['one', 'two']); + + $this->start('comp'); + $this->assertContains('one', $calls); + $this->assertContains('two', $calls); + } + + public function testConnectCdRunAndUploadFile() + { + $localFile = realpath(__DIR__ . '/../fixture/file'); + + $remote = $this->getMock('Deployer\Remote\RemoteInterface'); + $remote + ->expects($this->once()) + ->method('cd') + ->with('/home'); + + $remote + ->expects($this->once()) + ->method('execute') + ->with('command'); + + $remote + ->expects($this->once()) + ->method('uploadFile') + ->with($localFile, '/remote'); + + $remoteFactory = $this->getMock('Deployer\Remote\RemoteFactory'); + $remoteFactory + ->expects($this->once()) + ->method('create') + ->will($this->returnValue($remote)); + + $this->deployer($remoteFactory); + + connect('localhost', 'user', 'password'); + + cd('/home'); + run('command'); + upload($localFile, '/remote'); + } + + public function testUploadOfDirectory() + { + $local = realpath(__DIR__ . '/../fixture/'); + + $remote = $this->getMock('Deployer\Remote\RemoteInterface'); + + $remote + ->expects($this->at(0)) + ->method('uploadFile') + ->with($local . '/file', '/remote/file'); + + $remote + ->expects($this->at(1)) + ->method('uploadFile') + ->with($local . '/src/some.php', '/remote/src/some.php'); + + $remoteFactory = $this->getMock('Deployer\Remote\RemoteFactory'); + $remoteFactory + ->expects($this->once()) + ->method('create') + ->will($this->returnValue($remote)); + + $this->deployer($remoteFactory); + + connect('localhost', 'user', 'password'); + ignore(['ignor*']); + upload($local, '/remote'); + } + + public function testLocal() + { + $local = $this->getMock('\Deployer\Utils\Local'); + $local + ->expects($this->once()) + ->method('execute') + ->with('command'); + + $this->deployer(null, $local); + runLocally('command'); + } + + public function testGroup() + { + $remote1 = $this->getMock('Deployer\Remote\RemoteInterface'); + $remote1 + ->expects($this->exactly(1)) + ->method('execute') + ->with('command1'); + + $remote2 = $this->getMock('Deployer\Remote\RemoteInterface'); + $remote2 + ->expects($this->exactly(2)) + ->method('execute') + ->with('command2'); + + $remoteFactory = $this->getMock('Deployer\Remote\RemoteFactory'); + $remoteFactory + ->expects($this->any()) + ->method('create') + ->will($this->returnCallback(function ($host) use ($remote1, $remote2) { + return $host === 'host1' ? $remote1 : $remote2; + })); + + + $this->deployer($remoteFactory); + + connect('host1', 'user', 'password', 'one'); + connect('host2', 'user', 'password', 'two'); + connect('host3', 'user', 'password', 'two'); + + group('one', function () { + run('command1'); + }); + + group('two', function () { + run('command2'); + }); + } + + public static function setUpBeforeClass() + { + include_once __DIR__ . '/../../src/Deployer/functions.php'; + } + + private function deployer($remoteFactory = null, $local = null) + { + $this->app = new Application(); + $this->app->setAutoExit(false); + $this->app->setCatchExceptions(false); + + $this->tool = new Tool( + $this->app, + $this->getMock('\Symfony\Component\Console\Input\InputInterface'), + $this->getMock('\Symfony\Component\Console\Output\OutputInterface'), + null === $local ? $this->getMock('\Deployer\Utils\Local') : $local, + null === $remoteFactory ? $this->getMock('Deployer\Remote\RemoteFactory') : $remoteFactory + ); + + Context::push($this->tool); + } + + protected function tearDown() + { + Context::clear(); + } + + private function start($command = null) + { + $this->app->addCommands($this->tool->getCommands()); + $app = new ApplicationTester($this->app); + + if (null !== $command) { + $app->run(array('command' => $command)); + } + + return $app; + } +} + \ No newline at end of file diff --git a/test/fixture/file b/test/fixture/file new file mode 100644 index 00000000..e69de29b diff --git a/test/fixture/ignore/file b/test/fixture/ignore/file new file mode 100644 index 00000000..e69de29b diff --git a/test/fixture/src/some.php b/test/fixture/src/some.php new file mode 100644 index 00000000..e69de29b