Skip to content

Commit f19c34e

Browse files
authored
Merge pull request #57 from particleflux/add-objecthas-property
Add objectHasProperty assertions
2 parents 25b84a9 + 4f37d78 commit f19c34e

10 files changed

Lines changed: 156 additions & 13 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Verify is open-sourced software licensed under the [MIT][9] License.
147147
[4]: https://chaijs.com/
148148
[5]: https://jasmine.github.io/
149149
[6]: https://rspec.info/
150-
[7]: /docs/supported_verifiers.md
151-
[8]: /docs/supported_expectations.md
152-
[9]: /LICENSE
153-
[10]: /UPGRADE.md
150+
[7]: ./docs/supported_verifiers.md
151+
[8]: ./docs/supported_expectations.md
152+
[9]: ./LICENSE
153+
[10]: ./UPGRADE.md

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"require": {
1717
"php": "^7.4 || ^8.0",
1818
"ext-dom": "*",
19-
"phpunit/phpunit": "^9.5 | ^10.0"
19+
"phpunit/phpunit": "^9.6.11 || ^10.0"
2020
},
2121
"autoload": {
2222
"files": [

docs/supported_expectations.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ toHaveSameSizeAs
2121

2222
### BaseObject
2323
```
24-
notToHaveAttribute
25-
toHaveAttribute
24+
notToHaveProperty
25+
toHaveProperty
2626
```
2727

2828
### Callable

docs/supported_verifiers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ sameSize
2121

2222
### BaseObject
2323
```
24-
hasAttribute
25-
notHasAttribute
24+
hasProperty
25+
notHasProperty
2626
```
2727

2828
### Callable

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: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,56 @@ public function __construct(object $object)
2424
/**
2525
* Expect that an object does not have a specified attribute.
2626
*
27+
* @deprecated Deprecated in favour of notToHaveProperty
28+
*
2729
* @param string $attributeName
2830
* @param string $message
2931
* @return self
3032
*/
3133
public function notToHaveAttribute(string $attributeName, string $message = ''): self
3234
{
33-
Assert::assertObjectNotHasAttribute($attributeName, $this->actual, $message);
35+
Assert::assertObjectNotHasProperty($attributeName, $this->actual, $message);
3436
return $this;
3537
}
3638

3739
/**
3840
* Expect that an object has a specified attribute.
3941
*
42+
* @deprecated Deprecated in favour of toHaveProperty
43+
*
4044
* @param string $attributeName
4145
* @param string $message
4246
* @return self
4347
*/
4448
public function toHaveAttribute(string $attributeName, string $message = ''): self
4549
{
46-
Assert::assertObjectHasAttribute($attributeName, $this->actual, $message);
50+
Assert::assertObjectHasProperty($attributeName, $this->actual, $message);
51+
return $this;
52+
}
53+
54+
/**
55+
* Expect that an object does not have a specified property.
56+
*
57+
* @param string $propertyName
58+
* @param string $message
59+
* @return self
60+
*/
61+
public function notToHaveProperty(string $propertyName, string $message = ''): self
62+
{
63+
Assert::assertObjectNotHasProperty($propertyName, $this->actual, $message);
64+
return $this;
65+
}
66+
67+
/**
68+
* Expect that an object has a specified property.
69+
*
70+
* @param string $propertyName
71+
* @param string $message
72+
* @return self
73+
*/
74+
public function toHaveProperty(string $propertyName, string $message = ''): self
75+
{
76+
Assert::assertObjectHasProperty($propertyName, $this->actual, $message);
4777
return $this;
4878
}
4979
}

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: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,56 @@ public function __construct(object $object)
2424
/**
2525
* Verifies that an object has a specified attribute.
2626
*
27+
* @deprecated Deprecated in favour of hasProperty
28+
*
2729
* @param string $attributeName
2830
* @param string $message
2931
* @return self
3032
*/
3133
public function hasAttribute(string $attributeName, string $message = ''): self
3234
{
33-
Assert::assertObjectHasAttribute($attributeName, $this->actual, $message);
35+
Assert::assertObjectHasProperty($attributeName, $this->actual, $message);
3436
return $this;
3537
}
3638

3739
/**
3840
* Verifies that an object does not have a specified attribute.
3941
*
42+
* @deprecated Deprecated in favour of notHasProperty
43+
*
4044
* @param string $attributeName
4145
* @param string $message
4246
* @return self
4347
*/
4448
public function notHasAttribute(string $attributeName, string $message = ''): self
4549
{
46-
Assert::assertObjectNotHasAttribute($attributeName, $this->actual, $message);
50+
Assert::assertObjectNotHasProperty($attributeName, $this->actual, $message);
51+
return $this;
52+
}
53+
54+
/**
55+
* Verifies that an object has a specified property.
56+
*
57+
* @param string $propertyName
58+
* @param string $message
59+
* @return self
60+
*/
61+
public function hasProperty(string $propertyName, string $message = ''): self
62+
{
63+
Assert::assertObjectHasProperty($propertyName, $this->actual, $message);
64+
return $this;
65+
}
66+
67+
/**
68+
* Verifies that an object does not have a specified property.
69+
*
70+
* @param string $propertyName
71+
* @param string $message
72+
* @return self
73+
*/
74+
public function notHasProperty(string $propertyName, string $message = ''): self
75+
{
76+
Assert::assertObjectNotHasProperty($propertyName, $this->actual, $message);
4777
return $this;
4878
}
4979
}

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+
}

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)