-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53e630f
commit adc23ac
Showing
7 changed files
with
272 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of PHPUnit. | ||
* | ||
* (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace PHPUnit\Framework\Constraint; | ||
|
||
use function get_class; | ||
use function gettype; | ||
use function is_object; | ||
use function sprintf; | ||
use ReflectionObject; | ||
|
||
/** | ||
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit | ||
*/ | ||
final class ObjectHasProperty extends Constraint | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $propertyName; | ||
|
||
public function __construct(string $propertyName) | ||
{ | ||
$this->propertyName = $propertyName; | ||
} | ||
|
||
/** | ||
* Returns a string representation of the constraint. | ||
*/ | ||
public function toString(): string | ||
{ | ||
return sprintf( | ||
'has property "%s"', | ||
$this->propertyName, | ||
); | ||
} | ||
|
||
/** | ||
* Evaluates the constraint for parameter $other. Returns true if the | ||
* constraint is met, false otherwise. | ||
* | ||
* @param mixed $other value or object to evaluate | ||
*/ | ||
protected function matches($other): bool | ||
{ | ||
if (!is_object($other)) { | ||
return false; | ||
} | ||
|
||
return (new ReflectionObject($other))->hasProperty($this->propertyName); | ||
} | ||
|
||
/** | ||
* Returns the description of the failure. | ||
* | ||
* The beginning of failure messages is "Failed asserting that" in most | ||
* cases. This method should return the second part of that sentence. | ||
* | ||
* @param mixed $other evaluated value or object | ||
*/ | ||
protected function failureDescription($other): string | ||
{ | ||
if (is_object($other)) { | ||
return sprintf( | ||
'object of class "%s" %s', | ||
get_class($other), | ||
$this->toString(), | ||
); | ||
} | ||
|
||
return sprintf( | ||
'"%s" (%s) %s', | ||
$other, | ||
gettype($other), | ||
$this->toString(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of PHPUnit. | ||
* | ||
* (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace PHPUnit\Framework\Constraint; | ||
|
||
use PHPUnit\Framework\ExpectationFailedException; | ||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
/** | ||
* @covers \PHPUnit\Framework\Constraint\ObjectHasProperty | ||
* | ||
* @small | ||
*/ | ||
final class ObjectHasPropertyTest extends TestCase | ||
{ | ||
public function testCanBeEvaluated(): void | ||
{ | ||
$constraint = new ObjectHasProperty('theProperty'); | ||
|
||
$objectWithProperty = new stdClass; | ||
$objectWithProperty->theProperty = 'value'; | ||
|
||
$this->assertTrue($constraint->evaluate($objectWithProperty, '', true)); | ||
$this->assertFalse($constraint->evaluate(new stdClass, '', true)); | ||
$this->assertFalse($constraint->evaluate(null, '', true)); | ||
|
||
$this->expectException(ExpectationFailedException::class); | ||
$this->expectExceptionMessage('Failed asserting that object of class "stdClass" has property "theProperty".'); | ||
|
||
$constraint->evaluate(new stdClass); | ||
} | ||
|
||
public function testHandlesNonObjectsGracefully(): void | ||
{ | ||
$constraint = new ObjectHasProperty('theProperty'); | ||
|
||
$this->expectException(ExpectationFailedException::class); | ||
$this->expectExceptionMessage('Failed asserting that "non-object" (string) has property "theProperty".'); | ||
|
||
$constraint->evaluate('non-object'); | ||
} | ||
|
||
public function testCanBeRepresentedAsString(): void | ||
{ | ||
$constraint = new ObjectHasProperty('theProperty'); | ||
|
||
$this->assertSame('has property "theProperty"', $constraint->toString()); | ||
} | ||
|
||
public function testIsCountable(): void | ||
{ | ||
$constraint = new ObjectHasProperty('theProperty'); | ||
|
||
$this->assertCount(1, $constraint); | ||
} | ||
} |