|
| 1 | +<?php namespace Wirecli\Tests\Commands\Common; |
| 2 | + |
| 3 | +use Symfony\Component\Console\Tester\CommandTester; |
| 4 | +use Symfony\Component\Console\Question\Question; |
| 5 | +use Symfony\Component\Console\Input\InputInterface; |
| 6 | +use Symfony\Component\Console\Output\OutputInterface; |
| 7 | +use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 8 | +use Ofbeaton\Console\Tester\UnhandledQuestionException; |
| 9 | +use Wirecli\Tests\BaseTestCase as Base; |
| 10 | +use Wirecli\Commands\Common\NewCommand; |
| 11 | + |
| 12 | +class NewCommandTest extends Base { |
| 13 | + /** |
| 14 | + * @field array default config values |
| 15 | + */ |
| 16 | + protected $defaults = array( |
| 17 | + '--timezone' => 'Europe\Berlin', |
| 18 | + '--httpHosts' => 'wirecli.sek', |
| 19 | + '--username' => 'admin', |
| 20 | + '--userpass' => 'password', |
| 21 | + '--useremail' => '[email protected]' |
| 22 | + ); |
| 23 | + |
| 24 | + /** |
| 25 | + * @before |
| 26 | + */ |
| 27 | + public function setupCommand() { |
| 28 | + $this->app->add(new NewCommand()); |
| 29 | + $this->command = $this->app->find('new'); |
| 30 | + $this->tester = new CommandTester($this->command); |
| 31 | + |
| 32 | + $credentials = array( |
| 33 | + '--dbUser' => $GLOBALS['DB_USER'], |
| 34 | + '--dbPass' => $GLOBALS['DB_PASSWD'], |
| 35 | + '--dbName' => $GLOBALS['DB_DBNAME'] |
| 36 | + ); |
| 37 | + |
| 38 | + $this->extends = array( |
| 39 | + 'command' => $this->command->getName(), |
| 40 | + 'directory' => Base::INSTALLATION_FOLDER, |
| 41 | + ); |
| 42 | + |
| 43 | + $this->defaults = array_merge($this->defaults, $credentials, $this->extends); |
| 44 | + } |
| 45 | + |
| 46 | + public function testDownload() { |
| 47 | + $this->checkInstallation(); |
| 48 | + $options = array('--no-install' => true, '--src' => Base::INSTALLATION_ARCHIVE); |
| 49 | + $this->tester->execute(array_merge($this->defaults, $options)); |
| 50 | + |
| 51 | + $this->assertDirectoryExists(Base::INSTALLATION_FOLDER); |
| 52 | + $this->assertDirectoryExists(Base::INSTALLATION_FOLDER . '/wire'); |
| 53 | + $this->assertDirectoryNotExists(Base::INSTALLATION_FOLDER . '/site'); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @depends testDownload |
| 58 | + * @expectedException RuntimeException |
| 59 | + * @expectedExceptionMessageRegExp /(Database connection information did not work)./ |
| 60 | + */ |
| 61 | + public function testInstallWrongPassword() { |
| 62 | + // check ProcessWire has not been installed yet |
| 63 | + if ($this->fs->exists(Base::INSTALLATION_FOLDER . '/site/config.php')) return; |
| 64 | + |
| 65 | + // return the input you want to answer the question with |
| 66 | + $this->mockQuestionHelper($this->command, function($text, $order, Question $question) { |
| 67 | + if (strpos($text, 'database user name') !== false) return 'whatever'; |
| 68 | + if (strpos($text, 'database password') !== false) return 'wrong'; |
| 69 | + |
| 70 | + throw new UnhandledQuestionException(); |
| 71 | + }); |
| 72 | + |
| 73 | + $options = array( |
| 74 | + '--src' => Base::INSTALLATION_ARCHIVE, |
| 75 | + '--dbPass' => 'wrong' |
| 76 | + ); |
| 77 | + |
| 78 | + $this->tester->execute(array_merge($this->defaults, $options)); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @depends testDownload |
| 83 | + * @expectedExceptionMessageRegExp /(enter a valid email address)/ |
| 84 | + */ |
| 85 | + public function testInstallInvalidEmailAddress() { |
| 86 | + // check ProcessWire has not been installed yet |
| 87 | + if ($this->fs->exists(Base::INSTALLATION_FOLDER . '/site/config.php')) return; |
| 88 | + |
| 89 | + // return the input you want to answer the question with |
| 90 | + $this->mockQuestionHelper($this->command, function($text, $order, Question $question) { |
| 91 | + if (strpos($text, 'admin email address') !== false) return 'whatever'; |
| 92 | + |
| 93 | + throw new UnhandledQuestionException(); |
| 94 | + }); |
| 95 | + |
| 96 | + $options = array( |
| 97 | + '--src' => Base::INSTALLATION_ARCHIVE, |
| 98 | + '--useremail' => 'invalid' |
| 99 | + ); |
| 100 | + |
| 101 | + $this->tester->execute(array_merge($this->defaults, $options)); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @depends testDownload |
| 106 | + */ |
| 107 | + public function testInstall() { |
| 108 | + // check ProcessWire has not been installed yet |
| 109 | + if ($this->fs->exists(Base::INSTALLATION_FOLDER . '/site/config.php')) return; |
| 110 | + |
| 111 | + $this->tester->execute($this->defaults); |
| 112 | + $output = $this->tester->getDisplay(); |
| 113 | + |
| 114 | + $this->assertDirectoryExists(Base::INSTALLATION_FOLDER . '/site'); |
| 115 | + $this->assertFileExists(Base::INSTALLATION_FOLDER . '/site/config.php'); |
| 116 | + $this->assertContains('Congratulations, ProcessWire has been successfully installed.', $output); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @depends testInstall |
| 121 | + * @expectedExceptionMessageRegExp /(There is already a \')(.*)(\' project)/ |
| 122 | + */ |
| 123 | + public function testIsInstalled() { |
| 124 | + $this->tester->execute($this->defaults); |
| 125 | + } |
| 126 | +} |
0 commit comments