Skip to content

Commit

Permalink
bugfixed in check required parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
michaljantomczak committed Oct 27, 2017
1 parent 5a55fe3 commit f8a0dd0
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ README
======

![license](https://img.shields.io/packagist/l/bafs/via.svg?style=flat-square)
![PHP 5.6+](https://img.shields.io/badge/PHP-5.5+-brightgreen.svg?style=flat-square)
![PHP 5.6+](https://img.shields.io/badge/PHP-5.6+-brightgreen.svg?style=flat-square)

What is Impero?
-----------------
Expand Down
5 changes: 5 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
<testsuite name="TestSuite">
<directory>./test/</directory>
</testsuite>
<filter>
<whitelist>
<directory>./src/Impero</directory>
</whitelist>
</filter>

</phpunit>
1 change: 0 additions & 1 deletion src/Impero/CommandConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
class CommandConfig
{

/**
* @var string
*/
Expand Down
15 changes: 8 additions & 7 deletions src/Impero/InputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ private function parseOption(&$index, $parameters, CommandConfig $config)
$value = $parameters[++$index];
}
$option = $config->getOption($name);
if ($option->isRequired() && null === $value) {
throw new RequiredParameterException('-' . $name);
}

if (null !== $option->getDefault() && null === $value) {
$value = $option->getDefault();
}

if ($option->isRequired() && null === $value) {
throw new RequiredParameterException('-' . $name);
}

$this->values[$name] = $value;
}

Expand All @@ -105,14 +106,14 @@ private function parseOption(&$index, $parameters, CommandConfig $config)
*/
private function parseParameter($value, ConfigParameter $config)
{
if ($config->isRequired() && null === $value) {
throw new RequiredParameterException($config->getName());
}

if (null !== $config->getDefault() && null === $value) {
$value = $config->getDefault();
}

if ($config->isRequired() && null === $value) {
throw new RequiredParameterException($config->getName());
}

$this->values[$config->getName()] = $value;
}
}
1 change: 1 addition & 0 deletions test/Asset/CommandAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function doConfig(CommandConfig $commandConfig)
/**
* @param InputStream $in
* @param OutputStream $out
* @return int|void
*/
public function doExecute(InputStream $in, OutputStream $out)
{
Expand Down
51 changes: 51 additions & 0 deletions test/Impero/CommandConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Impero: Command manager
* Copyright (c) NewClass (http://newclass.pl)
*
* Licensed under The MIT License
* For full copyright and license information, please see the file LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) NewClass (http://newclass.pl)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

namespace Test\Impero;

use Impero\CommandConfig;
use Impero\InputStream;
use PHPUnit\Framework\TestCase;

/**
* Class CommandConfigTest
* @author Michal Tomczak ([email protected])
*/
class CommandConfigTest extends TestCase
{

/**
*
*/
public function testGetOption()
{
$config = new CommandConfig();
$config->addOption('first','def',false);
$option=$config->getOption('first');
$this->assertEquals('first', $option->getName());
$this->assertEquals('def', $option->getDefault());
$this->assertEquals(false, $option->isRequired());
}

/**
*
*/
public function testGetOptionUnknownOptionException()
{
$this->expectExceptionMessage('Unknow option last');
$config = new CommandConfig();
$config->addOption('first','def',false);
$option=$config->getOption('last');
}

}
70 changes: 70 additions & 0 deletions test/Impero/InputStreamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Impero: Command manager
* Copyright (c) NewClass (http://newclass.pl)
*
* Licensed under The MIT License
* For full copyright and license information, please see the file LICENSE
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) NewClass (http://newclass.pl)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

namespace Test\Impero;

use Impero\CommandConfig;
use Impero\InputStream;
use PHPUnit\Framework\TestCase;

/**
* Class InputStreamTest
* @author Michal Tomczak ([email protected])
*/
class InputStreamTest extends TestCase
{

/**
*
*/
public function testParse()
{
$config = new CommandConfig();
$config->addParameter('first')
->addParameter('second_optional', null, false)
->addParameter('last_default', true)
->addOption('opt1')
->addOption('opt2', null, false);

$input = new InputStream([
'test',
'-opt1="wow"'
], $config);

$this->assertEquals('test', $input->getValue('first'));
$this->assertEquals(true, $input->getValue('last_default'));
$this->assertEquals('wow', $input->getValue('opt1'));

}

/**
*
*/
public function testParseRequiredParameterException()
{
$config = new CommandConfig();
$config->addParameter('first')
->addParameter('second_optional', null, false)
->addParameter('last_default', true)
->addOption('opt1')
->addOption('opt2', null, false);

$this->expectExceptionMessage('Required parameter first');
$input = new InputStream([
'-opt1="wow"',
'-opt2',
'unknown'
], $config);

}
}

0 comments on commit f8a0dd0

Please sign in to comment.