Skip to content

Commit

Permalink
Merge pull request #57 from particleflux/add-objecthas-property
Browse files Browse the repository at this point in the history
Add objectHasProperty assertions
  • Loading branch information
Naktibalda committed Feb 4, 2024
2 parents 25b84a9 + 4f37d78 commit f19c34e
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 13 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Verify is open-sourced software licensed under the [MIT][9] License.
[4]: https://chaijs.com/
[5]: https://jasmine.github.io/
[6]: https://rspec.info/
[7]: /docs/supported_verifiers.md
[8]: /docs/supported_expectations.md
[9]: /LICENSE
[10]: /UPGRADE.md
[7]: ./docs/supported_verifiers.md
[8]: ./docs/supported_expectations.md
[9]: ./LICENSE
[10]: ./UPGRADE.md
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"require": {
"php": "^7.4 || ^8.0",
"ext-dom": "*",
"phpunit/phpunit": "^9.5 | ^10.0"
"phpunit/phpunit": "^9.6.11 || ^10.0"
},
"autoload": {
"files": [
Expand Down
4 changes: 2 additions & 2 deletions docs/supported_expectations.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ toHaveSameSizeAs

### BaseObject
```
notToHaveAttribute
toHaveAttribute
notToHaveProperty
toHaveProperty
```

### Callable
Expand Down
4 changes: 2 additions & 2 deletions docs/supported_verifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ sameSize

### BaseObject
```
hasAttribute
notHasAttribute
hasProperty
notHasProperty
```

### Callable
Expand Down
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
34 changes: 32 additions & 2 deletions src/Codeception/Verify/Expectations/ExpectBaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,56 @@ public function __construct(object $object)
/**
* Expect that an object does not have a specified attribute.
*
* @deprecated Deprecated in favour of notToHaveProperty
*
* @param string $attributeName
* @param string $message
* @return self
*/
public function notToHaveAttribute(string $attributeName, string $message = ''): self
{
Assert::assertObjectNotHasAttribute($attributeName, $this->actual, $message);
Assert::assertObjectNotHasProperty($attributeName, $this->actual, $message);
return $this;
}

/**
* Expect that an object has a specified attribute.
*
* @deprecated Deprecated in favour of toHaveProperty
*
* @param string $attributeName
* @param string $message
* @return self
*/
public function toHaveAttribute(string $attributeName, string $message = ''): self
{
Assert::assertObjectHasAttribute($attributeName, $this->actual, $message);
Assert::assertObjectHasProperty($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;
}
}
12 changes: 12 additions & 0 deletions src/Codeception/Verify/Verifiers/VerifyAny.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ public function baseObjectNotHasAttribute(string $attributeName, string $message
return $this;
}

public function baseObjectHasProperty(string $propertyName, string $message = ''): self
{
Verify::BaseObject($this->actual)->hasProperty($propertyName, $message);
return $this;
}

public function baseObjectNotHasProperty(string $propertyName, string $message = ''): self
{
Verify::BaseObject($this->actual)->notHasProperty($propertyName, $message);
return $this;
}

public function callableThrows($throws = null, string $message = ''): self
{
Verify::Callable($this->actual)->throws($throws, $message);
Expand Down
34 changes: 32 additions & 2 deletions src/Codeception/Verify/Verifiers/VerifyBaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,56 @@ public function __construct(object $object)
/**
* Verifies that an object has a specified attribute.
*
* @deprecated Deprecated in favour of hasProperty
*
* @param string $attributeName
* @param string $message
* @return self
*/
public function hasAttribute(string $attributeName, string $message = ''): self
{
Assert::assertObjectHasAttribute($attributeName, $this->actual, $message);
Assert::assertObjectHasProperty($attributeName, $this->actual, $message);
return $this;
}

/**
* Verifies that an object does not have a specified attribute.
*
* @deprecated Deprecated in favour of notHasProperty
*
* @param string $attributeName
* @param string $message
* @return self
*/
public function notHasAttribute(string $attributeName, string $message = ''): self
{
Assert::assertObjectNotHasAttribute($attributeName, $this->actual, $message);
Assert::assertObjectNotHasProperty($attributeName, $this->actual, $message);
return $this;
}

/**
* Verifies that an object has a specified property.
*
* @param string $propertyName
* @param string $message
* @return self
*/
public function hasProperty(string $propertyName, string $message = ''): self
{
Assert::assertObjectHasProperty($propertyName, $this->actual, $message);
return $this;
}

/**
* Verifies that an object does not have a specified property.
*
* @param string $propertyName
* @param string $message
* @return self
*/
public function notHasProperty(string $propertyName, string $message = ''): self
{
Assert::assertObjectNotHasProperty($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\"."));
}
}
24 changes: 24 additions & 0 deletions tests/VerifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,30 @@ public function testDoesNotThrow(): void
verify($func)->callableDoesNotThrow(Exception::class, 'foo');
})->callableThrows(new ExpectationFailedException("exception 'Exception' with message 'foo' was not expected to be thrown"));
}

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

verify($object)->baseObjectHasProperty('bar');

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

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

verify($object)->baseObjectNotHasProperty('foo');

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


Expand Down

0 comments on commit f19c34e

Please sign in to comment.