mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
feat(expect): adds toBe
This commit is contained in:
65
src/Expectation.php
Normal file
65
src/Expectation.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pest;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*
|
||||||
|
* @property Expectation $not Creates the opposite expectation.
|
||||||
|
*/
|
||||||
|
final class Expectation
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The expectation value.
|
||||||
|
*
|
||||||
|
* @var mixed
|
||||||
|
*/
|
||||||
|
private $value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new expectation.
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
public function __construct($value)
|
||||||
|
{
|
||||||
|
$this->value = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the opposite expectation for the value.
|
||||||
|
*/
|
||||||
|
public function not(): OppositeExpectation
|
||||||
|
{
|
||||||
|
return new OppositeExpectation($this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that two variables have the same type and
|
||||||
|
* value. Used on objects, it asserts that two
|
||||||
|
* variables reference the same object.
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
public function toBe($value): Expectation
|
||||||
|
{
|
||||||
|
Assert::assertSame($value, $this->value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamically calls methods on the class without any arguments.
|
||||||
|
*
|
||||||
|
* @return Expectation
|
||||||
|
*/
|
||||||
|
public function __get(string $name)
|
||||||
|
{
|
||||||
|
/* @phpstan-ignore-next-line */
|
||||||
|
return $this->{$name}();
|
||||||
|
}
|
||||||
|
}
|
||||||
45
src/OppositeExpectation.php
Normal file
45
src/OppositeExpectation.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pest;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*
|
||||||
|
* @mixin Expectation
|
||||||
|
*/
|
||||||
|
final class OppositeExpectation
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Expectation
|
||||||
|
*/
|
||||||
|
private $original;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new opposite expectation.
|
||||||
|
*/
|
||||||
|
public function __construct(Expectation $original)
|
||||||
|
{
|
||||||
|
$this->original = $original;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle dynamic method calls into the original expectation.
|
||||||
|
*
|
||||||
|
* @param array<int, mixed> $arguments
|
||||||
|
*/
|
||||||
|
public function __call(string $name, array $arguments): Expectation
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
/* @phpstan-ignore-next-line */
|
||||||
|
$this->original->{$name}(...$arguments);
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
return $this->original;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new ExpectationFailedException(sprintf('@todo'));
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,6 +3,7 @@
|
|||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Pest\Datasets;
|
use Pest\Datasets;
|
||||||
|
use Pest\Expectation;
|
||||||
use Pest\PendingObjects\AfterEachCall;
|
use Pest\PendingObjects\AfterEachCall;
|
||||||
use Pest\PendingObjects\BeforeEachCall;
|
use Pest\PendingObjects\BeforeEachCall;
|
||||||
use Pest\PendingObjects\TestCall;
|
use Pest\PendingObjects\TestCall;
|
||||||
@ -104,3 +105,11 @@ function afterAll(Closure $closure = null): void
|
|||||||
{
|
{
|
||||||
TestSuite::getInstance()->afterAll->set($closure);
|
TestSuite::getInstance()->afterAll->set($closure);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new expectation.
|
||||||
|
*/
|
||||||
|
function expect($value): Expectation
|
||||||
|
{
|
||||||
|
return new Expectation($value);
|
||||||
|
}
|
||||||
|
|||||||
@ -2,6 +2,11 @@
|
|||||||
PASS Tests\CustomTestCase\ExecutedTest
|
PASS Tests\CustomTestCase\ExecutedTest
|
||||||
✓ that gets executed
|
✓ that gets executed
|
||||||
|
|
||||||
|
PASS Tests\Expect\ToBe
|
||||||
|
✓ strict comparisons
|
||||||
|
✓ failures
|
||||||
|
✓ not failures
|
||||||
|
|
||||||
PASS Tests\Features\AfterAll
|
PASS Tests\Features\AfterAll
|
||||||
✓ deletes file after all
|
✓ deletes file after all
|
||||||
|
|
||||||
@ -167,5 +172,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, 96 passed
|
Tests: 6 skipped, 99 passed
|
||||||
Time: 3.43s
|
Time: 3.62s
|
||||||
|
|||||||
19
tests/Expect/ToBe.php
Normal file
19
tests/Expect/ToBe.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
test('strict comparisons', function () {
|
||||||
|
$nuno = new stdClass();
|
||||||
|
$dries = new stdClass();
|
||||||
|
|
||||||
|
expect($nuno)->toBe($nuno);
|
||||||
|
expect($nuno)->not->toBe($dries);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect(1)->toBe(2);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect(1)->not->toBe(1);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
Reference in New Issue
Block a user