feat(expect): adds toHaveProperty

This commit is contained in:
Nuno Maduro
2020-07-14 23:37:02 +02:00
parent 32ef377284
commit 1aec8bac55
6 changed files with 42 additions and 34 deletions

View File

@ -156,6 +156,16 @@ final class Expectation
return $this; return $this;
} }
/**
* Assert that the $value contains the property $name.
*/
public function toHaveProperty(string $name): Expectation
{
Assert::assertTrue(property_exists($this->value, $name));
return $this;
}
/** /**
* Asserts that two variables have the same value. * Asserts that two variables have the same value.
* *

View File

@ -120,31 +120,26 @@
PASS Tests\Expect\toContain PASS Tests\Expect\toContain
✓ passes ✓ passes
✓ failures ✓ failures
✓ not failures
PASS Tests\Expect\toCount
✓ pass
✓ failures
✓ not failures ✓ not failures
PASS Tests\Expect\toEqual PASS Tests\Expect\toEqual
✓ pass ✓ pass
✓ failures ✓ failures
✓ not failures
PASS Tests\Expect\toEqualCanonicalizing
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toEqualIgnoringCase
✓ pass
✓ failures
✓ not failures ✓ not failures
PASS Tests\Expect\toEqualWithDelta PASS Tests\Expect\toEqualWithDelta
✓ pass ✓ pass
✓ failures ✓ failures
✓ not failures
PASS Tests\Expect\toHaveCount
✓ pass
✓ failures
✓ not failures
PASS Tests\Expect\toHaveProperty
✓ pass
✓ failures
✓ not failures ✓ not failures
PASS Tests\Features\AfterAll PASS Tests\Features\AfterAll
@ -312,5 +307,5 @@
WARN Tests\Visual\Success WARN Tests\Visual\Success
- visual snapshot of test suite on success - visual snapshot of test suite on success
Tests: 2 risked, 6 skipped, 181 passed Tests: 2 risked, 6 skipped, 178 passed
Time: 5.72s Time: 5.70s

View File

@ -1,15 +0,0 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect('hello')->toEqualIgnoringCase('HELLO');
});
test('failures', function () {
expect('hello')->toEqualIgnoringCase('BAR');
})->throws(ExpectationFailedException::class);
test('not failures', function () {
expect('HELLO')->not->toEqualIgnoringCase('HelLo');
})->throws(ExpectationFailedException::class);

View File

@ -0,0 +1,18 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
$obj = new stdClass();
$obj->foo = 'bar';
test('pass', function () use ($obj) {
expect($obj)->toHaveProperty('foo');
});
test('failures', function () use ($obj) {
expect($obj)->toHaveProperty('bar');
})->throws(ExpectationFailedException::class);
test('not failures', function () use ($obj) {
expect($obj)->not->toHaveProperty('foo');
})->throws(ExpectationFailedException::class);

View File

@ -11,10 +11,10 @@ afterEach(function () use ($state) {
}); });
it('does not get executed before the test', function () { it('does not get executed before the test', function () {
expect(property_exists($this->state, 'bar'))->toBeFalse(); expect($this->state)->not->toHaveProperty('bar');
}); });
it('gets executed after the test', function () { it('gets executed after the test', function () {
expect(property_exists($this->state, 'bar'))->toBeTrue(); expect($this->state)->toHaveProperty('bar');
expect($this->state->bar)->toBe(2); expect($this->state->bar)->toBe(2);
}); });