From de7f666118df3f9b4ed6aebd798ed5ae8a186081 Mon Sep 17 00:00:00 2001 From: Leszek Prabucki Date: Sun, 22 Jan 2012 22:06:09 +0100 Subject: [PATCH] Added and extended some unit tests --- .../Test/DependencyResolver/PoolTest.php | 61 +++++ .../RuleSetIteratorTest.php | 19 ++ .../Test/DependencyResolver/RuleSetTest.php | 139 ++++++++++- .../Test/DependencyResolver/RuleTest.php | 170 ++++++++++++++ .../Test/Downloader/GitDownloaderTest.php | 111 +++++++++ .../Test/Downloader/HgDownloaderTest.php | 111 +++++++++ tests/Composer/Test/IO/ConsoleIOTest.php | 219 ++++++++++++++++++ 7 files changed, 829 insertions(+), 1 deletion(-) create mode 100644 tests/Composer/Test/DependencyResolver/RuleTest.php create mode 100644 tests/Composer/Test/Downloader/GitDownloaderTest.php create mode 100644 tests/Composer/Test/Downloader/HgDownloaderTest.php create mode 100644 tests/Composer/Test/IO/ConsoleIOTest.php diff --git a/tests/Composer/Test/DependencyResolver/PoolTest.php b/tests/Composer/Test/DependencyResolver/PoolTest.php index c4dcb0bcc..643be0428 100644 --- a/tests/Composer/Test/DependencyResolver/PoolTest.php +++ b/tests/Composer/Test/DependencyResolver/PoolTest.php @@ -30,4 +30,65 @@ class PoolTest extends TestCase $this->assertEquals(array($package), $pool->whatProvides('foo')); $this->assertEquals(array($package), $pool->whatProvides('foo')); } + + /** + * @expectedException \RuntimeException + */ + public function testGetPriorityForNotRegisteredRepository() + { + $pool = new Pool; + $repository = new ArrayRepository; + + $pool->getPriority($repository); + } + + public function testGetPriorityWhenRepositoryIsRegistered() + { + $pool = new Pool; + $firstRepository = new ArrayRepository; + $pool->addRepository($firstRepository); + $secondRepository = new ArrayRepository; + $pool->addRepository($secondRepository); + + $firstPriority = $pool->getPriority($firstRepository); + $secondPriority = $pool->getPriority($secondRepository); + + $this->assertEquals(0, $firstPriority); + $this->assertEquals(1, $secondPriority); + } + + public function testPackageById() + { + $pool = new Pool; + $repository = new ArrayRepository; + $package = $this->getPackage('foo', '1'); + + $repository->addPackage($package); + $pool->addRepository($repository); + + $this->assertSame($package, $pool->packageById(1)); + } + + public function testWhatProvidesWhenPackageCannotBeFound() + { + $pool = new Pool; + + $this->assertEquals(array(), $pool->whatProvides('foo')); + } + + public function testGetMaxId() + { + $pool = new Pool; + $repository = new ArrayRepository; + $firstPackage = $this->getPackage('foo', '1'); + $secondPackage = $this->getPackage('foo1', '1'); + + $this->assertEquals(0, $pool->getMaxId()); + + $repository->addPackage($firstPackage); + $repository->addPackage($secondPackage); + $pool->addRepository($repository); + + $this->assertEquals(2, $pool->getMaxId()); + } } diff --git a/tests/Composer/Test/DependencyResolver/RuleSetIteratorTest.php b/tests/Composer/Test/DependencyResolver/RuleSetIteratorTest.php index 2daa64eb6..d45f9a561 100644 --- a/tests/Composer/Test/DependencyResolver/RuleSetIteratorTest.php +++ b/tests/Composer/Test/DependencyResolver/RuleSetIteratorTest.php @@ -52,4 +52,23 @@ class ResultSetIteratorTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $result); } + + public function testKeys() + { + $ruleSetIterator = new RuleSetIterator($this->rules); + + $result = array(); + foreach ($ruleSetIterator as $key => $rule) + { + $result[] = $key; + } + + $expected = array( + RuleSet::TYPE_JOB, + RuleSet::TYPE_JOB, + RuleSet::TYPE_UPDATE, + ); + + $this->assertEquals($expected, $result); + } } diff --git a/tests/Composer/Test/DependencyResolver/RuleSetTest.php b/tests/Composer/Test/DependencyResolver/RuleSetTest.php index fa42d4522..be37b8795 100644 --- a/tests/Composer/Test/DependencyResolver/RuleSetTest.php +++ b/tests/Composer/Test/DependencyResolver/RuleSetTest.php @@ -14,8 +14,10 @@ namespace Composer\Test\DependencyResolver; use Composer\DependencyResolver\Rule; use Composer\DependencyResolver\RuleSet; +use Composer\DependencyResolver\Literal; +use Composer\Test\TestCase; -class RuleSetTest extends \PHPUnit_Framework_TestCase +class RuleSetTest extends TestCase { public function testAdd() { @@ -41,4 +43,139 @@ class RuleSetTest extends \PHPUnit_Framework_TestCase $this->assertEquals($rules, $ruleSet->getRules()); } + + /** + * @expectedException \OutOfBoundsException + */ + public function testAddWhenTypeIsNotRecognized() + { + $ruleSet = new RuleSet; + + $ruleSet->add(new Rule(array(), 'job1', null), 7); + } + + public function testAddWhenTypeIsUnknow() + { + $ruleSet = new RuleSet; + + $rule = new Rule(array(), 'job1', null); + $ruleSet->add($rule, -1); + + $rules = $ruleSet->getRules(); + $this->assertSame($rule, $rules[-1][0]); + } + + public function testCount() + { + $ruleSet = new RuleSet; + + $ruleSet->add(new Rule(array(), 'job1', null), RuleSet::TYPE_JOB); + $ruleSet->add(new Rule(array(), 'job2', null), RuleSet::TYPE_JOB); + + $this->assertEquals(2, $ruleSet->count()); + } + + public function testRuleById() + { + $ruleSet = new RuleSet; + + $rule = new Rule(array(), 'job1', null); + $ruleSet->add($rule, RuleSet::TYPE_JOB); + + $this->assertSame($rule, $ruleSet->ruleById(0)); + } + + public function testGetIterator() + { + $ruleSet = new RuleSet; + + $rule1 = new Rule(array(), 'job1', null); + $rule2 = new Rule(array(), 'job1', null); + $ruleSet->add($rule1, RuleSet::TYPE_JOB); + $ruleSet->add($rule2, RuleSet::TYPE_UPDATE); + + $iterator = $ruleSet->getIterator(); + + $this->assertSame($rule1, $iterator->current()); + $iterator->next(); + $this->assertSame($rule2, $iterator->current()); + } + + public function testGetIteratorFor() + { + $ruleSet = new RuleSet; + $rule1 = new Rule(array(), 'job1', null); + $rule2 = new Rule(array(), 'job1', null); + + $ruleSet->add($rule1, RuleSet::TYPE_JOB); + $ruleSet->add($rule2, RuleSet::TYPE_UPDATE); + + $iterator = $ruleSet->getIteratorFor(RuleSet::TYPE_UPDATE); + + $this->assertSame($rule2, $iterator->current()); + } + + public function testGetIteratorWithout() + { + $ruleSet = new RuleSet; + $rule1 = new Rule(array(), 'job1', null); + $rule2 = new Rule(array(), 'job1', null); + + $ruleSet->add($rule1, RuleSet::TYPE_JOB); + $ruleSet->add($rule2, RuleSet::TYPE_UPDATE); + + $iterator = $ruleSet->getIteratorWithout(RuleSet::TYPE_JOB); + + $this->assertSame($rule2, $iterator->current()); + } + + public function testContainsEqual() + { + $ruleSet = new RuleSet; + + $rule = $this->getRuleMock(); + $rule->expects($this->any()) + ->method('getHash') + ->will($this->returnValue('rule_1_hash')); + $rule->expects($this->any()) + ->method('equals') + ->will($this->returnValue(true)); + + $rule2 = $this->getRuleMock(); + $rule2->expects($this->any()) + ->method('getHash') + ->will($this->returnValue('rule_2_hash')); + + $rule3 = $this->getRuleMock(); + $rule3->expects($this->any()) + ->method('getHash') + ->will($this->returnValue('rule_1_hash')); + $rule3->expects($this->any()) + ->method('equal') + ->will($this->returnValue(false)); + + $ruleSet->add($rule, RuleSet::TYPE_UPDATE); + + $this->assertTrue($ruleSet->containsEqual($rule)); + $this->assertFalse($ruleSet->containsEqual($rule2)); + $this->assertFalse($ruleSet->containsEqual($rule3)); + } + + public function testToString() + { + $ruleSet = new RuleSet; + $literal = new Literal($this->getPackage('foo', '2.1'), true); + $rule = new Rule(array($literal), 'job1', null); + + $ruleSet->add($rule, RuleSet::TYPE_UPDATE); + + $this->assertContains('UPDATE : (+foo-2.1.0.0)', $ruleSet->__toString()); + } + + private function getRuleMock() + { + return $this->getMockBuilder('Composer\DependencyResolver\Rule') + ->disableOriginalConstructor() + ->getMock(); + } } diff --git a/tests/Composer/Test/DependencyResolver/RuleTest.php b/tests/Composer/Test/DependencyResolver/RuleTest.php new file mode 100644 index 000000000..f76f46eb1 --- /dev/null +++ b/tests/Composer/Test/DependencyResolver/RuleTest.php @@ -0,0 +1,170 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\DependencyResolver; + +use Composer\DependencyResolver\Rule; +use Composer\DependencyResolver\Literal; +use Composer\Test\TestCase; + +class RuleTest extends TestCase +{ + public function testGetHash() + { + $rule = new Rule(array(), 'job1', null); + $rule->ruleHash = '123'; + + $this->assertEquals('123', $rule->getHash()); + } + + public function testSetAndGetId() + { + $rule = new Rule(array(), 'job1', null); + $rule->setId(666); + + $this->assertEquals(666, $rule->getId()); + } + + public function testEqualsForRulesWithDifferentHashes() + { + $rule = new Rule(array(), 'job1', null); + $rule->ruleHash = '123'; + + $rule2 = new Rule(array(), 'job1', null); + $rule2->ruleHash = '321'; + + $this->assertFalse($rule->equals($rule2)); + } + + public function testEqualsForRulesWithDifferentLiterals() + { + $literal = $this->getLiteralMock(); + $literal->expects($this->any()) + ->method('getId') + ->will($this->returnValue(1)); + $rule = new Rule(array($literal), 'job1', null); + $rule->ruleHash = '123'; + + $literal = $this->getLiteralMock(); + $literal->expects($this->any()) + ->method('getId') + ->will($this->returnValue(12)); + $rule2 = new Rule(array($literal), 'job1', null); + $rule2->ruleHash = '123'; + + $this->assertFalse($rule->equals($rule2)); + } + + public function testEqualsForRulesWithDifferLiteralsQuantity() + { + $literal = $this->getLiteralMock(); + $literal->expects($this->any()) + ->method('getId') + ->will($this->returnValue(1)); + $literal2 = $this->getLiteralMock(); + $literal2->expects($this->any()) + ->method('getId') + ->will($this->returnValue(12)); + + $rule = new Rule(array($literal, $literal2), 'job1', null); + $rule->ruleHash = '123'; + $rule2 = new Rule(array($literal), 'job1', null); + $rule2->ruleHash = '123'; + + $this->assertFalse($rule->equals($rule2)); + } + + public function testEqualsForRulesWithThisSameLiterals() + { + $literal = $this->getLiteralMock(); + $literal->expects($this->any()) + ->method('getId') + ->will($this->returnValue(1)); + $literal2 = $this->getLiteralMock(); + $literal2->expects($this->any()) + ->method('getId') + ->will($this->returnValue(12)); + + $rule = new Rule(array($literal, $literal2), 'job1', null); + $rule2 = new Rule(array($literal, $literal2), 'job1', null); + + $this->assertTrue($rule->equals($rule2)); + } + + public function testSetAndGetType() + { + $rule = new Rule(array(), 'job1', null); + $rule->setType('someType'); + + $this->assertEquals('someType', $rule->getType()); + } + + public function testEnable() + { + $rule = new Rule(array(), 'job1', null); + $rule->disable(); + $rule->enable(); + + $this->assertTrue($rule->isEnabled()); + $this->assertFalse($rule->isDisabled()); + } + + public function testDisable() + { + $rule = new Rule(array(), 'job1', null); + $rule->enable(); + $rule->disable(); + + $this->assertTrue($rule->isDisabled()); + $this->assertFalse($rule->isEnabled()); + } + + public function testSetWeak() + { + $rule = new Rule(array(), 'job1', null); + $rule->setWeak(true); + + $rule2 = new Rule(array(), 'job1', null); + $rule2->setWeak(false); + + $this->assertTrue($rule->isWeak()); + $this->assertFalse($rule2->isWeak()); + } + + public function testIsAssertions() + { + $literal = $this->getLiteralMock(); + $literal2 = $this->getLiteralMock(); + $rule = new Rule(array($literal, $literal2), 'job1', null); + $rule2 = new Rule(array($literal), 'job1', null); + + $this->assertFalse($rule->isAssertion()); + $this->assertTrue($rule2->isAssertion()); + } + + public function testToString() + { + $literal = new Literal($this->getPackage('foo', '2.1'), true); + $literal2 = new Literal($this->getPackage('baz', '1.1'), false); + + $rule = new Rule(array($literal, $literal2), 'job1', null); + + $this->assertEquals('(-baz-1.1.0.0|+foo-2.1.0.0)', $rule->__toString()); + } + + private function getLiteralMock() + { + return $this->getMockBuilder('Composer\DependencyResolver\Literal') + ->disableOriginalConstructor() + ->getMock(); + } +} diff --git a/tests/Composer/Test/Downloader/GitDownloaderTest.php b/tests/Composer/Test/Downloader/GitDownloaderTest.php new file mode 100644 index 000000000..57cbd945d --- /dev/null +++ b/tests/Composer/Test/Downloader/GitDownloaderTest.php @@ -0,0 +1,111 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Downloader; + +use Composer\Downloader\GitDownloader; + +class GitDownloaderTest extends \PHPUnit_Framework_TestCase +{ + /** + * @expectedException \InvalidArgumentException + */ + public function testDownloadForPackageWithoutSourceReference() + { + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $packageMock->expects($this->once()) + ->method('getSourceReference') + ->will($this->returnValue(null)); + + $downloader = new GitDownloader(); + $downloader->download($packageMock, '/path'); + } + + public function testDownload() + { + $expectedGitCommand = 'git clone \'https://github.com/l3l0/composer\' composerPath && cd composerPath && git checkout \'ref\' && git reset --hard \'ref\''; + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $packageMock->expects($this->any()) + ->method('getSourceReference') + ->will($this->returnValue('ref')); + $packageMock->expects($this->once()) + ->method('getSourceUrl') + ->will($this->returnValue('https://github.com/l3l0/composer')); + $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); + $processExecutor->expects($this->once()) + ->method('execute') + ->with($this->equalTo($expectedGitCommand)); + + $downloader = new GitDownloader($processExecutor); + $downloader->download($packageMock, 'composerPath'); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testUpdateforPackageWithoutSourceReference() + { + $initialPackageMock = $this->getMock('Composer\Package\PackageInterface'); + $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface'); + $sourcePackageMock->expects($this->once()) + ->method('getSourceReference') + ->will($this->returnValue(null)); + + $downloader = new GitDownloader(); + $downloader->update($initialPackageMock, $sourcePackageMock, '/path'); + } + + public function testUpdate() + { + $expectedGitUpdateCommand = 'cd composerPath && git fetch && git checkout ref && git reset --hard ref'; + $expectedGitResetCommand = 'cd composerPath && git status --porcelain'; + + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $packageMock->expects($this->any()) + ->method('getSourceReference') + ->will($this->returnValue('ref')); + $packageMock->expects($this->any()) + ->method('getSourceUrl') + ->will($this->returnValue('https://github.com/l3l0/composer')); + $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); + $processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedGitResetCommand)); + $processExecutor->expects($this->at(1)) + ->method('execute') + ->with($this->equalTo($expectedGitUpdateCommand)); + + $downloader = new GitDownloader($processExecutor); + $downloader->update($packageMock, $packageMock, 'composerPath'); + } + + public function testRemove() + { + $expectedGitResetCommand = 'cd composerPath && git status --porcelain'; + + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); + $processExecutor->expects($this->any()) + ->method('execute') + ->with($this->equalTo($expectedGitResetCommand)); + + $downloader = new GitDownloader($processExecutor); + $downloader->remove($packageMock, 'composerPath'); + } + + public function testGetInstallationSource() + { + $downloader = new GitDownloader(); + + $this->assertEquals('source', $downloader->getInstallationSource()); + } +} diff --git a/tests/Composer/Test/Downloader/HgDownloaderTest.php b/tests/Composer/Test/Downloader/HgDownloaderTest.php new file mode 100644 index 000000000..d6752eb4a --- /dev/null +++ b/tests/Composer/Test/Downloader/HgDownloaderTest.php @@ -0,0 +1,111 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Downloader; + +use Composer\Downloader\HgDownloader; + +class HgDownloaderTest extends \PHPUnit_Framework_TestCase +{ + /** + * @expectedException \InvalidArgumentException + */ + public function testDownloadForPackageWithoutSourceReference() + { + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $packageMock->expects($this->once()) + ->method('getSourceReference') + ->will($this->returnValue(null)); + + $downloader = new HgDownloader(); + $downloader->download($packageMock, '/path'); + } + + public function testDownload() + { + $expectedGitCommand = '(hg clone \'https://mercurial.dev/l3l0/composer\' composerPath 2> /dev/null) && cd composerPath && hg up \'ref\''; + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $packageMock->expects($this->any()) + ->method('getSourceReference') + ->will($this->returnValue('ref')); + $packageMock->expects($this->once()) + ->method('getSourceUrl') + ->will($this->returnValue('https://mercurial.dev/l3l0/composer')); + $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); + $processExecutor->expects($this->once()) + ->method('execute') + ->with($this->equalTo($expectedGitCommand)); + + $downloader = new HgDownloader($processExecutor); + $downloader->download($packageMock, 'composerPath'); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testUpdateforPackageWithoutSourceReference() + { + $initialPackageMock = $this->getMock('Composer\Package\PackageInterface'); + $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface'); + $sourcePackageMock->expects($this->once()) + ->method('getSourceReference') + ->will($this->returnValue(null)); + + $downloader = new HgDownloader(); + $downloader->update($initialPackageMock, $sourcePackageMock, '/path'); + } + + public function testUpdate() + { + $expectedGitUpdateCommand = 'cd composerPath && hg pull && hg up \'ref\''; + $expectedGitResetCommand = 'cd composerPath && hg st'; + + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $packageMock->expects($this->any()) + ->method('getSourceReference') + ->will($this->returnValue('ref')); + $packageMock->expects($this->any()) + ->method('getSourceUrl') + ->will($this->returnValue('https://github.com/l3l0/composer')); + $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); + $processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedGitResetCommand)); + $processExecutor->expects($this->at(1)) + ->method('execute') + ->with($this->equalTo($expectedGitUpdateCommand)); + + $downloader = new HgDownloader($processExecutor); + $downloader->update($packageMock, $packageMock, 'composerPath'); + } + + public function testRemove() + { + $expectedGitResetCommand = 'cd composerPath && hg st'; + + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); + $processExecutor->expects($this->any()) + ->method('execute') + ->with($this->equalTo($expectedGitResetCommand)); + + $downloader = new HgDownloader($processExecutor); + $downloader->remove($packageMock, 'composerPath'); + } + + public function testGetInstallationSource() + { + $downloader = new HgDownloader(); + + $this->assertEquals('source', $downloader->getInstallationSource()); + } +} diff --git a/tests/Composer/Test/IO/ConsoleIOTest.php b/tests/Composer/Test/IO/ConsoleIOTest.php new file mode 100644 index 000000000..78450eb53 --- /dev/null +++ b/tests/Composer/Test/IO/ConsoleIOTest.php @@ -0,0 +1,219 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\IO; + +use Composer\IO\ConsoleIO; +use Composer\Test\TestCase; + +class ConsoleIOTest extends TestCase +{ + public function testIsInteractive() + { + $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $inputMock->expects($this->at(0)) + ->method('isInteractive') + ->will($this->returnValue(true)); + $inputMock->expects($this->at(1)) + ->method('isInteractive') + ->will($this->returnValue(false)); + + $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet'); + + $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock); + + $this->assertTrue($consoleIO->isInteractive()); + $this->assertFalse($consoleIO->isInteractive()); + } + + public function testWrite() + { + $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $outputMock->expects($this->once()) + ->method('write') + ->with($this->equalTo('some information about something'), $this->equalTo(false)); + $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet'); + + $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock); + $consoleIO->write('some information about something', false); + } + + public function testOverwrite() + { + $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $outputMock->expects($this->at(0)) + ->method('write') + ->with($this->equalTo("\x08"), $this->equalTo(false)); + $outputMock->expects($this->at(19)) + ->method('write') + ->with($this->equalTo("\x08"), $this->equalTo(false)); + $outputMock->expects($this->at(20)) + ->method('write') + ->with($this->equalTo('some information'), $this->equalTo(false)); + $outputMock->expects($this->at(21)) + ->method('write') + ->with($this->equalTo(' '), $this->equalTo(false)); + $outputMock->expects($this->at(24)) + ->method('write') + ->with($this->equalTo(' '), $this->equalTo(false)); + $outputMock->expects($this->at(25)) + ->method('write') + ->with($this->equalTo("\x08"), $this->equalTo(false)); + $outputMock->expects($this->at(28)) + ->method('write') + ->with($this->equalTo("\x08"), $this->equalTo(false)); + $outputMock->expects($this->at(29)) + ->method('write') + ->with($this->equalTo('')); + + $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet'); + + $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock); + $consoleIO->overwrite('some information', true, 20); + } + + public function testAsk() + { + $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $dialogMock = $this->getMock('Symfony\Component\Console\Helper\DialogHelper'); + $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet'); + + $dialogMock->expects($this->once()) + ->method('ask') + ->with($this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'), + $this->equalTo('Why?'), + $this->equalTo('default')); + $helperMock->expects($this->once()) + ->method('get') + ->with($this->equalTo('dialog')) + ->will($this->returnValue($dialogMock)); + + $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock); + $consoleIO->ask('Why?', 'default'); + } + + public function testAskConfirmation() + { + $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $dialogMock = $this->getMock('Symfony\Component\Console\Helper\DialogHelper'); + $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet'); + + $dialogMock->expects($this->once()) + ->method('askConfirmation') + ->with($this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'), + $this->equalTo('Why?'), + $this->equalTo('default')); + $helperMock->expects($this->once()) + ->method('get') + ->with($this->equalTo('dialog')) + ->will($this->returnValue($dialogMock)); + + $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock); + $consoleIO->askConfirmation('Why?', 'default'); + } + + public function testAskAndValidate() + { + $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $dialogMock = $this->getMock('Symfony\Component\Console\Helper\DialogHelper'); + $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet'); + + $dialogMock->expects($this->once()) + ->method('askAndValidate') + ->with($this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'), + $this->equalTo('Why?'), + $this->equalTo('validator'), + $this->equalTo(10), + $this->equalTo('default')); + $helperMock->expects($this->once()) + ->method('get') + ->with($this->equalTo('dialog')) + ->will($this->returnValue($dialogMock)); + + $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock); + $consoleIO->askAndValidate('Why?', 'validator', 10, 'default'); + } + + public function testSetAndGetAuthorization() + { + $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet'); + + $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock); + $consoleIO->setAuthorization('repoName', 'l3l0', 'passwd'); + + $this->assertEquals( + array('username' => 'l3l0', 'password' => 'passwd'), + $consoleIO->getAuthorization('repoName') + ); + } + + public function testGetAuthorizationWhenDidNotSet() + { + $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet'); + + $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock); + + $this->assertEquals( + array('username' => null, 'password' => null), + $consoleIO->getAuthorization('repoName') + ); + } + + public function testHasAuthorization() + { + $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet'); + + $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock); + $consoleIO->setAuthorization('repoName', 'l3l0', 'passwd'); + + $this->assertTrue($consoleIO->hasAuthorization('repoName')); + $this->assertFalse($consoleIO->hasAuthorization('repoName2')); + } + + public function testGetLastUsername() + { + $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet'); + + $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock); + $consoleIO->setAuthorization('repoName', 'l3l0', 'passwd'); + $consoleIO->setAuthorization('repoName2', 'l3l02', 'passwd2'); + + $this->assertEquals('l3l02', $consoleIO->getLastUsername()); + } + + public function testGetLastPassword() + { + $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet'); + + $consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock); + $consoleIO->setAuthorization('repoName', 'l3l0', 'passwd'); + $consoleIO->setAuthorization('repoName2', 'l3l02', 'passwd2'); + + $this->assertEquals('passwd2', $consoleIO->getLastPassword()); + } +}