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:
@ -52,6 +52,101 @@ final class Expectation
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that value is false.
|
||||||
|
*/
|
||||||
|
public function toBeFalse()
|
||||||
|
{
|
||||||
|
Assert::assertFalse($this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that value is greater than expected one.
|
||||||
|
*
|
||||||
|
* @param int|float $value
|
||||||
|
*/
|
||||||
|
public function toBeGreaterThan($value)
|
||||||
|
{
|
||||||
|
Assert::assertGreaterThan($value, $this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that value is greater than or equal to the expected one.
|
||||||
|
*
|
||||||
|
* @param int|float $value
|
||||||
|
*/
|
||||||
|
public function toBeGreaterThanOrEqual($value)
|
||||||
|
{
|
||||||
|
Assert::assertGreaterThanOrEqual($value, $this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that value is less than or equal to the expected one.
|
||||||
|
*
|
||||||
|
* @param int|float $value
|
||||||
|
*/
|
||||||
|
public function toBeLessThan($value)
|
||||||
|
{
|
||||||
|
Assert::assertLessThan($value, $this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that value is less than the expected one.
|
||||||
|
*
|
||||||
|
* @param int|float $value
|
||||||
|
*/
|
||||||
|
public function toBeLessThanOrEqual($value)
|
||||||
|
{
|
||||||
|
Assert::assertLessThanOrEqual($value, $this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that needles is an element of value.
|
||||||
|
*
|
||||||
|
* @param mixed $needle
|
||||||
|
*/
|
||||||
|
public function toContain($needle)
|
||||||
|
{
|
||||||
|
Assert::assertContains($needle, $this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that needles is a substring of value.
|
||||||
|
*
|
||||||
|
* @param string $needle
|
||||||
|
*/
|
||||||
|
public function toContainString($needle)
|
||||||
|
{
|
||||||
|
Assert::assertStringContainsString($needle, $this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that needles is a substring of value, ignoring the
|
||||||
|
* difference in casing.
|
||||||
|
*
|
||||||
|
* @param string $needle
|
||||||
|
*/
|
||||||
|
public function toContainStringIgnoringCase($needle)
|
||||||
|
{
|
||||||
|
Assert::assertStringContainsStringIgnoringCase($needle, $this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dynamically calls methods on the class without any arguments.
|
* Dynamically calls methods on the class without any arguments.
|
||||||
*
|
*
|
||||||
|
|||||||
15
tests/Expect/toBeFalse.php
Normal file
15
tests/Expect/toBeFalse.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('strict comparisons', function () {
|
||||||
|
expect(false)->toBeFalse();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect('')->toBeFalse();
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect(false)->not->toBe(false);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
16
tests/Expect/toBeGreatherThan.php
Normal file
16
tests/Expect/toBeGreatherThan.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('passes', function () {
|
||||||
|
expect(42)->toBeGreaterThan(41);
|
||||||
|
expect(4)->toBeGreaterThan(3.9);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect(4)->toBeGreaterThan(4);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect(5)->not->toBeGreaterThan(4);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
16
tests/Expect/toBeGreatherThanOrEqual.php
Normal file
16
tests/Expect/toBeGreatherThanOrEqual.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('passes', function () {
|
||||||
|
expect(42)->toBeGreaterThanOrEqual(41);
|
||||||
|
expect(4)->toBeGreaterThanOrEqual(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect(4)->toBeGreaterThanOrEqual(4.1);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect(5)->not->toBeGreaterThanOrEqual(5);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
16
tests/Expect/toBeLessThan.php
Normal file
16
tests/Expect/toBeLessThan.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('passes', function () {
|
||||||
|
expect(41)->toBeLessThan(42);
|
||||||
|
expect(4)->toBeLessThan(5);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect(4)->toBeLessThan(4);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect(5)->not->toBeLessThan(6);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
16
tests/Expect/toBeLessThanOrEqual.php
Normal file
16
tests/Expect/toBeLessThanOrEqual.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('passes', function () {
|
||||||
|
expect(41)->toBeLessThanOrEqual(42);
|
||||||
|
expect(4)->toBeLessThanOrEqual(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect(4)->toBeLessThanOrEqual(3.9);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect(5)->not->toBeLessThanOrEqual(5);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
15
tests/Expect/toContain.php
Normal file
15
tests/Expect/toContain.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('passes', function () {
|
||||||
|
expect([1, 2, 42])->toContain(42);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect([1, 2, 42])->toContain(3);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect([1, 2, 42])->not->toContain(42);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
16
tests/Expect/toContainString.php
Normal file
16
tests/Expect/toContainString.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('is case sensitive', function () {
|
||||||
|
expect('hello world')->toContainString('world');
|
||||||
|
expect('hello world')->not->toContainString('World');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect('hello world')->toContainString('Hello');
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect('hello world')->not->toContainString('hello');
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
17
tests/Expect/toContainStringIgnoringCase.php
Normal file
17
tests/Expect/toContainStringIgnoringCase.php
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('ignore difference in casing', function () {
|
||||||
|
expect('hello world')->toContainStringIgnoringCase('world');
|
||||||
|
expect('hello world')->toContainStringIgnoringCase('World');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect('hello world')->toContainStringIgnoringCase('hi');
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect('hello world')->not->toContainStringIgnoringCase('Hello');
|
||||||
|
expect('hello world')->not->toContainStringIgnoringCase('hello');
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
Reference in New Issue
Block a user