Skip to content

Commit

Permalink
Fix assertObjectHasAttribute by making a distinction between class at…
Browse files Browse the repository at this point in the history
…tributes and object attributes, as their rules for naming are different.
  • Loading branch information
markrogoyski authored and sebastianbergmann committed Mar 6, 2019
1 parent bdfd54b commit c11f1e6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
25 changes: 15 additions & 10 deletions src/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ public static function assertNan($actual, string $message = ''): void
*/
public static function assertClassHasAttribute(string $attributeName, string $className, string $message = ''): void
{
if (!self::isValidAttributeName($attributeName)) {
if (!self::isValidClassAttributeName($attributeName)) {
throw InvalidArgumentHelper::factory(1, 'valid attribute name');
}

Expand All @@ -1271,7 +1271,7 @@ public static function assertClassHasAttribute(string $attributeName, string $cl
*/
public static function assertClassNotHasAttribute(string $attributeName, string $className, string $message = ''): void
{
if (!self::isValidAttributeName($attributeName)) {
if (!self::isValidClassAttributeName($attributeName)) {
throw InvalidArgumentHelper::factory(1, 'valid attribute name');
}

Expand All @@ -1296,7 +1296,7 @@ public static function assertClassNotHasAttribute(string $attributeName, string
*/
public static function assertClassHasStaticAttribute(string $attributeName, string $className, string $message = ''): void
{
if (!self::isValidAttributeName($attributeName)) {
if (!self::isValidClassAttributeName($attributeName)) {
throw InvalidArgumentHelper::factory(1, 'valid attribute name');
}

Expand All @@ -1319,7 +1319,7 @@ public static function assertClassHasStaticAttribute(string $attributeName, stri
*/
public static function assertClassNotHasStaticAttribute(string $attributeName, string $className, string $message = ''): void
{
if (!self::isValidAttributeName($attributeName)) {
if (!self::isValidClassAttributeName($attributeName)) {
throw InvalidArgumentHelper::factory(1, 'valid attribute name');
}

Expand All @@ -1346,7 +1346,7 @@ public static function assertClassNotHasStaticAttribute(string $attributeName, s
*/
public static function assertObjectHasAttribute(string $attributeName, $object, string $message = ''): void
{
if (!self::isValidAttributeName($attributeName)) {
if (!self::isValidObjectAttributeName($attributeName)) {
throw InvalidArgumentHelper::factory(1, 'valid attribute name');
}

Expand All @@ -1371,7 +1371,7 @@ public static function assertObjectHasAttribute(string $attributeName, $object,
*/
public static function assertObjectNotHasAttribute(string $attributeName, $object, string $message = ''): void
{
if (!self::isValidAttributeName($attributeName)) {
if (!self::isValidObjectAttributeName($attributeName)) {
throw InvalidArgumentHelper::factory(1, 'valid attribute name');
}

Expand Down Expand Up @@ -2717,7 +2717,7 @@ public static function fail(string $message = ''): void
*/
public static function readAttribute($classOrObject, string $attributeName)
{
if (!self::isValidAttributeName($attributeName)) {
if (!self::isValidClassAttributeName($attributeName)) {
throw InvalidArgumentHelper::factory(2, 'valid attribute name');
}

Expand Down Expand Up @@ -2763,7 +2763,7 @@ public static function getStaticAttribute(string $className, string $attributeNa
throw InvalidArgumentHelper::factory(1, 'class name');
}

if (!self::isValidAttributeName($attributeName)) {
if (!self::isValidClassAttributeName($attributeName)) {
throw InvalidArgumentHelper::factory(2, 'valid attribute name');
}

Expand Down Expand Up @@ -2803,7 +2803,7 @@ public static function getObjectAttribute($object, string $attributeName)
throw InvalidArgumentHelper::factory(1, 'object');
}

if (!self::isValidAttributeName($attributeName)) {
if (!self::isValidClassAttributeName($attributeName)) {
throw InvalidArgumentHelper::factory(2, 'valid attribute name');
}

Expand Down Expand Up @@ -2873,7 +2873,12 @@ public static function resetCount(): void
self::$count = 0;
}

private static function isValidAttributeName(string $attributeName): bool
private static function isValidObjectAttributeName(string $attributeName): bool
{
return \preg_match('/[^\x00-\x1f\x7f-\x9f]+/', $attributeName);
}

private static function isValidClassAttributeName(string $attributeName): bool
{
return \preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName);
}
Expand Down
56 changes: 39 additions & 17 deletions tests/unit/Framework/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -968,28 +968,26 @@ public function testAssertObjectHasAttribute(): void

public function testAssertObjectHasAttributeNumericAttribute(): void
{
$object = (object) [
'2016' => [
'hostCity' => 'Rio',
'games' => 'summer',
],
'2018' => [
'hostCity' => 'Pyeongchang',
'games' => 'winter',
],
'2020' => [
'hostCity' => 'Tokyo',
'games' => 'summer',
],
];
$object = new \stdClass();
$object->{'2020'} = 'Tokyo';

$this->assertObjectHasAttribute('2016', $object);
$this->assertObjectHasAttribute('2018', $object);
$this->assertObjectHasAttribute('2020', $object);

$this->expectException(AssertionFailedError::class);

$this->assertObjectHasAttribute('foo', $object);
$this->assertObjectHasAttribute('2018', $object);
}

public function testAssertObjectHasAttributeMultiByteAttribute(): void
{
$object = new \stdClass();
$object->{'東京'} = 2020;

$this->assertObjectHasAttribute('東京', $object);

$this->expectException(AssertionFailedError::class);

$this->assertObjectHasAttribute('長野', $object);
}

public function testAssertObjectNotHasAttribute(): void
Expand All @@ -1003,6 +1001,30 @@ public function testAssertObjectNotHasAttribute(): void
$this->assertObjectNotHasAttribute('name', $o);
}

public function testAssertObjectNotHasAttributeNumericAttribute(): void
{
$object = new \stdClass();
$object->{'2020'} = 'Tokyo';

$this->assertObjectNotHasAttribute('2018', $object);

$this->expectException(AssertionFailedError::class);

$this->assertObjectNotHasAttribute('2020', $object);
}

public function testAssertObjectNotHasAttributeMultiByteAttribute(): void
{
$object = new \stdClass();
$object->{'東京'} = 2020;

$this->assertObjectNotHasAttribute('長野', $object);

$this->expectException(AssertionFailedError::class);

$this->assertObjectNotHasAttribute('東京', $object);
}

public function testAssertFinite(): void
{
$this->assertFinite(1);
Expand Down

0 comments on commit c11f1e6

Please sign in to comment.