Skip to content

Commit e02a6ec

Browse files
committed
Add baseObject(Not)ToHaveProperty expectation
1 parent 4f3a74f commit e02a6ec

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

src/Codeception/Verify/Expectations/ExpectAny.php

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

101+
public function baseObjectToHaveProperty(string $propertyName, string $message = ''): self
102+
{
103+
Expect::BaseObject($this->actual)->toHaveProperty($propertyName, $message);
104+
return $this;
105+
}
106+
107+
public function baseObjectNotToHaveProperty(string $propertyName, string $message = ''): self
108+
{
109+
Expect::BaseObject($this->actual)->notToHaveProperty($propertyName, $message);
110+
return $this;
111+
}
112+
101113
public function callableToThrow($throws = null, string $message = ''): self
102114
{
103115
Expect::Callable($this->actual)->toThrow($throws, $message);

src/Codeception/Verify/Expectations/ExpectBaseObject.php

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

tests/ExpectTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
include_once __DIR__.'/../src/Codeception/bootstrap.php';
5+
6+
use PHPUnit\Framework\ExpectationFailedException;
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class ExpectTest extends TestCase
10+
{
11+
12+
public function testObjectToHaveProperty(): void
13+
{
14+
$object = new \Stdclass();
15+
$object->bar = 'baz';
16+
17+
expect($object)->baseObjectToHaveProperty('bar');
18+
19+
verify(function () use ($object): void {
20+
expect($object)->baseObjectToHaveProperty('foo', 'foobar');
21+
})->callableThrows(new ExpectationFailedException("foobar\nFailed asserting that object of class \"stdClass\" has property \"foo\"."));
22+
}
23+
24+
public function testObjectNotToHaveProperty(): void
25+
{
26+
$object = new \Stdclass();
27+
$object->bar = 'baz';
28+
29+
expect($object)->baseObjectNotToHaveProperty('foo');
30+
31+
verify(function () use ($object): void {
32+
expect($object)->baseObjectNotToHaveProperty('bar', 'foobar');
33+
})->callableThrows(new ExpectationFailedException("foobar\nFailed asserting that object of class \"stdClass\" does not have property \"bar\"."));
34+
}
35+
}

0 commit comments

Comments
 (0)