Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to check property name and value with toHaveProperties #760

Merged
merged 1 commit into from Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Mixins/Expectation.php
Expand Up @@ -296,8 +296,8 @@ public function toHaveProperty(string $name, mixed $value = new Any(), string $m
*/
public function toHaveProperties(iterable $names, string $message = ''): self
{
foreach ($names as $name) {
$this->toHaveProperty($name, message: $message);
foreach ($names as $name => $value) {
is_int($name) ? $this->toHaveProperty($value, message: $message) : $this->toHaveProperty($name, $value, $message);
}

return $this;
Expand Down
2 changes: 1 addition & 1 deletion tests/.snapshots/success.txt
Expand Up @@ -1016,4 +1016,4 @@
PASS Tests\Visual\Version
✓ visual snapshot of help command output

Tests: 2 deprecated, 3 warnings, 4 incomplete, 1 notice, 4 todos, 14 skipped, 709 passed (1711 assertions)
Tests: 2 deprecated, 3 warnings, 4 incomplete, 1 notice, 4 todos, 14 skipped, 709 passed (1717 assertions)
39 changes: 29 additions & 10 deletions tests/Features/Expect/toHaveProperties.php
Expand Up @@ -4,30 +4,49 @@

test('pass', function () {
$object = new stdClass();
$object->name = 'Jhon';
$object->name = 'John';
$object->age = 21;

expect($object)->toHaveProperties(['name', 'age']);
expect($object)
->toHaveProperties(['name', 'age'])
->toHaveProperties([
'name' => 'John',
'age' => 21,
]);
});

test('failures', function () {
$object = new stdClass();
$object->name = 'Jhon';

expect($object)->toHaveProperties(['name', 'age']);
$object->name = 'John';

expect($object)
->toHaveProperties(['name', 'age'])
nunomaduro marked this conversation as resolved.
Show resolved Hide resolved
->toHaveProperties([
'name' => 'John',
'age' => 21,
]);
})->throws(ExpectationFailedException::class);

test('failures with custom message', function () {
$object = new stdClass();
$object->name = 'Jhon';

expect($object)->toHaveProperties(['name', 'age'], 'oh no!');
$object->name = 'John';

expect($object)
->toHaveProperties(['name', 'age'], 'oh no!')
->toHaveProperties([
'name' => 'John',
'age' => 21,
], 'oh no!');
})->throws(ExpectationFailedException::class, 'oh no!');

test('not failures', function () {
$object = new stdClass();
$object->name = 'Jhon';
$object->name = 'John';
$object->age = 21;

expect($object)->not->toHaveProperties(['name', 'age']);
expect($object)->not->toHaveProperties(['name', 'age'])
->not->toHaveProperties([
'name' => 'John',
'age' => 21,
]);
})->throws(ExpectationFailedException::class);
2 changes: 1 addition & 1 deletion tests/Visual/Parallel.php
Expand Up @@ -18,7 +18,7 @@

test('parallel', function () use ($run) {
expect($run('--exclude-group=integration'))
->toContain('Tests: 2 deprecated, 3 warnings, 4 incomplete, 1 notice, 4 todos, 11 skipped, 697 passed (1696 assertions)')
->toContain('Tests: 2 deprecated, 3 warnings, 4 incomplete, 1 notice, 4 todos, 11 skipped, 697 passed (1702 assertions)')
->toContain('Parallel: 3 processes');
})->skipOnWindows();

Expand Down