mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
feat(expect): add more methods
This commit is contained in:
@ -53,7 +53,17 @@ final class Expectation
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assert that value is false.
|
* Assert the value is empty.
|
||||||
|
*/
|
||||||
|
public function toBeEmpty()
|
||||||
|
{
|
||||||
|
Assert::assertEmpty($this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert the value is false.
|
||||||
*/
|
*/
|
||||||
public function toBeFalse()
|
public function toBeFalse()
|
||||||
{
|
{
|
||||||
@ -63,7 +73,7 @@ final class Expectation
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assert that value is greater than expected one.
|
* Assert the value is greater than expected one.
|
||||||
*
|
*
|
||||||
* @param int|float $value
|
* @param int|float $value
|
||||||
*/
|
*/
|
||||||
@ -123,9 +133,29 @@ final class Expectation
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assert that needles is a substring of value.
|
* Assert the value contains only variables of type.
|
||||||
*
|
*
|
||||||
* @param string $needle
|
* @param string|mixed $type
|
||||||
|
*/
|
||||||
|
public function toContainOnly($type)
|
||||||
|
{
|
||||||
|
Assert::assertContainsOnly($type, $this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert the value contains only instances of $instance.
|
||||||
|
*/
|
||||||
|
public function toContainOnlyInstancesOf(string $instance)
|
||||||
|
{
|
||||||
|
Assert::assertContainsOnlyInstancesOf($instance, $this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that needles is a substring of value.
|
||||||
*/
|
*/
|
||||||
public function toContainString(string $needle)
|
public function toContainString(string $needle)
|
||||||
{
|
{
|
||||||
@ -137,8 +167,6 @@ final class Expectation
|
|||||||
/**
|
/**
|
||||||
* Assert that needles is a substring of value, ignoring the
|
* Assert that needles is a substring of value, ignoring the
|
||||||
* difference in casing.
|
* difference in casing.
|
||||||
*
|
|
||||||
* @param string $needle
|
|
||||||
*/
|
*/
|
||||||
public function toContainStringIgnoringCase(string $needle)
|
public function toContainStringIgnoringCase(string $needle)
|
||||||
{
|
{
|
||||||
@ -147,6 +175,102 @@ final class Expectation
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that $count matches the number of elements of $value.
|
||||||
|
*/
|
||||||
|
public function toCount(int $count)
|
||||||
|
{
|
||||||
|
Assert::assertCount($count, $this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that two variables have the same value.
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
public function toEqual($value)
|
||||||
|
{
|
||||||
|
Assert::assertEquals($value, $this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that two variables are equals, ignoring the casing
|
||||||
|
* for the comparison.
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
public function toEqualIgnoringCase($value)
|
||||||
|
{
|
||||||
|
Assert::assertEqualsIgnoringCase($value, $this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that two variables have the same value.
|
||||||
|
* The contents of $expected and $actual are canonicalized before
|
||||||
|
* they are compared. For instance, when the two variables $value and
|
||||||
|
* $this->value are arrays, then these arrays are sorted before they are
|
||||||
|
* compared. When $value and $this->value are objects,
|
||||||
|
* each object is converted to an array containing all private,
|
||||||
|
* protected and public attributes.
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
public function toEqualCanonicalizing($value)
|
||||||
|
{
|
||||||
|
Assert::assertEqualsCanonicalizing($value, $this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that the absolute difference between $value and $this->value
|
||||||
|
* is greather thatn $delta.
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
public function toEqualWithDelta($value, float $delta)
|
||||||
|
{
|
||||||
|
Assert::assertEqualsWithDelta($value, $this->value, $delta);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that the value is a directory.
|
||||||
|
*/
|
||||||
|
public function toBeExistingDirectory()
|
||||||
|
{
|
||||||
|
Assert::assertDirectoryExists($this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that the value is a directory and is readable.
|
||||||
|
*/
|
||||||
|
public function toBeReadableDirectory()
|
||||||
|
{
|
||||||
|
Assert::assertDirectoryIsReadable($this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that the value is a directory and is writable.
|
||||||
|
*/
|
||||||
|
public function toBeWritableDirectory()
|
||||||
|
{
|
||||||
|
Assert::assertDirectoryIsWritable($this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dynamically calls methods on the class without any arguments.
|
* Dynamically calls methods on the class without any arguments.
|
||||||
*
|
*
|
||||||
|
|||||||
@ -5,6 +5,96 @@
|
|||||||
PASS Tests\Expect\ToBe
|
PASS Tests\Expect\ToBe
|
||||||
✓ strict comparisons
|
✓ strict comparisons
|
||||||
✓ failures
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\thatDirectoryExists
|
||||||
|
✓ pass
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\thatDirectoryIsReadable
|
||||||
|
✓ pass
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toBeEmpty
|
||||||
|
✓ pass
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toBeFalse
|
||||||
|
✓ strict comparisons
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toBeGreatherThan
|
||||||
|
✓ passes
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toBeGreatherThanOrEqual
|
||||||
|
✓ passes
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toBeLessThan
|
||||||
|
✓ passes
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toBeLessThanOrEqual
|
||||||
|
✓ passes
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toContain
|
||||||
|
✓ passes
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toContainOnly
|
||||||
|
✓ pass
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toContainOnlyInstancesOf
|
||||||
|
✓ pass
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toContainString
|
||||||
|
✓ is case sensitive
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toContainStringIgnoringCase
|
||||||
|
✓ ignore difference in casing
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toCount
|
||||||
|
✓ pass
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toEqual
|
||||||
|
✓ pass
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toEqualCanonicalizing
|
||||||
|
✓ pass
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toEqualIgnoringCase
|
||||||
|
✓ pass
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
|
PASS Tests\Expect\toEqualWithDelta
|
||||||
|
✓ pass
|
||||||
|
✓ failures
|
||||||
✓ not failures
|
✓ not failures
|
||||||
|
|
||||||
PASS Tests\Features\AfterAll
|
PASS Tests\Features\AfterAll
|
||||||
@ -172,5 +262,5 @@
|
|||||||
WARN Tests\Visual\Success
|
WARN Tests\Visual\Success
|
||||||
- visual snapshot of test suite on success
|
- visual snapshot of test suite on success
|
||||||
|
|
||||||
Tests: 6 skipped, 99 passed
|
Tests: 6 skipped, 153 passed
|
||||||
Time: 3.62s
|
Time: 4.35s
|
||||||
|
|||||||
17
tests/Expect/thatDirectoryExists.php
Normal file
17
tests/Expect/thatDirectoryExists.php
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
$temp = sys_get_temp_dir();
|
||||||
|
|
||||||
|
expect($temp)->toBeExistingDirectory();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect('/random/path/whatever')->toBeExistingDirectory();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect('.')->not->toBeExistingDirectory();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
15
tests/Expect/thatDirectoryIsReadable.php
Normal file
15
tests/Expect/thatDirectoryIsReadable.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
expect(sys_get_temp_dir())->toBeWritableDirectory();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect('/random/path/whatever')->toBeWritableDirectory();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect(sys_get_temp_dir())->not->toBeWritableDirectory();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
18
tests/Expect/toBeEmpty.php
Normal file
18
tests/Expect/toBeEmpty.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
expect([])->toBeEmpty();
|
||||||
|
expect(null)->toBeEmpty();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect([1, 2])->toBeEmpty();
|
||||||
|
expect(' ')->toBeEmpty();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect([])->not->toBeEmpty();
|
||||||
|
expect(null)->not->toBeEmpty();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
17
tests/Expect/toContainOnly.php
Normal file
17
tests/Expect/toContainOnly.php
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
expect([1, 2, 3])->toContainOnly('int');
|
||||||
|
expect(['hello', 'world'])->toContainOnly('string');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect([1, 2, '3'])->toContainOnly('string');
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect([1, 2, 3])->not->toContainOnly('int');
|
||||||
|
expect(['hello', 'world'])->not->toContainOnly('string');
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
23
tests/Expect/toContainOnlyInstancesOf.php
Normal file
23
tests/Expect/toContainOnlyInstancesOf.php
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Pest\Actions\AddsTests;
|
||||||
|
use Pest\Expectation;
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
$expected = [new Expectation('whatever')];
|
||||||
|
|
||||||
|
expect($expected)->toContainOnlyInstancesOf(Expectation::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
$expected = [new Expectation('whatever')];
|
||||||
|
|
||||||
|
expect($expected)->toContainOnlyInstancesOf(AddsTests::class);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
$expected = [new Expectation('whatever')];
|
||||||
|
|
||||||
|
expect($expected)->not->toContainOnlyInstancesOf(Expectation::class);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
15
tests/Expect/toCount.php
Normal file
15
tests/Expect/toCount.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
expect([1, 2, 3])->toCount(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect([1, 2, 3])->toCount(4);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect([1, 2, 3])->not->toCount(3);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
15
tests/Expect/toEqual.php
Normal file
15
tests/Expect/toEqual.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
expect('00123')->toEqual(123);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect(['a', 'b', 'c'])->toEqual(['a', 'b']);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect('042')->not->toEqual(42);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
16
tests/Expect/toEqualCanonicalizing.php
Normal file
16
tests/Expect/toEqualCanonicalizing.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
expect('00123')->toEqualCanonicalizing(123);
|
||||||
|
expect(['a', 'b', 'c'])->toEqualCanonicalizing(['c', 'a', 'b']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect(['a', 'b', 'c'])->toEqualCanonicalizing(['a', 'b']);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect('042')->not->toEqualCanonicalizing(42);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
15
tests/Expect/toEqualIgnoringCase.php
Normal file
15
tests/Expect/toEqualIgnoringCase.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?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);
|
||||||
15
tests/Expect/toEqualWithDelta.php
Normal file
15
tests/Expect/toEqualWithDelta.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
expect(1.0)->toEqualWithDelta(1.3, .4);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect(1.0)->toEqualWithDelta(1.5, .1);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect(1.0)->not->toEqualWithDelta(1.6, .7);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
Reference in New Issue
Block a user