Permalink
Browse files

Merge pull request #859 from agop/master

Attribute validations added to assert methods
  • Loading branch information...
2 parents ab6a868 + 5684d75 commit 8a2f0c60e735015efdb2588d87b13343f4d20044 @whatthejeff whatthejeff committed Mar 14, 2013
Showing with 101 additions and 0 deletions.
  1. +36 −0 PHPUnit/Framework/Assert.php
  2. +65 −0 Tests/Framework/AssertTest.php
@@ -935,6 +935,10 @@ public static function assertClassHasAttribute($attributeName, $className, $mess
if (!is_string($attributeName)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
}
+
+ if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
+ throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
+ }
if (!is_string($className) || !class_exists($className, FALSE)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
@@ -960,6 +964,10 @@ public static function assertClassNotHasAttribute($attributeName, $className, $m
if (!is_string($attributeName)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
}
+
+ if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
+ throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
+ }
if (!is_string($className) || !class_exists($className, FALSE)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
@@ -985,6 +993,10 @@ public static function assertClassHasStaticAttribute($attributeName, $className,
if (!is_string($attributeName)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
}
+
+ if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
+ throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
+ }
if (!is_string($className) || !class_exists($className, FALSE)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
@@ -1010,6 +1022,10 @@ public static function assertClassNotHasStaticAttribute($attributeName, $classNa
if (!is_string($attributeName)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
}
+
+ if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
+ throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
+ }
if (!is_string($className) || !class_exists($className, FALSE)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name');
@@ -1037,6 +1053,10 @@ public static function assertObjectHasAttribute($attributeName, $object, $messag
if (!is_string($attributeName)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
}
+
+ if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
+ throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
+ }
if (!is_object($object)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'object');
@@ -1062,6 +1082,10 @@ public static function assertObjectNotHasAttribute($attributeName, $object, $mes
if (!is_string($attributeName)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
}
+
+ if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
+ throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
+ }
if (!is_object($object)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'object');
@@ -2758,6 +2782,10 @@ public static function readAttribute($classOrObject, $attributeName)
if (!is_string($attributeName)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
}
+
+ if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
+ throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'valid attribute name');
+ }
if (is_string($classOrObject)) {
if (!class_exists($classOrObject)) {
@@ -2854,6 +2882,10 @@ protected static function getStaticAttribute($className, $attributeName)
if (!is_string($attributeName)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
}
+
+ if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
+ throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'valid attribute name');
+ }
$class = new ReflectionClass($className);
@@ -2895,6 +2927,10 @@ protected static function getObjectAttribute($object, $attributeName)
if (!is_string($attributeName)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
}
+
+ if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
+ throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'valid attribute name');
+ }
try {
$attribute = new ReflectionProperty($object, $attributeName);
@@ -1709,6 +1709,17 @@ public function testReadAttribute5()
{
$this->readAttribute(NULL, 'foo');
}
+
+ /**
+ * @covers PHPUnit_Framework_Assert::readAttribute
+ * @covers PHPUnit_Framework_Assert::getStaticAttribute
+ * @covers PHPUnit_Framework_Assert::getObjectAttribute
+ * @expectedException PHPUnit_Framework_Exception
+ */
+ public function testReadAttributeIfAttributeNameIsNotValid()
+ {
+ $this->readAttribute('StdClass', '2');
+ }
/**
* @covers PHPUnit_Framework_Assert::assertAttributeContains
@@ -2155,6 +2166,15 @@ public function testAssertClassHasAttributeThrowsException2()
{
$this->assertClassHasAttribute('foo', NULL);
}
+
+ /**
+ * @covers PHPUnit_Framework_Assert::assertClassHasAttribute
+ * @expectedException PHPUnit_Framework_Exception
+ */
+ public function testAssertClassHasAttributeThrowsExceptionIfAttributeNameIsNotValid()
+ {
+ $this->assertClassHasAttribute('1', 'ClassWithNonPublicAttributes');
+ }
/**
* @covers PHPUnit_Framework_Assert::assertClassNotHasAttribute
@@ -2173,6 +2193,15 @@ public function testAssertClassNotHasAttributeThrowsException2()
{
$this->assertClassNotHasAttribute('foo', NULL);
}
+
+ /**
+ * @covers PHPUnit_Framework_Assert::assertClassNotHasAttribute
+ * @expectedException PHPUnit_Framework_Exception
+ */
+ public function testAssertClassNotHasAttributeThrowsExceptionIfAttributeNameIsNotValid()
+ {
+ $this->assertClassNotHasAttribute('1', 'ClassWithNonPublicAttributes');
+ }
/**
* @covers PHPUnit_Framework_Assert::assertClassHasStaticAttribute
@@ -2191,6 +2220,15 @@ public function testAssertClassHasStaticAttributeThrowsException2()
{
$this->assertClassHasStaticAttribute('foo', NULL);
}
+
+ /**
+ * @covers PHPUnit_Framework_Assert::assertClassHasStaticAttribute
+ * @expectedException PHPUnit_Framework_Exception
+ */
+ public function testAssertClassHasStaticAttributeThrowsExceptionIfAttributeNameIsNotValid()
+ {
+ $this->assertClassHasStaticAttribute('1', 'ClassWithNonPublicAttributes');
+ }
/**
* @covers PHPUnit_Framework_Assert::assertClassNotHasStaticAttribute
@@ -2209,6 +2247,15 @@ public function testAssertClassNotHasStaticAttributeThrowsException2()
{
$this->assertClassNotHasStaticAttribute('foo', NULL);
}
+
+ /**
+ * @covers PHPUnit_Framework_Assert::assertClassNotHasStaticAttribute
+ * @expectedException PHPUnit_Framework_Exception
+ */
+ public function testAssertClassNotHasStaticAttributeThrowsExceptionIfAttributeNameIsNotValid()
+ {
+ $this->assertClassNotHasStaticAttribute('1', 'ClassWithNonPublicAttributes');
+ }
/**
* @covers PHPUnit_Framework_Assert::assertObjectHasAttribute
@@ -2227,6 +2274,15 @@ public function testAssertObjectHasAttributeThrowsException2()
{
$this->assertObjectHasAttribute('foo', NULL);
}
+
+ /**
+ * @covers PHPUnit_Framework_Assert::assertObjectHasAttribute
+ * @expectedException PHPUnit_Framework_Exception
+ */
+ public function testAssertObjectHasAttributeThrowsExceptionIfAttributeNameIsNotValid()
+ {
+ $this->assertObjectHasAttribute('1', 'ClassWithNonPublicAttributes');
+ }
/**
* @covers PHPUnit_Framework_Assert::assertObjectNotHasAttribute
@@ -2245,6 +2301,15 @@ public function testAssertObjectNotHasAttributeThrowsException2()
{
$this->assertObjectNotHasAttribute('foo', NULL);
}
+
+ /**
+ * @covers PHPUnit_Framework_Assert::assertObjectNotHasAttribute
+ * @expectedException PHPUnit_Framework_Exception
+ */
+ public function testAssertObjectNotHasAttributeThrowsExceptionIfAttributeNameIsNotValid()
+ {
+ $this->assertObjectNotHasAttribute('1', 'ClassWithNonPublicAttributes');
+ }
/**
* @covers PHPUnit_Framework_Assert::assertClassHasAttribute

0 comments on commit 8a2f0c6

Please sign in to comment.