Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename objectHasAttribute to objectHasProperty #5223

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 48 additions & 20 deletions src/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
use Generator;
use PHPUnit\Framework\Constraint\ArrayHasKey;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\Constraint\ClassHasAttribute;
use PHPUnit\Framework\Constraint\ClassHasStaticAttribute;
use PHPUnit\Framework\Constraint\ClassHasProperty;
use PHPUnit\Framework\Constraint\ClassHasStaticProperty;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\Count;
use PHPUnit\Framework\Constraint\DirectoryExists;
Expand Down Expand Up @@ -72,7 +72,7 @@
use PHPUnit\Framework\Constraint\LogicalOr;
use PHPUnit\Framework\Constraint\LogicalXor;
use PHPUnit\Framework\Constraint\ObjectEquals;
use PHPUnit\Framework\Constraint\ObjectHasAttribute;
use PHPUnit\Framework\Constraint\ObjectHasProperty;
use PHPUnit\Framework\Constraint\RegularExpression;
use PHPUnit\Framework\Constraint\SameSize;
use PHPUnit\Framework\Constraint\StringContains;
Expand Down Expand Up @@ -1200,7 +1200,7 @@ public static function assertClassHasAttribute(string $attributeName, string $cl
throw InvalidArgumentException::create(2, 'class name');
}

static::assertThat($className, new ClassHasAttribute($attributeName), $message);
static::assertThat($className, new ClassHasProperty($attributeName), $message);
}

/**
Expand All @@ -1227,7 +1227,7 @@ public static function assertClassNotHasAttribute(string $attributeName, string
static::assertThat(
$className,
new LogicalNot(
new ClassHasAttribute($attributeName)
new ClassHasProperty($attributeName)
),
$message
);
Expand Down Expand Up @@ -1256,7 +1256,7 @@ public static function assertClassHasStaticAttribute(string $attributeName, stri

static::assertThat(
$className,
new ClassHasStaticAttribute($attributeName),
new ClassHasStaticProperty($attributeName),
$message
);
}
Expand Down Expand Up @@ -1285,7 +1285,7 @@ public static function assertClassNotHasStaticAttribute(string $attributeName, s
static::assertThat(
$className,
new LogicalNot(
new ClassHasStaticAttribute($attributeName)
new ClassHasStaticProperty($attributeName)
),
$message
);
Expand All @@ -1306,8 +1306,22 @@ public static function assertObjectHasAttribute(string $attributeName, $object,
{
self::createWarning('assertObjectHasAttribute() is deprecated and will be removed in PHPUnit 10.');

if (!self::isValidObjectAttributeName($attributeName)) {
throw InvalidArgumentException::create(1, 'valid attribute name');
self::assertObjectHasProperty($attributeName, $object, $message);
}

/**
* Asserts that an object has a specified attribute.
*
* @param object $object
*
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
* @throws Exception
* @throws ExpectationFailedException
*/
public static function assertObjectHasProperty(string $propertyName, $object, string $message = ''): void
{
if (!self::isValidObjectPropertyName($propertyName)) {
throw InvalidArgumentException::create(1, 'valid property name');
}

if (!is_object($object)) {
Expand All @@ -1316,7 +1330,7 @@ public static function assertObjectHasAttribute(string $attributeName, $object,

static::assertThat(
$object,
new ObjectHasAttribute($attributeName),
new ObjectHasProperty($propertyName),
$message
);
}
Expand All @@ -1336,8 +1350,22 @@ public static function assertObjectNotHasAttribute(string $attributeName, $objec
{
self::createWarning('assertObjectNotHasAttribute() is deprecated and will be removed in PHPUnit 10.');

if (!self::isValidObjectAttributeName($attributeName)) {
throw InvalidArgumentException::create(1, 'valid attribute name');
self::assertObjectNotHasProperty($attributeName, $object, $message);
}

/**
* Asserts that an object does not have a specified attribute.
*
* @param object $object
*
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
* @throws Exception
* @throws ExpectationFailedException
*/
public static function assertObjectNotHasProperty(string $propertyName, $object, string $message = ''): void
{
if (!self::isValidObjectPropertyName($propertyName)) {
throw InvalidArgumentException::create(1, 'valid property name');
}

if (!is_object($object)) {
Expand All @@ -1347,7 +1375,7 @@ public static function assertObjectNotHasAttribute(string $attributeName, $objec
static::assertThat(
$object,
new LogicalNot(
new ObjectHasAttribute($attributeName)
new ObjectHasProperty($propertyName)
),
$message
);
Expand Down Expand Up @@ -2728,31 +2756,31 @@ public static function greaterThanOrEqual($value): LogicalOr
/**
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/4601
*/
public static function classHasAttribute(string $attributeName): ClassHasAttribute
public static function classHasAttribute(string $attributeName): ClassHasProperty
{
self::createWarning('classHasAttribute() is deprecated and will be removed in PHPUnit 10.');

return new ClassHasAttribute($attributeName);
return new ClassHasProperty($attributeName);
}

/**
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/4601
*/
public static function classHasStaticAttribute(string $attributeName): ClassHasStaticAttribute
public static function classHasStaticAttribute(string $attributeName): ClassHasStaticProperty
{
self::createWarning('classHasStaticAttribute() is deprecated and will be removed in PHPUnit 10.');

return new ClassHasStaticAttribute($attributeName);
return new ClassHasStaticProperty($attributeName);
}

/**
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/4601
*/
public static function objectHasAttribute($attributeName): ObjectHasAttribute
public static function objectHasAttribute($attributeName): ObjectHasProperty
{
self::createWarning('objectHasAttribute() is deprecated and will be removed in PHPUnit 10.');

return new ObjectHasAttribute($attributeName);
return new ObjectHasProperty($attributeName);
}

public static function identicalTo($value): IsIdentical
Expand Down Expand Up @@ -2904,7 +2932,7 @@ private static function detectLocationHint(string $message): ?array
return $hint;
}

private static function isValidObjectAttributeName(string $attributeName): bool
private static function isValidObjectPropertyName(string $attributeName): bool
{
return (bool) preg_match('/[^\x00-\x1f\x7f-\x9f]+/', $attributeName);
}
Expand Down
20 changes: 10 additions & 10 deletions src/Framework/Assert/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use DOMElement;
use PHPUnit\Framework\Constraint\ArrayHasKey;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\Constraint\ClassHasAttribute;
use PHPUnit\Framework\Constraint\ClassHasStaticAttribute;
use PHPUnit\Framework\Constraint\ClassHasProperty;
use PHPUnit\Framework\Constraint\ClassHasStaticProperty;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\Count;
use PHPUnit\Framework\Constraint\DirectoryExists;
Expand Down Expand Up @@ -48,7 +48,7 @@
use PHPUnit\Framework\Constraint\LogicalOr;
use PHPUnit\Framework\Constraint\LogicalXor;
use PHPUnit\Framework\Constraint\ObjectEquals;
use PHPUnit\Framework\Constraint\ObjectHasAttribute;
use PHPUnit\Framework\Constraint\ObjectHasProperty;
use PHPUnit\Framework\Constraint\RegularExpression;
use PHPUnit\Framework\Constraint\StringContains;
use PHPUnit\Framework\Constraint\StringEndsWith;
Expand Down Expand Up @@ -1416,11 +1416,11 @@ function assertClassNotHasStaticAttribute(string $attributeName, string $classNa
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
* @see Assert::assertObjectHasAttribute
* @see Assert::assertObjectHasProperty
*/
function assertObjectHasAttribute(string $attributeName, $object, string $message = ''): void
{
Assert::assertObjectHasAttribute(...func_get_args());
Assert::assertObjectHasProperty(...func_get_args());
}
}

Expand All @@ -1436,11 +1436,11 @@ function assertObjectHasAttribute(string $attributeName, $object, string $messag
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
* @see Assert::assertObjectNotHasAttribute
* @see Assert::assertObjectNotHasProperty
*/
function assertObjectNotHasAttribute(string $attributeName, $object, string $message = ''): void
{
Assert::assertObjectNotHasAttribute(...func_get_args());
Assert::assertObjectNotHasProperty(...func_get_args());
}
}

Expand Down Expand Up @@ -2794,21 +2794,21 @@ function greaterThanOrEqual($value): LogicalOr
}

if (!function_exists('PHPUnit\Framework\classHasAttribute')) {
function classHasAttribute(string $attributeName): ClassHasAttribute
function classHasAttribute(string $attributeName): ClassHasProperty
{
return Assert::classHasAttribute(...func_get_args());
}
}

if (!function_exists('PHPUnit\Framework\classHasStaticAttribute')) {
function classHasStaticAttribute(string $attributeName): ClassHasStaticAttribute
function classHasStaticAttribute(string $attributeName): ClassHasStaticProperty
{
return Assert::classHasStaticAttribute(...func_get_args());
}
}

if (!function_exists('PHPUnit\Framework\objectHasAttribute')) {
function objectHasAttribute($attributeName): ObjectHasAttribute
function objectHasAttribute($attributeName): ObjectHasProperty
{
return Assert::objectHasAttribute(...func_get_args());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,17 @@

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/4601
*/
class ClassHasAttribute extends Constraint
class ClassHasProperty extends Constraint
{
/**
* @var string
*/
private $attributeName;
private $propertyName;

public function __construct(string $attributeName)
{
$this->attributeName = $attributeName;
$this->propertyName = $attributeName;
}

/**
Expand All @@ -39,8 +37,8 @@ public function __construct(string $attributeName)
public function toString(): string
{
return sprintf(
'has attribute "%s"',
$this->attributeName
'has property "%s"',
$this->propertyName
);
}

Expand All @@ -53,7 +51,7 @@ public function toString(): string
protected function matches($other): bool
{
try {
return (new ReflectionClass($other))->hasProperty($this->attributeName);
return (new ReflectionClass($other))->hasProperty($this->propertyName);
// @codeCoverageIgnoreStart
} catch (ReflectionException $e) {
throw new Exception(
Expand Down Expand Up @@ -83,8 +81,8 @@ protected function failureDescription($other): string
);
}

protected function attributeName(): string
protected function propertyName(): string
{
return $this->attributeName;
return $this->propertyName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/4601
*/
final class ClassHasStaticAttribute extends ClassHasAttribute
final class ClassHasStaticProperty extends ClassHasProperty
{
/**
* Returns a string representation of the constraint.
Expand All @@ -28,7 +28,7 @@ public function toString(): string
{
return sprintf(
'has static attribute "%s"',
$this->attributeName()
$this->propertyName()
);
}

Expand All @@ -43,8 +43,8 @@ protected function matches($other): bool
try {
$class = new ReflectionClass($other);

if ($class->hasProperty($this->attributeName())) {
return $class->getProperty($this->attributeName())->isStatic();
if ($class->hasProperty($this->propertyName())) {
return $class->getProperty($this->propertyName())->isStatic();
}
// @codeCoverageIgnoreStart
} catch (ReflectionException $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/4601
*/
final class ObjectHasAttribute extends ClassHasAttribute
final class ObjectHasProperty extends ClassHasProperty
{
/**
* Evaluates the constraint for parameter $other. Returns true if the
Expand All @@ -26,6 +24,6 @@ final class ObjectHasAttribute extends ClassHasAttribute
*/
protected function matches($other): bool
{
return (new ReflectionObject($other))->hasProperty($this->attributeName());
return (new ReflectionObject($other))->hasProperty($this->propertyName());
}
}
4 changes: 2 additions & 2 deletions tests/unit/Framework/Constraint/ClassHasAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class ClassHasAttributeTest extends ConstraintTestCase
{
public function testConstraintClassHasAttribute(): void
{
$constraint = new ClassHasAttribute(
$constraint = new ClassHasProperty(
'privateAttribute'
);

Expand Down Expand Up @@ -54,7 +54,7 @@ public function testConstraintClassHasAttribute(): void

public function testConstraintClassHasAttribute2(): void
{
$constraint = new ClassHasAttribute(
$constraint = new ClassHasProperty(
'privateAttribute'
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class ClassHasStaticAttributeTest extends ConstraintTestCase
{
public function testConstraintClassHasStaticAttribute(): void
{
$constraint = new ClassHasStaticAttribute('privateStaticAttribute');
$constraint = new ClassHasStaticProperty('privateStaticAttribute');

$this->assertTrue($constraint->evaluate(ClassWithNonPublicAttributes::class, '', true));
$this->assertFalse($constraint->evaluate(stdClass::class, '', true));
Expand Down Expand Up @@ -52,7 +52,7 @@ public function testConstraintClassHasStaticAttribute(): void

public function testConstraintClassHasStaticAttribute2(): void
{
$constraint = new ClassHasStaticAttribute('foo');
$constraint = new ClassHasStaticProperty('foo');

try {
$constraint->evaluate(stdClass::class, 'custom message');
Expand Down
Loading