mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
feat(expect): adds toHaveProperty
This commit is contained in:
@ -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.
|
||||||
*
|
*
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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);
|
|
||||||
18
tests/Expect/toHaveProperty.php
Normal file
18
tests/Expect/toHaveProperty.php
Normal 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);
|
||||||
@ -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);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user