* @license PHP License * @package WB * @subpackage unittest */ // Parameter class is required WBClass::load( 'WBParam' ); /** * Unit Test * * Parameter observer mock up * * @version 0.2.0 * @package WB * @subpackage unittest */ class myParamObs implements WBParam_Observer { protected $_counts = array(); /** * receive unique id of this object * * @return string onject id */ public function getObjectId() { return spl_object_hash( $this ); } /** * Interface on set for parameter * * @param string $name * @param mixed $value * @return bool true on success */ public function notifyOnSet( $name, $value ) { if( !isset( $this->_counts[$name] ) ) { $this->_counts[$name] = 0; } ++$this->_counts[$name]; return true; } /** * receive notify counts for each parameter * * @param string $name * @return int $count */ public function getCount( $name ) { if( !isset( $this->_counts[$name] ) ) { $this->_counts[$name] = 0; } return $this->_counts[$name]; } } /** * Unit Test * * Parameter observer mock up * * @version 0.1.1 * @package WB * @subpackage unittest */ class TestCaseBaseParam extends UnitTestCase { /** * simply set parameter * */ public function testParam() { $this->assertTrue( WBParam::set( 'somthing', 'somewhat' ) ); $this->assertTrue( WBParam::set( 'path/to/something', 'with path' ) ); $this->assertEqual( WBParam::get( 'somthing' ), 'somewhat' ); $this->assertEqual( WBParam::get( 'path/to/something' ), 'with path' ); $this->assertEqual( WBParam::get( 'non/existing/path/that/goes/to/nothing', 'default' ), 'default' ); // get the same result on the second run $this->assertEqual( WBParam::get( 'somthing' ), 'somewhat' ); $this->assertEqual( WBParam::get( 'path/to/something' ), 'with path' ); $this->assertEqual( WBParam::get( 'non/existing/path/that/goes/to/nothing', 'default2' ), 'default2' ); } /** * Verify that genesis happend in the past * */ public function testGenesis() { $mt = microtime( true ); $this->assertTrue( WBParam::get( 'wb/time/genesis' ) > 0 ); $this->assertTrue( ( $mt > WBParam::get( 'wb/time/genesis' ) ) ); } /** * Attach parameter observer * */ public function testAttachObserver() { $obsFoo = new myParamObs(); WBParam::attach( 'foo', $obsFoo ); $this->assertTrue( WBParam::set( 'foo', 'bar' ) ); $this->assertEqual( $obsFoo->getCount( 'foo' ), 1 ); $this->assertEqual( $obsFoo->getCount( 'bar' ), 0 ); $this->assertTrue( WBParam::set( 'foo', 'bar2' ) ); $this->assertEqual( $obsFoo->getCount( 'foo' ), 2 ); $this->assertEqual( $obsFoo->getCount( 'bar' ), 0 ); $this->assertTrue( WBParam::set( 'foo', 'bar3' ) ); $this->assertEqual( $obsFoo->getCount( 'foo' ), 3 ); $this->assertEqual( $obsFoo->getCount( 'bar' ), 0 ); $obsBar = new myParamObs(); WBParam::attach( 'bar/foo', $obsBar ); $this->assertTrue( WBParam::set( 'bar/foo', 'bar' ) ); $this->assertEqual( $obsBar->getCount( 'bar/foo' ), 1 ); $this->assertEqual( $obsBar->getCount( 'bar/oof' ), 0 ); $this->assertEqual( $obsFoo->getCount( 'foo' ), 3 ); $this->assertEqual( $obsFoo->getCount( 'bar' ), 0 ); $this->assertTrue( WBParam::set( 'bar/foo', 'bar2' ) ); $this->assertEqual( $obsBar->getCount( 'bar/foo' ), 2 ); $this->assertEqual( $obsBar->getCount( 'bar/oof' ), 0 ); $this->assertEqual( $obsFoo->getCount( 'foo' ), 3 ); $this->assertEqual( $obsFoo->getCount( 'bar' ), 0 ); $this->assertTrue( WBParam::set( 'bar/foo', 'bar3' ) ); $this->assertEqual( $obsBar->getCount( 'bar/foo' ), 3 ); $this->assertEqual( $obsBar->getCount( 'bar/oof' ), 0 ); $this->assertEqual( $obsFoo->getCount( 'foo' ), 3 ); $this->assertEqual( $obsFoo->getCount( 'bar' ), 0 ); WBParam::detach( 'foo', $obsFoo ); WBParam::detach( 'bar/foo', $obsBar ); } /** * Attach $ detach parameter observer * */ public function testDetachObserver() { $obs = new myParamObs(); WBParam::attach( 'foo', $obs ); WBParam::attach( 'bar', $obs ); $this->assertTrue( WBParam::set( 'foo', 'bar' ) ); $this->assertTrue( WBParam::set( 'bar', 'foo' ) ); $this->assertEqual( $obs->getCount( 'foo' ), 1 ); $this->assertEqual( $obs->getCount( 'bar' ), 1 ); WBParam::detach( 'foo', $obs ); $this->assertTrue( WBParam::set( 'foo', 'bar2' ) ); $this->assertTrue( WBParam::set( 'bar', 'foo2' ) ); $this->assertEqual( $obs->getCount( 'foo' ), 1 ); $this->assertEqual( $obs->getCount( 'bar' ), 2 ); $this->assertTrue( WBParam::set( 'foo', 'bar3' ) ); $this->assertTrue( WBParam::set( 'bar', 'foo3' ) ); $this->assertEqual( $obs->getCount( 'foo' ), 1 ); $this->assertEqual( $obs->getCount( 'bar' ), 3 ); WBParam::attach( 'foo', $obs ); $this->assertTrue( WBParam::set( 'foo', 'bar4' ) ); $this->assertTrue( WBParam::set( 'bar', 'foo4' ) ); $this->assertEqual( $obs->getCount( 'foo' ), 2 ); $this->assertEqual( $obs->getCount( 'bar' ), 4 ); $this->assertTrue( WBParam::set( 'foo', 'bar5' ) ); $this->assertTrue( WBParam::set( 'bar', 'foo5' ) ); $this->assertEqual( $obs->getCount( 'foo' ), 3 ); $this->assertEqual( $obs->getCount( 'bar' ), 5 ); } /** * case sensitivity * */ public function testCaseSensitivity() { $path = 'CaseInSensitive/something'; WBParam::set($path, 'CaseInSensitive'); $this->assertEqual('CaseInSensitive', WBParam::get($path)); $this->assertEqual('CaseInSensitive', WBParam::get(strtolower($path))); $this->assertEqual('CaseInSensitive', WBParam::get(strtoupper($path))); $path = 'Case/In/Sensitive/stuff'; WBParam::set($path, 'CaseInSensitive'); $this->assertEqual('CaseInSensitive', WBParam::get($path)); $this->assertEqual('CaseInSensitive', WBParam::get(strtolower($path))); $this->assertEqual('CaseInSensitive', WBParam::get(strtoupper($path))); $path = 'CaseInSensitive/StuffThatMatters/WellWhoCares'; WBParam::set($path, 'CaseInSensitive'); $this->assertEqual('CaseInSensitive', WBParam::get($path)); $this->assertEqual('CaseInSensitive', WBParam::get(strtolower($path))); $this->assertEqual('CaseInSensitive', WBParam::get(strtoupper($path))); } } ?>