<?php
/**
* began : July 15, 2006
* copyright : (c) 2006 - 2007, Russ Collier
* support : hide@address.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version
* 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Or visit http://www.gnu.org/licenses/lgpl.html
*/
require_once 'PHPUnit/Framework.php';
require_once dirname( realpath( __FILE__ ) ) . '/../PeaUtils.php';
/**
* The PHPeas_PeaUtils unit test suite.
*
* @package PHPeas
* @author russ
*/
class PHPeas_Tests_PeaUtilsTests extends PHPUnit_Framework_TestCase
{
const TEST_ENABLED = true;
const TEST_ID = 1;
const TEST_NAME = 'FooBar';
/**
* @test
*/
public function testClonePea()
{
$srcPea = new StubPea( self::TEST_ID, self::TEST_NAME, self::TEST_ENABLED );
try
{
// Invalid source pea
$tgtPea = PHPeas_PeaUtils::clonePea( null );
$this->assertTrue( false );
}
catch ( ReflectionException $re )
{
$this->assertTrue( true );
}
$tgtPea = PHPeas_PeaUtils::clonePea( $srcPea );
// No setter for the Id property (read-only property)
$this->assertNotEquals( $srcPea->getId(), $tgtPea->getId() );
$this->assertEquals( $srcPea->getName(), $tgtPea->getName() );
$this->assertEquals( $srcPea->isEnabled(), $tgtPea->isEnabled() );
}
/**
* @test
*/
public function testCopyProperties()
{
$srcPea = new StubPea();
$tgtPea = new StubPea();
try
{
// Invalid source pea
PHPeas_PeaUtils::copyProperties( null, $tgtPea );
$this->assertTrue( false );
}
catch ( ReflectionException $re )
{
$this->assertTrue( true );
}
try
{
// Invalid target pea
PHPeas_PeaUtils::copyProperties( $srcPea, null );
$this->assertTrue( false );
}
catch ( ReflectionException $re )
{
$this->assertTrue( true );
}
$srcPea->setEnabled( true );
$srcPea->setName( 'source' );
PHPeas_PeaUtils::copyProperties( $srcPea, $tgtPea );
$this->assertEquals( $srcPea->isEnabled(), $tgtPea->isEnabled() );
$this->assertEquals( $srcPea->getName(), $tgtPea->getName() );
}
/**
* @test
*/
public function testDescribe()
{
$peaDesc = array();
try
{
// Invalid pea
$peaDesc = PHPeas_PeaUtils::describe( array() );
$this->assertTrue( false );
}
catch ( ReflectionException $re )
{
$this->assertTrue( true );
}
$pea = new StubPea( self::TEST_ID, self::TEST_NAME, self::TEST_ENABLED );
$peaDesc = PHPeas_PeaUtils::describe( $pea );
$this->assertEquals( 5, count( array_keys( $peaDesc ) ) );
$this->assertEquals( 'class ' . get_class( $pea ), $peaDesc[ PHPeas_PeaUtils::DESCRIBE_CLASS_KEY ] );
$this->assertEquals( self::TEST_ENABLED, $peaDesc[ 'enabled' ] );
$this->assertEquals( self::TEST_NAME, $peaDesc[ 'name' ] );
$this->assertEquals( self::TEST_ID, $peaDesc[ 'id' ] );
$rightNow = time();
$this->assertEquals( date( 'Y', $rightNow ), date( 'Y', $peaDesc[ 'currentTime' ] ) );
$this->assertEquals( date( 'm', $rightNow ), date( 'm', $peaDesc[ 'currentTime' ] ) );
$this->assertEquals( date( 'd', $rightNow ), date( 'd', $peaDesc[ 'currentTime' ] ) );
$this->assertEquals( date( 'H', $rightNow ), date( 'H', $peaDesc[ 'currentTime' ] ) );
$this->assertEquals( date( 'i', $rightNow ), date( 'i', $peaDesc[ 'currentTime' ] ) );
$this->assertEquals( date( 's', $rightNow ), date( 's', $peaDesc[ 'currentTime' ] ) ); // Too precise?
}
/**
* @test
*/
public function testGetReadMethod()
{
$pea = new StubPea();
$this->assertEquals( 'isEnabled', PHPeas_PeaUtils::getReadMethod( $pea, 'enabled' ) );
$this->assertEquals( 'getCurrentTime', PHPeas_PeaUtils::getReadMethod( $pea, 'currentTime' ) );
$this->assertEquals( 'getId', PHPeas_PeaUtils::getReadMethod( $pea, 'id' ) );
$this->assertEquals( 'getName', PHPeas_PeaUtils::getReadMethod( $pea, 'name' ) );
$this->assertEquals( null, PHPeas_PeaUtils::getReadMethod( $pea, 'updateTimestamp' ) );
$this->assertEquals( null, PHPeas_PeaUtils::getReadMethod( $pea, 'foobar' ) );
}
/**
* @test
*/
public function testGetWriteMethod()
{
$pea = new StubPea();
$this->assertEquals( 'setEnabled', PHPeas_PeaUtils::getWriteMethod( $pea, 'enabled' ) );
$this->assertEquals( 'setName', PHPeas_PeaUtils::getWriteMethod( $pea, 'name' ) );
$this->assertEquals( 'setUpdateTimestamp', PHPeas_PeaUtils::getWriteMethod( $pea, 'updateTimestamp' ) );
$this->assertEquals( null, PHPeas_PeaUtils::getWriteMethod( $pea, 'id' ) );
$this->assertEquals( null, PHPeas_PeaUtils::getWriteMethod( $pea, 'currentTime' ) );
$this->assertEquals( null, PHPeas_PeaUtils::getWriteMethod( $pea, 'foobar' ) );
}
/**
* @test
*/
public function testInvokeMethod()
{
try
{
// Invalid pea
$returnValue = PHPeas_PeaUtils::invokeMethod( null, 'isEnabled' );
$this->assertTrue( false );
}
catch ( ReflectionException $re )
{
$this->assertTrue( true );
}
$pea = new StubPea();
try
{
// Null method name
$returnValue = PHPeas_PeaUtils::invokeMethod( $pea, null );
$this->assertTrue( false );
}
catch ( ReflectionException $re )
{
$this->assertTrue( true );
}
try
{
// Method doesn't exist
$returnValue = PHPeas_PeaUtils::invokeMethod( $pea, 'foobar' );
$this->assertTrue( false );
}
catch ( ReflectionException $re )
{
$this->assertTrue( true );
}
$returnValue = PHPeas_PeaUtils::invokeMethod( $pea, 'setName', 'baz' );
$this->assertEquals( 'baz', $pea->getName() );
$this->assertEquals( null, $returnValue );
$returnValue = PHPeas_PeaUtils::invokeMethod( $pea, 'getName' );
$this->assertEquals( 'baz', $returnValue );
}
/**
* @test
*/
public function testInvokeStaticMethod()
{
try
{
// Invalid Pea
$returnValue = PHPeas_PeaUtils::invokeStaticMethod( null, 'guid' );
$this->assertTrue( false );
}
catch ( ReflectionException $re )
{
$this->assertTrue( true );
}
$pea = new StubPea();
try
{
// Null method name
$returnValue = PHPeas_PeaUtils::invokeStaticMethod( $pea, null );
$this->assertTrue( false );
}
catch ( ReflectionException $re )
{
$this->assertTrue( true );
}
try
{
// Method that doesn't exist
$returnValue = PHPeas_PeaUtils::invokeStaticMethod( $pea, 'foobar' );
$this->assertTrue( false );
}
catch ( ReflectionException $re )
{
$this->assertTrue( true );
}
try
{
// Method that's not static
$returnValue = PHPeas_PeaUtils::invokeStaticMethod( $pea, 'isEnabled' );
$this->assertTrue( false );
}
catch ( ReflectionException $re )
{
$this->assertTrue( true );
}
$returnValue = PHPeas_PeaUtils::invokeMethod( $pea, 'guid' );
$this->assertEquals( StubPea::GUID, $returnValue );
}
/**
* @test
*/
public function testPopulate()
{
try
{
// Invalid pea
PHPeas_PeaUtils::populate( null, array() );
$this->assertTrue( false );
}
catch ( ReflectionException $re )
{
$this->assertTrue( true );
}
$pea = new StubPea();
try
{
// Invalid properties set
PHPeas_PeaUtils::populate( $pea, null );
$this->assertTrue( false );
}
catch ( ReflectionException $re )
{
$this->assertTrue( true );
}
try
{
// Non-existant method name
PHPeas_PeaUtils::populate( $pea, 'foo' );
$this->assertTrue( false );
}
catch ( ReflectionException $re )
{
$this->assertTrue( true );
}
$props = array( 'enabled' => true, 'id' => 1, 'name' => 'foobar' );
PHPeas_PeaUtils::populate( $pea, $props );
$this->assertEquals( true, $pea->isEnabled() );
$this->assertEquals( 'foobar', $pea->getName() );
$this->assertEquals( null, $pea->getId() );
}
/**
* @test
*/
public function testIsValidPea()
{
$this->assertFalse( PHPeas_PeaUtils::isValidPea( null ) );
$this->assertFalse( PHPeas_PeaUtils::isValidPea( '' ) );
$this->assertFalse( PHPeas_PeaUtils::isValidPea( 1 ) );
$this->assertFalse( PHPeas_PeaUtils::isValidPea( array() ) );
$this->assertFalse( PHPeas_PeaUtils::isValidPea( array( 0, 1, 2, 3 ) ) );
$this->assertFalse( PHPeas_PeaUtils::isValidPea( array( 'foo' => 'bar' ) ) );
$this->assertTrue( PHPeas_PeaUtils::isValidPea( new StubPea() ) );
$pea1 = new StubPea( 1, 'baz', true );
$this->assertTrue( PHPeas_PeaUtils::isValidPea( $pea1 ) );
$pea2 = new StubClassWithNoConstructor();
$this->assertTrue( PHPeas_PeaUtils::isValidPea( $pea2 ) );
$nonPea1 = new StubClassWithConstructorParams( 1 );
$this->assertFalse( PHPeas_PeaUtils::isValidPea( $nonPea1 ) );
$this->assertFalse( PHPeas_PeaUtils::isValidPea( StubSingleton::getInstance() ) );
}
/**
* @test
*/
public function testPublicMethodExists()
{
$pea = new StubPea();
$this->assertEquals( true, PHPeas_PeaUtils::publicMethodExists( $pea, 'isEnabled' ) );
$this->assertEquals( true, PHPeas_PeaUtils::publicMethodExists( $pea, 'setUpdateTimestamp' ) );
$this->assertEquals( false, PHPeas_PeaUtils::publicMethodExists( $pea, 'protectedMethod' ) );
$this->assertEquals( false, PHPeas_PeaUtils::publicMethodExists( $pea, 'privateMethod' ) );
$this->assertEquals( false, PHPeas_PeaUtils::publicMethodExists( $pea, 'foobar' ) );
}
}
/**
* @ignore
*/
class StubPea
{
const GUID = '12345';
private $enabled;
private $id;
private $name;
private $updateTimestamp;
public function __construct( $idIn = 0, $nameIn = '', $enabledIn = false )
{
if ( 0 < $idIn )
{
$this->id = $idIn;
}
if ( '' != $nameIn )
{
$this->name = $nameIn;
}
$this->enabled = $enabledIn;
$this->updateTimestamp = $this->getCurrentTime();
}
public function getCurrentTime()
{
return time();
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function isEnabled()
{
return ( bool ) $this->enabled;
}
public function setEnabled( $valueIn )
{
$this->enabled = ( bool ) $valueIn;
}
public function setName( $valueIn )
{
$this->name = $valueIn;
}
public function setUpdateTimestamp( $valueIn )
{
$this->updateTimestamp = $valueIn;
}
public static function guid()
{
return self::GUID;
}
protected function protectedMethod()
{
return 1;
}
private function privateMethod()
{
return 1;
}
}
/**
* @ignore
*/
class StubClassWithNoConstructor
{
private $id;
public function getId()
{
return $this->id;
}
}
/**
* @ignore
*/
class StubClassWithConstructorParams
{
private $id;
public function __construct( $idIn )
{
$this->id = $idIn;
}
}
/**
* @ignore
*/
class StubSingleton
{
private static $instance;
private function __construct()
{
}
public static function getInstance()
{
if ( null == self::$instance )
{
self::$instance = new StubSingleton();
}
return self::$instance;
}
}
?>