cvs: pear /PHPUnit2 package.xml /PHPUnit2/Framework Assert.php /PHPUnit2/Tests ClassWithNonPublicProperties.php /PHPUnit2/Tests/Framework
AssertTest.php
| From: | Sebastian Bergmann | Date: | Sun, 16 Oct 2005 11:15:14 +0000 |
| Subject: | cvs: pear /PHPUnit2 package.xml /PHPUnit2/Framework Assert.php /PHPUnit2/Tests ClassWithNonPublicProperties.php /PHPUnit2/Tests/Framework AssertTest.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-35590@lists.php.net to get a copy of this message | ||
sebastian Sun Oct 16 07:15:14 2005 EDT
Added files:
/pear/PHPUnit2/Tests ClassWithNonPublicProperties.php
Modified files:
/pear/PHPUnit2/Framework Assert.php
/pear/PHPUnit2 package.xml
/pear/PHPUnit2/Tests/Framework AssertTest.php
Log:
For Derick: Add PHPUnit2_Framework_Assert::getNonPublicProperty() method to read non-public
properties from objects.
http://cvs.php.net/diff.php/pear/PHPUnit2/Framework/Assert.php?r1=1.48&r2=1.49&ty=u
Index: pear/PHPUnit2/Framework/Assert.php
diff -u pear/PHPUnit2/Framework/Assert.php:1.48 pear/PHPUnit2/Framework/Assert.php:1.49
--- pear/PHPUnit2/Framework/Assert.php:1.48 Sat Oct 15 09:39:54 2005
+++ pear/PHPUnit2/Framework/Assert.php Sun Oct 16 07:15:11 2005
@@ -15,7 +15,7 @@
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.php.net/license/3_0.txt
PHP License 3.0
- * @version CVS: $Id: Assert.php,v 1.48 2005/10/15 13:39:54 sebastian Exp $
+ * @version CVS: $Id: Assert.php,v 1.49 2005/10/16 11:15:11 sebastian Exp $
* @link http://pear.php.net/package/PHPUnit2
* @since File available since Release 2.0.0
*/
@@ -555,6 +555,53 @@
}
/**
+ * Returns the value of an object's property that is declared
+ * protected or private.
+ *
+ * @param mixed $object
+ * @param string $propertyName
+ * @return mixed
+ * @throws InvalidArgumentException
+ * @access public
+ * @static
+ * @since Method available since Release 2.4.0
+ */
+ public static function getNonPublicProperty($object, $propertyName) {
+ try {
+ $class = new ReflectionClass($object);
+
+ if ($class->hasProperty($propertyName)) {
+ $property = $class->getProperty($propertyName);
+
+ if (!$property->isPublic()) {
+ if ($property->isProtected()) {
+ $propertyName = "\0*\0" . $propertyName;
+ } else {
+ $propertyName = sprintf(
+ "\0%s\0%s",
+
+ get_class($object),
+ $propertyName
+ );
+ }
+
+ $tmp = (array) $object;
+
+ return $tmp[$propertyName];
+ } else {
+ return $object->$propertyName;
+ }
+ } else {
+ throw new InvalidArgumentException;
+ }
+ }
+
+ catch (ReflectionException $e) {
+ throw new InvalidArgumentException;
+ }
+ }
+
+ /**
* Converts a value to a string.
*
* @param mixed $value
http://cvs.php.net/diff.php/pear/PHPUnit2/package.xml?r1=1.123&r2=1.124&ty=u
Index: pear/PHPUnit2/package.xml
diff -u pear/PHPUnit2/package.xml:1.123 pear/PHPUnit2/package.xml:1.124
--- pear/PHPUnit2/package.xml:1.123 Wed Oct 12 13:07:56 2005
+++ pear/PHPUnit2/package.xml Sun Oct 16 07:15:13 2005
@@ -20,6 +20,8 @@
<![CDATA[
+ Added support for Mock Objects.
++ Added PHPUnit2_Framework_Assert::getNonPublicProperty() method to read non-public properties from
objects.
+
* PHPUnit2 now uses the Standard PHP Library (SPL)'s specialized exceptions
InvalidArgumentException and RuntimeException instead of the generic Exception exception class.
! PHP 5.1.0 (or greater) is now required.
@@ -230,6 +232,9 @@
<file name="AClass.php">
<replace type="package-info" from="@package_version@"
to="version"/>
</file>
+ <file name="ClassWithNonPublicProperties.php">
+ <replace type="package-info" from="@package_version@"
to="version"/>
+ </file>
<file name="DoubleTestCase.php">
<replace type="package-info" from="@package_version@"
to="version"/>
</file>
http://cvs.php.net/diff.php/pear/PHPUnit2/Tests/Framework/AssertTest.php?r1=1.18&r2=1.19&ty=u
Index: pear/PHPUnit2/Tests/Framework/AssertTest.php
diff -u pear/PHPUnit2/Tests/Framework/AssertTest.php:1.18
pear/PHPUnit2/Tests/Framework/AssertTest.php:1.19
--- pear/PHPUnit2/Tests/Framework/AssertTest.php:1.18 Sat Apr 23 04:47:17 2005
+++ pear/PHPUnit2/Tests/Framework/AssertTest.php Sun Oct 16 07:15:13 2005
@@ -15,13 +15,14 @@
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.php.net/license/3_0.txt
PHP License 3.0
- * @version CVS: $Id: AssertTest.php,v 1.18 2005/04/23 08:47:17 sebastian Exp $
+ * @version CVS: $Id: AssertTest.php,v 1.19 2005/10/16 11:15:13 sebastian Exp $
* @link http://pear.php.net/package/PHPUnit2
* @since File available since Release 2.0.0
*/
require_once 'PHPUnit2/Framework/TestCase.php';
+require_once 'ClassWithNonPublicProperties.php';
require_once 'TestIterator.php';
/**
@@ -536,6 +537,13 @@
$this->fail();
}
+
+ public function testGetNonPublicProperty() {
+ $obj = new ClassWithNonPublicProperties;
+
+ $this->assertEquals('foo', $this->getNonPublicProperty($obj,
'protectedProperty'));
+ $this->assertEquals('bar', $this->getNonPublicProperty($obj,
'privateProperty'));
+ }
}
/*
http://cvs.php.net/co.php/pear/PHPUnit2/Tests/ClassWithNonPublicProperties.php?r=1.1&p=1
Index: pear/PHPUnit2/Tests/ClassWithNonPublicProperties.php
+++ pear/PHPUnit2/Tests/ClassWithNonPublicProperties.php
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* PHP Version 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did
not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category Testing
* @package PHPUnit2
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.php.net/license/3_0.txt
PHP License 3.0
* @version CVS: $Id: ClassWithNonPublicProperties.php,v 1.1 2005/10/16 11:15:13 sebastian Exp $
* @link http://pear.php.net/package/PHPUnit2
* @since File available since Release 2.0.0
*/
/**
*
*
* @category Testing
* @package PHPUnit2
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @copyright 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.php.net/license/3_0.txt
PHP License 3.0
* @version Release: @package_version@
* @link http://pear.php.net/package/PHPUnit2
* @since Class available since Release 2.4.0
*/
class ClassWithNonPublicProperties {
protected $protectedProperty = 'foo';
private $privateProperty = 'bar';
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>