Skip to content

Commit

Permalink
Adding the ability to define simple Option defaults.
Browse files Browse the repository at this point in the history
The defaults (and any values overriding them) are no longer validated
within the Option object. In fact, to use this functionality you simply
declare the value for your default, it does not get wrapped within an
Option object.
  • Loading branch information
trq committed Jul 26, 2012
1 parent 41ae3f7 commit 7bca3e0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/Proem/Api/Util/Opt/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function setOptions($defaults, $options)
if (isset($defaults[$key]) && ($defaults[$key] instanceof Option)) {
$defaults[$key]->setValue($value);
} else {
$defaults[$key] = new Option($value);
$defaults[$key] = $value;
}
}

Expand All @@ -69,6 +69,8 @@ public function setOptions($defaults, $options)
} catch (\RuntimeException $e) {
throw new \RuntimeException($e->getMessage());
}
} else {
$payload->set($key, $value);
}
}
return $payload;
Expand Down
25 changes: 25 additions & 0 deletions tests/lib/Proem/Tests/Util/OptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

use Proem\Tests\Util\Options\Fixtures\OptionsFixture,
Proem\Tests\Util\Options\Fixtures\OptionsFixture2,
Proem\Tests\Util\Options\Fixtures\OptionsFixture3,
Proem\Proem,
Proem\Service\Asset\Standard as GenericAsset,
Proem\Service\Manager\Standard as ServiceManager;
Expand Down Expand Up @@ -214,4 +215,28 @@ public function testCustomValidatorFail()
'custom-arg' => 100
]);
}

/**
* The fowllowing tests test none Option values.
*
* This is, functions which don't use Option objects to define there defaults.
* This in turn allows validation to be skipped all together.
*/

public function testDefaultArgs()
{
$fixture = new OptionsFixture3;
$this->assertEquals($fixture->getFoo(), 'this is foo');
$this->assertEquals($fixture->getBar(), 'this is bar');
}

public function testCanOverrideDefaultArgs()
{
$fixture = new OptionsFixture3([
'foo' => 'foo',
'bar' => 'bar'
]);
$this->assertEquals($fixture->getFoo(), 'foo');
$this->assertEquals($fixture->getBar(), 'bar');
}
}
54 changes: 54 additions & 0 deletions tests/lib/Proem/Tests/Util/Options/Fixtures/OptionsFixture3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* The MIT License
*
* Copyright (c) 2010 - 2012 Tony R Quilkey <trq@proemframework.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace Proem\Tests\Util\Options\Fixtures;

use Proem\Util\Opt\Options,
Proem\Util\Opt\Option;

class OptionsFixture3
{
use Options;

public function __construct(array $options = [])
{
$this->options = $this->setOptions([
'foo' => 'this is foo',
'bar' => 'this is bar'
], $options);
}

public function getFoo()
{
return $this->options->foo;
}

public function getBar()
{
return $this->options->bar;
}

}

0 comments on commit 7bca3e0

Please sign in to comment.