Skip to content

Commit

Permalink
Configurable Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Sep 9, 2014
1 parent 73d7854 commit 79ec821
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/ConfigurableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace Packaged\Config;

use Packaged\Config\Provider\ConfigSection;

trait ConfigurableTrait
{
/**
* @var ConfigSectionInterface
*/
protected $_configuration;

/**
* Configure the data connection
*
* @param ConfigSectionInterface $configuration
*
* @return static
*/
public function configure(ConfigSectionInterface $configuration)
{
$this->_configuration = $configuration;
}

/**
* Retrieve the configuration
*
* @return ConfigSectionInterface
*/
protected function _config()
{
if($this->_configuration === null)
{
$this->_configuration = new ConfigSection();
}
return $this->_configuration;
}
}
33 changes: 33 additions & 0 deletions tests/ConfigurableTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
use Packaged\Config\Provider\ConfigSection;
use Packaged\Config\ConfigurableTrait;

class ConfigurableTraitTest extends \PHPUnit_Framework_TestCase
{
public function testTrait()
{
$config = new ConfigSection('abstract', ['name' => 'test']);
$mock = new MockConfigurableTrait();
$mock->configure($config);
$this->assertSame($config, $mock->config());
}

public function testDefaultConfig()
{
$mock = new MockConfigurableTrait();
$this->assertEmpty($mock->config()->getItems());
}
}

class MockConfigurableTrait
{
use ConfigurableTrait;

/**
* @return \Packaged\Config\ConfigSectionInterface
*/
public function config()
{
return $this->_config();
}
}

0 comments on commit 79ec821

Please sign in to comment.