Skip to content

Commit

Permalink
Merge pull request #859 from agop/master
Browse files Browse the repository at this point in the history
Attribute validations added to assert methods
  • Loading branch information
whatthejeff committed Mar 14, 2013
2 parents ab6a868 + 5684d75 commit 8a2f0c6
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
36 changes: 36 additions & 0 deletions PHPUnit/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
65 changes: 65 additions & 0 deletions Tests/Framework/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 8a2f0c6

Please sign in to comment.