diff options
author | mattab <matthieu.aubry@gmail.com> | 2014-04-07 06:29:56 +0400 |
---|---|---|
committer | mattab <matthieu.aubry@gmail.com> | 2014-04-07 06:29:56 +0400 |
commit | 8716d5884950a9a1b89efee75ee95a94cebb92ea (patch) | |
tree | da7b597ccdac843cdda37034981fcf47fcb81a48 /plugins | |
parent | ba14bccac99ccaa01e33e901afa14067716f474b (diff) | |
parent | d7d4f443a91a020e95cab6b7978a12a76dda32c4 (diff) |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'plugins')
3 files changed, 204 insertions, 2 deletions
diff --git a/plugins/CustomVariables/Commands/SetNumberOfCustomVariables.php b/plugins/CustomVariables/Commands/SetNumberOfCustomVariables.php index 60cd63ee55..7b26b32d0f 100644 --- a/plugins/CustomVariables/Commands/SetNumberOfCustomVariables.php +++ b/plugins/CustomVariables/Commands/SetNumberOfCustomVariables.php @@ -14,6 +14,7 @@ use Piwik\Tracker\Cache; use Piwik\Plugins\CustomVariables\Model; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** @@ -110,13 +111,13 @@ class SetNumberOfCustomVariables extends ConsoleCommand $maxCustomVars = $input->getArgument('maxCustomVars'); if (!is_numeric($maxCustomVars)) { - throw new \Exception('The number of available custom variables has to be a number'); + throw new \InvalidArgumentException('The number of available custom variables has to be a number'); } $maxCustomVars = (int) $maxCustomVars; if ($maxCustomVars <= 1) { - throw new \Exception('There has to be at least two custom variables'); + throw new \InvalidArgumentException('There has to be at least two custom variables'); } return $maxCustomVars; diff --git a/plugins/CustomVariables/tests/Commands/InfoTest.php b/plugins/CustomVariables/tests/Commands/InfoTest.php new file mode 100644 index 0000000000..82c9d3803e --- /dev/null +++ b/plugins/CustomVariables/tests/Commands/InfoTest.php @@ -0,0 +1,54 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ + +namespace Piwik\Plugins\CustomVariables\tests\Commands; + +use Piwik\Plugins\CustomVariables\Commands\Info; +use Piwik\Plugins\CustomVariables\Model; +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * @group CustomVariables + * @group CustomVariablesTest + * @group Database + * @group Plugins + */ +class InfoTest extends \DatabaseTestCase +{ + public function testExecute_ShouldOutputInfoSuccess_IfEverythingIsOk() + { + $this->assertEquals('Your Piwik is configured for 5 custom variables.', $this->executeCommand()); + } + + public function testExecute_ShouldOutputErrorMessage_IfColumnsDoNotMatch() + { + $model = new Model(Model::SCOPE_PAGE); + $model->removeCustomVariable(); + + $this->assertEquals('There is a problem with your custom variables configuration', $this->executeCommand()); + } + + private function executeCommand() + { + $application = new Application(); + $application->add(new Info()); + + $command = $application->find('customvariables:info'); + $commandTester = new CommandTester($command); + + $commandTester->execute(array('command' => $command->getName())); + $result = $commandTester->getDisplay(); + + return $result; + } +} diff --git a/plugins/CustomVariables/tests/Commands/SetNumberOfCustomVariablesTest.php b/plugins/CustomVariables/tests/Commands/SetNumberOfCustomVariablesTest.php new file mode 100644 index 0000000000..9be01c55b0 --- /dev/null +++ b/plugins/CustomVariables/tests/Commands/SetNumberOfCustomVariablesTest.php @@ -0,0 +1,147 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ + +namespace Piwik\Plugins\CustomVariables\tests\Commands; + +use Piwik\Plugins\CustomVariables\Commands\SetNumberOfCustomVariables; +use Piwik\Plugins\CustomVariables\CustomVariables; +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * @group CustomVariables + * @group CustomVariablesTest + * @group Database + * @group Plugins + */ +class SetNumberOfCustomVariablesTest extends \DatabaseTestCase +{ + /** + * @expectedException \RuntimeException + * @expectedExceptionMessage Not enough arguments + */ + public function testExecute_ShouldThrowException_IfArgumentIsMissing() + { + $this->executeCommand(array(), true); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The number of available custom variables has to be a number + */ + public function testExecute_ShouldThrowException_HasToBeANumber() + { + $this->executeCommand(array('maxCustomVars' => 'a'), true); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage There has to be at least two custom variables + */ + public function testExecute_ShouldThrowException_Minimum2CustomVarsRequired() + { + $this->executeCommand(array('maxCustomVars' => 1), true); + } + + public function testExecute_ShouldThrowException_IfUserCancelsConfirmation() + { + $result = $this->executeCommand(array('maxCustomVars' => 4), false); + $this->assertStringEndsWith('Are you sure you want to perform these actions? (y/N)', $result); + } + + public function testExecute_ShouldDoNothingIfExpectedResult_IsAlreadyTheCase() + { + $result = $this->executeCommand(array('maxCustomVars' => 5), true); + + $this->assertContains('Your Piwik is already configured for 5 custom variables', $result); + } + + public function testExecute_ShouldRemoveMaxCustomVars_IfNumberIsLessThanActual() + { + $this->assertEquals(5, CustomVariables::getMaxCustomVariables()); + + $result = $this->executeCommand(array('maxCustomVars' => 4), true); + + $this->assertContains('Configuring Piwik for 4 custom variables', $result); + $this->assertContains('1 existing custom variables having the index(es) 5 will be REMOVED.', $result); + $this->assertContains('Starting to apply changes', $result); + $this->assertContains('Removed a variable in scope "Page" having the index 5', $result); + $this->assertContains('Removed a variable in scope "Visit" having the index 5', $result); + $this->assertContains('Removed a variable in scope "Conversion" having the index 5', $result); + $this->assertContains('Your Piwik is now configured for 4 custom variables.', $result); + + $this->assertEquals(4, CustomVariables::getMaxCustomVariables()); + } + + public function testExecute_ShouldAddMaxCustomVars_IfNumberIsHigherThanActual() + { + $this->assertEquals(5, CustomVariables::getMaxCustomVariables()); + + $result = $this->executeCommand(array('maxCustomVars' => 6), true); + + $this->assertContains('Configuring Piwik for 6 custom variables', $result); + $this->assertContains('1 new custom variables having the index(es) 6 will be ADDED', $result); + $this->assertContains('Starting to apply changes', $result); + $this->assertContains('Added a variable in scope "Page" having the index 6', $result); + $this->assertContains('Added a variable in scope "Visit" having the index 6', $result); + $this->assertContains('Added a variable in scope "Conversion" having the index 6', $result); + $this->assertContains('Your Piwik is now configured for 6 custom variables.', $result); + + $this->assertEquals(6, CustomVariables::getMaxCustomVariables()); + } + + public function testExecute_AddMultiple_RemoveMultiple() + { + $this->assertEquals(5, CustomVariables::getMaxCustomVariables()); + + $this->executeCommand(array('maxCustomVars' => 8), true); + $this->assertEquals(8, CustomVariables::getMaxCustomVariables()); + + $this->executeCommand(array('maxCustomVars' => 3), true); + $this->assertEquals(3, CustomVariables::getMaxCustomVariables()); + } + + /** + * @param array $params + * @param bool $confirm + * + * @return string + */ + private function executeCommand(array $params, $confirm) + { + $confirm = $confirm ? 'yes' : 'no'; + + $application = new Application(); + $application->add(new SetNumberOfCustomVariables()); + + $command = $application->find('customvariables:set-max-custom-variables'); + $commandTester = new CommandTester($command); + + $dialog = $command->getHelper('dialog'); + $dialog->setInputStream($this->getInputStream($confirm . '\n')); + + $params['command'] = $command->getName(); + $commandTester->execute($params); + $result = $commandTester->getDisplay(); + + return $result; + } + + protected function getInputStream($input) + { + $stream = fopen('php://memory', 'r+', false); + fputs($stream, $input); + rewind($stream); + + return $stream; + } +} |