Skip to content

Commit

Permalink
Add baseObject(Not)ToHaveProperty expectation
Browse files Browse the repository at this point in the history
  • Loading branch information
particleflux committed Oct 10, 2023
1 parent 4f3a74f commit e02a6ec
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Codeception/Verify/Expectations/ExpectAny.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ public function baseObjectNotToHaveAttribute(string $attributeName, string $mess
return $this;
}

public function baseObjectToHaveProperty(string $propertyName, string $message = ''): self
{
Expect::BaseObject($this->actual)->toHaveProperty($propertyName, $message);
return $this;
}

public function baseObjectNotToHaveProperty(string $propertyName, string $message = ''): self
{
Expect::BaseObject($this->actual)->notToHaveProperty($propertyName, $message);
return $this;
}

public function callableToThrow($throws = null, string $message = ''): self
{
Expect::Callable($this->actual)->toThrow($throws, $message);
Expand Down
26 changes: 26 additions & 0 deletions src/Codeception/Verify/Expectations/ExpectBaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,30 @@ public function toHaveAttribute(string $attributeName, string $message = ''): se
Assert::assertObjectHasAttribute($attributeName, $this->actual, $message);
return $this;
}

/**
* Expect that an object does not have a specified property.
*
* @param string $propertyName
* @param string $message
* @return self
*/
public function notToHaveProperty(string $propertyName, string $message = ''): self
{
Assert::assertObjectNotHasProperty($propertyName, $this->actual, $message);
return $this;
}

/**
* Expect that an object has a specified property.
*
* @param string $propertyName
* @param string $message
* @return self
*/
public function toHaveProperty(string $propertyName, string $message = ''): self
{
Assert::assertObjectHasProperty($propertyName, $this->actual, $message);
return $this;
}
}
35 changes: 35 additions & 0 deletions tests/ExpectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

include_once __DIR__.'/../src/Codeception/bootstrap.php';

use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;

final class ExpectTest extends TestCase
{

public function testObjectToHaveProperty(): void
{
$object = new \Stdclass();
$object->bar = 'baz';

expect($object)->baseObjectToHaveProperty('bar');

verify(function () use ($object): void {
expect($object)->baseObjectToHaveProperty('foo', 'foobar');
})->callableThrows(new ExpectationFailedException("foobar\nFailed asserting that object of class \"stdClass\" has property \"foo\"."));
}

public function testObjectNotToHaveProperty(): void
{
$object = new \Stdclass();
$object->bar = 'baz';

expect($object)->baseObjectNotToHaveProperty('foo');

verify(function () use ($object): void {
expect($object)->baseObjectNotToHaveProperty('bar', 'foobar');
})->callableThrows(new ExpectationFailedException("foobar\nFailed asserting that object of class \"stdClass\" does not have property \"bar\"."));
}
}

0 comments on commit e02a6ec

Please sign in to comment.