Skip to content

Commit 4f3a74f

Browse files
committed
Add baseObject(Not)HasProperty verifier
1 parent 25b84a9 commit 4f3a74f

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

src/Codeception/Verify/Verifiers/VerifyAny.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ public function baseObjectNotHasAttribute(string $attributeName, string $message
9898
return $this;
9999
}
100100

101+
public function baseObjectHasProperty(string $propertyName, string $message = ''): self
102+
{
103+
Verify::BaseObject($this->actual)->hasProperty($propertyName, $message);
104+
return $this;
105+
}
106+
107+
public function baseObjectNotHasProperty(string $propertyName, string $message = ''): self
108+
{
109+
Verify::BaseObject($this->actual)->notHasProperty($propertyName, $message);
110+
return $this;
111+
}
112+
101113
public function callableThrows($throws = null, string $message = ''): self
102114
{
103115
Verify::Callable($this->actual)->throws($throws, $message);

src/Codeception/Verify/Verifiers/VerifyBaseObject.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,30 @@ public function notHasAttribute(string $attributeName, string $message = ''): se
4646
Assert::assertObjectNotHasAttribute($attributeName, $this->actual, $message);
4747
return $this;
4848
}
49+
50+
/**
51+
* Verifies that an object has a specified property.
52+
*
53+
* @param string $propertyName
54+
* @param string $message
55+
* @return self
56+
*/
57+
public function hasProperty(string $propertyName, string $message = ''): self
58+
{
59+
Assert::assertObjectHasProperty($propertyName, $this->actual, $message);
60+
return $this;
61+
}
62+
63+
/**
64+
* Verifies that an object does not have a specified property.
65+
*
66+
* @param string $propertyName
67+
* @param string $message
68+
* @return self
69+
*/
70+
public function notHasProperty(string $propertyName, string $message = ''): self
71+
{
72+
Assert::assertObjectNotHasProperty($propertyName, $this->actual, $message);
73+
return $this;
74+
}
4975
}

tests/VerifyTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,30 @@ public function testDoesNotThrow(): void
324324
verify($func)->callableDoesNotThrow(Exception::class, 'foo');
325325
})->callableThrows(new ExpectationFailedException("exception 'Exception' with message 'foo' was not expected to be thrown"));
326326
}
327+
328+
public function testObjectHasProperty(): void
329+
{
330+
$object = new \Stdclass();
331+
$object->bar = 'baz';
332+
333+
verify($object)->baseObjectHasProperty('bar');
334+
335+
verify(function () use ($object): void {
336+
verify($object)->baseObjectHasProperty('foo', 'foobar');
337+
})->callableThrows(new ExpectationFailedException("foobar\nFailed asserting that object of class \"stdClass\" has property \"foo\"."));
338+
}
339+
340+
public function testObjectNotHasProperty(): void
341+
{
342+
$object = new \Stdclass();
343+
$object->bar = 'baz';
344+
345+
verify($object)->baseObjectNotHasProperty('foo');
346+
347+
verify(function () use ($object): void {
348+
verify($object)->baseObjectNotHasProperty('bar', 'foobar');
349+
})->callableThrows(new ExpectationFailedException("foobar\nFailed asserting that object of class \"stdClass\" does not have property \"bar\"."));
350+
}
327351
}
328352

329353

0 commit comments

Comments
 (0)